(library (sph storage dg data-structure set) (export dg-ordered-set dg-set dg-set-add dg-set-contains? dg-set-delete dg-set-delete-relations dg-set-elements dg-set-elements-intersection) (import (sph) (sph storage dg) (sph storage dg one) (only (sph list) list-set-equal?)) (define (dg-set txn . elements) "dg-txn integer:node-ids:elements ... -> integer create a new set with the given node-ids" (let (ids (dg-id-create txn)) (dg-relation-ensure txn ids elements) (first ids))) (define (dg-set-add txn ids . elements) "dg-txn (integer ...) integer ... -> all all elements to all sets identified by \"ids\"" (dg-relation-ensure txn ids elements)) (define (dg-set-delete txn ids . elements) "dg-txn list integer ... -> delete the set and disconnected elements" (dg-relation-delete txn ids elements) (dg-delete-disconnected txn elements)) (define (dg-set-elements-intersection txn ids elements) "dg-txn list list -> list filter elements that are contained in the sets identified by \"ids\"" (dg-relation-select-read txn ids elements null (q right))) (define (dg-set-contains? txn ids . elements) "dg-txn list integer ... -> boolean true if the sets identified by \"ids\" contain all elements" (list-set-equal? elements (dg-set-elements-intersection txn ids elements))) (define (dg-set-intersection txn . ids) "dg-txn (integer ...) ... -> (integer ...) returns a list of all elements that are contained in all sets" (fold (l (id r) (dg-relation-select-read txn (list id) r null (q right))) null ids)) (define (dg-set-elements txn . ids) "dg-txn integer ... -> list" (dg-relation-select-read txn ids null null (q right))) (define (dg-ordered-set txn . elements) "dg-txn integer ... -> integer" (let (ids (dg-id-create txn)) (dg-relation-ensure txn ids elements null (l (value) (+ 1 value))) (first ids))))