mapping.scm 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 1996, 2001, 2006 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. ;;;;
  18. (define-module (ice-9 mapping)
  19. :use-module (ice-9 poe)
  20. :export (mapping-hooks-type make-mapping-hooks mapping-hooks?
  21. mapping-hooks-get-handle mapping-hooks-create-handle
  22. mapping-hooks-remove mapping-type make-mapping mapping?
  23. mapping-hooks mapping-data set-mapping-hooks! set-mapping-data!
  24. mapping-get-handle mapping-create-handle! mapping-remove!
  25. mapping-ref mapping-set! hash-table-mapping-hooks
  26. make-hash-table-mapping hash-table-mapping))
  27. (define mapping-hooks-type (make-record-type 'mapping-hooks '(get-handle
  28. create-handle
  29. remove)))
  30. (define make-mapping-hooks (perfect-funcq 17 (record-constructor mapping-hooks-type)))
  31. (define mapping-hooks? (record-predicate mapping-hooks-type))
  32. (define mapping-hooks-get-handle (record-accessor mapping-hooks-type 'get-handle))
  33. (define mapping-hooks-create-handle (record-accessor mapping-hooks-type 'create-handle))
  34. (define mapping-hooks-remove (record-accessor mapping-hooks-type 'remove))
  35. (define mapping-type (make-record-type 'mapping '(hooks data)))
  36. (define make-mapping (record-constructor mapping-type))
  37. (define mapping? (record-predicate mapping-type))
  38. (define mapping-hooks (record-accessor mapping-type 'hooks))
  39. (define mapping-data (record-accessor mapping-type 'data))
  40. (define set-mapping-hooks! (record-modifier mapping-type 'hooks))
  41. (define set-mapping-data! (record-modifier mapping-type 'data))
  42. (define (mapping-get-handle map key)
  43. ((mapping-hooks-get-handle (mapping-hooks map)) map key))
  44. (define (mapping-create-handle! map key . opts)
  45. (apply (mapping-hooks-create-handle (mapping-hooks map)) map key opts))
  46. (define (mapping-remove! map key)
  47. ((mapping-hooks-remove (mapping-hooks map)) map key))
  48. (define (mapping-ref map key . dflt)
  49. (cond
  50. ((mapping-get-handle map key) => cdr)
  51. (dflt => car)
  52. (else #f)))
  53. (define (mapping-set! map key val)
  54. (set-cdr! (mapping-create-handle! map key #f) val))
  55. (define hash-table-mapping-hooks
  56. (let ((wrap (lambda (proc) (lambda (1st . rest) (apply proc (mapping-data 1st) rest)))))
  57. (perfect-funcq 17
  58. (lambda (hash-proc assoc-proc delete-proc)
  59. (let ((procs (list hash-proc assoc-proc delete-proc)))
  60. (cond
  61. ((equal? procs `(,hashq ,assq ,delq!))
  62. (make-mapping-hooks (wrap hashq-get-handle)
  63. (wrap hashq-create-handle!)
  64. (wrap hashq-remove!)))
  65. ((equal? procs `(,hashv ,assv ,delv!))
  66. (make-mapping-hooks (wrap hashv-get-handle)
  67. (wrap hashv-create-handle!)
  68. (wrap hashv-remove!)))
  69. ((equal? procs `(,hash ,assoc ,delete!))
  70. (make-mapping-hooks (wrap hash-get-handle)
  71. (wrap hash-create-handle!)
  72. (wrap hash-remove!)))
  73. (else
  74. (make-mapping-hooks (wrap
  75. (lambda (table key)
  76. (hashx-get-handle hash-proc assoc-proc table key)))
  77. (wrap
  78. (lambda (table key)
  79. (hashx-create-handle hash-proc assoc-proc table key)))
  80. (wrap
  81. (lambda (table key)
  82. (hashx-get-handle hash-proc assoc-proc delete-proc table key)))))))))))
  83. (define (make-hash-table-mapping table hash-proc assoc-proc delete-proc)
  84. (make-mapping (hash-table-mapping-hooks hash-proc assoc-proc delete-proc) table))
  85. (define (hash-table-mapping . options)
  86. (let* ((size (or (and options (number? (car options)) (car options))
  87. 71))
  88. (hash-proc (or (kw-arg-ref options #:hash-proc) hash))
  89. (assoc-proc (or (kw-arg-ref options #:assoc-proc)
  90. (cond
  91. ((eq? hash-proc hash) assoc)
  92. ((eq? hash-proc hashv) assv)
  93. ((eq? hash-proc hashq) assq)
  94. (else (error 'hash-table-mapping
  95. "Hash-procedure specified with no known assoc function."
  96. hash-proc)))))
  97. (delete-proc (or (kw-arg-ref options #:delete-proc)
  98. (cond
  99. ((eq? hash-proc hash) delete!)
  100. ((eq? hash-proc hashv) delv!)
  101. ((eq? hash-proc hashq) delq!)
  102. (else (error 'hash-table-mapping
  103. "Hash-procedure specified with no known delete function."
  104. hash-proc)))))
  105. (table-constructor (or (kw-arg-ref options #:table-constructor)
  106. (lambda (len) (make-vector len '())))))
  107. (make-hash-table-mapping (table-constructor size)
  108. hash-proc
  109. assoc-proc
  110. delete-proc)))