public void SortedColumn_GrowAndShrink() { SortedColumn <int> c = ColumnFactory.CreateSortedColumn <int>(new ValueTypeColumn <int>(-1), 0); c.SetSize(5); CommitIfRequired(c); ColumnTests.AssertConsistent(c); c[0] = 10; c[1] = -10; c[2] = 5; c[3] = -2; c[4] = 20; CommitIfRequired(c); ColumnTests.AssertConsistent(c); Assert.AreEqual("1, 3, 2, 0, 4", ColumnTests.GetSortedIndexes(c)); // Grow - verify IDs inserted for -1 default c.SetSize(8); CommitIfRequired(c); ColumnTests.AssertConsistent(c); Assert.AreEqual("1, 3, 5, 6, 7, 2, 0, 4", ColumnTests.GetSortedIndexes(c)); // Shrink (unset values) - verify IDs removed properly c.SetSize(5); ColumnTests.AssertConsistent(c); Assert.AreEqual("1, 3, 2, 0, 4", ColumnTests.GetSortedIndexes(c)); // Shrink (set values) - verify IDs removed properly c.SetSize(3); ColumnTests.AssertConsistent(c); Assert.AreEqual("1, 2, 0", ColumnTests.GetSortedIndexes(c)); // Shrink (everything) c.SetSize(0); ColumnTests.AssertConsistent(c); Assert.AreEqual("", ColumnTests.GetSortedIndexes(c)); Assert.AreEqual(0, (int)c.Count); // Grow enough to require underlying array resize IList <ushort> actualArray; int actualArrayCount; c.SetSize(ArrayExtensions.MinimumSize * 2); CommitIfRequired(c); ColumnTests.AssertConsistent(c); Assert.IsTrue(c.TryGetSortedIndexes(out actualArray, out actualArrayCount)); Assert.AreEqual(ArrayExtensions.MinimumSize * 2, (int)c.Count); Assert.AreEqual(ArrayExtensions.MinimumSize * 2, (int)actualArray.Count); Assert.AreEqual(ArrayExtensions.MinimumSize * 2, actualArrayCount); // Shrink enough to require underlying array resize c.SetSize(ArrayExtensions.MinimumSize); ColumnTests.AssertConsistent(c); Assert.IsTrue(c.TryGetSortedIndexes(out actualArray, out actualArrayCount)); Assert.AreEqual(ArrayExtensions.MinimumSize, (int)c.Count); Assert.AreEqual(ArrayExtensions.MinimumSize, (int)actualArray.Count); Assert.AreEqual(ArrayExtensions.MinimumSize, actualArrayCount); }
/// <summary> /// Add matches to the given set for all words starting with the provided prefix. /// </summary> /// <param name="prefix">Prefix for which to add matches</param> /// <param name="result">Set to add all items containing words beginning with prefix</param> public void WhereMatches(ByteBlock prefix, ShortSet result) { // Look for prefixes if above the length minimum; equality otherwise if (prefix.Length < MinimumPrefixExpandLength) { WhereMatchesExact(prefix, result); return; } IComparable <ByteBlock> isPrefixOf = prefix.GetExtendedIComparable(ByteBlock.Comparison.IsPrefixOf); // Otherwise, find all words starting with this prefix int firstIndex = _words.FindFirstWhere(isPrefixOf); if (firstIndex < 0) { return; } int lastIndex = _words.FindLastWhere(isPrefixOf); IList <ushort> sortedIndexes; int sortedIndexescount; _words.TryGetSortedIndexes(out sortedIndexes, out sortedIndexescount); for (int i = firstIndex; i <= lastIndex; ++i) { GetInSet(sortedIndexes[i], result); } }
public void SortedColumn_SortedIndexesSize() { SortedColumn <int> c = ColumnFactory.CreateSortedColumn <int>(new ValueTypeColumn <int>(-1), 0); // preload 1 more than minimum size which will force a resize bigger // resize policy will never allow resizing by just 1 element so this // should ensure that realSize and actualSize are not the same ushort insertSize = ArrayExtensions.MinimumSize + 1; c.SetSize(insertSize); for (ushort i = 0; i < insertSize; ++i) { c[i] = i; } CommitIfRequired(c); // check the size IList <ushort> sortedIndexes; int sortedIndexesCount; Assert.IsTrue(c.TryGetSortedIndexes(out sortedIndexes, out sortedIndexesCount)); int realSize = sortedIndexes.Count; Assert.AreNotEqual(insertSize, realSize); Assert.AreEqual(insertSize, sortedIndexesCount); }
public void FastAddSortedColumn_InsertMax2() { SortedColumn <int> c = ColumnFactory.CreateSortedColumn <int>(new ValueTypeColumn <int>(-1), ushort.MaxValue); ushort seedLimit = ushort.MaxValue - 1; int lastValue = 0; c.SetSize(seedLimit); for (ushort i = 0; i < seedLimit; ++i) { lastValue = ushort.MaxValue + (int)i * 10; c[i] = lastValue; } ColumnTests.AssertConsistent(c); int firstValue = -1; c.SetSize(ushort.MaxValue); for (ushort i = seedLimit; i < ushort.MaxValue; ++i) { if (firstValue == -1) { firstValue = (int)i; } c[i] = (int)i; } ColumnTests.AssertConsistent(c); Assert.AreEqual("65534", ColumnTests.GetMatches(c, Operator.Equals, firstValue)); Assert.AreEqual("65533", ColumnTests.GetMatches(c, Operator.Equals, lastValue)); IList <ushort> sortedIndexes; int sortedIndexesCount; Assert.IsTrue(c.TryGetSortedIndexes(out sortedIndexes, out sortedIndexesCount)); // Check sort order - lowest one was the last value added Assert.AreEqual("65534", sortedIndexes.First().ToString()); Assert.AreEqual("65533", sortedIndexes.Last().ToString()); }