raz / lib / Map.raz

129 lines · 3.5 KiB · raw · history · last change e0d008cd

1(* A hash map from K to V, by open addressing; it hashes and compares
2 keys with the procedures it is given:
3 import Count := Map([]const U8, INT, Str.hash, Str.eq); *)
4module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL);
5import Mem;
6import SYSTEM;
7
8type
9 Entry = record
10 key: K;
11 val: V;
12 used: BOOL;
13 end;
14 Map* = record
15 a: ^Mem.Allocator;
16 slots: ^Entry;
17 cap: INT; (* a power of two, or 0 *)
18 len*: INT;
19 end;
20
21(* An empty map, its memory from a. *)
22proc init*(var m: Map; a: ^Mem.Allocator);
23begin
24 m := {a: a};
25end;
26
27(* The slot of k, or of the empty slot where it would go. *)
28proc slot(const m: Map; k: K): INT;
29var i: INT;
30begin
31 i := INT(hash(k) & U64(m.cap - 1));
32 while m.slots[i].used and not eq(m.slots[i].key, k) do
33 i := (i + 1) & (m.cap - 1);
34 end;
35 return i;
36end;
37
38proc grow(var m: Map): BOOL;
39var old: ^Entry; n, i, j: INT;
40begin
41 old := m.slots;
42 n := m.cap;
43 if n = 0 then m.cap := 16; else m.cap := n * 2; end;
44 m.slots := ^Entry(Mem.alloc(m.a, m.cap * SIZE(Entry), 8));
45 if m.slots = nil then m.slots := old; m.cap := n; return false; end;
46 for i := 0 to m.cap - 1 do m.slots[i].used := false; end;
47 for i := 0 to n - 1 do
48 if old[i].used then
49 j := slot(m, old[i].key);
50 m.slots[j] := old[i];
51 end;
52 end;
53 if old # nil then Mem.free(m.a, ^U8(old)); end;
54 return true;
55end;
56
57(* k now maps to v; false if there was no room. *)
58proc put*(var m: Map; k: K; v: V): BOOL;
59var i: INT;
60begin
61 if (m.len + 1) * 4 > m.cap * 3 then
62 if not grow(m) then return false; end;
63 end;
64 i := slot(m, k);
65 if not m.slots[i].used then
66 m.slots[i] := {key: k, used: true};
67 INC(m.len);
68 end;
69 m.slots[i].val := v;
70 return true;
71end;
72
73(* k maps to nothing; false if it didn't map to anything. The entries
74 after it that were pushed on past its slot move back, so that a
75 search for each still finds it before an empty slot. *)
76proc remove*(var m: Map; k: K): BOOL;
77var i, j, home: INT;
78begin
79 if m.cap = 0 then return false; end;
80 i := slot(m, k);
81 if not m.slots[i].used then return false; end;
82 j := (i + 1) & (m.cap - 1);
83 while m.slots[j].used do
84 (* the entry at j may fill i if i lies between its home and j *)
85 home := INT(hash(m.slots[j].key) & U64(m.cap - 1));
86 if (j - home) & (m.cap - 1) >= (j - i) & (m.cap - 1) then
87 m.slots[i] := m.slots[j];
88 i := j;
89 end;
90 j := (j + 1) & (m.cap - 1);
91 end;
92 m.slots[i].used := false;
93 DEC(m.len);
94 return true;
95end;
96
97(* What k maps to, and whether it maps to anything. *)
98proc get*(const m: Map; k: K): (V, BOOL);
99var i: INT; none: V;
100begin
101 if m.cap = 0 then return none, false; end;
102 i := slot(m, k);
103 if not m.slots[i].used then return none, false; end;
104 return m.slots[i].val, true;
105end;
106
107(* The entry after i, walking from 0: the number to pass for the one
108 after it, or -1 if there are no more, then its key and value. *)
109proc next*(const m: Map; i: INT): (INT, K, V);
110var k: K; v: V;
111begin
112 while i < m.cap do
113 if m.slots[i].used then return i + 1, m.slots[i].key, m.slots[i].val; end;
114 INC(i);
115 end;
116 return -1, k, v;
117end;
118
119(* Gives the map's memory back to its allocator, leaving it empty, as
120 init does: it may be used again, and grows anew. *)
121proc done*(var m: Map);
122begin
123 if m.slots # nil then Mem.free(m.a, ^U8(m.slots)); end;
124 m.slots := nil;
125 m.cap := 0;
126 m.len := 0;
127end;
128
129end Map.