aboutsummaryrefslogtreecommitdiff
path: root/core/sort
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2022-01-01 17:13:11 +0000
committergingerBill <bill@gingerbill.org>2022-01-01 17:13:11 +0000
commit50188f03086da716a8f23926ae10be6fd87abab4 (patch)
tree7d6fa608909c7c9bca89c161b32e48a9ec15dd27 /core/sort
parenta60b9735a2ce49d4d8389db83ed53372b7f6c413 (diff)
Add `sort.map_entries_by_key` `sort.map_entries_by_value`
Diffstat (limited to 'core/sort')
-rw-r--r--core/sort/map.odin33
1 files changed, 33 insertions, 0 deletions
diff --git a/core/sort/map.odin b/core/sort/map.odin
new file mode 100644
index 000000000..dff2dced3
--- /dev/null
+++ b/core/sort/map.odin
@@ -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)
+} \ No newline at end of file