-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSectorCollection.cs
200 lines (157 loc) · 4.89 KB
/
SectorCollection.cs
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Text;
using System.Collections;
using System.Collections.Generic;
namespace OpenMcdf
{
/// <summary>
/// Action to implement when transaction support - sector
/// has to be written to the underlying stream (see specs).
/// </summary>
public delegate void Ver3SizeLimitReached();
/// <summary>
/// Ad-hoc Heap Friendly sector collection to avoid using
/// large array that may create some problem to GC collection
/// (see http://www.simple-talk.com/dotnet/.net-framework/the-dangers-of-the-large-object-heap/ )
/// </summary>
internal class SectorCollection : IList<Sector>
{
private const int MAX_SECTOR_V4_COUNT_LOCK_RANGE = 524287; //0x7FFFFF00 for Version 4
private const int SLICE_SIZE = 4096;
private int count = 0;
public event Ver3SizeLimitReached OnVer3SizeLimitReached;
private List<List<Sector>> largeArraySlices = new List<List<Sector>>();
public SectorCollection()
{
}
private bool sizeLimitReached = false;
private void DoCheckSizeLimitReached()
{
if (!sizeLimitReached && (count - 1 > MAX_SECTOR_V4_COUNT_LOCK_RANGE))
{
if (OnVer3SizeLimitReached != null)
OnVer3SizeLimitReached();
sizeLimitReached = true;
}
}
#region IList<T> Members
public int IndexOf(Sector item)
{
throw new NotImplementedException();
}
public void Insert(int index, Sector item)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
public Sector this[int index]
{
get
{
int itemIndex = index / SLICE_SIZE;
int itemOffset = index % SLICE_SIZE;
if ((index > -1) && (index < count))
{
return (Sector)largeArraySlices[itemIndex][itemOffset];
}
else
throw new ArgumentOutOfRangeException("index", index, "Argument out of range");
}
set
{
int itemIndex = index / SLICE_SIZE;
int itemOffset = index % SLICE_SIZE;
if (index > -1 && index < count)
{
largeArraySlices[itemIndex][itemOffset] = value;
}
else
throw new ArgumentOutOfRangeException("index", index, "Argument out of range");
}
}
#endregion
#region ICollection<T> Members
private int add(Sector item)
{
int itemIndex = count / SLICE_SIZE;
if (itemIndex < largeArraySlices.Count)
{
largeArraySlices[itemIndex].Add(item);
count++;
}
else
{
List<Sector> ar = new List<Sector>(SLICE_SIZE);
ar.Add(item);
largeArraySlices.Add(ar);
count++;
}
return count - 1;
}
public void Add(Sector item)
{
DoCheckSizeLimitReached();
add(item);
}
public void Clear()
{
foreach (List<Sector> slice in largeArraySlices)
{
slice.Clear();
}
largeArraySlices.Clear();
count = 0;
}
public bool Contains(Sector item)
{
throw new NotImplementedException();
}
public void CopyTo(Sector[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public int Count
{
get
{
return count;
}
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(Sector item)
{
throw new NotImplementedException();
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<Sector> GetEnumerator()
{
for (int i = 0; i < largeArraySlices.Count; i++)
{
for (int j = 0; j < largeArraySlices[i].Count; j++)
{
yield return (Sector)largeArraySlices[i][j];
}
}
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
for (int i = 0; i < largeArraySlices.Count; i++)
{
for (int j = 0; j < largeArraySlices[i].Count; j++)
{
yield return largeArraySlices[i][j];
}
}
}
#endregion
}
}