-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathListSegmentsDataCollection.cs
More file actions
64 lines (52 loc) · 1.36 KB
/
Copy pathListSegmentsDataCollection.cs
File metadata and controls
64 lines (52 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using SampleAnalysis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lib;
public class ListSegmentsDataCollection<T> : IDataCollection<T>
{
List<T[]> segments;
const int SegmentSize = 1000;
long count = 0;
public ListSegmentsDataCollection(int numSegments = 1000)
{
segments = new List<T[]>(capacity: numSegments);
}
public void Add(T item)
{
T[] segment;
if (count == Capacity)
{
segment = new T[SegmentSize];
this.segments.Add(segment);
}
else
{
// 2045
// 2045
int segmentIndex = (int)((count) / SegmentSize);
segment = this.segments[segmentIndex];
}
int itemIndex = (int)((count) % SegmentSize);
segment[itemIndex] = item;
count++;
}
public T GetItem(long index)
{
if (index >= count)
{
throw new IndexOutOfRangeException();
}
int segmentIndex = (int)((index) / SegmentSize);
var segment = this.segments[segmentIndex];
int itemIndex = (int)((index) % SegmentSize);
return segment[itemIndex];
}
public long GetLength()
{
return count;
}
public long Capacity => segments.Count * SegmentSize;
}