Esempio n. 1
0
        public static IBTreeRange Union(BTreeRangeUnion union, BTreeRangeSingle single)
        {
            if (single.IsEmpty())
            {
                return(union);
            }
            SortedCollection4 sorted = NewBTreeRangeSingleCollection();

            sorted.Add(single);
            BTreeRangeSingle range  = single;
            IEnumerator      ranges = union.Ranges();

            while (ranges.MoveNext())
            {
                BTreeRangeSingle current = (BTreeRangeSingle)ranges.Current;
                if (CanBeMerged(current, range))
                {
                    sorted.Remove(range);
                    range = Merge(current, range);
                    sorted.Add(range);
                }
                else
                {
                    sorted.Add(current);
                }
            }
            return(ToRange(sorted));
        }
Esempio n. 2
0
 private static IBTreeRange ToRange(SortedCollection4 sorted)
 {
     if (1 == sorted.Size())
     {
         return((IBTreeRange)sorted.SingleElement());
     }
     return(new BTreeRangeUnion(sorted));
 }
Esempio n. 3
0
        public static IBTreeRange Intersect(BTreeRangeUnion union, BTreeRangeSingle single
                                            )
        {
            SortedCollection4 collection = NewBTreeRangeSingleCollection();

            CollectIntersections(collection, union, single);
            return(ToRange(collection));
        }
Esempio n. 4
0
 public BTreeRangeUnion(SortedCollection4 sorted)
 {
     if (null == sorted)
     {
         throw new ArgumentNullException();
     }
     _ranges = ToArray(sorted);
 }
Esempio n. 5
0
        public virtual void TestAddAllAndToArray()
        {
            object[]          array      = IntArrays4.ToObjectArray(new int[] { 6, 4, 1, 2, 7, 3 });
            SortedCollection4 collection = NewSortedCollection();

            Assert.AreEqual(0, collection.Size());
            collection.AddAll(new ArrayIterator4(array));
            AssertCollection(new int[] { 1, 2, 3, 4, 6, 7 }, collection);
        }
Esempio n. 6
0
        public virtual void TestAddRemove()
        {
            SortedCollection4 collection = NewSortedCollection();

            collection.Add(3);
            collection.Add(1);
            collection.Add(5);
            AssertCollection(new int[] { 1, 3, 5 }, collection);
            collection.Remove(3);
            AssertCollection(new int[] { 1, 5 }, collection);
            collection.Remove(1);
            AssertCollection(new int[] { 5 }, collection);
        }
Esempio n. 7
0
        public static IBTreeRange Intersect(BTreeRangeUnion union1, BTreeRangeUnion union2
                                            )
        {
            SortedCollection4 collection = NewBTreeRangeSingleCollection();
            IEnumerator       ranges     = union1.Ranges();

            while (ranges.MoveNext())
            {
                BTreeRangeSingle current = (BTreeRangeSingle)ranges.Current;
                CollectIntersections(collection, union2, current);
            }
            return(ToRange(collection));
        }
Esempio n. 8
0
        private static void CollectIntersections(SortedCollection4 collection, BTreeRangeUnion
                                                 union, BTreeRangeSingle single)
        {
            IEnumerator ranges = union.Ranges();

            while (ranges.MoveNext())
            {
                BTreeRangeSingle current = (BTreeRangeSingle)ranges.Current;
                if (single.Overlaps(current))
                {
                    collection.Add(single.Intersect(current));
                }
            }
        }
Esempio n. 9
0
        private static SortedCollection4 ToSortedCollection(BTreeRangeSingle[] ranges)
        {
            if (null == ranges)
            {
                throw new ArgumentNullException();
            }
            var collection = new SortedCollection4(BTreeRangeSingle.Comparison);

            for (var i = 0; i < ranges.Length; i++)
            {
                var range = ranges[i];
                if (!range.IsEmpty())
                {
                    collection.Add(range);
                }
            }
            return(collection);
        }
Esempio n. 10
0
 private void AssertCollection(int[] expected, SortedCollection4 collection)
 {
     Assert.AreEqual(expected.Length, collection.Size());
     ArrayAssert.AreEqual(IntArrays4.ToObjectArray(expected), collection.ToArray(new object
                                                                                 [collection.Size()]));
 }
Esempio n. 11
0
 private static BTreeRangeSingle[] ToArray(SortedCollection4 collection)
 {
     return((BTreeRangeSingle[])collection.ToArray(new BTreeRangeSingle[collection.Size
                                                                            ()]));
 }