srfi-69.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. ;;; srfi-69.scm --- Basic hash tables
  2. ;; Copyright (C) 2007 Free Software Foundation, Inc.
  3. ;;
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 2.1 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;; Commentary:
  18. ;; My `hash' is compatible with core `hash', so I replace it.
  19. ;; However, my `hash-table?' and `make-hash-table' are different, so
  20. ;; importing this module will warn about them. If you don't rename my
  21. ;; imports, you shouldn't use both my hash tables and Guile's hash
  22. ;; tables in the same module.
  23. ;;
  24. ;; SRFI-13 `string-hash' and `string-hash-ci' have more arguments, but
  25. ;; are compatible with my `string-hash' and `string-ci-hash', and are
  26. ;; furthermore primitive in Guile, so I use them as my own.
  27. ;;
  28. ;; I also have the extension of allowing hash functions that require a
  29. ;; second argument to be used as the `hash-table-hash-function', and use
  30. ;; these in defaults to avoid an indirection in the hashx functions. The
  31. ;; only deviation this causes is:
  32. ;;
  33. ;; ((hash-table-hash-function (make-hash-table)) obj)
  34. ;; error> Wrong number of arguments to #<primitive-procedure hash>
  35. ;;
  36. ;; I don't think that SRFI 69 actually specifies that I *can't* do this,
  37. ;; because it only implies the signature of a hash function by way of the
  38. ;; named, exported hash functions. However, if this matters enough I can
  39. ;; add a private derivation of hash-function to the srfi-69:hash-table
  40. ;; record type, like associator is to equivalence-function.
  41. ;;
  42. ;; Also, outside of the issue of how weak keys and values are referenced
  43. ;; outside the table, I always interpret key equivalence to be that of
  44. ;; the `hash-table-equivalence-function'. For example, given the
  45. ;; requirement that `alist->hash-table' give earlier associations
  46. ;; priority, what should these answer?
  47. ;;
  48. ;; (hash-table-keys
  49. ;; (alist->hash-table '(("xY" . 1) ("Xy" . 2)) string-ci=?))
  50. ;;
  51. ;; (let ((ht (make-hash-table string-ci=?)))
  52. ;; (hash-table-set! ht "xY" 2)
  53. ;; (hash-table-set! ht "Xy" 1)
  54. ;; (hash-table-keys ht))
  55. ;;
  56. ;; My interpretation is that they can answer either ("Xy") or ("xY"),
  57. ;; where `hash-table-values' will of course always answer (1), because
  58. ;; the keys are the same according to the equivalence function. In this
  59. ;; implementation, both answer ("xY"). However, I don't guarantee that
  60. ;; this won't change in the future.
  61. ;;; Code:
  62. ;;;; Module definition & exports
  63. (define-module (srfi srfi-69)
  64. #:use-module (srfi srfi-1) ;alist-cons,second&c,assoc
  65. #:use-module (srfi srfi-9)
  66. #:use-module (srfi srfi-13) ;string-hash,string-hash-ci
  67. #:use-module (ice-9 optargs)
  68. #:export (;; Type constructors & predicate
  69. make-hash-table hash-table? alist->hash-table
  70. ;; Reflective queries
  71. hash-table-equivalence-function hash-table-hash-function
  72. ;; Dealing with single elements
  73. hash-table-ref hash-table-ref/default hash-table-set!
  74. hash-table-delete! hash-table-exists? hash-table-update!
  75. hash-table-update!/default
  76. ;; Dealing with the whole contents
  77. hash-table-size hash-table-keys hash-table-values
  78. hash-table-walk hash-table-fold hash-table->alist
  79. hash-table-copy hash-table-merge!
  80. ;; Hashing
  81. string-ci-hash hash-by-identity)
  82. #:re-export (string-hash)
  83. #:replace (hash))
  84. (cond-expand-provide (current-module) '(srfi-37))
  85. ;;;; Hashing
  86. ;;; The largest fixnum is in `most-positive-fixnum' in module (guile),
  87. ;;; though not documented anywhere but libguile/numbers.c.
  88. (define (caller-with-default-size hash-fn)
  89. "Answer a function that makes `most-positive-fixnum' the default
  90. second argument to HASH-FN, a 2-arg procedure."
  91. (lambda* (obj #:optional (size most-positive-fixnum))
  92. (hash-fn obj size)))
  93. (define hash (caller-with-default-size (@ (guile) hash)))
  94. (define string-ci-hash string-hash-ci)
  95. (define hash-by-identity (caller-with-default-size hashq))
  96. ;;;; Reflective queries, construction, predicate
  97. (define-record-type srfi-69:hash-table
  98. (make-srfi-69-hash-table real-table associator size weakness
  99. equivalence-function hash-function)
  100. hash-table?
  101. (real-table ht-real-table)
  102. (associator ht-associator)
  103. ;; required for O(1) by SRFI-69. It really makes a mess of things,
  104. ;; and I'd like to compute it in O(n) and memoize it because it
  105. ;; doesn't seem terribly useful, but SRFI-69 is final.
  106. (size ht-size ht-size!)
  107. ;; required for `hash-table-copy'
  108. (weakness ht-weakness)
  109. ;; used only to implement hash-table-equivalence-function; I don't
  110. ;; use it internally other than for `ht-associator'.
  111. (equivalence-function hash-table-equivalence-function)
  112. (hash-function hash-table-hash-function))
  113. (define (guess-hash-function equal-proc)
  114. "Guess a hash function for EQUAL-PROC, falling back on `hash', as
  115. specified in SRFI-69 for `make-hash-table'."
  116. (cond ((eq? equal? equal-proc) (@ (guile) hash)) ;shortcut most common case
  117. ((eq? eq? equal-proc) hashq)
  118. ((eq? eqv? equal-proc) hashv)
  119. ((eq? string=? equal-proc) string-hash)
  120. ((eq? string-ci=? equal-proc) string-ci-hash)
  121. (else (@ (guile) hash))))
  122. (define (without-keyword-args rest-list)
  123. "Answer REST-LIST with all keywords removed along with items that
  124. follow them."
  125. (let lp ((acc '()) (rest-list rest-list))
  126. (cond ((null? rest-list) (reverse! acc))
  127. ((keyword? (first rest-list))
  128. (lp acc (cddr rest-list)))
  129. (else (lp (cons (first rest-list) acc) (cdr rest-list))))))
  130. (define (guile-ht-ctor weakness)
  131. "Answer the Guile HT constructor for the given WEAKNESS."
  132. (case weakness
  133. ((#f) (@ (guile) make-hash-table))
  134. ((key) make-weak-key-hash-table)
  135. ((value) make-weak-value-hash-table)
  136. ((key-or-value) make-doubly-weak-hash-table)
  137. (else (error "Invalid weak hash table type" weakness))))
  138. (define (equivalence-proc->associator equal-proc)
  139. "Answer an `assoc'-like procedure that compares the argument key to
  140. alist keys with EQUAL-PROC."
  141. (cond ((or (eq? equal? equal-proc)
  142. (eq? string=? equal-proc)) (@ (guile) assoc))
  143. ((eq? eq? equal-proc) assq)
  144. ((eq? eqv? equal-proc) assv)
  145. (else (lambda (item alist)
  146. (assoc item alist equal-proc)))))
  147. (define* (make-hash-table
  148. #:optional (equal-proc equal?)
  149. (hash-proc (guess-hash-function equal-proc))
  150. #:key (weak #f) #:rest guile-opts)
  151. "Answer a new hash table using EQUAL-PROC as the comparison
  152. function, and HASH-PROC as the hash function. See the reference
  153. manual for specifics, of which there are many."
  154. (make-srfi-69-hash-table
  155. (apply (guile-ht-ctor weak) (without-keyword-args guile-opts))
  156. (equivalence-proc->associator equal-proc)
  157. 0 weak equal-proc hash-proc))
  158. (define (alist->hash-table alist . mht-args)
  159. "Convert ALIST to a hash table created with MHT-ARGS."
  160. (let* ((result (apply make-hash-table mht-args))
  161. (size (ht-size result)))
  162. (with-hashx-values (hash-proc associator real-table) result
  163. (for-each (lambda (pair)
  164. (let ((handle (hashx-get-handle hash-proc associator
  165. real-table (car pair))))
  166. (cond ((not handle)
  167. (set! size (1+ size))
  168. (hashx-set! hash-proc associator real-table
  169. (car pair) (cdr pair))))))
  170. alist))
  171. (ht-size! result size)
  172. result))
  173. ;;;; Accessing table items
  174. ;; We use this to denote missing or unspecified values to avoid
  175. ;; possible collision with *unspecified*.
  176. (define ht-unspecified (cons *unspecified* "ht-value"))
  177. ;; I am a macro only for efficiency, to avoid varargs/apply.
  178. (define-macro (hashx-invoke hashx-proc ht-var . args)
  179. "Invoke HASHX-PROC, a `hashx-*' procedure taking a hash-function,
  180. assoc-function, and the hash-table as first args."
  181. `(,hashx-proc (hash-table-hash-function ,ht-var)
  182. (ht-associator ,ht-var)
  183. (ht-real-table ,ht-var)
  184. . ,args))
  185. (define-macro (with-hashx-values bindings ht-var . body-forms)
  186. "Bind BINDINGS to the hash-function, associator, and real-table of
  187. HT-VAR, while evaluating BODY-FORMS."
  188. `(let ((,(first bindings) (hash-table-hash-function ,ht-var))
  189. (,(second bindings) (ht-associator ,ht-var))
  190. (,(third bindings) (ht-real-table ,ht-var)))
  191. . ,body-forms))
  192. (define (hash-table-ref ht key . default-thunk-lst)
  193. "Lookup KEY in HT and answer the value, invoke DEFAULT-THUNK if KEY
  194. isn't present, or signal an error if DEFAULT-THUNK isn't provided."
  195. (let ((result (hashx-invoke hashx-ref ht key ht-unspecified)))
  196. (if (eq? ht-unspecified result)
  197. (if (pair? default-thunk-lst)
  198. ((first default-thunk-lst))
  199. (error "Key not in table" key ht))
  200. result)))
  201. (define (hash-table-ref/default ht key default)
  202. "Lookup KEY in HT and answer the value. Answer DEFAULT if KEY isn't
  203. present."
  204. (hashx-invoke hashx-ref ht key default))
  205. (define (hash-table-set! ht key new-value)
  206. "Set KEY to NEW-VALUE in HT."
  207. (let ((handle (hashx-invoke hashx-create-handle! ht key ht-unspecified)))
  208. (if (eq? ht-unspecified (cdr handle))
  209. (ht-size! ht (1+ (ht-size ht))))
  210. (set-cdr! handle new-value))
  211. *unspecified*)
  212. (define (hash-table-delete! ht key)
  213. "Remove KEY's association in HT."
  214. (with-hashx-values (h a real-ht) ht
  215. (if (hashx-get-handle h a real-ht key)
  216. (begin
  217. (ht-size! ht (1- (ht-size ht)))
  218. (hashx-remove! h a real-ht key))))
  219. *unspecified*)
  220. (define (hash-table-exists? ht key)
  221. "Return whether KEY is a key in HT."
  222. (and (hashx-invoke hashx-get-handle ht key) #t))
  223. ;;; `hashx-set!' duplicates the hash lookup, but we use it anyway to
  224. ;;; avoid creating a handle in case DEFAULT-THUNK exits
  225. ;;; `hash-table-update!' non-locally.
  226. (define (hash-table-update! ht key modifier . default-thunk-lst)
  227. "Modify HT's value at KEY by passing its value to MODIFIER and
  228. setting it to the result thereof. Invoke DEFAULT-THUNK for the old
  229. value if KEY isn't in HT, or signal an error if DEFAULT-THUNK is not
  230. provided."
  231. (with-hashx-values (hash-proc associator real-table) ht
  232. (let ((handle (hashx-get-handle hash-proc associator real-table key)))
  233. (cond (handle
  234. (set-cdr! handle (modifier (cdr handle))))
  235. (else
  236. (hashx-set! hash-proc associator real-table key
  237. (if (pair? default-thunk-lst)
  238. (modifier ((car default-thunk-lst)))
  239. (error "Key not in table" key ht)))
  240. (ht-size! ht (1+ (ht-size ht)))))))
  241. *unspecified*)
  242. (define (hash-table-update!/default ht key modifier default)
  243. "Modify HT's value at KEY by passing its old value, or DEFAULT if it
  244. doesn't have one, to MODIFIER, and setting it to the result thereof."
  245. (hash-table-update! ht key modifier (lambda () default)))
  246. ;;;; Accessing whole tables
  247. (define (hash-table-size ht)
  248. "Return the number of associations in HT. This is guaranteed O(1)
  249. for tables where #:weak was #f or not specified at creation time."
  250. (if (ht-weakness ht)
  251. (hash-table-fold ht (lambda (k v ans) (1+ ans)) 0)
  252. (ht-size ht)))
  253. (define (hash-table-keys ht)
  254. "Return a list of the keys in HT."
  255. (hash-table-fold ht (lambda (k v lst) (cons k lst)) '()))
  256. (define (hash-table-values ht)
  257. "Return a list of the values in HT."
  258. (hash-table-fold ht (lambda (k v lst) (cons v lst)) '()))
  259. (define (hash-table-walk ht proc)
  260. "Call PROC with each key and value as two arguments."
  261. (hash-table-fold ht (lambda (k v unspec) (proc k v) unspec)
  262. *unspecified*))
  263. (define (hash-table-fold ht f knil)
  264. "Invoke (F KEY VAL PREV) for each KEY and VAL in HT, where PREV is
  265. the result of the previous invocation, using KNIL as the first PREV.
  266. Answer the final F result."
  267. (hash-fold f knil (ht-real-table ht)))
  268. (define (hash-table->alist ht)
  269. "Return an alist for HT."
  270. (hash-table-fold ht alist-cons '()))
  271. (define (hash-table-copy ht)
  272. "Answer a copy of HT."
  273. (with-hashx-values (h a real-ht) ht
  274. (let* ((size (hash-table-size ht)) (weak (ht-weakness ht))
  275. (new-real-ht ((guile-ht-ctor weak) size)))
  276. (hash-fold (lambda (k v ign) (hashx-set! h a new-real-ht k v))
  277. #f real-ht)
  278. (make-srfi-69-hash-table ;real,assoc,size,weak,equiv,h
  279. new-real-ht a size weak
  280. (hash-table-equivalence-function ht) h))))
  281. (define (hash-table-merge! ht other-ht)
  282. "Add all key/value pairs from OTHER-HT to HT, overriding HT's
  283. mappings where present. Return HT."
  284. (hash-table-fold
  285. ht (lambda (k v ign) (hash-table-set! ht k v)) #f)
  286. ht)
  287. ;;; srfi-69.scm ends here