public void TestCopyToValidation() { TreeSet <int> set = new TreeSet <int>(Enumerable.Range(0, 10)); Assert.Throws <ArgumentNullException>("array", () => set.CopyTo(null, 0, set.Count)); Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => set.CopyTo(new int[set.Count], -1, set.Count)); Assert.Throws <ArgumentOutOfRangeException>("count", () => set.CopyTo(new int[set.Count], 0, -1)); Assert.Throws <ArgumentException>(string.Empty, () => set.CopyTo(new int[set.Count], 1, set.Count)); Assert.Throws <ArgumentException>(() => set.CopyTo(new int[set.Count], 0, set.Count + 1)); }
public void TestCopyTo() { var set = new TreeSet <int>(branchingFactor: 4, collection: Enumerable.Range(0, 100), comparer: null); var reference = new SortedSet <int>(Enumerable.Range(0, 100)); int[] listArray = new int[set.Count * 2]; int[] referenceArray = new int[reference.Count * 2]; set.CopyTo(listArray); reference.CopyTo(referenceArray); Assert.Equal(referenceArray, listArray); set.CopyTo(listArray, 0); Assert.Equal(referenceArray, listArray); set.CopyTo(listArray, set.Count / 2); reference.CopyTo(referenceArray, reference.Count / 2); Assert.Equal(referenceArray, listArray); }
public void TestTreeSetSimpleOperations() { ISortedSet <int> set = new TreeSet <int>(); var count = 0; foreach (var item in set) { count++; } Assert.True(count == 0); Assert.True(set.Count == 0); set.Add(10); set.Add(20); set.Add(30); set.Add(5); set.Add(1); Assert.True(set.Contains(20)); Assert.False(set.Contains(100)); Assert.Equal(5, set.Count); Assert.Equal(30, set.Max); Assert.Equal(1, set.Min); var list = new List <int>(); foreach (var item in set) { list.Add(item); } Assert.True(list.Count == set.Count); foreach (var item in list) { Assert.True(set.Contains(item)); } var array = new int[5]; set.CopyTo(array, 0); foreach (var item in array) { Assert.True(set.Contains(item)); } Assert.True(set.Remove(5)); Assert.Equal(4, set.Count); Assert.False(set.Contains(5)); set.RemoveMin(); Assert.Equal(3, set.Count); Assert.False(set.Contains(1)); set.Clear(); Assert.Equal(0, set.Count); }
/** * @see <a href="http://en.wikipedia.org/wiki/Sweep_line_algorithm">Sweep line algorithm</a> */ private void SweepCleanColumns(CT_Cols cols, CT_Col[] flattenedColsArray, CT_Col overrideColumn) { List <CT_Col> flattenedCols = new List <CT_Col>(flattenedColsArray); TreeSet <CT_Col> currentElements = new TreeSet <CT_Col>(CTColComparator.BY_MAX); IEnumerator <CT_Col> flIter = flattenedCols.GetEnumerator(); CT_Col haveOverrideColumn = null; long lastMaxIndex = 0; long currentMax = 0; IList <CT_Col> toRemove = new List <CT_Col>(); int pos = -1; //while (flIter.hasNext()) while ((pos + 1) < flattenedCols.Count) { //CTCol col = flIter.next(); pos++; CT_Col col = flattenedCols[pos]; long currentIndex = col.min; long colMax = col.max; long nextIndex = (colMax > currentMax) ? colMax : currentMax; //if (flIter.hasNext()) { if ((pos + 1) < flattenedCols.Count) { //nextIndex = flIter.next().getMin(); nextIndex = flattenedCols[pos + 1].min; //flIter.previous(); } IEnumerator <CT_Col> iter = currentElements.GetEnumerator(); toRemove.Clear(); while (iter.MoveNext()) { CT_Col elem = iter.Current; if (currentIndex <= elem.max) { break; // all passed elements have been purged } toRemove.Add(elem); } foreach (CT_Col rc in toRemove) { currentElements.Remove(rc); } if (!(currentElements.Count == 0) && lastMaxIndex < currentIndex) { // we need to process previous elements first CT_Col[] copyCols = new CT_Col[currentElements.Count]; currentElements.CopyTo(copyCols); insertCol(cols, lastMaxIndex, currentIndex - 1, copyCols, true, haveOverrideColumn); } currentElements.Add(col); if (colMax > currentMax) { currentMax = colMax; } if (col.Equals(overrideColumn)) { haveOverrideColumn = overrideColumn; } while (currentIndex <= nextIndex && !(currentElements.Count == 0)) { NPOI.Util.Collections.HashSet <CT_Col> currentIndexElements = new NPOI.Util.Collections.HashSet <CT_Col>(); long currentElemIndex; { // narrow scope of currentElem CT_Col currentElem = currentElements.First(); currentElemIndex = currentElem.max; currentIndexElements.Add(currentElem); while (true) { CT_Col higherElem = currentElements.Higher(currentElem); if (higherElem == null || higherElem.max != currentElemIndex) { break; } currentElem = higherElem; currentIndexElements.Add(currentElem); if (colMax > currentMax) { currentMax = colMax; } if (col.Equals(overrideColumn)) { haveOverrideColumn = overrideColumn; } } } //if (currentElemIndex < nextIndex || !flIter.hasNext()) { if (currentElemIndex < nextIndex || !((pos + 1) < flattenedCols.Count)) { CT_Col[] copyCols = new CT_Col[currentElements.Count]; currentElements.CopyTo(copyCols); insertCol(cols, currentIndex, currentElemIndex, copyCols, true, haveOverrideColumn); //if (flIter.hasNext()) { if ((pos + 1) < flattenedCols.Count) { if (nextIndex > currentElemIndex) { //currentElements.removeAll(currentIndexElements); foreach (CT_Col rc in currentIndexElements) { currentElements.Remove(rc); } if (currentIndexElements.Contains(overrideColumn)) { haveOverrideColumn = null; } } } else { //currentElements.removeAll(currentIndexElements); foreach (CT_Col rc in currentIndexElements) { currentElements.Remove(rc); } if (currentIndexElements.Contains(overrideColumn)) { haveOverrideColumn = null; } } lastMaxIndex = currentIndex = currentElemIndex + 1; } else { lastMaxIndex = currentIndex; currentIndex = nextIndex + 1; } } } SortColumns(cols); }