Skip to content

Commit a71cbfa

Browse files
Adds LargeSpan and LargeMemoryStream.
Fixs some bugs.
1 parent e0d0073 commit a71cbfa

File tree

11 files changed

+533
-49
lines changed

11 files changed

+533
-49
lines changed

LargeCollections.Test/LargeArrayTest.cs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2626

2727
using NUnit.Framework;
2828
using System;
29+
using System.Buffers;
2930
using System.Collections;
3031
using System.Collections.Generic;
3132
using System.Linq;
@@ -127,17 +128,11 @@ public static void SetGetTest(ILargeArray<long> largeArray, long offset)
127128
return;
128129
}
129130

130-
// create array with ascending order
131+
// create and verify array with ascending order
131132
for (long i = 0; i < capacity; i++)
132133
{
133134
largeArray[i] = i;
134-
}
135-
136-
// verify ascending order
137-
for (long i = 0; i < capacity; i++)
138-
{
139-
long expectedValue = i;
140-
Assert.AreEqual(expectedValue, largeArray[i]);
135+
Assert.AreEqual(i, largeArray[i]);
141136
}
142137

143138
long dummy = 0L;
@@ -179,17 +174,11 @@ public static void EnumerationTest(ILargeArray<long> largeArray, long offset)
179174
return;
180175
}
181176

182-
// create array with ascending order
177+
// create and verify array with ascending order
183178
for (long i = 0; i < capacity; i++)
184179
{
185180
largeArray[i] = i;
186-
}
187-
188-
// verify ascending order
189-
for (long i = 0; i < capacity; i++)
190-
{
191-
long expectedValue = i;
192-
Assert.AreEqual(expectedValue, largeArray[i]);
181+
Assert.AreEqual(i, largeArray[i]);
193182
}
194183

195184
// GetAll
@@ -295,14 +284,9 @@ public static void SortTest(ILargeArray<long> largeArray, long offset)
295284

296285
// create array with descending order
297286
for (long i = 0; i < capacity; i++)
298-
{
299-
largeArray[i] = capacity - 1L - i;
300-
}
301-
302-
// verify descending order
303-
for (long i = 0; i < capacity; i++)
304287
{
305288
long expectedValue = capacity - 1L - i;
289+
largeArray[i] = expectedValue;
306290
Assert.AreEqual(expectedValue, largeArray[i]);
307291
}
308292

@@ -361,17 +345,11 @@ public static void BinarySearchTest(ILargeArray<long> largeArray, long offset)
361345
return;
362346
}
363347

364-
// create array with ascending order
348+
// create and verify array with ascending order
365349
for (long i = 0; i < capacity; i++)
366350
{
367351
largeArray[i] = i;
368-
}
369-
370-
// verify ascending order
371-
for (long i = 0; i < capacity; i++)
372-
{
373-
long expectedValue = i;
374-
Assert.AreEqual(expectedValue, largeArray[i]);
352+
Assert.AreEqual(i, largeArray[i]);
375353
}
376354

377355
// Binary Search
@@ -443,17 +421,11 @@ public static void ContainsTest(ILargeArray<long> largeArray, long offset)
443421
return;
444422
}
445423

446-
// create array with ascending order
424+
// create and verify array with ascending order
447425
for (long i = 0; i < capacity; i++)
448426
{
449427
largeArray[i] = i;
450-
}
451-
452-
// Verify ascending order
453-
for (long i = 0; i < capacity; i++)
454-
{
455-
long expectedValue = i;
456-
Assert.AreEqual(expectedValue, largeArray[i]);
428+
Assert.AreEqual(i, largeArray[i]);
457429
}
458430

459431
// Contains
@@ -476,5 +448,35 @@ public static void ContainsTest(ILargeArray<long> largeArray, long offset)
476448
// capacity must not be contained
477449
Assert.AreEqual(false, largeArray.Contains(capacity));
478450
}
451+
452+
public static void CopyTest(ILargeArray<long> largeArray, long offset)
453+
{
454+
long capacity = largeArray.Count;
455+
long count = capacity - 2L * offset;
456+
457+
// offset must not be less than 0
458+
Assert.Throws<ArgumentException>(() => largeArray.Contains(0L, -1L, count));
459+
460+
// count must not be less than 0
461+
Assert.Throws<ArgumentException>(() => largeArray.Contains(0L, 0L, -1L));
462+
463+
// range must not exceed
464+
Assert.Throws<ArgumentException>(() => largeArray.Contains(0L, 1L, capacity));
465+
466+
if (count < 0L || offset + count > capacity)
467+
{
468+
return;
469+
}
470+
471+
// create and verify array with ascending order
472+
for (long i = 0; i < capacity; i++)
473+
{
474+
largeArray[i] = i;
475+
Assert.AreEqual(i, largeArray[i]);
476+
}
477+
478+
LargeArray<long> targetArray = new LargeArray<long>(capacity, 0L);
479+
largeArray.CopyTo(targetArray, capacity);
480+
}
479481
}
480482
}

LargeCollections/Concurrent/ConcurrentLargeArray.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,49 @@ public bool Contains(T item)
9898
}
9999
}
100100

101+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
101102
public bool Contains(T item, long offset, long count)
102103
{
103-
throw new NotImplementedException();
104+
lock (_storage)
105+
{
106+
return _storage.Contains(item, offset, count);
107+
}
108+
}
109+
110+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
111+
public void CopyFrom(IReadOnlyLargeArray<T> source, long count, long targetOffset = 0L, long sourceOffset = 0L)
112+
{
113+
lock (_storage)
114+
{
115+
_storage.CopyFrom(source, count, targetOffset, sourceOffset);
116+
}
117+
}
118+
119+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
120+
public void CopyFrom(T[] source, int count, long targetOffset = 0L, int sourceOffset = 0)
121+
{
122+
lock (_storage)
123+
{
124+
_storage.CopyFrom(source, count, targetOffset, sourceOffset);
125+
}
126+
}
127+
128+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
129+
public void CopyTo(ILargeArray<T> target, long count, long sourceOffset = 0L, long targetOffset = 0L)
130+
{
131+
lock (_storage)
132+
{
133+
_storage.CopyTo(target, count, sourceOffset, targetOffset);
134+
}
135+
}
136+
137+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
138+
public void CopyTo(T[] target, int count, long sourceOffset = 0L, int targetOffset = 0)
139+
{
140+
lock (_storage)
141+
{
142+
_storage.CopyTo(target, count, sourceOffset, targetOffset);
143+
}
104144
}
105145

106146
[MethodImpl(MethodImplOptions.AggressiveInlining)]

LargeCollections/Concurrent/ConcurrentLargeList.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,5 +270,41 @@ public long BinarySearch(T item, long offset, long count, Comparer<T> comparer =
270270
return _storage.BinarySearch(item, offset, count, comparer);
271271
}
272272
}
273+
274+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
275+
public void CopyFrom(IReadOnlyLargeArray<T> source, long count, long targetOffset = 0L, long sourceOffset = 0L)
276+
{
277+
lock (_storage)
278+
{
279+
_storage.CopyFrom(source, count, targetOffset, sourceOffset);
280+
}
281+
}
282+
283+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
284+
public void CopyFrom(T[] source, int count, long targetOffset = 0L, int sourceOffset = 0)
285+
{
286+
lock (_storage)
287+
{
288+
_storage.CopyFrom(source, count, targetOffset, sourceOffset);
289+
}
290+
}
291+
292+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
293+
public void CopyTo(ILargeArray<T> target, long count, long sourceOffset = 0L, long targetOffset = 0L)
294+
{
295+
lock (_storage)
296+
{
297+
_storage.CopyTo(target, count, sourceOffset, targetOffset);
298+
}
299+
}
300+
301+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
302+
public void CopyTo(T[] target, int count, long sourceOffset = 0L, int targetOffset = 0)
303+
{
304+
lock (_storage)
305+
{
306+
_storage.CopyTo(target, count, sourceOffset, targetOffset);
307+
}
308+
}
273309
}
274310
}

LargeCollections/EnumerableLargeCollectionsExtensions.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,40 @@ public static class EnumerableLargeCollectionsExtensions
3535
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3636
public static LargeList<T> ToLargeList<T>(this IEnumerable<T> items)
3737
{
38-
LargeList<T> largeList = new LargeList<T>();
39-
largeList.Add(items);
38+
LargeList<T> largeList = new LargeList<T>(items);
4039
return largeList;
4140
}
4241

4342
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4443
public static LargeSet<T> ToLargeSet<T>(this IEnumerable<T> items)
4544
{
46-
LargeSet<T> largeSet = new LargeSet<T>();
47-
largeSet.Add(items);
45+
LargeSet<T> largeSet = new LargeSet<T>(items);
4846
return largeSet;
4947
}
5048

49+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
50+
public static LargeArray<T> ToLargeArray<T>(this IEnumerable<T> items)
51+
{
52+
LargeList<T> largeList = new LargeList<T>(items);
53+
LargeArray<T> largeArray = new LargeArray<T>(largeList.Count);
54+
largeList.CopyTo(largeArray, largeList.Count);
55+
56+
return largeArray;
57+
}
58+
5159
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5260
public static IEnumerable<T> Skip<T>(this IEnumerable<T> items, long count)
5361
{
62+
if (count <= 0L)
63+
{
64+
yield break;
65+
}
66+
5467
long currentCount = 0L;
5568

5669
foreach (T item in items)
5770
{
58-
if(currentCount > count)
71+
if(currentCount >= count)
5972
{
6073
yield return item;
6174
}
@@ -69,6 +82,11 @@ public static IEnumerable<T> Skip<T>(this IEnumerable<T> items, long count)
6982
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7083
public static IEnumerable<T> Take<T>(this IEnumerable<T> items, long count)
7184
{
85+
if(count <= 0L)
86+
{
87+
yield break;
88+
}
89+
7290
long currentCount = 0L;
7391

7492
foreach (T item in items)

LargeCollections/Interfaces/LargeCollectionsInterfaces.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ public interface IReadOnlyLargeArray<T> : IReadOnlyLageCollection<T>
158158
/// <param name="count">The <paramref name="count"/> of elements that belong to the range.</param>
159159
/// <param name="action">The function that that will be called for each item of the collection.</param>
160160
void DoForEach(long offset, long count, Action<T> action);
161+
162+
void CopyTo(ILargeArray<T> target, long count, long sourceOffset, long targetOffset);
163+
164+
void CopyTo(T[] target, int count, long sourceOffset, int targetOffset);
161165
}
162166

163167
public interface ILargeArray<T> : IReadOnlyLargeArray<T>
@@ -189,6 +193,10 @@ public interface ILargeArray<T> : IReadOnlyLargeArray<T>
189193
/// <param name="count">The <paramref name="count"/> of elements that belong to the range.</param>
190194
/// <param name="comparer">The <paramref name="comparer"/> whose <see cref="Comparer{T}.Compare(T, T)"/> function will be used to compare the items of the collection.</param>
191195
void Sort(long offset, long count, Comparer<T> comparer);
196+
197+
void CopyFrom(IReadOnlyLargeArray<T> source, long count, long targetOffset, long sourceOffset);
198+
199+
void CopyFrom(T[] source, int count, long targetOffset, int sourceOffset);
192200
}
193201

194202
public interface ILargeList<T> : ILargeArray<T>, ILargeCollection<T>

0 commit comments

Comments
 (0)