Exemplo 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));
        }
Exemplo n.º 2
0
 private static IBTreeRange ToRange(SortedCollection4 sorted)
 {
     if (1 == sorted.Size())
     {
         return((IBTreeRange)sorted.SingleElement());
     }
     return(new BTreeRangeUnion(sorted));
 }
Exemplo n.º 3
0
        public static IBTreeRange Intersect(BTreeRangeUnion union, BTreeRangeSingle single
                                            )
        {
            SortedCollection4 collection = NewBTreeRangeSingleCollection();

            CollectIntersections(collection, union, single);
            return(ToRange(collection));
        }
Exemplo n.º 4
0
 public BTreeRangeUnion(SortedCollection4 sorted)
 {
     if (null == sorted)
     {
         throw new ArgumentNullException();
     }
     _ranges = ToArray(sorted);
 }
Exemplo 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);
        }
Exemplo 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);
        }
Exemplo 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));
        }
Exemplo 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));
                }
            }
        }
Exemplo 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);
        }
Exemplo 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()]));
 }
Exemplo n.º 11
0
 private static BTreeRangeSingle[] ToArray(SortedCollection4 collection)
 {
     return((BTreeRangeSingle[])collection.ToArray(new BTreeRangeSingle[collection.Size
                                                                            ()]));
 }