/scheme/_srfi/69/hashtable.mzscheme.ss

https://github.com/LFY/bpm · Scheme · 270 lines · 233 code · 37 blank · 0 comment · 1 complexity · 2fa25f68a7b5915f451dd0d9b0572e26 MD5 · raw file

  1. #!r6rs
  2. (library (_srfi :69 hashtable)
  3. (export
  4. make-hash-table hash-table? alist->hash-table
  5. hash-table-equivalence-function hash-table-hash-function
  6. hash-table-ref hash-table-ref/default hash-table-set! hash-table-delete! hash-table-exists? hash-table-update! hash-table-update!/default
  7. hash-table-size hash-table-keys hash-table-values hash-table-walk hash-table-fold hash-table->alist hash-table-copy hash-table-merge!
  8. hash string-hash string-ci-hash hash-by-identity)
  9. (import (except (rnrs) define-record-type error string-hash string-ci-hash symbol-hash)
  10. (rnrs mutable-pairs (6))
  11. (only (mzscheme) modulo)
  12. (_srfi :23) ; error
  13. (_srfi :9)) ; records
  14. (define *default-bound* (- (expt 2 29) 3))
  15. (define (%string-hash s ch-conv bound)
  16. (let ((hash 31)
  17. (len (string-length s)))
  18. (do ((index 0 (+ index 1)))
  19. ((>= index len) (modulo hash bound))
  20. (set! hash (modulo (+ (* 37 hash)
  21. (char->integer (ch-conv (string-ref s index))))
  22. *default-bound*)))))
  23. (define (string-hash s . maybe-bound)
  24. (let ((bound (if (null? maybe-bound) *default-bound* (car maybe-bound))))
  25. (%string-hash s (lambda (x) x) bound)))
  26. (define (string-ci-hash s . maybe-bound)
  27. (let ((bound (if (null? maybe-bound) *default-bound* (car maybe-bound))))
  28. (%string-hash s char-downcase bound)))
  29. (define (symbol-hash s . maybe-bound)
  30. (let ((bound (if (null? maybe-bound) *default-bound* (car maybe-bound))))
  31. (%string-hash (symbol->string s) (lambda (x) x) bound)))
  32. (define (hash obj . maybe-bound)
  33. (let ((bound (if (null? maybe-bound) *default-bound* (car maybe-bound))))
  34. (cond ((integer? obj) (modulo obj bound))
  35. ((string? obj) (string-hash obj bound))
  36. ((symbol? obj) (symbol-hash obj bound))
  37. ((real? obj) (modulo (+ (numerator obj) (denominator obj)) bound))
  38. ((number? obj)
  39. (modulo (+ (hash (real-part obj)) (* 3 (hash (imag-part obj))))
  40. bound))
  41. ((char? obj) (modulo (char->integer obj) bound))
  42. ((vector? obj) (vector-hash obj bound))
  43. ((pair? obj) (modulo (+ (hash (car obj)) (* 3 (hash (cdr obj))))
  44. bound))
  45. ((null? obj) 0)
  46. ((not obj) 0)
  47. ((procedure? obj) (error "hash: procedures cannot be hashed" obj))
  48. (else 1))))
  49. (define hash-by-identity hash)
  50. (define (vector-hash v bound)
  51. (let ((hashvalue 571)
  52. (len (vector-length v)))
  53. (do ((index 0 (+ index 1)))
  54. ((>= index len) (modulo hashvalue bound))
  55. (set! hashvalue (modulo (+ (* 257 hashvalue) (hash (vector-ref v index)))
  56. *default-bound*)))))
  57. (define %make-hash-node cons)
  58. (define %hash-node-set-value! set-cdr!)
  59. (define %hash-node-key car)
  60. (define %hash-node-value cdr)
  61. (define-record-type <srfi-hash-table>
  62. (%make-hash-table size hash compare associate entries)
  63. hash-table?
  64. (size hash-table-size hash-table-set-size!)
  65. (hash hash-table-hash-function)
  66. (compare hash-table-equivalence-function)
  67. (associate hash-table-association-function)
  68. (entries hash-table-entries hash-table-set-entries!))
  69. (define *default-table-size* 64)
  70. (define (appropriate-hash-function-for comparison)
  71. (or (and (eq? comparison eq?) hash-by-identity)
  72. (and (eq? comparison string=?) string-hash)
  73. (and (eq? comparison string-ci=?) string-ci-hash)
  74. hash))
  75. (define (make-hash-table . args)
  76. (let* ((comparison (if (null? args) equal? (car args)))
  77. (hash
  78. (if (or (null? args) (null? (cdr args)))
  79. (appropriate-hash-function-for comparison) (cadr args)))
  80. (size
  81. (if (or (null? args) (null? (cdr args)) (null? (cddr args)))
  82. *default-table-size* (caddr args)))
  83. (association
  84. (or (and (eq? comparison eq?) assq)
  85. (and (eq? comparison eqv?) assv)
  86. (and (eq? comparison equal?) assoc)
  87. (letrec
  88. ((associate
  89. (lambda (val alist)
  90. (cond ((null? alist) #f)
  91. ((comparison val (caar alist)) (car alist))
  92. (else (associate val (cdr alist)))))))
  93. associate))))
  94. (%make-hash-table 0 hash comparison association (make-vector size '()))))
  95. (define (make-hash-table-maker comp hash)
  96. (lambda args (apply make-hash-table (cons comp (cons hash args)))))
  97. (define make-symbol-hash-table
  98. (make-hash-table-maker eq? symbol-hash))
  99. (define make-string-hash-table
  100. (make-hash-table-maker string=? string-hash))
  101. (define make-string-ci-hash-table
  102. (make-hash-table-maker string-ci=? string-ci-hash))
  103. (define make-integer-hash-table
  104. (make-hash-table-maker = modulo))
  105. (define (%hash-table-hash hash-table key)
  106. ((hash-table-hash-function hash-table)
  107. key (vector-length (hash-table-entries hash-table))))
  108. (define (%hash-table-find entries associate hash key)
  109. (associate key (vector-ref entries hash)))
  110. (define (%hash-table-add! entries hash key value)
  111. (vector-set! entries hash
  112. (cons (%make-hash-node key value)
  113. (vector-ref entries hash))))
  114. (define (%hash-table-delete! entries compare hash key)
  115. (let ((entrylist (vector-ref entries hash)))
  116. (cond ((null? entrylist) #f)
  117. ((compare key (caar entrylist))
  118. (vector-set! entries hash (cdr entrylist)) #t)
  119. (else
  120. (let loop ((current (cdr entrylist)) (previous entrylist))
  121. (cond ((null? current) #f)
  122. ((compare key (caar current))
  123. (set-cdr! previous (cdr current)) #t)
  124. (else (loop (cdr current) current))))))))
  125. (define (%hash-table-walk proc entries)
  126. (do ((index (- (vector-length entries) 1) (- index 1)))
  127. ((< index 0)) (for-each proc (vector-ref entries index))))
  128. (define (%hash-table-maybe-resize! hash-table)
  129. (let* ((old-entries (hash-table-entries hash-table))
  130. (hash-length (vector-length old-entries)))
  131. (if (> (hash-table-size hash-table) hash-length)
  132. (let* ((new-length (* 2 hash-length))
  133. (new-entries (make-vector new-length '()))
  134. (hash (hash-table-hash-function hash-table)))
  135. (%hash-table-walk
  136. (lambda (node)
  137. (%hash-table-add! new-entries
  138. (hash (%hash-node-key node) new-length)
  139. (%hash-node-key node) (%hash-node-value node)))
  140. old-entries)
  141. (hash-table-set-entries! hash-table new-entries)))))
  142. (define (hash-table-ref hash-table key . maybe-default)
  143. (cond ((%hash-table-find (hash-table-entries hash-table)
  144. (hash-table-association-function hash-table)
  145. (%hash-table-hash hash-table key) key)
  146. => %hash-node-value)
  147. ((null? maybe-default)
  148. (error "hash-table-ref: no value associated with" key))
  149. (else ((car maybe-default)))))
  150. (define (hash-table-ref/default hash-table key default)
  151. (hash-table-ref hash-table key (lambda () default)))
  152. (define (hash-table-set! hash-table key value)
  153. (let ((hash (%hash-table-hash hash-table key))
  154. (entries (hash-table-entries hash-table)))
  155. (cond ((%hash-table-find entries
  156. (hash-table-association-function hash-table)
  157. hash key)
  158. => (lambda (node) (%hash-node-set-value! node value)))
  159. (else (%hash-table-add! entries hash key value)
  160. (hash-table-set-size! hash-table
  161. (+ 1 (hash-table-size hash-table)))
  162. (%hash-table-maybe-resize! hash-table)))))
  163. (define (hash-table-update! hash-table key function . maybe-default)
  164. (let ((hash (%hash-table-hash hash-table key))
  165. (entries (hash-table-entries hash-table)))
  166. (cond ((%hash-table-find entries
  167. (hash-table-association-function hash-table)
  168. hash key)
  169. => (lambda (node)
  170. (%hash-node-set-value!
  171. node (function (%hash-node-value node)))))
  172. ((null? maybe-default)
  173. (error "hash-table-update!: no value exists for key" key))
  174. (else (%hash-table-add! entries hash key
  175. (function ((car maybe-default))))
  176. (hash-table-set-size! hash-table
  177. (+ 1 (hash-table-size hash-table)))
  178. (%hash-table-maybe-resize! hash-table)))))
  179. (define (hash-table-update!/default hash-table key function default)
  180. (hash-table-update! hash-table key function (lambda () default)))
  181. (define (hash-table-delete! hash-table key)
  182. (if (%hash-table-delete! (hash-table-entries hash-table)
  183. (hash-table-equivalence-function hash-table)
  184. (%hash-table-hash hash-table key) key)
  185. (hash-table-set-size! hash-table (- (hash-table-size hash-table) 1))))
  186. (define (hash-table-exists? hash-table key)
  187. (and (%hash-table-find (hash-table-entries hash-table)
  188. (hash-table-association-function hash-table)
  189. (%hash-table-hash hash-table key) key) #t))
  190. (define (hash-table-walk hash-table proc)
  191. (%hash-table-walk
  192. (lambda (node) (proc (%hash-node-key node) (%hash-node-value node)))
  193. (hash-table-entries hash-table)))
  194. (define (hash-table-fold hash-table f acc)
  195. (hash-table-walk hash-table
  196. (lambda (key value) (set! acc (f key value acc))))
  197. acc)
  198. (define (alist->hash-table alist . args)
  199. (let* ((comparison (if (null? args) equal? (car args)))
  200. (hash
  201. (if (or (null? args) (null? (cdr args)))
  202. (appropriate-hash-function-for comparison) (cadr args)))
  203. (size
  204. (if (or (null? args) (null? (cdr args)) (null? (cddr args)))
  205. (max *default-table-size* (* 2 (length alist))) (caddr args)))
  206. (hash-table (make-hash-table comparison hash size)))
  207. (for-each
  208. (lambda (elem)
  209. (hash-table-update!/default
  210. hash-table (car elem) (lambda (x) x) (cdr elem)))
  211. alist)
  212. hash-table))
  213. (define (hash-table->alist hash-table)
  214. (hash-table-fold hash-table
  215. (lambda (key val acc) (cons (cons key val) acc)) '()))
  216. (define (hash-table-copy hash-table)
  217. (let ((new (make-hash-table (hash-table-equivalence-function hash-table)
  218. (hash-table-hash-function hash-table)
  219. (max *default-table-size*
  220. (* 2 (hash-table-size hash-table))))))
  221. (hash-table-walk hash-table
  222. (lambda (key value) (hash-table-set! new key value)))
  223. new))
  224. (define (hash-table-merge! hash-table1 hash-table2)
  225. (hash-table-walk
  226. hash-table2
  227. (lambda (key value) (hash-table-set! hash-table1 key value)))
  228. hash-table1)
  229. (define (hash-table-keys hash-table)
  230. (hash-table-fold hash-table (lambda (key val acc) (cons key acc)) '()))
  231. (define (hash-table-values hash-table)
  232. (hash-table-fold hash-table (lambda (key val acc) (cons val acc)) '()))
  233. )