| 1 | (* A generic hash map, open addressing with linear probing; it hashes and |
| 2 | compares its keys with the procedures it is given. *) |
| 3 | module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); |
| 4 | import Mem; |
| 5 | |
| 6 | type |
| 7 | Entry = record key: K; val: V; used: BOOL; end; |
| 8 | Map* = record |
| 9 | len*: INT; |
| 10 | slots: ^Entry; (* cap of them; cap is 0 or a power of 2 *) |
| 11 | cap: INT; |
| 12 | a: ^Mem.Allocator; |
| 13 | end; |
| 14 | |
| 15 | proc init*(m: ^Map; a: ^Mem.Allocator); |
| 16 | begin |
| 17 | m^.len := 0; |
| 18 | m^.slots := nil; |
| 19 | m^.cap := 0; |
| 20 | m^.a := a; |
| 21 | end; |
| 22 | |
| 23 | (* the slot of k: where it is, or the free slot it would go in *) |
| 24 | proc slot(m: ^Map; k: K): INT; |
| 25 | var i: INT; |
| 26 | begin |
| 27 | i := INT(hash(k) & U64(m^.cap - 1)); |
| 28 | while m^.slots[i].used and not eq(m^.slots[i].key, k) do |
| 29 | i := (i + 1) & (m^.cap - 1); |
| 30 | end; |
| 31 | return i; |
| 32 | end; |
| 33 | |
| 34 | (* twice the slots, every entry moved; false if there is no room *) |
| 35 | proc grow(m: ^Map): BOOL; |
| 36 | var old: ^Entry; n, i, j, cap: INT; p: ^U8; |
| 37 | begin |
| 38 | cap := m^.cap * 2; |
| 39 | if cap = 0 then cap := 8; end; |
| 40 | p := Mem.alloc(m^.a, cap * SIZE(Entry), 8); |
| 41 | if p = nil then return false; end; |
| 42 | for i := 0 to cap * SIZE(Entry) - 1 do p[i] := 0; end; |
| 43 | old := m^.slots; |
| 44 | n := m^.cap; |
| 45 | m^.slots := ^Entry(p); |
| 46 | m^.cap := cap; |
| 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; |
| 55 | end; |
| 56 | |
| 57 | (* k now maps to v; false if there was no room *) |
| 58 | proc put*(m: ^Map; k: K; v: V): BOOL; |
| 59 | var i: INT; |
| 60 | begin |
| 61 | if (m^.len + 1) * 4 > m^.cap * 3 and not grow(m) then return false; end; |
| 62 | i := slot(m, k); |
| 63 | if not m^.slots[i].used then |
| 64 | m^.slots[i].used := true; |
| 65 | m^.slots[i].key := k; |
| 66 | INC(m^.len); |
| 67 | end; |
| 68 | m^.slots[i].val := v; |
| 69 | return true; |
| 70 | end; |
| 71 | |
| 72 | (* the slot k's hash points at *) |
| 73 | proc home(m: ^Map; k: K): INT; |
| 74 | begin |
| 75 | return INT(hash(k) & U64(m^.cap - 1)); |
| 76 | end; |
| 77 | |
| 78 | (* false if k wasn't there *) |
| 79 | proc remove*(m: ^Map; k: K): BOOL; |
| 80 | var i, j, h: INT; |
| 81 | begin |
| 82 | if m^.cap = 0 then return false; end; |
| 83 | i := slot(m, k); |
| 84 | if not m^.slots[i].used then return false; end; |
| 85 | m^.slots[i].used := false; |
| 86 | DEC(m^.len); |
| 87 | (* each entry after it in the run moves back into the hole, unless its |
| 88 | home is between the hole and where it is *) |
| 89 | j := (i + 1) & (m^.cap - 1); |
| 90 | while m^.slots[j].used do |
| 91 | h := home(m, m^.slots[j].key); |
| 92 | if (j > i and (h <= i or h > j)) or (j < i and h <= i and h > j) then |
| 93 | m^.slots[i] := m^.slots[j]; |
| 94 | m^.slots[j].used := false; |
| 95 | i := j; |
| 96 | end; |
| 97 | j := (j + 1) & (m^.cap - 1); |
| 98 | end; |
| 99 | return true; |
| 100 | end; |
| 101 | |
| 102 | (* what k maps to, and whether it maps to anything *) |
| 103 | proc get*(m: ^Map; k: K): (V, BOOL); |
| 104 | var i: INT; v: V; |
| 105 | begin |
| 106 | if m^.cap = 0 then return v, false; end; |
| 107 | i := slot(m, k); |
| 108 | if not m^.slots[i].used then return v, false; end; |
| 109 | return m^.slots[i].val, true; |
| 110 | end; |
| 111 | |
| 112 | (* the entry after i: from 0, the next entry and the number to pass for |
| 113 | the one after it; -1 when there are no more *) |
| 114 | proc next*(m: ^Map; i: INT): (INT, K, V); |
| 115 | var k: K; v: V; |
| 116 | begin |
| 117 | while i < m^.cap do |
| 118 | if m^.slots[i].used then return i + 1, m^.slots[i].key, m^.slots[i].val; end; |
| 119 | INC(i); |
| 120 | end; |
| 121 | return -1, k, v; |
| 122 | end; |
| 123 | |
| 124 | end Map. |