home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgambit-20-compiler-src-p2 / Thomas / hash.scm < prev    next >
Encoding:
Text File  |  1994-07-26  |  5.0 KB  |  168 lines  |  [TEXT/gamI]

  1. ; -*-Scheme-*-
  2. ;
  3. ; $Id: gambit_hash.scm,v 1.2 1992/09/23 15:24:00 birkholz Exp $
  4. ; $MIT-Header: prop1d.scm,v 14.4 89/09/15 17:16:35 GMT jinx Exp $
  5. ;
  6. ; Copyright (c) 1988, 1989 Massachusetts Institute of Technology
  7. ;
  8. ; This material was developed by the Scheme project at the Massachusetts
  9. ; Institute of Technology, Department of Electrical Engineering and
  10. ; Computer Science.  Permission to copy this software, to redistribute
  11. ; it, and to use it for any purpose is granted, subject to the following
  12. ; restrictions and understandings.
  13. ;
  14. ; 1. Any copy made of this software must include this copyright notice
  15. ; in full.
  16. ;
  17. ; 2. Users of this software agree to make their best efforts (a) to
  18. ; return to the MIT Scheme project any improvements or extensions that
  19. ; they make, so that these may be included in future releases; and (b)
  20. ; to inform MIT of noteworthy uses of this software.
  21. ; 3. All materials developed as a consequence of the use of this
  22. ; software shall duly acknowledge such use, in accordance with the usual
  23. ; standards of acknowledging credit in academic research.
  24. ;
  25. ; 4. MIT has made no warrantee or representation that the operation of
  26. ; this software will be error-free, and MIT is under no obligation to
  27. ; provide any services, by way of maintenance, update, or otherwise.
  28. ;
  29. ; 5. In conjunction with products arising from the use of this material,
  30. ; there shall be no use of the name of the Massachusetts Institute of
  31. ; Technology nor of any adaptation thereof in any advertising,
  32. ; promotional, or sales literature without prior written consent from
  33. ; MIT in each case.
  34.  
  35. ; This file requires the following non-IEEE primitives:
  36.  
  37. ; ##weak-cons, ##weak-car, ##weak-cdr, ##weak-set-cdr! for manipulating
  38. ; "weak-cons cells," whose cdr is normal but whose car turns to #F
  39. ; during a garbage collection if no non-weak references are found to
  40. ; the object in the car.
  41.  
  42. ; ##add-gc-finalize-job registers a thunk (procedure of no arguments) to be
  43. ; called after each garbage collection is complete and before Scheme resumes
  44. ; running.
  45.  
  46. ;;;; One Dimensional Property Tables
  47.  
  48. (define (initialize-oned-table-package!)
  49.   (set! population-of-oned-tables (make-population))
  50.   (##add-gc-finalize-job ; setup GC finalization
  51.     gc-oned-tables!))
  52.  
  53. (define population-of-oned-tables #f)
  54.  
  55. (define (gc-oned-tables!)
  56.   (map-over-population! population-of-oned-tables oned-table/clean!))
  57.  
  58. (define (make-oned-table)
  59.   (let ((table (list oned-table-tag)))
  60.     (add-to-population! population-of-oned-tables table)
  61.     table))
  62.  
  63. (define (oned-table? object)
  64.   (and (pair? object)
  65.        (eq? (car object) oned-table-tag)))
  66.  
  67. (define oned-table-tag
  68.   "1D table")
  69.  
  70. (define false-key
  71.   "false key")
  72.  
  73. (define (weak-assq key table)
  74.   (let loop ((previous table) (alist (cdr table)))
  75.     (and (not (null? alist))
  76.      (let ((entry (car alist))
  77.            (next (cdr alist)))
  78.        (let ((key* (##weak-car entry)))
  79.          (cond ((not key*)
  80.             (set-cdr! previous next)
  81.             (loop previous next))
  82.            ((eq? key* key)
  83.             entry)
  84.            (else
  85.             (loop alist next))))))))
  86.  
  87. (define (oned-table/get table key default)
  88.   (let ((entry (weak-assq (or key false-key) table)))
  89.     (if entry
  90.     (##weak-cdr entry)
  91.     default)))
  92.  
  93. (define (oned-table/lookup table key if-found if-not-found)
  94.   (let ((entry (weak-assq (or key false-key) table)))
  95.     (if entry
  96.     (if-found (##weak-cdr entry))
  97.     (if-not-found))))
  98.  
  99. (define (oned-table/put! table key value)
  100.   (let ((key (or key false-key)))
  101.     (let ((entry (weak-assq key table)))
  102.       (if entry
  103.       (##weak-set-cdr! entry value)
  104.       (set-cdr! table
  105.             (cons (##weak-cons key value)
  106.               (cdr table))))
  107.       #f)))
  108.  
  109. (define (oned-table/remove! table key)
  110.   (let ((key (or key false-key)))
  111.     (let loop ((previous table) (alist (cdr table)))
  112.       (if (not (null? alist))
  113.       (let ((key* (##weak-car (car alist)))
  114.         (next (cdr alist)))
  115.         (loop (if (or (not key*) (eq? key* key))
  116.               ;; Might as well clean whole list.
  117.               (begin
  118.             (set-cdr! previous next)
  119.             previous)
  120.               alist)
  121.           next))))))
  122.  
  123. (define (oned-table/clean! table)
  124.   (let loop ((previous table) (alist (cdr table)))
  125.     (if (not (null? alist))
  126.     (let ((next (cdr alist)))
  127.       (loop (if (##weak-car (car alist))
  128.             alist
  129.             (begin
  130.               (set-cdr! previous next)
  131.               previous))
  132.         next)))))
  133.  
  134. (define (oned-table/alist table)
  135.   (let loop ((previous table) (alist (cdr table)) (result '()))
  136.     (if (null? alist)
  137.     result
  138.     (let ((entry (car alist))
  139.           (next (cdr alist)))
  140.       (let ((key (##weak-car entry)))
  141.         (if (not key)
  142.         (begin
  143.           (set-cdr! previous next)
  144.           (loop previous next result))
  145.         (loop alist
  146.               next
  147.               (cons (cons (and (not (eq? key false-key)) key)
  148.                   (##weak-cdr entry))
  149.                 result))))))))
  150.  
  151. (define (oned-table/for-each proc table)
  152.   (let loop ((previous table) (alist (cdr table)))
  153.     (if (not (null? alist))
  154.     (let ((entry (car alist))
  155.           (next (cdr alist)))
  156.       (let ((key (##weak-car entry)))
  157.         (if key
  158.         (begin
  159.           (proc (and (not (eq? key false-key)) key)
  160.             (##weak-cdr entry))
  161.           (loop alist next))
  162.         (begin
  163.           (set-cdr! previous next)
  164.           (loop previous next))))))))
  165.  
  166. (initialize-oned-table-package!)
  167.