Skip to content

Commit 3513a59

Browse files
authored
Merge pull request #176 from Visionaid-International-Ltd/refactor-sectorcollection
Refactor SectorCollection
2 parents 08113a4 + 4ce0eae commit 3513a59

File tree

1 file changed

+25
-51
lines changed

1 file changed

+25
-51
lines changed

sources/OpenMcdf/SectorCollection.cs

Lines changed: 25 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,24 @@ namespace OpenMcdf
2525
/// </summary>
2626
internal sealed class SectorCollection : IList<Sector>
2727
{
28-
private const int MAX_SECTOR_V4_COUNT_LOCK_RANGE = 524287; //0x7FFFFF00 for Version 4
28+
private const int MAX_SECTOR_V4_COUNT_LOCK_RANGE = 0x7FFFFF00;
2929
private const int SLICE_SIZE = 4096;
3030

3131
public event Ver3SizeLimitReached OnVer3SizeLimitReached;
3232

33-
private readonly List<ArrayList> largeArraySlices = new List<ArrayList>();
33+
private readonly List<List<Sector>> largeArraySlices = new();
34+
private bool sizeLimitReached = false;
3435

3536
public SectorCollection()
3637
{
3738
}
3839

39-
private bool sizeLimitReached = false;
4040
private void DoCheckSizeLimitReached()
4141
{
42-
if (OnVer3SizeLimitReached != null && !sizeLimitReached && (Count - 1 > MAX_SECTOR_V4_COUNT_LOCK_RANGE))
42+
if (!sizeLimitReached && (Count - 1 > MAX_SECTOR_V4_COUNT_LOCK_RANGE))
4343
{
4444
sizeLimitReached = true;
45-
OnVer3SizeLimitReached();
45+
OnVer3SizeLimitReached?.Invoke();
4646
}
4747
}
4848

@@ -67,69 +67,51 @@ public Sector this[int index]
6767
{
6868
get
6969
{
70-
int itemIndex = Math.DivRem(index, SLICE_SIZE, out int itemOffset);
71-
72-
if ((index > -1) && (index < Count))
73-
{
74-
return (Sector)largeArraySlices[itemIndex][itemOffset];
75-
}
76-
else
70+
if (index <= -1 || index >= Count)
7771
throw new CFException("Argument Out of Range, possibly corrupted file", new ArgumentOutOfRangeException(nameof(index), index, "Argument out of range"));
72+
73+
int itemIndex = Math.DivRem(index, SLICE_SIZE, out int itemOffset);
74+
return largeArraySlices[itemIndex][itemOffset];
7875
}
7976

8077
set
8178
{
82-
int itemIndex = Math.DivRem(index, SLICE_SIZE, out int itemOffset);
83-
84-
if (index > -1 && index < Count)
85-
{
86-
largeArraySlices[itemIndex][itemOffset] = value;
87-
}
88-
else
79+
if (index <= -1 || index >= Count)
8980
throw new ArgumentOutOfRangeException(nameof(index), index, "Argument out of range");
81+
82+
int itemIndex = Math.DivRem(index, SLICE_SIZE, out int itemOffset);
83+
largeArraySlices[itemIndex][itemOffset] = value;
9084
}
9185
}
9286

9387
#endregion
9488

9589
#region ICollection<T> Members
9690

97-
private int add(Sector item)
91+
public void Add(Sector item)
9892
{
99-
int itemIndex = Count / SLICE_SIZE;
93+
DoCheckSizeLimitReached();
10094

95+
int itemIndex = Count / SLICE_SIZE;
10196
if (itemIndex < largeArraySlices.Count)
10297
{
10398
largeArraySlices[itemIndex].Add(item);
104-
Count++;
10599
}
106100
else
107101
{
108-
ArrayList ar = new ArrayList(SLICE_SIZE);
109-
ar.Add(item);
102+
List<Sector> ar = new(SLICE_SIZE)
103+
{
104+
item
105+
};
110106
largeArraySlices.Add(ar);
111-
Count++;
112107
}
113108

114-
return Count - 1;
115-
}
116-
117-
public void Add(Sector item)
118-
{
119-
DoCheckSizeLimitReached();
120-
121-
add(item);
109+
Count++;
122110
}
123111

124112
public void Clear()
125113
{
126-
foreach (ArrayList slice in largeArraySlices)
127-
{
128-
slice.Clear();
129-
}
130-
131114
largeArraySlices.Clear();
132-
133115
Count = 0;
134116
}
135117

@@ -160,9 +142,10 @@ public IEnumerator<Sector> GetEnumerator()
160142
{
161143
for (int i = 0; i < largeArraySlices.Count; i++)
162144
{
163-
for (int j = 0; j < largeArraySlices[i].Count; j++)
145+
List<Sector> slice = largeArraySlices[i];
146+
for (int j = 0; j < slice.Count; j++)
164147
{
165-
yield return (Sector)largeArraySlices[i][j];
148+
yield return slice[j];
166149
}
167150
}
168151
}
@@ -171,16 +154,7 @@ public IEnumerator<Sector> GetEnumerator()
171154

172155
#region IEnumerable Members
173156

174-
IEnumerator IEnumerable.GetEnumerator()
175-
{
176-
for (int i = 0; i < largeArraySlices.Count; i++)
177-
{
178-
for (int j = 0; j < largeArraySlices[i].Count; j++)
179-
{
180-
yield return largeArraySlices[i][j];
181-
}
182-
}
183-
}
157+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
184158

185159
#endregion
186160
}

0 commit comments

Comments
 (0)