hcons.scm 2.4 KB

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