@@ -9,47 +9,40 @@ import 'package:meta/meta.dart';
99import 'text_utils.dart' ;
1010
1111/// Represents an evaluated score as an {id: score} map.
12- class Score {
13- final Map <String , double > _values;
12+ extension type const Score ._(Map <String , double > _values)
13+ implements Map <String , double > {
14+ static const Score empty = Score ._({});
1415
1516 Score (this ._values);
16- Score .empty () : _values = const < String , double > {};
1717
18- bool get isEmpty => _values.isEmpty;
19- bool get isNotEmpty => ! isEmpty ;
18+ factory Score . fromEntries ( Iterable < MapEntry < String , double >> entries) =>
19+ Score ( Map . fromEntries (entries)) ;
2020
21- Set <String > getKeys ({bool Function (String key)? where}) =>
22- _values.keys.where ((e) => where == null || where (e)).toSet ();
23- late final double maxValue = _values.values.fold (0.0 , math.max);
24- Map <String , double > getValues () => _values;
25- bool containsKey (String key) => _values.containsKey (key);
26- int get length => _values.length;
27-
28- double operator [](String key) => _values[key] ?? 0.0 ;
21+ double get maxValue => _values.values.fold (0.0 , math.max);
2922
3023 /// Calculates the intersection of the [scores] , by multiplying the values.
3124 static Score multiply (List <Score > scores) {
3225 if (scores.isEmpty) {
33- return Score .empty () ;
26+ return Score .empty;
3427 }
3528 if (scores.length == 1 ) {
3629 return scores.single;
3730 }
3831 if (scores.any ((score) => score.isEmpty)) {
39- return Score .empty () ;
32+ return Score .empty;
4033 }
41- var keys = scores.first.getValues (). keys.toSet ();
34+ var keys = scores.first.keys.toSet ();
4235 for (var i = 1 ; i < scores.length; i++ ) {
43- keys = keys.intersection (scores[i].getValues (). keys.toSet ());
36+ keys = keys.intersection (scores[i].keys.toSet ());
4437 }
4538 if (keys.isEmpty) {
46- return Score .empty () ;
39+ return Score .empty;
4740 }
4841 final values = < String , double > {};
4942 for (final key in keys) {
50- var value = scores.first. getValues () [key]! ;
43+ var value = scores.first[key]! ;
5144 for (var i = 1 ; i < scores.length; i++ ) {
52- value *= scores[i]. getValues () [key]! ;
45+ value *= scores[i][key]! ;
5346 }
5447 values[key] = value;
5548 }
@@ -63,17 +56,17 @@ class Score {
6356 scores.removeWhere ((s) => s.isEmpty);
6457
6558 if (scores.isEmpty) {
66- return Score .empty () ;
59+ return Score .empty;
6760 }
6861 if (scores.length == 1 ) {
6962 return scores.single;
7063 }
71- final keys = scores.expand ((e) => e.getValues (). keys).toSet ();
64+ final keys = scores.expand ((e) => e.keys).toSet ();
7265 final result = < String , double > {};
7366 for (final key in keys) {
7467 var value = 0.0 ;
7568 for (var i = 0 ; i < scores.length; i++ ) {
76- final v = scores[i]. getValues () [key];
69+ final v = scores[i][key];
7770 if (v != null ) {
7871 value = math.max (value, v);
7972 }
@@ -97,24 +90,18 @@ class Score {
9790 if (threshold == null ) {
9891 return this ;
9992 }
100- final result = Map < String , double > .fromEntries (
93+ return Score .fromEntries (
10194 _values.entries.where ((entry) => entry.value >= threshold! ));
102- return Score (result);
10395 }
10496
10597 /// Keeps the scores only for values in [keys] .
106- Score project (Set <String > keys) {
107- final result = Map <String , double >.fromEntries (
108- _values.entries.where ((entry) => keys.contains (entry.key)));
109- return Score (result);
110- }
98+ Score project (Set <String > keys) => Score .fromEntries (
99+ _values.entries.where ((entry) => keys.contains (entry.key)));
111100
112101 /// Transfer the score values with [f] .
113- Score map (double Function (String key, double value) f) {
114- final result = Map <String , double >.fromEntries (
115- _values.entries.map ((e) => MapEntry (e.key, f (e.key, e.value))));
116- return Score (result);
117- }
102+ Score mapValues (double Function (String key, double value) f) =>
103+ Score .fromEntries (
104+ _values.entries.map ((e) => MapEntry (e.key, f (e.key, e.value))));
118105
119106 /// Returns a new [Score] object with the top [count] entry.
120107 Score top (int count, {double ? minValue}) {
@@ -276,7 +263,7 @@ class TokenIndex {
276263 Score searchWords (List <String > words,
277264 {double weight = 1.0 , Set <String >? limitToIds}) {
278265 if (limitToIds != null && limitToIds.isEmpty) {
279- return Score .empty () ;
266+ return Score .empty;
280267 }
281268 final scores = < Score > [];
282269 for (final w in words) {
@@ -288,7 +275,7 @@ class TokenIndex {
288275 limitToIds: limitToIds,
289276 );
290277 if (values.isEmpty) {
291- return Score .empty () ;
278+ return Score .empty;
292279 }
293280 scores.add (Score (values));
294281 }
0 commit comments