Add sort.map_entries_by_key sort.map_entries_by_value

This commit is contained in:
gingerBill
2022-01-01 17:13:11 +00:00
parent a60b9735a2
commit 50188f0308
+33
View File
@@ -0,0 +1,33 @@
package sort
import "core:intrinsics"
import "core:runtime"
import "core:slice"
map_entries_by_key :: proc(m: ^$M/map[$K]$V, loc := #caller_location) where intrinsics.type_is_ordered(K) {
Entry :: struct {
hash: uintptr,
next: int,
key: K,
value: V,
}
header := runtime.__get_map_header(m)
entries := (^[dynamic]Entry)(&header.m.entries)
slice.sort_by_key(entries[:], proc(e: Entry) -> K { return e.key })
runtime.__dynamic_map_reset_entries(header, loc)
}
map_entries_by_value :: proc(m: ^$M/map[$K]$V, loc := #caller_location) where intrinsics.type_is_ordered(V) {
Entry :: struct {
hash: uintptr,
next: int,
key: K,
value: V,
}
header := runtime.__get_map_header(m)
entries := (^[dynamic]Entry)(&header.m.entries)
slice.sort_by_key(entries[:], proc(e: Entry) -> V { return e.value })
runtime.__dynamic_map_reset_entries(header, loc)
}