@@ -25,24 +25,24 @@ namespace OpenMcdf
25
25
/// </summary>
26
26
internal sealed class SectorCollection : IList < Sector >
27
27
{
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 ;
29
29
private const int SLICE_SIZE = 4096 ;
30
30
31
31
public event Ver3SizeLimitReached OnVer3SizeLimitReached ;
32
32
33
- private readonly List < ArrayList > largeArraySlices = new List < ArrayList > ( ) ;
33
+ private readonly List < List < Sector > > largeArraySlices = new ( ) ;
34
+ private bool sizeLimitReached = false ;
34
35
35
36
public SectorCollection ( )
36
37
{
37
38
}
38
39
39
- private bool sizeLimitReached = false ;
40
40
private void DoCheckSizeLimitReached ( )
41
41
{
42
- if ( OnVer3SizeLimitReached != null && ! sizeLimitReached && ( Count - 1 > MAX_SECTOR_V4_COUNT_LOCK_RANGE ) )
42
+ if ( ! sizeLimitReached && ( Count - 1 > MAX_SECTOR_V4_COUNT_LOCK_RANGE ) )
43
43
{
44
44
sizeLimitReached = true ;
45
- OnVer3SizeLimitReached ( ) ;
45
+ OnVer3SizeLimitReached ? . Invoke ( ) ;
46
46
}
47
47
}
48
48
@@ -67,69 +67,51 @@ public Sector this[int index]
67
67
{
68
68
get
69
69
{
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 )
77
71
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 ] ;
78
75
}
79
76
80
77
set
81
78
{
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 )
89
80
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 ;
90
84
}
91
85
}
92
86
93
87
#endregion
94
88
95
89
#region ICollection<T> Members
96
90
97
- private int add ( Sector item )
91
+ public void Add ( Sector item )
98
92
{
99
- int itemIndex = Count / SLICE_SIZE ;
93
+ DoCheckSizeLimitReached ( ) ;
100
94
95
+ int itemIndex = Count / SLICE_SIZE ;
101
96
if ( itemIndex < largeArraySlices . Count )
102
97
{
103
98
largeArraySlices [ itemIndex ] . Add ( item ) ;
104
- Count ++ ;
105
99
}
106
100
else
107
101
{
108
- ArrayList ar = new ArrayList ( SLICE_SIZE ) ;
109
- ar . Add ( item ) ;
102
+ List < Sector > ar = new ( SLICE_SIZE )
103
+ {
104
+ item
105
+ } ;
110
106
largeArraySlices . Add ( ar ) ;
111
- Count ++ ;
112
107
}
113
108
114
- return Count - 1 ;
115
- }
116
-
117
- public void Add ( Sector item )
118
- {
119
- DoCheckSizeLimitReached ( ) ;
120
-
121
- add ( item ) ;
109
+ Count ++ ;
122
110
}
123
111
124
112
public void Clear ( )
125
113
{
126
- foreach ( ArrayList slice in largeArraySlices )
127
- {
128
- slice . Clear ( ) ;
129
- }
130
-
131
114
largeArraySlices . Clear ( ) ;
132
-
133
115
Count = 0 ;
134
116
}
135
117
@@ -160,9 +142,10 @@ public IEnumerator<Sector> GetEnumerator()
160
142
{
161
143
for ( int i = 0 ; i < largeArraySlices . Count ; i ++ )
162
144
{
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 ++ )
164
147
{
165
- yield return ( Sector ) largeArraySlices [ i ] [ j ] ;
148
+ yield return slice [ j ] ;
166
149
}
167
150
}
168
151
}
@@ -171,16 +154,7 @@ public IEnumerator<Sector> GetEnumerator()
171
154
172
155
#region IEnumerable Members
173
156
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 ( ) ;
184
158
185
159
#endregion
186
160
}
0 commit comments