home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mitsch75.zip / scheme-7_5_17-src.zip / scheme-7.5.17 / src / runtime / prop1d.scm < prev    next >
Text File  |  1999-01-02  |  4KB  |  149 lines

  1. #| -*-Scheme-*-
  2.  
  3. $Id: prop1d.scm,v 14.6 1999/01/02 06:11:34 cph Exp $
  4.  
  5. Copyright (c) 1988-1999 Massachusetts Institute of Technology
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful, but
  13. WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15. General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. |#
  21.  
  22. ;;;; One Dimensional Property Tables
  23. ;;; package: (runtime 1d-property)
  24.  
  25. (declare (usual-integrations))
  26.  
  27. (define (initialize-package!)
  28.   (set! population-of-1d-tables (make-population))
  29.   (add-secondary-gc-daemon! gc-1d-tables!))
  30.  
  31. (define (initialize-unparser!)
  32.   (unparser/set-tagged-pair-method! 1d-table-tag
  33.                     (standard-unparser-method '1D-TABLE #f)))
  34.  
  35. (define population-of-1d-tables)
  36.  
  37. (define (gc-1d-tables!)
  38.   (map-over-population! population-of-1d-tables 1d-table/clean!))
  39.  
  40. (define (make-1d-table)
  41.   (let ((table (list 1d-table-tag)))
  42.     (add-to-population! population-of-1d-tables table)
  43.     table))
  44.  
  45. (define (1d-table? object)
  46.   (and (pair? object)
  47.        (eq? (car object) 1d-table-tag)))
  48.  
  49. (define 1d-table-tag
  50.   "1D table")
  51.  
  52. (define false-key
  53.   "false key")
  54.  
  55. (define-integrable (weak-cons car cdr)
  56.   (system-pair-cons (ucode-type weak-cons) car cdr))
  57.  
  58. (define (weak-assq key table)
  59.   (let loop ((previous table) (alist (cdr table)))
  60.     (and (not (null? alist))
  61.      (let ((entry (car alist))
  62.            (next (cdr alist)))
  63.        (let ((key* (system-pair-car entry)))
  64.          (cond ((not key*)
  65.             (set-cdr! previous next)
  66.             (loop previous next))
  67.            ((eq? key* key)
  68.             entry)
  69.            (else
  70.             (loop alist next))))))))
  71.  
  72. (define (1d-table/get table key default)
  73.   (let ((entry (weak-assq (or key false-key) table)))
  74.     (if entry
  75.     (system-pair-cdr entry)
  76.     default)))
  77.  
  78. (define (1d-table/lookup table key if-found if-not-found)
  79.   (let ((entry (weak-assq (or key false-key) table)))
  80.     (if entry
  81.     (if-found (system-pair-cdr entry))
  82.     (if-not-found))))
  83.  
  84. (define (1d-table/put! table key value)
  85.   (let ((key (or key false-key)))
  86.     (let ((entry (weak-assq key table)))
  87.       (if entry
  88.       (system-pair-set-cdr! entry value)
  89.       (set-cdr! table
  90.             (cons (weak-cons key value)
  91.               (cdr table))))
  92.       unspecific)))
  93.  
  94. (define (1d-table/remove! table key)
  95.   (let ((key (or key false-key)))
  96.     (let loop ((previous table) (alist (cdr table)))
  97.       (if (not (null? alist))
  98.       (let ((key* (system-pair-car (car alist)))
  99.         (next (cdr alist)))
  100.         (loop (if (or (not key*) (eq? key* key))
  101.               ;; Might as well clean whole list.
  102.               (begin
  103.             (set-cdr! previous next)
  104.             previous)
  105.               alist)
  106.           next))))))
  107.  
  108. (define (1d-table/clean! table)
  109.   (let loop ((previous table) (alist (cdr table)))
  110.     (if (not (null? alist))
  111.     (let ((next (cdr alist)))
  112.       (loop (if (system-pair-car (car alist))
  113.             alist
  114.             (begin
  115.               (set-cdr! previous next)
  116.               previous))
  117.         next)))))
  118.  
  119. (define (1d-table/alist table)
  120.   (let loop ((previous table) (alist (cdr table)) (result '()))
  121.     (if (null? alist)
  122.     result
  123.     (let ((entry (car alist))
  124.           (next (cdr alist)))
  125.       (let ((key (system-pair-car entry)))
  126.         (if (not key)
  127.         (begin
  128.           (set-cdr! previous next)
  129.           (loop previous next result))
  130.         (loop alist
  131.               next
  132.               (cons (cons (and (not (eq? key false-key)) key)
  133.                   (system-pair-cdr entry))
  134.                 result))))))))
  135.  
  136. (define (1d-table/for-each proc table)
  137.   (let loop ((previous table) (alist (cdr table)))
  138.     (if (not (null? alist))
  139.     (let ((entry (car alist))
  140.           (next (cdr alist)))
  141.       (let ((key (system-pair-car entry)))
  142.         (if key
  143.         (begin
  144.           (proc (and (not (eq? key false-key)) key)
  145.             (system-pair-cdr entry))
  146.           (loop alist next))
  147.         (begin
  148.           (set-cdr! previous next)
  149.           (loop previous next))))))))