From 70d7fc059534f74c4eda16ba961904606c3cc5a0 Mon Sep 17 00:00:00 2001 From: Chris Hanson Date: Mon, 15 Jan 2018 20:02:20 -0800 Subject: [PATCH] Implement simple weak sets. --- src/runtime/boot.scm | 76 +++++++++++++++++++++++++++++++++++++++++ src/runtime/runtime.pkg | 6 ++++ 2 files changed, 82 insertions(+) diff --git a/src/runtime/boot.scm b/src/runtime/boot.scm index 783d9e3ba..88cffd2d9 100644 --- a/src/runtime/boot.scm +++ b/src/runtime/boot.scm @@ -104,6 +104,82 @@ USA. (object-new-type (ucode-type constant) 10))))) (ugh))) +;;;; Simple weak-set implementation + +;;; Does not support #f as an item of the set. + +(define (%make-weak-set) + (%weak-cons 'weak-set '())) + +(define (%weak-set->list weak-set) + (weak-list->list (weak-cdr weak-set))) + +(define (%add-to-weak-set item weak-set) + (let loop + ((this (weak-cdr weak-set)) + (prev weak-set)) + (if (weak-pair? this) + (let ((item* (%weak-car this)) + (next (weak-cdr this))) + (cond ((not item*) + (weak-set-cdr! prev next) + (loop next prev)) + ((eq? item item*) + #f) + (else + (loop next this)))) + (begin + (weak-set-cdr! prev (%weak-cons item '())) + #t)))) + +(define (%remove-from-weak-set item weak-set) + (let loop + ((this (weak-cdr weak-set)) + (prev weak-set)) + (if (weak-pair? this) + (let ((item* (%weak-car this)) + (next (weak-cdr this))) + (cond ((not item*) + (weak-set-cdr! prev next) + (loop next prev)) + ((eq? item item*) + (weak-set-cdr! prev next) + #t) + (else + (loop next this)))) + #f))) + +(define (%weak-set-any predicate weak-set) + (let loop + ((this (weak-cdr weak-set)) + (prev weak-set)) + (if (weak-pair? this) + (let ((item (%weak-car this)) + (next (weak-cdr this))) + (cond ((not item) + (weak-set-cdr! prev next) + (loop next prev)) + ((predicate item) + #t) + (else + (loop next this)))) + #f))) + +(define (%weak-set-for-each procedure weak-set) + (let loop + ((this (weak-cdr weak-set)) + (prev weak-set)) + (if (weak-pair? this) + (let ((item (%weak-car this)) + (next (weak-cdr this))) + (if item + (begin + (procedure item) + (loop next this)) + (begin + (weak-set-cdr! prev next) + (loop next prev))))))) + ;;;; Interrupt control (define interrupt-bit/stack #x0001) diff --git a/src/runtime/runtime.pkg b/src/runtime/runtime.pkg index 80f2290a6..a1a646d53 100644 --- a/src/runtime/runtime.pkg +++ b/src/runtime/runtime.pkg @@ -191,6 +191,12 @@ USA. with-limited-interrupts without-interrupts) (export (runtime) + %add-to-weak-set + %make-weak-set + %remove-from-weak-set + %weak-set->list + %weak-set-any + %weak-set-for-each add-boot-init! defer-boot-action run-deferred-boot-actions) -- 2.25.1