示例#1
0
        protected SortedCollectionBase(ISortedCollection <TKey, TValue> collection)
            : this()
        {
            if (collection is SortedCollectionBase <TKey, TValue> )
            {
                // Optimization for when the input list is a BlockIntegerList
                var blockIndex = (SortedCollectionBase <TKey, TValue>)collection;

                var inBlocks      = blockIndex.Blocks;
                var inBlocksCount = inBlocks.Count;
                // For each block in 'blockIndex'
                for (int i = 0; i < inBlocksCount; ++i)
                {
                    // get the block.
                    var block = inBlocks[i];
                    // Insert a new block in this object.
                    var destBlock = InsertBlock(i, NewBlock());
                    // Copy the contents of the source block to the new destination block.
                    block.CopyTo(destBlock);
                }

                // Set the size of the list
                Count = blockIndex.Count;                 //count;
            }
            else
            {
                // The case when IIntegerList type is not known
                using (var i = collection.GetEnumerator()) {
                    while (i.MoveNext())
                    {
                        Add(i.Current);
                    }
                }
            }

            // If the given list is immutable then set this list to immutable
            if (collection.IsReadOnly)
            {
                IsReadOnly = true;
            }
        }
示例#2
0
        public void Run(TestConfig config)
        {
            int        i;
            int        count = config.Count;
            var        res   = new TestTtreeResult();
            DateTime   start = DateTime.Now;
            IDatabase  db    = config.GetDatabase();
            PersonList root  = (PersonList)db.Root;

            Tests.Assert(root == null);
            root      = new PersonList();
            root.list = db.CreateSortedCollection <Name, Person>(new NameComparator(), IndexType.Unique);
            db.Root   = root;
            ISortedCollection <Name, Person> list = root.list;

            Tests.Assert(!list.IsReadOnly);
            Tests.Assert(!list.RecursiveLoading());
            PopulateIndex(list, count);
            db.Commit();
            res.InsertTime = DateTime.Now - start;

            start = DateTime.Now;
            foreach (var key in Tests.KeySeq(count))
            {
                Name name = new Name(key);
                int  age  = (int)key % 100;

                Person p = list[name];
                Tests.Assert(p != null);
                Tests.Assert(list.Contains(p));
                Tests.Assert(p.age == age);
            }
            res.IndexSearchTime = DateTime.Now - start;

            start = DateTime.Now;
            Name nm = new Name();

            nm.first = nm.last = "";
            PersistentComparator <Name, Person> comparator = list.GetComparator();

            i = 0;
            foreach (Person p in list)
            {
                Tests.Assert(comparator.CompareMemberWithKey(p, nm) > 0);
                nm.first = p.first;
                nm.last  = p.last;
                Tests.Assert(list.Remove(p));
                i += 1;
            }
            Tests.Assert(i == count);
            res.RemoveTime = DateTime.Now - start;
            Tests.Assert(list.Count == 0);
            PopulateIndex(list, count);

            Person[] els = list.ToArray();
            Tests.Assert(els.Length == list.Count);
            Name firstKey = new Name(els[0]);
            Name lastKey  = new Name(els[els.Length - 1]);
            Name midKey   = new Name(els[els.Length / 2]);

            Person[] els2 = list[firstKey, lastKey];
            Tests.Assert(els.Length == els2.Length);
            var e = list.Range(firstKey, midKey).GetEnumerator();

            TestEnumerator(e);
            e = list.GetEnumerator(midKey, lastKey);
            TestEnumerator(e);

            foreach (var key in Tests.KeySeq(count))
            {
                var p = RemoveEl(els, key);
                Tests.Assert(list.Contains(p));
                Tests.Assert(list.Remove(p));
                Tests.Assert(!list.Contains(p));
            }
            Tests.Assert(null == list.Get(firstKey));
            Tests.Assert(null == list.Get(new Name(-123345)));
            db.Commit();
            PopulateIndex(list, 20);
            Tests.Assert(20 == list.Count);
            Tests.Assert(null == list.Get(new Name(-123456)));
            var arr = list.ToArray();

            Tests.Assert(20 == arr.Length);
            Person pTmp = arr[0];

            list.Clear();
            Tests.Assert(!list.Remove(pTmp));
            list.Deallocate();
            db.Commit();
            db.Close();
        }