| 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); *) |
| 4 | module Map(K, V; hash: proc(k: K): U64; eq: proc(a, b: K): BOOL); |
| 5 | import Mem; |
| 6 | import SYSTEM; |
| 7 | |
| 8 | type |
| 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. *) |
| 22 | proc init*(var m: Map; a: ^Mem.Allocator); |
| 23 | begin |
| 24 | m := {a: a}; |
| 25 | end; |
| 26 | |
| 27 | (* The slot of k, or of the empty slot where it would go. *) |
| 28 | proc slot(const m: Map; k: K): INT; |
| 29 | var i: INT; |
| 30 | begin |
| 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; |
| 36 | end; |
| 37 | |
| 38 | proc grow(var m: Map): BOOL; |
| 39 | var old: ^Entry; n, i, j: INT; |
| 40 | begin |
| 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; |
| 55 | end; |
| 56 | |
| 57 | (* k now maps to v; false if there was no room. *) |
| 58 | proc put*(var m: Map; k: K; v: V): BOOL; |
| 59 | var i: INT; |
| 60 | begin |
| 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; |
| 71 | end; |
| 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. *) |
| 76 | proc remove*(var m: Map; k: K): BOOL; |
| 77 | var i, j, home: INT; |
| 78 | begin |
| 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; |
| 95 | end; |
| 96 | |
| 97 | (* What k maps to, and whether it maps to anything. *) |
| 98 | proc get*(const m: Map; k: K): (V, BOOL); |
| 99 | var i: INT; none: V; |
| 100 | begin |
| 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; |
| 105 | end; |
| 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. *) |
| 109 | proc next*(const m: Map; i: INT): (INT, K, V); |
| 110 | var k: K; v: V; |
| 111 | begin |
| 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; |
| 117 | end; |
| 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. *) |
| 121 | proc done*(var m: Map); |
| 122 | begin |
| 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; |
| 127 | end; |
| 128 | |
| 129 | end Map. |