Esempio n. 1
0
        public IPatriciaNode <T> Modify(int key, IPatriciaOperation <T> operation)
        {
            var result = operation.OnInsert();

            if (result != null)
            {
                return(new PatriciaLeaf <T>(key, result));
            }

            return(this);
        }
Esempio n. 2
0
        public IPatriciaNode <T> Modify(int key, IPatriciaOperation <T> operation)
        {
            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            if (key != Key)
            {
                var result = operation.OnInsert();

                // This situation shouldn't occur, maybe it should throw exception instead?
                if (result == null)
                {
                    return(this);
                }

                // Create new leaf with new item.
                var newLeaf = new PatriciaLeaf <T>(key, result);
                return(PatriciaHelper.Join(Key, this, key, newLeaf));
            }
            else
            {
                // Modify item.
                var result = operation.OnFound(Items);

                // Remove case.
                if (result == null)
                {
                    return(null);
                }

                // Nothing changed.
                if (result == Items)
                {
                    return(this);
                }

                return(new PatriciaLeaf <T>(key, result));
            }
        }
Esempio n. 3
0
        public IPatriciaNode <T> Modify(int key, IPatriciaOperation <T> operation)
        {
            if (!PatriciaHelper.MatchPrefix(key, Prefix, Mask))
            {
                // Key doesn't match the prefix, so we need to create new leaf and join with it.
                var item = operation.OnInsert();

                // Unless operation doesn't create any item.
                if (item == null)
                {
                    return(this);
                }

                var leaf = new PatriciaLeaf <T>(key, item);
                return(PatriciaHelper.Join(key, leaf, Prefix, this));
            }

            var index       = PropagationIndex(key);
            var child       = Children[index];
            var propagation = child.Modify(key, operation);

            // Child item was removed.
            if (propagation == null)
            {
                return(Children[OtherIndex(key)]);
            }

            // Child didn't change.
            if (propagation == child)
            {
                return(this);
            }

            // Child changed.
            var newChildren = Children.Change(propagation, index);

            return(new PatriciaBranch <T>(Prefix, Mask, newChildren));
        }