| 1 | module scheme |
| 2 | |
| 3 | import hash.fnv |
| 4 | import math |
| 5 | import math.big |
| 6 | |
| 7 | // All eqv?/equal? numeric hashing goes through this helper. New exact-number |
| 8 | // tags must emit the same signed numerator/positive denominator word encoding. |
| 9 | fn numeric_equivalence_hash(value Value) ?u64 { |
| 10 | match value.tag { |
| 11 | .integer { return hash_small_exact_parts(value.whole(), 1) } |
| 12 | .rational { |
| 13 | part := value.rational_pointer() |
| 14 | return hash_small_exact_parts(part.numerator, part.denominator) |
| 15 | } |
| 16 | .big_integer, .big_rational { |
| 17 | numerator, denominator := big_exact_parts(value) or { return none } |
| 18 | return hash_big_component(hash_big_component(hash_word(fnv.offset64, 0x4558414354), numerator), denominator) |
| 19 | } |
| 20 | .exact_complex, .inexact_complex { |
| 21 | re, im := complex_parts(value) |
| 22 | rh := numeric_equivalence_hash(re) or { return none } |
| 23 | ih := numeric_equivalence_hash(im) or { return none } |
| 24 | return hash_word(hash_word(hash_word(fnv.offset64, 0x434f4d504c4558), rh), ih) |
| 25 | } |
| 26 | .real { |
| 27 | // eqv? identifies both signed zeros in this dialect. |
| 28 | bits := if value.floating() == 0 { u64(0) } else { math.f64_bits(value.floating()) } |
| 29 | return hash_word(hash_word(fnv.offset64, 0x5245414c), bits) |
| 30 | } |
| 31 | else { return none } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | fn hash_word(hash u64, word u64) u64 { |
| 36 | mut bytes := [8]u8{} |
| 37 | for index in 0 .. 8 { bytes[index] = u8(word >> (index * 8)) } |
| 38 | return fnv.write64a(hash, unsafe { (&u8(bytes)).vbytes(8) }) |
| 39 | } |
| 40 | |
| 41 | fn hash_small_component(hash u64, value i64) u64 { |
| 42 | negative := value < 0 |
| 43 | absolute := if negative { u64(-(value + 1)) + 1 } else { u64(value) } |
| 44 | words := if absolute == 0 { |
| 45 | 0 |
| 46 | } else if absolute <= 0xffffffff { |
| 47 | 1 |
| 48 | } else { |
| 49 | 2 |
| 50 | } |
| 51 | mut result := hash_word(hash_word(hash, u64(negative)), u64(words)) |
| 52 | if words > 0 { result = hash_word(result, u64(u32(absolute))) } |
| 53 | if words > 1 { result = hash_word(result, absolute >> 32) } |
| 54 | return result |
| 55 | } |
| 56 | |
| 57 | // The same signed magnitude stream as hash_small_component, read through |
| 58 | // approved immutable public-header views. Fixed work also keeps every bit index |
| 59 | // below 65536, even when bit_len itself exceeds the u32 get_bit index range. |
| 60 | const big_hash_word_limit = 2048 |
| 61 | |
| 62 | fn hash_big_component(hash u64, value big.Integer) u64 { |
| 63 | absolute := big_abs_view(value) |
| 64 | bits := absolute.bit_len() |
| 65 | words := bits / 32 + int(bits % 32 != 0) |
| 66 | mut result := hash_word(hash_word(hash, u64(value.signum < 0)), u64(words)) |
| 67 | observed := if words < big_hash_word_limit { words } else { big_hash_word_limit } |
| 68 | for index in 0 .. observed { |
| 69 | mut word := u64(0) |
| 70 | for bit in 0 .. 32 { |
| 71 | // index < 2048 proves this conversion is lossless and in range. |
| 72 | if absolute.get_bit(u32(index * 32 + bit)) { word |= u64(1) << bit } |
| 73 | } |
| 74 | result = hash_word(result, word) |
| 75 | } |
| 76 | if words > observed { result = hash_word(result, 0x424947435554) } |
| 77 | return result |
| 78 | } |
| 79 | |
| 80 | fn hash_small_exact_parts(numerator i64, denominator i64) u64 { |
| 81 | return hash_small_component(hash_small_component(hash_word(fnv.offset64, 0x4558414354), numerator), denominator) |
| 82 | } |
| 83 | |
| 84 | fn hash_raw_text(text string) u64 { |
| 85 | return fnv.write64a(fnv.offset64, unsafe { text.str.vbytes(text.len) }) |
| 86 | } |
| 87 | |
| 88 | fn identity_key_hash(value Value) u64 { |
| 89 | if numeric := numeric_equivalence_hash(value) { return numeric } |
| 90 | hash := hash_word(fnv.offset64, u64(value.tag)) |
| 91 | return match value.tag { |
| 92 | .unspecified, .nil { hash } |
| 93 | .boolean, .character, .codec, .transcoder { hash_word(hash, u64(value.whole())) } |
| 94 | .symbol, .keyword { hash_word(hash, hash_raw_text(value.text())) } |
| 95 | .syntax { hash_word(hash_word(hash, u64(value.pointer())), u64(u32(value.syntax_index))) } |
| 96 | else { hash_word(hash, u64(value.pointer())) } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | fn equal_leaf_hash(value Value) u64 { |
| 101 | if value.tag == .string { |
| 102 | return hash_word(hash_word(fnv.offset64, u64(value.tag)), hash_raw_text(value.text())) |
| 103 | } |
| 104 | if value.tag == .bytevector { |
| 105 | return hash_word(hash_word(fnv.offset64, u64(value.tag)), fnv.sum64a(value.bytevector_pointer().bytes)) |
| 106 | } |
| 107 | return identity_key_hash(value) |
| 108 | } |
| 109 | |
| 110 | struct StructuralHashFrame { |
| 111 | value Value |
| 112 | mut: |
| 113 | next int = -1 |
| 114 | } |
| 115 | |
| 116 | // These deterministic bounds describe the unfolded ordered tree, never graph |
| 117 | // identity or sharing. Thus coinductively equal cycles receive equal hashes. |
| 118 | // Fixed stack frames and a visit cap avoid both graph maps and exponential DAG |
| 119 | // unfolding. Long/deep keys with equal observed prefixes may collide. |
| 120 | @[direct_array_access] |
| 121 | fn bounded_structural_hash(root Value) u64 { |
| 122 | mut frames := [64]StructuralHashFrame{} |
| 123 | frames[0] = StructuralHashFrame{ value: root } |
| 124 | mut top := 0 |
| 125 | mut visited := 0 |
| 126 | mut hash := fnv.offset64 |
| 127 | for top >= 0 { |
| 128 | value := frames[top].value |
| 129 | if frames[top].next < 0 { |
| 130 | if visited == 1024 { return hash_word(hash, 0x574f524b435554) } |
| 131 | visited++ |
| 132 | if value.tag !in [.pair, .vector] { |
| 133 | hash = hash_word(hash, equal_leaf_hash(value)) |
| 134 | top-- |
| 135 | continue |
| 136 | } |
| 137 | hash = hash_word(hash, u64(value.tag)) |
| 138 | if value.tag == .vector { |
| 139 | hash = hash_word(hash, u64(value.vector_pointer().items.len)) |
| 140 | } |
| 141 | if top == frames.len - 1 { |
| 142 | hash = hash_word(hash, 0x4445505448435554) |
| 143 | top-- |
| 144 | continue |
| 145 | } |
| 146 | frames[top].next = 0 |
| 147 | } |
| 148 | index := frames[top].next |
| 149 | mut child := unspecified() |
| 150 | if value.tag == .pair { |
| 151 | if index == 2 { |
| 152 | top-- |
| 153 | continue |
| 154 | } |
| 155 | pair := value.pair_pointer() |
| 156 | child = if index == 0 { pair.car } else { pair.cdr } |
| 157 | } else { |
| 158 | items := value.vector_pointer().items |
| 159 | if index == items.len { |
| 160 | top-- |
| 161 | continue |
| 162 | } |
| 163 | child = items[index] |
| 164 | } |
| 165 | frames[top].next++ |
| 166 | top++ |
| 167 | frames[top] = StructuralHashFrame{ value: child } |
| 168 | } |
| 169 | return hash |
| 170 | } |
| 171 | |
| 172 | fn hashtable_equal_hash(mut _vm VM, args []Value) !Value { |
| 173 | check_arity('equal-hash', args.len, 1, 1)! |
| 174 | hash := if args[0].tag in [.pair, .vector] { |
| 175 | bounded_structural_hash(args[0]) |
| 176 | } else { |
| 177 | equal_leaf_hash(args[0]) |
| 178 | } |
| 179 | return integer(i64(hash & 0x7fffffffffffffff)) |
| 180 | } |
| 181 | |
| 182 | fn string_key_hash(value Value, fold bool) !Value { |
| 183 | text := string_data(value)! |
| 184 | mut hash := fnv.offset64 |
| 185 | // Match string=? / string-ci=? exactly, including replacement runes in |
| 186 | // malformed host UTF-8. equal? itself compares raw string bytes instead. |
| 187 | if fold { |
| 188 | mut cursor := UnicodeFoldCursor{ text: text } |
| 189 | for { |
| 190 | ch := cursor.next() or { break } |
| 191 | hash = hash_word(hash, u64(ch)) |
| 192 | } |
| 193 | } else { |
| 194 | for character in text.runes_iterator() { hash = hash_word(hash, u64(character)) } |
| 195 | } |
| 196 | return integer(i64(hash & 0x7fffffffffffffff)) |
| 197 | } |
| 198 | |
| 199 | fn hashtable_string_hash(mut _vm VM, args []Value) !Value { |
| 200 | check_arity('string-hash', args.len, 1, 1)! |
| 201 | return string_key_hash(args[0], false) |
| 202 | } |
| 203 | |
| 204 | fn hashtable_string_ci_hash(mut _vm VM, args []Value) !Value { |
| 205 | check_arity('string-ci-hash', args.len, 1, 1)! |
| 206 | return string_key_hash(args[0], true) |
| 207 | } |
| 208 | |
| 209 | fn hashtable_symbol_hash(mut _vm VM, args []Value) !Value { |
| 210 | check_arity('symbol-hash', args.len, 1, 1)! |
| 211 | if args[0].tag != .symbol { return failure('symbol-hash needs a symbol') } |
| 212 | return integer(i64(hash_raw_text(args[0].text()) & 0x7fffffffffffffff)) |
| 213 | } |
| 214 | |
| 215 | // Hash callbacks may return arbitrarily large nonnegative exact integers. |
| 216 | // Internal bucket hashes reduce them modulo 2^64 without allocating or narrowing |
| 217 | // through the lossy math.big.Integer.int() conversion. |
| 218 | fn hashtable_callback_hash(result Value) !u64 { |
| 219 | if !is_exact_integer(result) || exact_sign(result) < 0 { |
| 220 | return failure('hashtable hash must return a non-negative exact integer') |
| 221 | } |
| 222 | if result.tag == .integer { return u64(result.whole()) } |
| 223 | absolute := big_abs_view(result.big_integer_pointer().value) |
| 224 | mut hash := u64(0) |
| 225 | for bit in 0 .. 64 { |
| 226 | if absolute.get_bit(u32(bit)) { hash |= u64(1) << bit } |
| 227 | } |
| 228 | return hash |
| 229 | } |
| 230 | |