@@ -107,7 +107,7 @@ class TokenIndex<K> {
107
107
108
108
weight = math.pow (weight, 1 / words.length).toDouble ();
109
109
for (final w in words) {
110
- final s = _scorePool._acquire (0.0 );
110
+ final s = _scorePool._acquire ();
111
111
searchAndAccumulate (w, score: s, weight: weight);
112
112
if (score == null ) {
113
113
score = s;
@@ -116,7 +116,7 @@ class TokenIndex<K> {
116
116
_scorePool._release (s);
117
117
}
118
118
}
119
- score ?? = _scorePool._acquire (0.0 );
119
+ score ?? = _scorePool._acquire ();
120
120
final r = fn (score);
121
121
_scorePool._release (score);
122
122
return r;
@@ -151,64 +151,62 @@ extension StringTokenIndexExt on TokenIndex<String> {
151
151
}
152
152
}
153
153
154
- /// A reusable pool for [IndexedScore] instances to spare some memory allocation.
155
- class ScorePool <K > {
156
- final List <K > _keys;
157
- final _pool = < IndexedScore <K >> [];
154
+ abstract class _AllocationPool <T > {
155
+ final _pool = < T > [];
156
+
157
+ /// Creates a ready-to-use item for the pool.
158
+ final T Function () _allocate;
158
159
159
- ScorePool (this ._keys);
160
+ /// Resets a previously used item to its initial state.
161
+ final void Function (T ) _reset;
160
162
161
- IndexedScore <K > _acquire (double value) {
162
- late IndexedScore <K > score;
163
+ _AllocationPool (this ._allocate, this ._reset);
164
+
165
+ T _acquire () {
166
+ final T t;
163
167
if (_pool.isNotEmpty) {
164
- score = _pool.removeLast ();
165
- score._values. setAll ( 0 , Iterable . generate (score.length, (_) => value) );
168
+ t = _pool.removeLast ();
169
+ _reset (t );
166
170
} else {
167
- score = IndexedScore < K >(_keys, value );
171
+ t = _allocate ( );
168
172
}
169
- return score ;
173
+ return t ;
170
174
}
171
175
172
- void _release (IndexedScore < K > score ) {
173
- _pool.add (score );
176
+ void _release (T item ) {
177
+ _pool.add (item );
174
178
}
175
179
176
- R withScore <R >({
177
- required double value,
178
- required R Function (IndexedScore <K > score) fn,
180
+ R withPoolItem <R >({
181
+ required R Function (T array) fn,
179
182
}) {
180
- final score = _acquire (value );
181
- final r = fn (score );
182
- _release (score );
183
+ final item = _acquire ();
184
+ final r = fn (item );
185
+ _release (item );
183
186
return r;
184
187
}
185
188
}
186
189
187
- /// A reusable pool for [BitArray] instances to spare some memory allocation.
188
- class BitArrayPool <K > {
189
- final int _length;
190
- final _pool = < BitArray > [];
191
-
192
- BitArrayPool (this ._length);
193
-
194
- BitArray _acquireAllSet () {
195
- final array = _pool.isNotEmpty ? _pool.removeLast () : BitArray (_length);
196
- array.setRange (0 , _length);
197
- return array;
198
- }
199
-
200
- void _release (BitArray array) {
201
- _pool.add (array);
202
- }
190
+ /// A reusable pool for [IndexedScore] instances to spare some memory allocation.
191
+ class ScorePool <K > extends _AllocationPool <IndexedScore <K >> {
192
+ ScorePool (List <K > keys)
193
+ : super (
194
+ () => IndexedScore (keys),
195
+ // sets all values to 0.0
196
+ (score) => score._values
197
+ .setAll (0 , Iterable .generate (score.length, (_) => 0.0 )),
198
+ );
199
+ }
203
200
204
- R withBitArrayAllSet <R >({
205
- required R Function (BitArray array) fn,
206
- }) {
207
- final array = _acquireAllSet ();
208
- final r = fn (array);
209
- _release (array);
210
- return r;
211
- }
201
+ /// A reusable pool for [BitArray] instances to spare some memory allocation.
202
+ class BitArrayPool extends _AllocationPool <BitArray > {
203
+ BitArrayPool (int length)
204
+ : super (
205
+ // sets all bits to 1
206
+ () => BitArray (length)..setRange (0 , length),
207
+ // sets all bits to 1
208
+ (array) => array.setRange (0 , length),
209
+ );
212
210
}
213
211
214
212
/// Mutable score list that can accessed via integer index.
0 commit comments