Exemplo n.º 1
0
        public MutableNode <TKey, TValue> ReplaceChild(MutableNode <TKey, TValue> childToReplace, MutableNode <TKey, TValue> newChild)
        {
            if (object.ReferenceEquals(childToReplace, newChild))
            {
                return(this); // nothing to do
            }
            if (!childToReplace.TryGetKey(out var childToReplaceKey))
            {
                throw new ArgumentException("child node must have a key", nameof(childToReplace));
            }

            if (!newChild.TryGetKey(out var newChildKey))
            {
                throw new ArgumentException("child node must have a key", nameof(newChild));
            }

            if (!EqualityComparer <TKey> .Default.Equals(childToReplaceKey, newChildKey))
            {
                throw new InvalidOperationException($"Key of child to replace (key='{childToReplaceKey}') and new child (key='{newChildKey}') must be equal");
            }

            for (int i = 0; i < this.childNodes.Length; i++)
            {
                if (this.childNodes[i].Equals(childToReplace))
                {
                    this.childNodes[i] = newChild;
                    return(this);
                }
            }

            throw new InvalidOperationException($"The node (id={newChildKey}) doesn't substutite any of the existing child nodes");
        }
Exemplo n.º 2
0
        public MutableNode <TKey, TValue> AddChild(MutableNode <TKey, TValue> newChild)

        {
            // copy the existing children to a new array, and append the new one.
            MutableNode <TKey, TValue>[] newChildNodes = new MutableNode <TKey, TValue> [this.childNodes.Length + 1];
            Array.Copy(this.childNodes, newChildNodes, this.childNodes.Length);
            newChildNodes[this.childNodes.Length] = newChild;

            // set new child array instead of current child node array
            this.childNodes = newChildNodes;

            return(this);
        }
Exemplo n.º 3
0
 public MutableNode <TKey, TValue> RemoveChild(MutableNode <TKey, TValue> child)
 {
     this.childNodes = this.ChildNodes.Except(new[] { child }).ToArray();
     return(this);
 }