|
| 1 | +package dotty.tools.dotc.util |
| 2 | + |
| 3 | +object PerfectHashing: |
| 4 | + |
| 5 | + /** The number of elements up to which dense packing is used. |
| 6 | + * If the number of elements reaches `DenseLimit` a hash table is used instead |
| 7 | + */ |
| 8 | + inline val DenseLimit = 16 |
| 9 | + |
| 10 | +/** A map that maps keys to unique integers in a dense interval starting at 0. |
| 11 | + * @param initialCapacity Indicates the initial number of slots in the hash table. |
| 12 | + * The actual number of slots is always a power of 2, so the |
| 13 | + * initial size of the table will be the smallest power of two |
| 14 | + * that is equal or greater than the given `initialCapacity`. |
| 15 | + * Minimum value is 4. |
| 16 | + * @param capacityMultiple The minimum multiple of capacity relative to used elements. |
| 17 | + * The hash table will be re-sized once the number of elements |
| 18 | + * multiplied by capacityMultiple exceeds the current size of the hash table. |
| 19 | + * However, a table of size up to DenseLimit will be re-sized only |
| 20 | + * once the number of elements reaches the table's size. |
| 21 | + */ |
| 22 | +class PerfectHashing[Key](initialCapacity: Int = 8, capacityMultiple: Int = 2): |
| 23 | + import PerfectHashing.DenseLimit |
| 24 | + |
| 25 | + private var used: Int = _ |
| 26 | + private var table: Array[Int] = _ |
| 27 | + private var keys: Array[AnyRef] = _ |
| 28 | + |
| 29 | + clear() |
| 30 | + |
| 31 | + protected def allocate(capacity: Int) = |
| 32 | + keys = new Array[AnyRef](capacity) |
| 33 | + if capacity > DenseLimit then |
| 34 | + table = new Array[Int](capacity * roundToPower(capacityMultiple)) |
| 35 | + |
| 36 | + private def roundToPower(n: Int) = |
| 37 | + if Integer.bitCount(n) == 1 then n |
| 38 | + else 1 << (32 - Integer.numberOfLeadingZeros(n)) |
| 39 | + |
| 40 | + /** Remove keys from this map and set back to initial configuration */ |
| 41 | + def clear(): Unit = |
| 42 | + used = 0 |
| 43 | + allocate(roundToPower(initialCapacity max 4)) |
| 44 | + |
| 45 | + /** The number of keys */ |
| 46 | + final def size: Int = used |
| 47 | + |
| 48 | + /** The number of keys that can be stored without growing the tables */ |
| 49 | + final def capacity: Int = keys.length |
| 50 | + |
| 51 | + private final def isDense = capacity <= DenseLimit |
| 52 | + |
| 53 | + /** Hashcode, by default `x.hashCode`, can be overridden */ |
| 54 | + protected def hash(x: Key): Int = x.hashCode |
| 55 | + |
| 56 | + /** Hashcode, by default `equals`, can be overridden */ |
| 57 | + protected def isEqual(x: Key, y: Key): Boolean = x.equals(y) |
| 58 | + |
| 59 | + private def matches(entry: Int, k: Key) = isEqual(key(entry), k) |
| 60 | + |
| 61 | + private def tableIndex(x: Int): Int = x & (table.length - 1) |
| 62 | + private def firstIndex(k: Key) = tableIndex(hash(k)) |
| 63 | + private def nextIndex(idx: Int) = tableIndex(idx + 1) |
| 64 | + |
| 65 | + /** The key at index `idx` */ |
| 66 | + def key(idx: Int) = keys(idx).asInstanceOf[Key] |
| 67 | + |
| 68 | + private def setKey(e: Int, k: Key) = keys(e) = k.asInstanceOf[AnyRef] |
| 69 | + |
| 70 | + private def entry(idx: Int): Int = table(idx) - 1 |
| 71 | + private def setEntry(idx: Int, entry: Int) = table(idx) = entry + 1 |
| 72 | + |
| 73 | + /** An index `idx` such that `key(idx) == k`, or -1 if no such index exists */ |
| 74 | + def index(k: Key): Int = |
| 75 | + if isDense then |
| 76 | + var e = 0 |
| 77 | + while e < used do |
| 78 | + if matches(e, k) then return e |
| 79 | + e += 1 |
| 80 | + -1 |
| 81 | + else |
| 82 | + var idx = firstIndex(k) |
| 83 | + var e = entry(idx) |
| 84 | + while e >= 0 && !matches(e, k) do |
| 85 | + idx = nextIndex(idx) |
| 86 | + e = entry(idx) |
| 87 | + e |
| 88 | + |
| 89 | + /** An index `idx` such that key(idx) == k. |
| 90 | + * If no such index exists, create an entry with an index one |
| 91 | + * larger than the previous one. |
| 92 | + */ |
| 93 | + def add(k: Key): Int = |
| 94 | + if isDense then |
| 95 | + var e = 0 |
| 96 | + while e < used do |
| 97 | + if matches(e, k) then return e |
| 98 | + e += 1 |
| 99 | + else |
| 100 | + var idx = firstIndex(k) |
| 101 | + var e = entry(idx) |
| 102 | + while e >= 0 do |
| 103 | + if matches(e, k) then return e |
| 104 | + idx = nextIndex(idx) |
| 105 | + e = entry(idx) |
| 106 | + setEntry(idx, used) |
| 107 | + end if |
| 108 | + setKey(used, k) |
| 109 | + used = used + 1 |
| 110 | + if used == capacity then growTable() |
| 111 | + used - 1 |
| 112 | + |
| 113 | + private def rehash(): Unit = |
| 114 | + var e = 0 |
| 115 | + while e < used do |
| 116 | + var idx = firstIndex(key(e)) |
| 117 | + while entry(idx) >= 0 do idx = nextIndex(idx) |
| 118 | + setEntry(idx, e) |
| 119 | + e += 1 |
| 120 | + |
| 121 | + /** Grow backing arrays */ |
| 122 | + protected def growTable(): Unit = |
| 123 | + val oldKeys = keys |
| 124 | + allocate(capacity * 2) |
| 125 | + Array.copy(oldKeys, 0, keys, 0, oldKeys.length) |
| 126 | + if !isDense then rehash() |
| 127 | + |
| 128 | + def keysIterator: Iterator[Key] = |
| 129 | + keys.iterator.take(used).asInstanceOf[Iterator[Key]] |
| 130 | +end PerfectHashing |
0 commit comments