public void RemoveRange() { var items = new int[] { 1, 2, 3, 4, 5, 6, 7 }; var l = new DenseList <int>(items); l.RemoveRange(5, 1); l.RemoveRange(1, 2); var l2 = new List <int>(items); l2.RemoveRange(5, 1); l2.RemoveRange(1, 2); Assert.AreEqual( l2.ToArray(), l.ToArray() ); }
public void CloneWithList() { var dl = new DenseList <int> { 1, 2, 3, 4, 5, 6 }; var cloned = dl.Clone(); cloned.Add(7); Assert.IsTrue(cloned.HasList); Assert.AreEqual( new int[] { 1, 2, 3, 4, 5, 6 }, dl.ToArray() ); Assert.AreEqual( new int[] { 1, 2, 3, 4, 5, 6, 7 }, cloned.ToArray() ); }
public void SimpleBenchmark() { // The list contains 4 items so that the DenseList doesn't need a heap allocation // You can test the heap allocated backing store with 5 items, it ends up being somewhat slower var dense = new DenseList <decimal> { 1, 2, 3, 4 }; var list = new List <decimal> { 1, 2, 3, 4 }; // for int64 try something like 32 const int count = 1024000 * 10; var started1 = Stopwatch.GetTimestamp(); unchecked { for (int i = 0; i < count; i++) { for (int j = 0; j < dense.Count; j++) { dense[j]++; } } } var ended1 = Stopwatch.GetTimestamp(); for (int i = 0; i < 4; i++) { dense[i] = i + 1; } var started2 = Stopwatch.GetTimestamp(); unchecked { for (int i = 0; i < count; i++) { for (int j = 0; j < dense.Count; j++) { ref var item = ref dense.Item(j); item++; } } }
public void CloneWithoutList() { var dl = new DenseList <int> { 1, 2 }; var cloned = dl.Clone(); cloned.Add(3); Assert.IsFalse(dl.HasList); Assert.IsFalse(cloned.HasList); Assert.AreEqual( new int[] { 1, 2 }, dl.ToArray() ); Assert.AreEqual( new int[] { 1, 2, 3 }, cloned.ToArray() ); }
public void Clear() { var l = new DenseList <int> { 1, 2 }; l.Clear(); Assert.AreEqual( new int[0], l.ToArray() ); l.Add(1); l.Add(2); Assert.AreEqual( new int[] { 1, 2 }, l.ToArray() ); }
public void CopyToWithList() { var dl = new DenseList <int> { 1, 2, 3, 4, 5, 6 }; var destination = new DenseList <int> { 7, 8, 9, 10, 11 }; dl.CopyTo(ref destination); destination.Add(12); Assert.IsTrue(destination.HasList); Assert.AreEqual( new int[] { 1, 2, 3, 4, 5, 6 }, dl.ToArray() ); Assert.AreEqual( new int[] { 7, 8, 9, 10, 11, 1, 2, 3, 4, 5, 6, 12 }, destination.ToArray() ); }
public void SortRandomSequencesOfLengthWithIndices(int length) { var comparer = new RefComparerAdapter <Comparer <int>, int>(Comparer <int> .Default); var r = new System.Random(37); int sequenceCount = (length >= 500) ? 128 : 1024; int[] indicesA = GenerateSequence(length), indicesB = GenerateSequence(length); for (int i = 0; i < sequenceCount; i++) { Array.Sort(indicesA); Array.Sort(indicesB); var seq = GenerateRandomInts(r, length); var dl = new DenseList <int>(seq); dl.Sort(comparer, indicesB); // FIXME: Why does the argument order here need to be the opposite of the backwards sequence test? Array.Sort( // HACK: Make a temporary copy of the array because we only want to sort the indices. indicesB, seq.ToArray(), Comparer <int> .Default ); try { Assert.AreEqual(MapIndirect(seq, indicesB), MapIndirect(dl.ToArray(), indicesA), "indirect mapped values"); } catch { Console.WriteLine("dl.values: {0}", string.Join(", ", dl.ToArray())); Console.WriteLine("dl.indices: {0}", string.Join(", ", indicesA.ToArray())); Console.WriteLine("arr.values: {0}", string.Join(", ", seq.ToArray())); Console.WriteLine("arr.indices: {0}", string.Join(", ", indicesB.ToArray())); Console.WriteLine(); Console.WriteLine("dl.mapindirect: {0}", string.Join(", ", MapIndirect(dl.ToArray(), indicesA))); Console.WriteLine("arr.mapindirect: {0}", string.Join(", ", MapIndirect(seq, indicesB))); throw; } } }
public void CopyToWithoutList() { var dl = new DenseList <int> { 1 }; var destination = new DenseList <int> { 2, 3 }; dl.CopyTo(ref destination); destination.Add(4); Assert.IsFalse(dl.HasList); Assert.IsFalse(destination.HasList); Assert.AreEqual( new int[] { 1 }, dl.ToArray() ); Assert.AreEqual( new int[] { 2, 3, 1, 4 }, destination.ToArray() ); }