private void GetFullSubscriptions(ChannelTreeNode <T> node, List <T> subscriptions) { subscriptions.AddRange(node.Items.ToList()); foreach (var child in node.Childs) { GetFullSubscriptions(child.Value, subscriptions); } }
/// <summary> /// Constructor. It adds itself as a child to the parent node. /// </summary> /// <param name="parent">The parent node.</param> /// <param name="channelName">The channel this node represents.</param> public ChannelTreeNode(ChannelTreeNode <T> parent, string channelName) { Parent = parent; Name = channelName; if (Parent != null) { Parent.Childs.TryAdd(Name, this); } }
private bool RemoveAll(ChannelTreeNode <T> node, T itemToBeRemoved) { var nodeFound = node.Items.Remove(itemToBeRemoved); foreach (var subnode in node.Childs) { nodeFound |= RemoveAll(subnode.Value, itemToBeRemoved); } return(nodeFound); }
/// <summary> /// Defines equality to another node if their fullname matches. /// </summary> /// <param name="obj">Another object.</param> /// <returns>True if the comparison object is a node and has the same fullname.</returns> public override bool Equals(object obj) { ChannelTreeNode <T> treeNode = null; if ((treeNode = obj as ChannelTreeNode <T>) == null) { return(false); } return(treeNode.FullName?.Equals(FullName) ?? false); }
private ChannelTreeNode <T> CreateNewHierarchy(ChannelTreeNode <T> parent, string[] channelsHierarchy, int fromIndex = 0) { var currentNode = parent; for (int i = fromIndex; i < channelsHierarchy.Length; i++) { currentNode = new ChannelTreeNode <T>(currentNode, channelsHierarchy[i]); } return(currentNode); }
/// <summary> /// Adds an item to a channel. /// </summary> /// <param name="channelsHierarchy">The hierarchy of channels of which the last is the channel to which the item will be added.</param> /// <param name="item">The item to be added.</param> /// <returns>True if the operation succeeded.</returns> public bool Add(T item, params string[] channelsHierarchy) { if (item == null) { throw new ArgumentNullException(nameof(item)); } CheckChannelsNamesValidity(channelsHierarchy); ChannelTreeNode <T> node = null; var rootChannel = channelsHierarchy[0]; if (_root.Childs.ContainsKey(rootChannel)) // If root already exists { // Get the root node = _root.Childs[rootChannel]; // Find if all channels hierarchy exists var firstNewChannelIndex = 1; // Index of the first new channel to be added to the hierarchy for (; firstNewChannelIndex < channelsHierarchy.Length; firstNewChannelIndex++) { var currentChannelName = channelsHierarchy[firstNewChannelIndex]; if (!node.Childs.ContainsKey(currentChannelName)) { break; } // Move the current node "down" the hierarchy node = node.Childs[currentChannelName]; } // If there is a new branch to be created if (firstNewChannelIndex < channelsHierarchy.Length) { node = CreateNewHierarchy(node, channelsHierarchy, firstNewChannelIndex); } // node is representing the last channel in this hierarchy } else // New root hierarchy { node = new ChannelTreeNode <T>(_root, rootChannel); // Start creating the hierarchy from 1 because index 0 is the root channel node = CreateNewHierarchy(node, channelsHierarchy, fromIndex: 1); } return(node?.Items.Add(item) ?? false); }
private ChannelTreeNode <T> FindNode(string channel) { var channelsHierarchy = channel.Split(ChannelsSeparator); CheckChannelsNamesValidity(channelsHierarchy); ChannelTreeNode <T> node = _root; int i; for (i = 0; i < channelsHierarchy.Length; i++) { var subChannel = channelsHierarchy[i]; if (!node.Childs.ContainsKey(subChannel)) { break; } node = node.Childs[subChannel]; } return(i == channelsHierarchy.Length ? node : null); }
/// <summary> /// Clears the dictionary from all the channels. /// </summary> public void Clear() => _root = new ChannelTreeNode <T>(null, null);