69.body.scm 12 KB

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