Example #1
0
        public void BTree_IndexOf_Empty()
        {
            var btree = new BTreeDictionary <string, int>();
            int i     = btree.Keys.IndexOf("3");

            Assert.AreEqual(-1, i);
        }
Example #2
0
        public void BTree_Get()
        {
            var btree = new BTreeDictionary <string, int>();

            btree.Add("3", 3);
            Assert.AreEqual(3, btree["3"]);
        }
Example #3
0
        public void BTree_SetNonExisting()
        {
            var btree = new BTreeDictionary <string, int>();

            btree["3"] = 3;
            Assert.AreEqual(3, btree["3"]);
        }
Example #4
0
            /// <summary>
            /// Splits this node into subnodes by creating a new "right" node
            /// and adds the (key,value) to the appropriate subnode.
            /// </summary>
            private Node SplitAndInsert(TKey key, TValue value, BTreeDictionary <TKey, TValue> tree)
            {
                var iSplit = (count + 1) / 2;
                var right  = new LeafNode(tree.LeafNodeChildren);

                right.count      = count - iSplit;
                this.count       = iSplit;
                right.totalCount = right.count;
                this.totalCount  = this.count;
                Array.Copy(this.keys, iSplit, right.keys, 0, right.count);
                Array.Clear(this.keys, iSplit, right.count);
                Array.Copy(this.values, iSplit, right.values, 0, right.count);
                Array.Clear(this.values, iSplit, right.count);
                right.nextLeaf = this.nextLeaf;
                this.nextLeaf  = right;
                if (tree.cmp.Compare(right.keys[0], key) < 0)
                {
                    right.Add(key, value, tree);
                }
                else
                {
                    this.Add(key, value, tree);
                }
                return(right);
            }
Example #5
0
        public void BTree_AddItem()
        {
            var btree = new BTreeDictionary <string, int>();

            btree.Add("3", 3);
            Assert.AreEqual(1, btree.Count);
        }
Example #6
0
        public void BTree_IndexOf_existing_leaf_item()
        {
            var btree = new BTreeDictionary <string, int> {
                { "3", 3 }
            };
            int i = btree.Keys.IndexOf("3");

            Assert.AreEqual(0, i);
        }
Example #7
0
        public void BTree_IndexOf_nonexisting_large_leafitem()
        {
            var btree = new BTreeDictionary <string, int> {
                { "4", 4 },
                { "2", 2 }
            };
            int i = btree.Keys.IndexOf("5");

            Assert.AreEqual(~2, i);
        }
Example #8
0
        public void BTree_IndexOf_nonexisting_small_leafitem()
        {
            var btree = new BTreeDictionary <string, int> {
                { "3", 3 },
                { "2", 2 }
            };
            int i = btree.Keys.IndexOf("1");

            Assert.AreEqual(~0, i);
        }
Example #9
0
            public override (TValue, bool) Get(TKey key, BTreeDictionary <TKey, TValue> tree)
            {
                int idx = Array.BinarySearch(keys, 0, count, key, tree.cmp);

                if (idx < 0)
                {
                    return(default(TValue), false);
                }
                return(values[idx], true);
            }
Example #10
0
        private BTreeDictionary <string, int> Given_Dictionary(IEnumerable <int> items)
        {
            var btree = new BTreeDictionary <string, int>();

            foreach (var item in items)
            {
                btree.Add(item.ToString(), item);
            }
            return(btree);
        }
Example #11
0
        public void BTree_ForceInternalNode()
        {
            var btree = new BTreeDictionary <string, int>();

            foreach (var i in Enumerable.Range(0, 256))
            {
                btree.Add(i.ToString(), i);
            }
            btree.Add("256", 256);
        }
Example #12
0
            public override (Node, Node) Add(TKey key, TValue value, BTreeDictionary <TKey, TValue> tree)
            {
                int idx = Array.BinarySearch(keys, 0, count, key, tree.cmp);

                if (idx >= 0)
                {
                    throw new ArgumentException("Duplicate key.");
                }
                return(Insert(~idx, key, value, tree));
            }
Example #13
0
            public override (Node, Node) Set(TKey key, TValue value, BTreeDictionary <TKey, TValue> tree)
            {
                int idx = Array.BinarySearch(keys, 0, count, key, tree.cmp);

                if (idx >= 0)
                {
                    values[idx] = value;
                    return(this, null);
                }
                return(Insert(~idx, key, value, tree));
            }
Example #14
0
        public void BTree_GetFromDeepTree()
        {
            var btree = new BTreeDictionary <string, int>();

            foreach (var i in Enumerable.Range(0, 1000))
            {
                btree.Add(i.ToString(), i);
            }
            btree.Dump();
            Assert.AreEqual(0, btree["0"]);
            Assert.AreEqual(500, btree["500"]);
        }
Example #15
0
            public Node Add(TKey key, Node node, BTreeDictionary <TKey, TValue> tree)
            {
                int idx = Array.BinarySearch(keys, 1, count - 1, key, tree.cmp);

                if (idx >= 0)
                {
                    throw new ArgumentException("Duplicate key.");
                }
                var subnode = nodes[~idx];

                return(Insert(~idx, node.keys[0], node, tree).Item1);
            }
Example #16
0
            public override (TValue, bool) Get(TKey key, BTreeDictionary <TKey, TValue> tree)
            {
                int idx = Array.BinarySearch(keys, 1, count - 1, key, tree.cmp);

                if (idx >= 0)
                {
                    return(nodes[idx].Get(key, tree));
                }
                else
                {
                    var iPos = (~idx) - 1;
                    return(nodes[iPos].Get(key, tree));
                }
            }
Example #17
0
            public override bool Remove(TKey key, BTreeDictionary <TKey, TValue> tree)
            {
                int idx = Array.BinarySearch(keys, 0, count, key, tree.cmp);

                if (idx >= 0)
                {
                    --count;
                    --totalCount;
                    Array.Copy(keys, idx + 1, keys, idx, count - idx);
                    Array.Copy(values, idx + 1, values, idx, count - idx);
                    keys[count]   = default(TKey);
                    values[count] = default(TValue);
                    return(true);
                }
                return(false);
            }
Example #18
0
        public void BTree_Enumerate()
        {
            var btree = new BTreeDictionary <string, int>();

            btree.Add("3", 3);
            btree.Add("2", 2);
            var e = btree.GetEnumerator();

            Assert.IsTrue(e.MoveNext());
            Assert.AreEqual("2", e.Current.Key);
            Assert.AreEqual(2, e.Current.Value);
            Assert.IsTrue(e.MoveNext());
            Assert.AreEqual("3", e.Current.Key);
            Assert.AreEqual(3, e.Current.Value);
            Assert.IsFalse(e.MoveNext());
        }
Example #19
0
            public override bool Remove(TKey key, BTreeDictionary <TKey, TValue> tree)
            {
                int  idx = Array.BinarySearch(keys, 1, count - 1, key, tree.cmp);
                bool removed;

                if (idx >= 0)
                {
                    removed = nodes[idx].Remove(key, tree);
                }
                else
                {
                    var iPos = (~idx) - 1;
                    removed = nodes[iPos].Remove(key, tree);
                }
                --this.totalCount;
                return(removed);
            }
Example #20
0
 private (Node, Node) Insert(int idx, TKey key, Node node, BTreeDictionary <TKey, TValue> tree)
 {
     if (count == keys.Length)
     {
         var newRight = SplitAndInsert(key, node, tree);
         return(this, newRight);
     }
     if (idx < count)
     {
         Array.Copy(keys, idx, keys, idx + 1, count - idx);
         Array.Copy(nodes, idx, nodes, idx + 1, count - idx);
     }
     keys[idx]  = key;
     nodes[idx] = node;
     ++this.count;
     this.totalCount = SumNodeCounts(this.nodes, this.count);
     return(this, null);
 }
Example #21
0
        public void BTree_EnumeratorThrowIfMutated()
        {
            var btree = new BTreeDictionary <string, int>();

            btree.Add("3", 3);
            var e = btree.GetEnumerator();

            Assert.True(e.MoveNext());
            btree.Add("2", 2);
            try
            {
                e.MoveNext();
                Assert.Fail("Should have thrown exception");
            }
            catch (InvalidOperationException)
            {
            }
        }
Example #22
0
        public void BTree_IndexOf()
        {
            var rnd   = new Random(42);
            var btree = new BTreeDictionary <string, int>();

            while (btree.Count < 100)
            {
                var n = rnd.Next(200);
                var s = n.ToString();
                btree[s] = n;
            }
            var items = btree.Keys.ToArray();

            for (int i = 0; i < items.Length; ++i)
            {
                Assert.AreEqual(i, btree.Keys.IndexOf(items[i]));
            }
        }
Example #23
0
 private (Node, Node) Insert(int idx, TKey key, TValue value, BTreeDictionary <TKey, TValue> tree)
 {
     if (count == keys.Length)
     {
         var newRight = SplitAndInsert(key, value, tree);
         return(this, newRight);
     }
     else if (idx < count)
     {
         // Make a hole
         Array.Copy(keys, idx, keys, idx + 1, count - idx);
         Array.Copy(values, idx, values, idx + 1, count - idx);
     }
     keys[idx]   = key;
     values[idx] = value;
     ++this.count;
     ++this.totalCount;
     return(this, null);
 }
Example #24
0
            public override (Node, Node) Set(TKey key, TValue value, BTreeDictionary <TKey, TValue> tree)
            {
                int idx  = Array.BinarySearch(keys, 1, count - 1, key, tree.cmp);
                int iPos = (idx >= 0)
                    ? idx
                    : (~idx) - 1;
                var subnode = nodes[iPos];

                var(leftNode, rightNode) = subnode.Set(key, value, tree);
                if (rightNode == null)
                {
                    this.totalCount = SumNodeCounts(this.nodes, this.count);
                    tree.Validate(this);
                    return(leftNode, null);
                }
                else
                {
                    return(Insert(iPos + 1, rightNode.keys[0], rightNode, tree));
                }
            }
Example #25
0
        public void BTree_ItemsSorted()
        {
            var rnd   = new Random(42);
            var btree = new BTreeDictionary <string, int>();

            while (btree.Count < 500)
            {
                var n = rnd.Next(3000);
                var s = n.ToString();
                btree[s] = n;
            }
            string prev = "";

            foreach (var item in btree)
            {
                var cur = item.Key;
                Debug.Print("item.Key: {0}", item.Key);
                Assert.Less(prev, cur);
                prev = cur;
            }
        }
Example #26
0
            private Node SplitAndInsert(TKey key, Node node, BTreeDictionary <TKey, TValue> tree)
            {
                var iSplit = (count + 1) / 2;
                var right  = new InternalNode(tree.InternalNodeChildren);

                right.count = count - iSplit;
                this.count  = iSplit;
                Array.Copy(this.keys, iSplit, right.keys, 0, right.count);
                Array.Clear(this.keys, iSplit, right.count);
                Array.Copy(this.nodes, iSplit, right.nodes, 0, right.count);
                Array.Clear(this.nodes, iSplit, right.count);
                if (tree.cmp.Compare(right.keys[0], key) < 0)
                {
                    right.Add(key, node, tree);
                    this.totalCount = SumNodeCounts(this.nodes, this.count);
                }
                else
                {
                    this.Add(key, node, tree);
                    right.totalCount = SumNodeCounts(right.nodes, right.count);
                }
                return(right);
            }
Example #27
0
 public abstract bool Remove(TKey key, BTreeDictionary <TKey, TValue> tree);
Example #28
0
 internal ValueCollection(BTreeDictionary <TKey, TValue> btree) :
     base(btree)
 {
 }
Example #29
0
 public abstract (Node, Node) Set(TKey key, TValue value, BTreeDictionary <TKey, TValue> tree);
Example #30
0
 public abstract (TValue, bool) Get(TKey key, BTreeDictionary <TKey, TValue> tree);