@@ -102,15 +102,6 @@ extension type const Score._(Map<String, double> _values)
102
102
Score mapValues (double Function (String key, double value) f) =>
103
103
Score .fromEntries (
104
104
_values.entries.map ((e) => MapEntry (e.key, f (e.key, e.value))));
105
-
106
- /// Returns a new [Score] object with the top [count] entry.
107
- Score top (int count, {double ? minValue}) {
108
- final entries = _values.entries
109
- .where ((e) => minValue == null || e.value >= minValue)
110
- .toList ();
111
- entries.sort ((a, b) => - a.value.compareTo (b.value));
112
- return Score (Map .fromEntries (entries.take (count)));
113
- }
114
105
}
115
106
116
107
/// The weighted tokens used for the final search.
@@ -261,6 +252,9 @@ class IndexedScore<K> {
261
252
factory IndexedScore (List <K > keys, [double value = 0.0 ]) =>
262
253
IndexedScore ._(keys, List <double >.filled (keys.length, value));
263
254
255
+ factory IndexedScore .fromMap (Map <K , double > values) =>
256
+ IndexedScore ._(values.keys.toList (), values.values.toList ());
257
+
264
258
List <K > get keys => _keys;
265
259
late final length = _values.length;
266
260
@@ -321,6 +315,24 @@ class IndexedScore<K> {
321
315
}
322
316
return set ;
323
317
}
318
+
319
+ Map <K , double > top (int count, {double ? minValue}) {
320
+ final list = < int > [];
321
+ double ? lastValue;
322
+ for (var i = 0 ; i < length; i++ ) {
323
+ final v = _values[i];
324
+ if (minValue != null && v < minValue) continue ;
325
+ if (list.length == count) {
326
+ if (lastValue != null && lastValue >= v) continue ;
327
+ list[count - 1 ] = i;
328
+ } else {
329
+ list.add (i);
330
+ }
331
+ list.sort ((a, b) => - _values[a].compareTo (_values[b]));
332
+ lastValue = _values[list.last];
333
+ }
334
+ return Map .fromEntries (list.map ((i) => MapEntry (_keys[i], _values[i])));
335
+ }
324
336
}
325
337
326
338
extension StringIndexedScoreExt on IndexedScore <String > {
0 commit comments