Work on new ldict implementation.
This commit is contained in:
parent
c34d4ca159
commit
6d3d7079cf
2 changed files with 44 additions and 7 deletions
18
doc/utils.md
18
doc/utils.md
|
@ -31,6 +31,24 @@ representing its contents.
|
||||||
|
|
||||||
Splits given loaded CSV into two tables at the first empty row.
|
Splits given loaded CSV into two tables at the first empty row.
|
||||||
|
|
||||||
|
### Dictionary
|
||||||
|
|
||||||
|
(import util-dict)
|
||||||
|
|
||||||
|
This module implements a simple key/value dictionary using lists as
|
||||||
|
backend. All operations are O(n) with respect to time.
|
||||||
|
|
||||||
|
(make-ldict [equality?/pairs [pairs]])
|
||||||
|
|
||||||
|
* ```equality?/pairs``` - procedure or list of pairs
|
||||||
|
* ```pairs``` - list of pairs
|
||||||
|
|
||||||
|
Creates a new ldict with configurable equality procedure, optionally
|
||||||
|
populating it with initial data.
|
||||||
|
|
||||||
|
If only one argument is given, the procedure checks whether it is a
|
||||||
|
list of equality procedure and acts accordingly.
|
||||||
|
|
||||||
### IO
|
### IO
|
||||||
|
|
||||||
(import util-io)
|
(import util-io)
|
||||||
|
|
|
@ -28,7 +28,10 @@
|
||||||
(module
|
(module
|
||||||
util-dict-list
|
util-dict-list
|
||||||
(
|
(
|
||||||
|
TAG-LDICT
|
||||||
|
|
||||||
make-ldict
|
make-ldict
|
||||||
|
|
||||||
ldict-has-key?
|
ldict-has-key?
|
||||||
ldict-ref
|
ldict-ref
|
||||||
ldict-remove
|
ldict-remove
|
||||||
|
@ -43,14 +46,30 @@
|
||||||
|
|
||||||
(import scheme
|
(import scheme
|
||||||
(chicken base)
|
(chicken base)
|
||||||
testing)
|
testing
|
||||||
|
util-tag)
|
||||||
|
|
||||||
;; Returns an empty dictionary represented as empty list or a list of
|
;; Tag used for identifying list dictionaries from this module
|
||||||
;; pre-initialized cons pairs.
|
(define TAG-LDICT (make-tag LDICT))
|
||||||
(define (make-dict . pairs)
|
|
||||||
(if (null? pairs)
|
;; Creates an empty dictionary and optionally populates it with
|
||||||
'()
|
;; provided pairs. Default equality is eq?, default are '().
|
||||||
(car pairs)))
|
(define (make-ldict . equality?/pairs)
|
||||||
|
(let ((equality? (if (or (null? equality?/pairs)
|
||||||
|
(not (procedure? (car equality?/pairs))))
|
||||||
|
eq?
|
||||||
|
(car equality?/pairs)))
|
||||||
|
(pairs (if (or (null? equality?/pairs)
|
||||||
|
(procedure? (car equality?/pairs)))
|
||||||
|
'()
|
||||||
|
(car equality?/pairs))))
|
||||||
|
(let loop ((ld (list TAG-LDICT
|
||||||
|
(list equality?)))
|
||||||
|
(pairs pairs))
|
||||||
|
(if (null? pairs)
|
||||||
|
ld
|
||||||
|
(loop (ldict-set ld (caar pairs) (cdar pairs))
|
||||||
|
(cdr pairs))))))
|
||||||
|
|
||||||
;; Checks whether given dictionary d contains the key k.
|
;; Checks whether given dictionary d contains the key k.
|
||||||
(define (dict-has-key? d k)
|
(define (dict-has-key? d k)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue