Change representation of hashsets to avoid actual sets: use hashes-to-#t instead

This commit is contained in:
Tony Garnock-Jones 2018-05-01 17:27:49 +01:00
parent aaea276ec5
commit 36e4de74ad
3 changed files with 22 additions and 12 deletions

View File

@ -41,9 +41,8 @@
(define (dataflow-forget-subject! g subject-id)
(define rev (dataflow-graph-edges-reverse g))
(define subject-objects (hash-ref rev subject-id set))
(set-dataflow-graph-edges-reverse! g (hash-remove rev subject-id))
(for [(object-id (in-set subject-objects))]
(for [(object-id (in-hashset-values rev subject-id))]
(define fwd (dataflow-graph-edges-forward g))
(set-dataflow-graph-edges-forward! g (hashset-remove fwd object-id subject-id))))
@ -54,8 +53,8 @@
(set-dataflow-graph-damaged-nodes! g (hash))
(hash-for-each workset
(lambda (object-id _)
(define subjects (hash-ref (dataflow-graph-edges-forward g) object-id set))
(for [(subject-id (in-set subjects))]
(for [(subject-id
(in-hashset-values (dataflow-graph-edges-forward g) object-id))]
(dataflow-forget-subject! g subject-id)
(parameterize ((current-dataflow-subject-id subject-id))
(repair-node! subject-id)))))

View File

@ -49,7 +49,7 @@
(spawn (define/query-hash-set thinkers (philosopher-status $who $status) status who)
(begin/dataflow
(log-info "~a" (for/list (((status names) (in-hash (thinkers))))
(format "~a: ~a" status (set->list names))))))
(format "~a: ~a" status (hash-keys names))))))
(define (philosopher name)
(spawn (field [status 'starting])

View File

@ -4,9 +4,10 @@
(provide hash-set/remove
hashset-member?
hashset-add
hashset-remove)
hashset-remove
in-hashset-values)
(require racket/set)
(require (only-in racket/set set seteq seteqv))
(define (hash-set/remove ht key val [default-val #f] #:compare [compare equal?])
(if (compare val default-val)
@ -15,16 +16,26 @@
(define (hashset-member? ht key val)
(define s (hash-ref ht key #f))
(and s (set-member? s val)))
(and s (hash-has-key? s val)))
(define (hashset-add ht key val #:set [set set])
(hash-set ht key (set-add (hash-ref ht key set) val)))
(define (make-hash-from-set-ctor ctor)
(cond [(eq? ctor set) (hash)]
[(eq? ctor seteq) (hasheq)]
[(eq? ctor seteqv) (hasheqv)]
[else (error 'hash-ctor-for-set-ctor "Cannot use hashset with set constructor ~v" ctor)]))
(define (hashset-add ht key val #:set [set-ctor set])
(define old-set (hash-ref ht key (lambda () (make-hash-from-set-ctor set-ctor))))
(hash-set ht key (hash-set old-set val #t)))
(define (hashset-remove ht k v)
(define old (hash-ref ht k #f))
(if old
(let ((new (set-remove old v)))
(if (set-empty? new)
(let ((new (hash-remove old v)))
(if (hash-empty? new)
(hash-remove ht k)
(hash-set ht k new)))
ht))
(define-syntax-rule (in-hashset-values ht key)
(in-hash-keys (hash-ref ht key hash)))