Skip to content

Commit b5584bd

Browse files
authored
Remove Score as we no longer need that abstraction. (#8289)
1 parent ca42f46 commit b5584bd

File tree

3 files changed

+13
-32
lines changed

3 files changed

+13
-32
lines changed

app/lib/search/mem_index.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ class PackageNameIndex {
479479

480480
/// Search [text] and return the matching packages with scores.
481481
@visibleForTesting
482-
Score search(String text) {
482+
Map<String, double> search(String text) {
483483
IndexedScore<String>? score;
484484
for (final w in splitForQuery(text)) {
485485
final s = searchWord(w, filterOnNonZeros: score);
@@ -489,7 +489,7 @@ class PackageNameIndex {
489489
score.multiplyAllFrom(s);
490490
}
491491
}
492-
return score?.toScore() ?? Score.empty;
492+
return score?.toMap() ?? {};
493493
}
494494

495495
/// Search using the parsed [word] and return the matching packages with scores

app/lib/search/sdk_mem_index.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:math';
6+
57
import 'package:_pub_shared/utils/http.dart';
68
// ignore: implementation_imports
79
import 'package:pana/src/dartdoc/dartdoc_index.dart';
@@ -135,18 +137,17 @@ class SdkMemIndex {
135137
final isQualifiedQuery = query.contains(library.split(':').last);
136138

137139
final tokens = _tokensPerLibrary[library]!;
138-
final plainResults =
139-
Score(tokens.searchWords(words).top(3, minValue: 0.05));
140+
final plainResults = tokens.searchWords(words).top(3, minValue: 0.05);
140141
if (plainResults.isEmpty) continue;
141142

142143
final libraryWeight = _libraryWeights[library] ?? 1.0;
143144
final weightedResults = isQualifiedQuery
144145
? plainResults
145-
: plainResults.mapValues(
146+
: plainResults.map(
146147
(key, value) {
147148
final dir = p.dirname(key);
148149
final w = (_apiPageDirWeights[dir] ?? 1.0) * libraryWeight;
149-
return w * value;
150+
return MapEntry(key, w * value);
150151
},
151152
);
152153

@@ -185,9 +186,9 @@ class SdkMemIndex {
185186

186187
class _Hit {
187188
final String library;
188-
final Score top;
189+
final Map<String, double> top;
189190

190191
_Hit(this.library, this.top);
191192

192-
late final score = top.maxValue;
193+
late final score = top.values.fold(0.0, (a, b) => max(a, b));
193194
}

app/lib/search/token_index.dart

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,6 @@ import 'package:meta/meta.dart';
88

99
import 'text_utils.dart';
1010

11-
/// Represents an evaluated score as an {id: score} map.
12-
extension type const Score._(Map<String, double> _values)
13-
implements Map<String, double> {
14-
static const Score empty = Score._({});
15-
16-
Score(this._values);
17-
18-
factory Score.fromEntries(Iterable<MapEntry<String, double>> entries) =>
19-
Score(Map.fromEntries(entries));
20-
21-
double get maxValue => _values.values.fold(0.0, math.max);
22-
23-
/// Transfer the score values with [f].
24-
Score mapValues(double Function(String key, double value) f) =>
25-
Score.fromEntries(
26-
_values.entries.map((e) => MapEntry(e.key, f(e.key, e.value))));
27-
}
28-
2911
/// The weighted tokens used for the final search.
3012
class TokenMatch {
3113
final Map<String, double> _tokenWeights = <String, double>{};
@@ -151,7 +133,7 @@ extension StringTokenIndexExt on TokenIndex<String> {
151133
/// scoring.
152134
@visibleForTesting
153135
Map<String, double> search(String text) {
154-
return searchWords(splitForQuery(text)).toScore();
136+
return searchWords(splitForQuery(text)).toMap();
155137
}
156138
}
157139

@@ -250,17 +232,15 @@ class IndexedScore<K> {
250232
}
251233
return Map.fromEntries(list.map((i) => MapEntry(_keys[i], _values[i])));
252234
}
253-
}
254235

255-
extension StringIndexedScoreExt on IndexedScore<String> {
256-
Score toScore() {
257-
final map = <String, double>{};
236+
Map<K, double> toMap() {
237+
final map = <K, double>{};
258238
for (var i = 0; i < _values.length; i++) {
259239
final v = _values[i];
260240
if (v > 0.0) {
261241
map[_keys[i]] = v;
262242
}
263243
}
264-
return Score._(map);
244+
return map;
265245
}
266246
}

0 commit comments

Comments
 (0)