Use the new BST dictionary for indexing members.
This commit is contained in:
parent
209ef27a90
commit
bebb8af611
4 changed files with 71 additions and 40 deletions
|
@ -49,6 +49,7 @@
|
|||
mbase-free-ids
|
||||
mbase-gen-id
|
||||
|
||||
mbase-update-by-id
|
||||
mbase-update
|
||||
|
||||
mbase-info
|
||||
|
@ -79,7 +80,8 @@
|
|||
progress
|
||||
mbase-dir
|
||||
util-tag
|
||||
util-kwargs)
|
||||
util-kwargs
|
||||
util-dict-bst)
|
||||
|
||||
;; Constant unique tag
|
||||
(define TAG-MBASE (make-tag mbase))
|
||||
|
@ -111,35 +113,31 @@
|
|||
mb0))
|
||||
(mb (ldict-reduce '()
|
||||
(lambda (acc id mr)
|
||||
(cons mr acc))
|
||||
(cons (cons id mr) acc))
|
||||
mb1)))
|
||||
mb)))))))
|
||||
(list->bdict (sort mb (lambda (a b) (< (car a) (car b))))))))))))
|
||||
|
||||
;; Predicate
|
||||
(define (mbase? v)
|
||||
(and (ldict? v)
|
||||
(eq? (ldict-ref v 'TAG #f) TAG-MBASE)))
|
||||
|
||||
;; Returns the internal members list
|
||||
;; Returns the internal members BST
|
||||
(define (mbase-members mb)
|
||||
(ldict-ref mb 'members))
|
||||
|
||||
;; Gets member based by generic predicate
|
||||
(define (find-member-by-predicate mb pred)
|
||||
(let loop ((mdb (mbase-members mb)))
|
||||
(if (null? mdb)
|
||||
#f
|
||||
(let ((mr (car mdb)))
|
||||
(if (pred mr)
|
||||
mr
|
||||
(loop (cdr mdb)))))))
|
||||
(define (find-member-by-predicate mb pred?)
|
||||
(bdict-find-value
|
||||
(mbase-members mb)
|
||||
(lambda (k v)
|
||||
(pred? v))))
|
||||
|
||||
;; Returns member record found by id
|
||||
;; Returns member record found by id, accepts numeric id or #f
|
||||
(define (find-member-by-id mb id)
|
||||
(find-member-by-predicate
|
||||
mb
|
||||
(lambda (mr)
|
||||
(eq? (ldict-ref mr 'id) id))))
|
||||
(if id
|
||||
(bdict-ref (mbase-members mb) id #f)
|
||||
#f))
|
||||
|
||||
;; Returns member record found by id
|
||||
(define (find-member-by-nick mb nick)
|
||||
|
@ -153,16 +151,11 @@
|
|||
nick))))
|
||||
|
||||
;; Returns a list of members which match given predicate.
|
||||
(define (find-members-by-predicate mb pred)
|
||||
(let loop ((mb (mbase-members mb))
|
||||
(res '()))
|
||||
(if (null? mb)
|
||||
res
|
||||
(let ((mr (car mb)))
|
||||
(loop (cdr mb)
|
||||
(if (pred mr)
|
||||
(cons mr res)
|
||||
res))))))
|
||||
(define (find-members-by-predicate mb pred?)
|
||||
(bdict-filter-values
|
||||
(mbase-members mb)
|
||||
(lambda (k v)
|
||||
(pred? v))))
|
||||
|
||||
;; Returns a list of members whose nick contains pat
|
||||
(define (find-members-by-nick mb pat)
|
||||
|
@ -173,13 +166,13 @@
|
|||
|
||||
;; Returns all ids found in the database
|
||||
(define (list-mbase-ids mb)
|
||||
(map (lambda (mr) (ldict-ref mr 'id))
|
||||
(mbase-members mb)))
|
||||
(bdict-keys (mbase-members mb)))
|
||||
|
||||
;; Returns all nicks found in the database
|
||||
(define (list-mbase-nicks mb)
|
||||
(map brmember-nick
|
||||
(mbase-members mb)))
|
||||
(bdict-map-list (mbase-members mb)
|
||||
(lambda (id mr)
|
||||
(brmember-nick mr))))
|
||||
|
||||
;; Returns all free ids
|
||||
(define (mbase-free-ids mb)
|
||||
|
@ -195,16 +188,23 @@
|
|||
(vfids (list->vector fids)))
|
||||
(vector-ref vfids (pseudo-random-integer (vector-length vfids)))))
|
||||
|
||||
;; Returns new members base with member specified by id processed by
|
||||
;; proc.
|
||||
(define (mbase-update-by-id mb id proc)
|
||||
(ldict-set mb
|
||||
'members
|
||||
(bdict-update (mbase-members mb) id proc)))
|
||||
|
||||
;; Returns new members base with member records matching the
|
||||
;; predicate processed by proc.
|
||||
(define (mbase-update mb pred? proc)
|
||||
(ldict-set mb
|
||||
'members
|
||||
(map (lambda (mr)
|
||||
(if (pred? mr)
|
||||
(proc mr)
|
||||
mr))
|
||||
(mbase-members mb))))
|
||||
(bdict-map-dict (mbase-members mb)
|
||||
(lambda (id mr)
|
||||
(if (pred? mr)
|
||||
(proc mr)
|
||||
mr)))))
|
||||
|
||||
;; Returns dictionary with statistics about the members base.
|
||||
(define (mbase-info mb-arg)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue