hcons.scm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 1995, 1996, 1998, 2001, 2003, 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 hcons)
  19. :export (hashq-cons-hash hashq-cons-assoc hashq-cons-get-handle
  20. hashq-cons-create-handle! hashq-cons-ref hashq-cons-set! hashq-cons
  21. hashq-conser make-gc-buffer))
  22. ;;; {Eq? hash-consing}
  23. ;;;
  24. ;;; A hash conser maintains a private universe of pairs s.t. if
  25. ;;; two cons calls pass eq? arguments, the pairs returned are eq?.
  26. ;;;
  27. ;;; A hash conser does not contribute life to the pairs it returns.
  28. ;;;
  29. (define (hashq-cons-hash pair n)
  30. (modulo (logxor (hashq (car pair) 4194303)
  31. (hashq (cdr pair) 4194303))
  32. n))
  33. (define (hashq-cons-assoc key l)
  34. (and (not (null? l))
  35. (or (and (pair? l) ; If not a pair, use its cdr?
  36. (pair? (car l))
  37. (pair? (caar l))
  38. (eq? (car key) (caaar l))
  39. (eq? (cdr key) (cdaar l))
  40. (car l))
  41. (hashq-cons-assoc key (cdr l)))))
  42. (define (hashq-cons-get-handle table key)
  43. (hashx-get-handle hashq-cons-hash hashq-cons-assoc table key))
  44. (define (hashq-cons-create-handle! table key init)
  45. (hashx-create-handle! hashq-cons-hash hashq-cons-assoc table key init))
  46. (define (hashq-cons-ref table key)
  47. (hashx-ref hashq-cons-hash hashq-cons-assoc table key #f))
  48. (define (hashq-cons-set! table key val)
  49. (hashx-set! hashq-cons-hash hashq-cons-assoc table key val))
  50. (define (hashq-cons table a d)
  51. (car (hashq-cons-create-handle! table (cons a d) #f)))
  52. (define (hashq-conser hash-tab-or-size)
  53. (let ((table (if (vector? hash-tab-or-size)
  54. hash-tab-or-size
  55. (make-doubly-weak-hash-table hash-tab-or-size))))
  56. (lambda (a d) (hashq-cons table a d))))
  57. (define (make-gc-buffer n)
  58. (let ((ring (make-list n #f)))
  59. (append! ring ring)
  60. (lambda (next)
  61. (set-car! ring next)
  62. (set! ring (cdr ring))
  63. next)))