Fuzzy map type

This commit is contained in:
2021-12-04 17:34:27 -07:00
parent 5d4ea606f0
commit 736245f7db
2 changed files with 45 additions and 1 deletions

View File

@@ -13,6 +13,7 @@
"hscript": "",
"uuid": "",
"tink_macro": "",
"tink_json": ""
"tink_json": "",
"haxe-strings": ""
}
}

43
src/kiss/FuzzyMap.hx Normal file
View File

@@ -0,0 +1,43 @@
package kiss;
import haxe.ds.StringMap;
using hx.strings.Strings;
abstract FuzzyMap<T>(StringMap<T>) from StringMap<T> to StringMap<T> {
public inline function new(m:StringMap<T>) {
this = m;
}
@:from
static public function fromMap<T>(m:StringMap<T>) {
return new FuzzyMap<T>(m);
}
@:to
public function toMap() {
return this;
}
@:arrayAccess
public inline function get(searchKey:String):Null<T> {
var bestMatch:Null<T> = null;
var bestScore = 0;
for (key => value in this) {
var score = searchKey.getFuzzyDistance(key);
if (score > bestScore) {
bestScore = score;
bestMatch = value;
}
}
return bestMatch;
}
@:arrayAccess
public inline function set(key:String, v:T):T {
this.set(key, v);
return v;
}
}