mapping.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 1996, 2001, 2006, 2013 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 3 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. (issue-deprecation-warning
  28. "(ice-9 mapping) is deprecated. Use srfi-69 or rnrs hash tables instead.")
  29. (define mapping-hooks-type (make-record-type 'mapping-hooks '(get-handle
  30. create-handle
  31. remove)))
  32. (define make-mapping-hooks (perfect-funcq 17 (record-constructor mapping-hooks-type)))
  33. (define mapping-hooks? (record-predicate mapping-hooks-type))
  34. (define mapping-hooks-get-handle (record-accessor mapping-hooks-type 'get-handle))
  35. (define mapping-hooks-create-handle (record-accessor mapping-hooks-type 'create-handle))
  36. (define mapping-hooks-remove (record-accessor mapping-hooks-type 'remove))
  37. (define mapping-type (make-record-type 'mapping '(hooks data)))
  38. (define make-mapping (record-constructor mapping-type))
  39. (define mapping? (record-predicate mapping-type))
  40. (define mapping-hooks (record-accessor mapping-type 'hooks))
  41. (define mapping-data (record-accessor mapping-type 'data))
  42. (define set-mapping-hooks! (record-modifier mapping-type 'hooks))
  43. (define set-mapping-data! (record-modifier mapping-type 'data))
  44. (define (mapping-get-handle map key)
  45. ((mapping-hooks-get-handle (mapping-hooks map)) map key))
  46. (define (mapping-create-handle! map key init)
  47. ((mapping-hooks-create-handle (mapping-hooks map)) map key init))
  48. (define (mapping-remove! map key)
  49. ((mapping-hooks-remove (mapping-hooks map)) map key))
  50. (define* (mapping-ref map key #:optional dflt)
  51. (cond
  52. ((mapping-get-handle map key) => cdr)
  53. (else dflt)))
  54. (define (mapping-set! map key val)
  55. (set-cdr! (mapping-create-handle! map key #f) val))
  56. (define hash-table-mapping-hooks
  57. (let ((wrap (lambda (proc) (lambda (1st . rest) (apply proc (mapping-data 1st) rest)))))
  58. (perfect-funcq 17
  59. (lambda (hash-proc assoc-proc)
  60. (let ((procs (list hash-proc assoc-proc)))
  61. (cond
  62. ((equal? procs `(,hashq ,assq))
  63. (make-mapping-hooks (wrap hashq-get-handle)
  64. (wrap hashq-create-handle!)
  65. (wrap hashq-remove!)))
  66. ((equal? procs `(,hashv ,assv))
  67. (make-mapping-hooks (wrap hashv-get-handle)
  68. (wrap hashv-create-handle!)
  69. (wrap hashv-remove!)))
  70. ((equal? procs `(,hash ,assoc))
  71. (make-mapping-hooks (wrap hash-get-handle)
  72. (wrap hash-create-handle!)
  73. (wrap hash-remove!)))
  74. (else
  75. (make-mapping-hooks (wrap
  76. (lambda (table key)
  77. (hashx-get-handle hash-proc assoc-proc table key)))
  78. (wrap
  79. (lambda (table key init)
  80. (hashx-create-handle! hash-proc assoc-proc table key init)))
  81. (wrap
  82. (lambda (table key)
  83. (hashx-remove! hash-proc assoc-proc table key)))))))))))
  84. (define (make-hash-table-mapping table hash-proc assoc-proc)
  85. (make-mapping (hash-table-mapping-hooks hash-proc assoc-proc) table))
  86. (define* (hash-table-mapping #:optional (size 71) #:key
  87. (hash-proc hash)
  88. (assoc-proc
  89. (or (assq-ref `((,hashq . ,assq)
  90. (,hashv . ,assv)
  91. (,hash . ,assoc))
  92. hash-proc)
  93. (error 'hash-table-mapping
  94. "Hash-procedure specified with no known assoc function."
  95. hash-proc)))
  96. (table-constructor
  97. (lambda (len) (make-vector len '()))))
  98. (make-hash-table-mapping (table-constructor size)
  99. hash-proc
  100. assoc-proc))