diff options
| author | 2021-10-17 12:09:08 +0300 | |
|---|---|---|
| committer | 2021-10-17 12:09:08 +0300 | |
| commit | 1e501c16b620c976a9b2d85ba5ad82b5e83ae0da (patch) | |
| tree | 000f4e1c767113245478e5485f2bf4cc05a6d3e0 /srfi/srfi-125-impl.scm | |
| parent | work (diff) | |
work
Diffstat (limited to 'srfi/srfi-125-impl.scm')
| -rw-r--r-- | srfi/srfi-125-impl.scm | 169 |
1 files changed, 90 insertions, 79 deletions
diff --git a/srfi/srfi-125-impl.scm b/srfi/srfi-125-impl.scm index f594f6b..5705613 100644 --- a/srfi/srfi-125-impl.scm +++ b/srfi/srfi-125-impl.scm @@ -1,86 +1,101 @@ (define hash-table-dtd (let () - (define (t125-make-hash-table* dtd comparator) - ;; make mutable table - (t125-hash-table-empty-copy (t125-hash-table comparator))) - - (define (t125-hash-table-set!* dtd table . obj) - (apply t125-hash-table-set! (cons table obj)) - table) - - (define (t125-hash-table-update!* dtd table key updater fail success) - (t125-hash-table-update! table key updater fail success) - table) - - (define (t125-hash-table-update!/default* dtd table key proc default) - (t125-hash-table-update!/default table key proc default) - table) - - (define (t125-hash-table-intern!* dtd table key failure) - (define val (t125-hash-table-intern! table key failure)) - (values table val)) - - (define (t125-hash-table-pop!* dtd table) + (define-syntax guard-immutable + (syntax-rules () + ((_ table body ... final-expr) + (if (t125-hash-table-mutable? table) + (let () + body ... + final-expr) + (let ((table (t125-hash-table-copy table #t))) + body ... + (let ((table (t125-hash-table-copy table #f))) + final-expr)))))) + + (define (t125-hash-table-mutable?* dtd table) + (t125-hash-table-mutable? table)) + + (define (t125-hash-table-set* dtd table . obj) + (guard-immutable table + (apply t125-hash-table-set! (cons table obj)) + table)) + + (define (t125-hash-table-update* dtd table key updater fail success) + (guard-immutable table + (t125-hash-table-update! table key updater fail success) + table)) + + (define (t125-hash-table-update/default* dtd table key proc default) + (guard-immutable table + (t125-hash-table-update!/default table key proc default) + table)) + + (define (t125-hash-table-intern* dtd table key failure) + (guard-immutable table + (define val (t125-hash-table-intern! table key failure)) + (values table val))) + + (define (t125-hash-table-pop* dtd table) (if (t125-hash-table-empty? table) (error "popped empty dictionary") - (call-with-values - (lambda () (t125-hash-table-pop! table)) - (lambda (key value) (values table key value))))) - - (define (t125-hash-table-delete-all!* dtd table keys) - (for-each - (lambda (key) - (t125-hash-table-delete! table key)) - keys) - table) - - (define (t125-hash-table-map!* dtd proc table) - (t125-hash-table-map! proc table) - table) - - (define (t125-hash-table-filter!* dtd proc table) - (t125-hash-table-prune! - (lambda (key value) - (not (proc key value))) - table) - table) + (guard-immutable table + (define-values + (key value) + (t125-hash-table-pop! table)) + (values table key value)))) + + (define (t125-hash-table-delete-all* dtd table keys) + (guard-immutable table + (for-each + (lambda (key) + (t125-hash-table-delete! table key)) + keys) + table)) - (define (t125-hash-table-filter* dtd proc table) - (dict-filter! dtd proc (dict-copy dtd table))) + (define (t125-hash-table-map* dtd proc table) + (guard-immutable table + (t125-hash-table-map! proc table) + table)) - (define (t125-hash-table-remove!* dtd proc table) - (t125-hash-table-prune! proc table) - table) + (define (t125-hash-table-filter* dtd proc table) + (guard-immutable table + (t125-hash-table-prune! + (lambda (key value) + (not (proc key value))) + table) + table)) (define (t125-hash-table-remove* dtd proc table) - (dict-remove! dtd proc (dict-copy dtd table))) + (guard-immutable table + (t125-hash-table-prune! proc table) + table)) - (define (t125-hash-table-search!* dtd table key fail success) + (define (t125-hash-table-alter* dtd table key fail success) (define (handle-success value) - (define (update new-key new-value obj) - (unless (eq? new-key key) - (t125-hash-table-delete! table key)) - (t125-hash-table-set! table new-key new-value) - (values table obj)) - (define (remove obj) - (t125-hash-table-delete! table key) - (values table obj)) + (define (update new-key new-value) + (guard-immutable table + (unless (eq? new-key key) + (t125-hash-table-delete! table key)) + (t125-hash-table-set! table new-key new-value) + table)) + (define (remove) + (guard-immutable table + (t125-hash-table-delete! table key) + table)) (success key value update remove)) (define (handle-fail) - (define (ignore obj) - (values table obj)) - (define (insert value obj) - (t125-hash-table-set! table key value) - (values table obj)) + (define (ignore) + table) + (define (insert value) + (guard-immutable table + (t125-hash-table-set! table key value) + table)) (fail insert ignore)) (define default (cons #f #f)) (t125-hash-table-ref table key handle-fail handle-success)) - (define (t125-hash-table-search* dtd table key fail success) - (t125-hash-table-search!* dtd (dict-copy dtd table) key fail success)) - (define (t125-hash-table-comparator* dtd table) (make-comparator (lambda args #t) (t125-hash-table-equivalence-function table) @@ -130,25 +145,22 @@ (t125-hash-table-ref/default table key default)) (make-dtd - make-dictionary-id t125-make-hash-table* dictionary?-id t125-hash-table?* + dict-mutable?-id t125-hash-table-mutable?* dict-empty?-id t125-hash-table-empty?* dict-contains?-id t125-hash-table-contains?* dict-ref-id t125-hash-table-ref* dict-ref/default-id t125-hash-table-ref/default* - dict-set!-id t125-hash-table-set!* - dict-delete-all!-id t125-hash-table-delete-all!* - dict-intern!-id t125-hash-table-intern!* - dict-update!-id t125-hash-table-update!* - dict-update/default!-id t125-hash-table-update!/default* - dict-pop!-id t125-hash-table-pop!* - dict-map!-id t125-hash-table-map!* - dict-filter!-id t125-hash-table-filter!* + dict-set-id t125-hash-table-set* + dict-delete-all-id t125-hash-table-delete-all* + dict-intern-id t125-hash-table-intern* + dict-update-id t125-hash-table-update* + dict-update/default-id t125-hash-table-update/default* + dict-pop-id t125-hash-table-pop* + dict-map-id t125-hash-table-map* dict-filter-id t125-hash-table-filter* - dict-remove!-id t125-hash-table-remove!* dict-remove-id t125-hash-table-remove* - dict-search!-id t125-hash-table-search!* - dict-search-id t125-hash-table-search* + dict-alter-id t125-hash-table-alter* dict-size-id t125-hash-table-size* dict-for-each-id t125-hash-table-for-each* dict-keys-id t125-hash-table-keys* @@ -157,5 +169,4 @@ dict-fold-id t125-hash-table-fold* dict-map->list-id t125-hash-table-map->list* dict->alist-id t125-hash-table->alist* - dict-comparator-id t125-hash-table-comparator* - dict-copy-id t125-hash-table-copy*))) + dict-comparator-id t125-hash-table-comparator*))) |
