69.upstream.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. ;;; Copyright © Panu Kalliokoski (2005). All Rights Reserved.
  2. ;;; Permission is hereby granted, free of charge, to any person obtaining a copy
  3. ;;; of this software and associated documentation files (the "Software"), to
  4. ;;; deal in the Software without restriction, including without limitation the
  5. ;;; rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  6. ;;; sell copies of the Software, and to permit persons to whom the Software is
  7. ;;; furnished to do so, subject to the following conditions:
  8. ;;; The above copyright notice and this permission notice shall be included in
  9. ;;; all copies or substantial portions of the Software.
  10. ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  11. ;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. ;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  13. ;;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  14. ;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  15. ;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  16. ;;; IN THE SOFTWARE.
  17. (define *default-bound* (- (expt 2 29) 3))
  18. (define (%string-hash s ch-conv bound)
  19. (let ((hash 31)
  20. (len (string-length s)))
  21. (do ((index 0 (+ index 1)))
  22. ((>= index len) (modulo hash bound))
  23. (set! hash (modulo (+ (* 37 hash)
  24. (char->integer (ch-conv (string-ref s index))))
  25. *default-bound*)))))
  26. (define (string-hash s . maybe-bound)
  27. (let ((bound (if (null? maybe-bound) *default-bound* (car maybe-bound))))
  28. (%string-hash s (lambda (x) x) bound)))
  29. (define (string-ci-hash s . maybe-bound)
  30. (let ((bound (if (null? maybe-bound) *default-bound* (car maybe-bound))))
  31. (%string-hash s char-downcase bound)))
  32. (define (symbol-hash s . maybe-bound)
  33. (let ((bound (if (null? maybe-bound) *default-bound* (car maybe-bound))))
  34. (%string-hash (symbol->string s) (lambda (x) x) bound)))
  35. (define (hash obj . maybe-bound)
  36. (let ((bound (if (null? maybe-bound) *default-bound* (car maybe-bound))))
  37. (cond ((integer? obj) (modulo obj bound))
  38. ((string? obj) (string-hash obj bound))
  39. ((symbol? obj) (symbol-hash obj bound))
  40. ((real? obj) (modulo (+ (numerator obj) (denominator obj)) bound))
  41. ((number? obj)
  42. (modulo (+ (hash (real-part obj)) (* 3 (hash (imag-part obj))))
  43. bound))
  44. ((char? obj) (modulo (char->integer obj) bound))
  45. ((vector? obj) (vector-hash obj bound))
  46. ((pair? obj) (modulo (+ (hash (car obj)) (* 3 (hash (cdr obj))))
  47. bound))
  48. ((null? obj) 0)
  49. ((not obj) 0)
  50. ((procedure? obj) (error "hash: procedures cannot be hashed" obj))
  51. (else 1))))
  52. (define hash-by-identity hash)
  53. (define (vector-hash v bound)
  54. (let ((hashvalue 571)
  55. (len (vector-length v)))
  56. (do ((index 0 (+ index 1)))
  57. ((>= index len) (modulo hashvalue bound))
  58. (set! hashvalue (modulo (+ (* 257 hashvalue) (hash (vector-ref v index)))
  59. *default-bound*)))))
  60. (define %make-hash-node cons)
  61. (define %hash-node-set-value! set-cdr!)
  62. (define %hash-node-key car)
  63. (define %hash-node-value cdr)
  64. (define-record-type <srfi-hash-table>
  65. (%make-hash-table size hash compare associate entries)
  66. hash-table?
  67. (size hash-table-size hash-table-set-size!)
  68. (hash hash-table-hash-function)
  69. (compare hash-table-equivalence-function)
  70. (associate hash-table-association-function)
  71. (entries hash-table-entries hash-table-set-entries!))
  72. (define *default-table-size* 64)
  73. (define (appropriate-hash-function-for comparison)
  74. (or (and (eq? comparison eq?) hash-by-identity)
  75. (and (eq? comparison string=?) string-hash)
  76. (and (eq? comparison string-ci=?) string-ci-hash)
  77. hash))
  78. (define (make-hash-table . args)
  79. (let* ((comparison (if (null? args) equal? (car args)))
  80. (hash
  81. (if (or (null? args) (null? (cdr args)))
  82. (appropriate-hash-function-for comparison) (cadr args)))
  83. (size
  84. (if (or (null? args) (null? (cdr args)) (null? (cddr args)))
  85. *default-table-size* (caddr args)))
  86. (association
  87. (or (and (eq? comparison eq?) assq)
  88. (and (eq? comparison eqv?) assv)
  89. (and (eq? comparison equal?) assoc)
  90. (letrec
  91. ((associate
  92. (lambda (val alist)
  93. (cond ((null? alist) #f)
  94. ((comparison val (caar alist)) (car alist))
  95. (else (associate val (cdr alist)))))))
  96. associate))))
  97. (%make-hash-table 0 hash comparison association (make-vector size '()))))
  98. (define (make-hash-table-maker comp hash)
  99. (lambda args (apply make-hash-table (cons comp (cons hash args)))))
  100. (define make-symbol-hash-table
  101. (make-hash-table-maker eq? symbol-hash))
  102. (define make-string-hash-table
  103. (make-hash-table-maker string=? string-hash))
  104. (define make-string-ci-hash-table
  105. (make-hash-table-maker string-ci=? string-ci-hash))
  106. (define make-integer-hash-table
  107. (make-hash-table-maker = modulo))
  108. (define (%hash-table-hash hash-table key)
  109. ((hash-table-hash-function hash-table)
  110. key (vector-length (hash-table-entries hash-table))))
  111. (define (%hash-table-find entries associate hash key)
  112. (associate key (vector-ref entries hash)))
  113. (define (%hash-table-add! entries hash key value)
  114. (vector-set! entries hash
  115. (cons (%make-hash-node key value)
  116. (vector-ref entries hash))))
  117. (define (%hash-table-delete! entries compare hash key)
  118. (let ((entrylist (vector-ref entries hash)))
  119. (cond ((null? entrylist) #f)
  120. ((compare key (caar entrylist))
  121. (vector-set! entries hash (cdr entrylist)) #t)
  122. (else
  123. (let loop ((current (cdr entrylist)) (previous entrylist))
  124. (cond ((null? current) #f)
  125. ((compare key (caar current))
  126. (set-cdr! previous (cdr current)) #t)
  127. (else (loop (cdr current) current))))))))
  128. (define (%hash-table-walk proc entries)
  129. (do ((index (- (vector-length entries) 1) (- index 1)))
  130. ((< index 0)) (for-each proc (vector-ref entries index))))
  131. (define (%hash-table-maybe-resize! hash-table)
  132. (let* ((old-entries (hash-table-entries hash-table))
  133. (hash-length (vector-length old-entries)))
  134. (if (> (hash-table-size hash-table) hash-length)
  135. (let* ((new-length (* 2 hash-length))
  136. (new-entries (make-vector new-length '()))
  137. (hash (hash-table-hash-function hash-table)))
  138. (%hash-table-walk
  139. (lambda (node)
  140. (%hash-table-add! new-entries
  141. (hash (%hash-node-key node) new-length)
  142. (%hash-node-key node) (%hash-node-value node)))
  143. old-entries)
  144. (hash-table-set-entries! hash-table new-entries)))))
  145. (define (hash-table-ref hash-table key . maybe-default)
  146. (cond ((%hash-table-find (hash-table-entries hash-table)
  147. (hash-table-association-function hash-table)
  148. (%hash-table-hash hash-table key) key)
  149. => %hash-node-value)
  150. ((null? maybe-default)
  151. (error "hash-table-ref: no value associated with" key))
  152. (else ((car maybe-default)))))
  153. (define (hash-table-ref/default hash-table key default)
  154. (hash-table-ref hash-table key (lambda () default)))
  155. (define (hash-table-set! hash-table key value)
  156. (let ((hash (%hash-table-hash hash-table key))
  157. (entries (hash-table-entries hash-table)))
  158. (cond ((%hash-table-find entries
  159. (hash-table-association-function hash-table)
  160. hash key)
  161. => (lambda (node) (%hash-node-set-value! node value)))
  162. (else (%hash-table-add! entries hash key value)
  163. (hash-table-set-size! hash-table
  164. (+ 1 (hash-table-size hash-table)))
  165. (%hash-table-maybe-resize! hash-table)))))
  166. (define (hash-table-update! hash-table key function . maybe-default)
  167. (let ((hash (%hash-table-hash hash-table key))
  168. (entries (hash-table-entries hash-table)))
  169. (cond ((%hash-table-find entries
  170. (hash-table-association-function hash-table)
  171. hash key)
  172. => (lambda (node)
  173. (%hash-node-set-value!
  174. node (function (%hash-node-value node)))))
  175. ((null? maybe-default)
  176. (error "hash-table-update!: no value exists for key" key))
  177. (else (%hash-table-add! entries hash key
  178. (function ((car maybe-default))))
  179. (hash-table-set-size! hash-table
  180. (+ 1 (hash-table-size hash-table)))
  181. (%hash-table-maybe-resize! hash-table)))))
  182. (define (hash-table-update!/default hash-table key function default)
  183. (hash-table-update! hash-table key function (lambda () default)))
  184. (define (hash-table-delete! hash-table key)
  185. (if (%hash-table-delete! (hash-table-entries hash-table)
  186. (hash-table-equivalence-function hash-table)
  187. (%hash-table-hash hash-table key) key)
  188. (hash-table-set-size! hash-table (- (hash-table-size hash-table) 1))))
  189. (define (hash-table-exists? hash-table key)
  190. (and (%hash-table-find (hash-table-entries hash-table)
  191. (hash-table-association-function hash-table)
  192. (%hash-table-hash hash-table key) key) #t))
  193. (define (hash-table-walk hash-table proc)
  194. (%hash-table-walk
  195. (lambda (node) (proc (%hash-node-key node) (%hash-node-value node)))
  196. (hash-table-entries hash-table)))
  197. (define (hash-table-fold hash-table f acc)
  198. (hash-table-walk hash-table
  199. (lambda (key value) (set! acc (f key value acc))))
  200. acc)
  201. (define (alist->hash-table alist . args)
  202. (let* ((comparison (if (null? args) equal? (car args)))
  203. (hash
  204. (if (or (null? args) (null? (cdr args)))
  205. (appropriate-hash-function-for comparison) (cadr args)))
  206. (size
  207. (if (or (null? args) (null? (cdr args)) (null? (cddr args)))
  208. (max *default-table-size* (* 2 (length alist))) (caddr args)))
  209. (hash-table (make-hash-table comparison hash size)))
  210. (for-each
  211. (lambda (elem)
  212. (hash-table-update!/default
  213. hash-table (car elem) (lambda (x) x) (cdr elem)))
  214. alist)
  215. hash-table))
  216. (define (hash-table->alist hash-table)
  217. (hash-table-fold hash-table
  218. (lambda (key val acc) (cons (cons key val) acc)) '()))
  219. (define (hash-table-copy hash-table)
  220. (let ((new (make-hash-table (hash-table-equivalence-function hash-table)
  221. (hash-table-hash-function hash-table)
  222. (max *default-table-size*
  223. (* 2 (hash-table-size hash-table))))))
  224. (hash-table-walk hash-table
  225. (lambda (key value) (hash-table-set! new key value)))
  226. new))
  227. (define (hash-table-merge! hash-table1 hash-table2)
  228. (hash-table-walk
  229. hash-table2
  230. (lambda (key value) (hash-table-set! hash-table1 key value)))
  231. hash-table1)
  232. (define (hash-table-keys hash-table)
  233. (hash-table-fold hash-table (lambda (key val acc) (cons key acc)) '()))
  234. (define (hash-table-values hash-table)
  235. (hash-table-fold hash-table (lambda (key val acc) (cons val acc)) '()))