mapping.scm 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 1996 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This program is free software; you can redistribute it and/or modify
  5. ;;;; it under the terms of the GNU General Public License as published by
  6. ;;;; the Free Software Foundation; either version 2, or (at your option)
  7. ;;;; any later version.
  8. ;;;;
  9. ;;;; This program 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
  12. ;;;; GNU General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU General Public License
  15. ;;;; along with this software; see the file COPYING. If not, write to
  16. ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. ;;;; Boston, MA 02111-1307 USA
  18. ;;;;
  19. (define-module (ice-9 mapping)
  20. :use-module (ice-9 poe))
  21. (define-public mapping-hooks-type (make-record-type 'mapping-hooks '(get-handle
  22. create-handle
  23. remove)))
  24. (define-public make-mapping-hooks (perfect-funcq 17 (record-constructor mapping-hooks-type)))
  25. (define-public mapping-hooks? (record-predicate mapping-hooks-type))
  26. (define-public mapping-hooks-get-handle (record-accessor mapping-hooks-type 'get-handle))
  27. (define-public mapping-hooks-create-handle (record-accessor mapping-hooks-type 'create-handle))
  28. (define-public mapping-hooks-remove (record-accessor mapping-hooks-type 'remove))
  29. (define-public mapping-type (make-record-type 'mapping '(hooks data)))
  30. (define-public make-mapping (record-constructor mapping-type))
  31. (define-public mapping? (record-predicate mapping-type))
  32. (define-public mapping-hooks (record-accessor mapping-type 'hooks))
  33. (define-public mapping-data (record-accessor mapping-type 'data))
  34. (define-public set-mapping-hooks! (record-modifier mapping-type 'hooks))
  35. (define-public set-mapping-data! (record-modifier mapping-type 'data))
  36. (define-public (mapping-get-handle map key)
  37. ((mapping-hooks-get-handle (mapping-hooks map)) map key))
  38. (define-public (mapping-create-handle! map key . opts)
  39. (apply (mapping-hooks-create-handle (mapping-hooks map)) map key opts))
  40. (define-public (mapping-remove! map key)
  41. ((mapping-hooks-remove (mapping-hooks map)) map key))
  42. (define-public (mapping-ref map key . dflt)
  43. (cond
  44. ((mapping-get-handle map key) => cdr)
  45. (dflt => car)
  46. (else #f)))
  47. (define-public (mapping-set! map key val)
  48. (set-cdr! (mapping-create-handle! map key #f) val))
  49. (define-public hash-table-mapping-hooks
  50. (let ((wrap (lambda (proc) (lambda (1st . rest) (apply proc (mapping-data 1st) rest)))))
  51. (perfect-funcq 17
  52. (lambda (hash-proc assoc-proc delete-proc)
  53. (let ((procs (list hash-proc assoc-proc delete-proc)))
  54. (cond
  55. ((equal? procs `(,hashq ,assq ,delq!))
  56. (make-mapping-hooks (wrap hashq-get-handle)
  57. (wrap hashq-create-handle!)
  58. (wrap hashq-remove!)))
  59. ((equal? procs `(,hashv ,assv ,delv!))
  60. (make-mapping-hooks (wrap hashv-get-handle)
  61. (wrap hashv-create-handle!)
  62. (wrap hashv-remove!)))
  63. ((equal? procs `(,hash ,assoc ,delete!))
  64. (make-mapping-hooks (wrap hash-get-handle)
  65. (wrap hash-create-handle!)
  66. (wrap hash-remove!)))
  67. (else
  68. (make-mapping-hooks (wrap
  69. (lambda (table key)
  70. (hashx-get-handle hash-proc assoc-proc table key)))
  71. (wrap
  72. (lambda (table key)
  73. (hashx-create-handle hash-proc assoc-proc table key)))
  74. (wrap
  75. (lambda (table key)
  76. (hashx-get-handle hash-proc assoc-proc delete-proc table key)))))))))))
  77. (define-public (make-hash-table-mapping table hash-proc assoc-proc delete-proc)
  78. (make-mapping (hash-table-mapping-hooks hash-proc assoc-proc delete-proc) table))
  79. (define-public (hash-table-mapping . options)
  80. (let* ((size (or (and options (number? (car options)) (car options))
  81. 71))
  82. (hash-proc (or (kw-arg-ref options :hash-proc) hash))
  83. (assoc-proc (or (kw-arg-ref options :assoc-proc)
  84. (cond
  85. ((eq? hash-proc hash) assoc)
  86. ((eq? hash-proc hashv) assv)
  87. ((eq? hash-proc hashq) assq)
  88. (else (error 'hash-table-mapping
  89. "Hash-procedure specified with no known assoc function."
  90. hash-proc)))))
  91. (delete-proc (or (kw-arg-ref options :delete-proc)
  92. (cond
  93. ((eq? hash-proc hash) delete!)
  94. ((eq? hash-proc hashv) delv!)
  95. ((eq? hash-proc hashq) delq!)
  96. (else (error 'hash-table-mapping
  97. "Hash-procedure specified with no known delete function."
  98. hash-proc)))))
  99. (table-constructor (or (kw-arg-ref options :table-constructor)
  100. (lambda (len) (make-vector len '())))))
  101. (make-hash-table-mapping (table-constructor size)
  102. hash-proc
  103. assoc-proc
  104. delete-proc)))