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); } } }
public void Add(string key, TValue value) { _root.AddItem(GetBits(key), value); Count++; }
public void AddItem(ReadOnlySpan <byte> key, TValue value) { root.AddItem(new Bits(key.ToArray()), value); }