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)); }
private static IBTreeRange ToRange(SortedCollection4 sorted) { if (1 == sorted.Size()) { return((IBTreeRange)sorted.SingleElement()); } return(new BTreeRangeUnion(sorted)); }
public static IBTreeRange Intersect(BTreeRangeUnion union, BTreeRangeSingle single ) { SortedCollection4 collection = NewBTreeRangeSingleCollection(); CollectIntersections(collection, union, single); return(ToRange(collection)); }
public BTreeRangeUnion(SortedCollection4 sorted) { if (null == sorted) { throw new ArgumentNullException(); } _ranges = ToArray(sorted); }
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); }
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); }
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)); }
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)); } } }
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); }
private void AssertCollection(int[] expected, SortedCollection4 collection) { Assert.AreEqual(expected.Length, collection.Size()); ArrayAssert.AreEqual(IntArrays4.ToObjectArray(expected), collection.ToArray(new object [collection.Size()])); }
private static BTreeRangeSingle[] ToArray(SortedCollection4 collection) { return((BTreeRangeSingle[])collection.ToArray(new BTreeRangeSingle[collection.Size ()])); }