Exemplo n.º 1
0
        private void AddToChild(ref TrieItem <T> child, IEnumerable <bool> enumerable, T value)
        {
            var bits = new Bits(enumerable);

            if (child == null)
            {
                child = new TrieItem <T>(bits, value);
            }
            else
            {
                var commonBits = child.Prefix.Common(bits);
                if (child.Prefix.Count == commonBits.Count)
                {
                    if (child.Prefix.Count == bits.Count)
                    {
                        // Set value, if not already set
                        if (child.HasValue)
                        {
                            throw new ArgumentException("Duplicate key");
                        }

                        var oldChild = child;
                        child = new TrieItem <T>(oldChild.Prefix, true, value, oldChild.True, oldChild.False);
                    }
                    else
                    {
                        child.AddItem(bits, value);
                    }
                }
                else
                {
                    //split subtrie along the common prefix
                    var oldChild = child;
                    if (commonBits.Count == bits.Count)
                    {
                        child = new TrieItem <T>(commonBits, value);
                    }
                    else
                    {
                        child = new TrieItem <T>(commonBits);
                        child.AddItem(bits, value);
                    }
                    child.MakeGrandchild(oldChild);
                }
            }
        }
Exemplo n.º 2
0
 public void Add(string key, TValue value)
 {
     _root.AddItem(GetBits(key), value);
     Count++;
 }
Exemplo n.º 3
0
 public void AddItem(ReadOnlySpan <byte> key, TValue value)
 {
     root.AddItem(new Bits(key.ToArray()), value);
 }