Changeset 14514 in project


Ignore:
Timestamp:
04/30/09 19:09:11 (10 years ago)
Author:
Kon Lovett
Message:

Rename of basic ops.

Location:
release/4/lookup-table
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • release/4/lookup-table/branches/primitive/lookup-table.scm

    r14512 r14514  
    1818
    1919(define-record-type/unsafe-inline-unchecked dict
    20         (%make-dict data)
    21         %dict?
     20        (%make-dictbase data)
     21        %dictbase?
    2222        (data %dict-data-ref %dict-data-set!)
    2323        (test %dict-test-ref %dict-test-set!)
     
    4949        dict )
    5050
    51 (define-inline (%dict-test dict) ((%dict-test-ref dict) (%dict-data-ref dict)))
    52 (define-inline (%dict->alist dict) ((%dict->alist-ref dict) (%dict-data-ref dict)))
    53 (define-inline (%dict-ref dict key def) ((%dict-ref-ref dict) (%dict-data-ref dict) key def))
    54 (define-inline (%dict-set! dict key val) ((%dict-set-ref dict) (%dict-data-ref dict) key val))
    55 (define-inline (%dict-delete! dict key) ((%dict-delete-ref dict) (%dict-data-ref dict) key))
    56 (define-inline (%dict-for-each dict proc) ((%dict-for-each-ref dict) (%dict-data-ref dict) proc))
    57 (define-inline (%dict-merge dict1 dict2) ((%dict-merge-ref dict1) (%dict-data-ref dict1) (%dict-data-ref dict2)))
    58 (define-inline (%dict-search dict proc def) ((%dict-search-ref dict) (%dict-data-ref dict) proc def))
    59 (define-inline (%dict-count dict) ((%dict-count-ref dict) (%dict-data-ref dict)))
    60 (define-inline (%dict-keys dict) ((%dict-keys-ref dict) (%dict-data-ref dict)))
    61 (define-inline (%dict-values dict) ((%dict-values-ref dict) (%dict-data-ref dict)))
    62 (define-inline (%dict-exists? dict key) ((%dict-exists-ref dict) (%dict-data-ref dict) key))
     51(define-inline (%dictbase-test dict) ((%dict-test-ref dict) (%dict-data-ref dict)))
     52(define-inline (%dictbase->alist dict) ((%dict->alist-ref dict) (%dict-data-ref dict)))
     53(define-inline (%dictbase-ref dict key def) ((%dict-ref-ref dict) (%dict-data-ref dict) key def))
     54(define-inline (%dictbase-set! dict key val) ((%dict-set-ref dict) (%dict-data-ref dict) key val))
     55(define-inline (%dictbase-delete! dict key) ((%dict-delete-ref dict) (%dict-data-ref dict) key))
     56(define-inline (%dictbase-for-each dict proc) ((%dict-for-each-ref dict) (%dict-data-ref dict) proc))
     57(define-inline (%dictbase-merge dict1 dict2) ((%dict-merge-ref dict1) (%dict-data-ref dict1) (%dict-data-ref dict2)))
     58(define-inline (%dictbase-search dict proc def) ((%dict-search-ref dict) (%dict-data-ref dict) proc def))
     59(define-inline (%dictbase-count dict) ((%dict-count-ref dict) (%dict-data-ref dict)))
     60(define-inline (%dictbase-keys dict) ((%dict-keys-ref dict) (%dict-data-ref dict)))
     61(define-inline (%dictbase-values dict) ((%dict-values-ref dict) (%dict-data-ref dict)))
     62(define-inline (%dictbase-exists? dict key) ((%dict-exists-ref dict) (%dict-data-ref dict) key))
    6363
    6464;; Association List
     
    108108;; Dictionary Type
    109109
    110 (define-inline (%dict-same-kind? dict1 dict2)
    111   (%eq? (%dict-test-ref dict1) (%dict-test-ref dict2)) )
    112 
    113 (define-inline (%dict-same-test? dict1 dict2) (%eq? (%dict-test dict1) (%dict-test dict2)))
     110(define-inline (%dict-same-kind? dict1 dict2) (%eq? (%dict-test-ref dict1) (%dict-test-ref dict2)))
     111
     112(define-inline (%dict-same-test? dict1 dict2) (%eq? (%dictbase-test dict1) (%dictbase-test dict2)))
    114113
    115114(define-inline (%dict-bestfit dict)
    116         (if (%fx< MAGIC-LIMIT (%dict-count dict))
     115        (if (%fx< MAGIC-LIMIT (%dictbase-count dict))
    117116      (unless (htable-dict? dict) (become-htable-dict dict))
    118117      (unless (alist-dict? dict) (become-alist-dict dict)) ) )
     
    209208
    210209(define (make-alist-dict test al)
    211   (%set-alist-dict-procs! (%make-dict (%make-alist-data test al))) )
    212 
    213 (define (alist-dict? dict) (%eq? alist-dict-test-ref (%dict-test dict)))
     210  (%set-alist-dict-procs! (%make-dictbase (%make-alist-data test al))) )
     211
     212(define (alist-dict? dict) (%eq? alist-dict-test-ref (%dictbase-test dict)))
    214213
    215214(define (become-alist-dict dict)
    216         (%dict-data-set! dict (%make-alist-data (%dict-test dict) (%dict->alist dict)))
     215        (%dict-data-set! dict (%make-alist-data (%dictbase-test dict) (%dictbase->alist dict)))
    217216        (%set-alist-dict-procs! dict) )
    218217
     
    257256
    258257(define (make-htable-dict test ht)
    259   (%set-htable-dict-procs! (%make-dict (%make-htable-data test ht))) )
    260 
    261 (define (htable-dict? dict) (%eq? htable-dict-test-ref (%dict-test dict)))
     258  (%set-htable-dict-procs! (%make-dictbase (%make-htable-data test ht))) )
     259
     260(define (htable-dict? dict) (%eq? htable-dict-test-ref (%dictbase-test dict)))
    262261
    263262(define (become-htable-dict dict)
    264         (let ((test (%dict-test dict)))
    265                 (%dict-data-set! dict (%make-htable-data test (alist->hash-table (%dict->alist dict) test))))
     263        (let ((test (%dictbase-test dict)))
     264                (%dict-data-set! dict (%make-htable-data test (alist->hash-table (%dictbase->alist dict) test))))
    266265        (%set-htable-dict-procs! dict) )
    267266
     
    274273(define (*dict-print dict)
    275274  ((rec (print-node-table dict spcr)
    276     (%dict-for-each dict
     275    (%dictbase-for-each dict
    277276      (lambda (key val)
    278277        (%list-for-each/1 display spcr)
    279         (cond ((%dict? val)
     278        (cond ((%dictbase? val)
    280279                (write key) (display " :") (newline)
    281280                (print-node-table val (%cons "  " spcr)))
     
    290289                    (when *dict-safe-mode* (%check-value loc val))
    291290                    val ) ) ) ) )
    292     (%dict-set! dict key val)
     291    (%dictbase-set! dict key val)
    293292    val ) )
    294293
     
    298297    (%check-procedure loc valu-func)
    299298    (%check-procedure loc updt-func) )
    300   (let* ((curr (%dict-ref dict key (%undefined-value)))
     299  (let* ((curr (%dictbase-ref dict key (%undefined-value)))
    301300         (updt (*dict-update! dict key valu-func updt-func curr loc)))
    302301    (unless (%undefined-value? curr) (%dict-bestfit dict))
     
    328327(define (dict->alist dict)
    329328  (when *dict-safe-mode* (%check-dict 'dict->alist dict))
    330         (%dict->alist dict) )
    331 
    332 (define (dict? obj) (%dict? obj))
     329        (%dictbase->alist dict) )
     330
     331(define (dict? obj) (%dictbase? obj))
    333332
    334333(define (dict-equivalence-function dict)
    335334  (when *dict-safe-mode* (%check-dict 'dict-equivalence-function dict))
    336         (%dict-test dict) )
     335        (%dictbase-test dict) )
    337336
    338337(define (dict-count dict)
    339338  (when *dict-safe-mode* (%check-dict 'dict-count dict))
    340         (%dict-count dict) )
     339        (%dictbase-count dict) )
    341340
    342341(define (dict-keys dict)
    343342  (when *dict-safe-mode* (%check-dict 'dict-keys dict))
    344         (%dict-keys dict) )
     343        (%dictbase-keys dict) )
    345344
    346345(define (dict-values dict)
    347346  (when *dict-safe-mode* (%check-dict 'dict-values dict))
    348         (%dict-values dict) )
     347        (%dictbase-values dict) )
    349348
    350349(define (dict-ref dict key #!optional def)
    351350  (when *dict-safe-mode* (%check-dict 'dict-ref dict))
    352         (%dict-ref dict key def) )
     351        (%dictbase-ref dict key def) )
    353352
    354353(define (dict-set! dict key obj)
     
    356355    (%check-value 'dict-set! obj)
    357356    (%check-dict 'dict-set! dict) )
    358         (%dict-set! dict key obj)
     357        (%dictbase-set! dict key obj)
    359358        (%dict-bestfit dict) )
    360359
    361360(define (dict-exists? dict key)
    362361  (when *dict-safe-mode* (%check-dict 'dict-exists? dict))
    363   (%dict-exists? dict key) )
     362  (%dictbase-exists? dict key) )
    364363
    365364(define (dict-update! dict key valu-func #!optional (updt-func identity))
     
    374373(define (dict-delete! dict key)
    375374  (when *dict-safe-mode* (%check-dict 'dict-delete! dict))
    376         (%dict-delete! dict key)
     375        (%dictbase-delete! dict key)
    377376        (%dict-bestfit dict) )
    378377
     
    381380    (%check-dict 'dict-for-each dict)
    382381    (%check-procedure 'dict-for-each proc) )
    383         (%dict-for-each dict proc) )
     382        (%dictbase-for-each dict proc) )
    384383
    385384(define (dict-search dict proc #!optional def)
     
    387386    (%check-dict 'dict-search dict)
    388387    (%check-procedure 'dict-search proc) )
    389         (%dict-search dict proc def) )
     388        (%dictbase-search dict proc def) )
    390389
    391390(define (dict-merge! dict . dicts)
     
    397396        (unless (%dict-same-test? dict dictx)
    398397          (error "cannot merge lookup-tables; incompatible test") ) )
    399                         (if (%dict-same-kind? dict dictx) (%dict-merge dict dictx)
    400           (%dict-for-each dictx (cut %dict-set! dict <> <>)) ) )
     398                        (if (%dict-same-kind? dict dictx) (%dictbase-merge dict dictx)
     399          (%dictbase-for-each dictx (cut dict-set! dict <> <>)) ) )
    401400                dicts)
    402401        (%dict-bestfit dict) )
  • release/4/lookup-table/trunk/lookup-table.scm

    r14513 r14514  
    1111
    1212;;;
     13
     14;; Element count when hash-table faster
     15;; (your milage may vary)
     16
     17(define-constant MAGIC-LIMIT 12)
     18
     19;;
    1320
    1421(module lookup-table (;export
     
    3744(require-library srfi-1 srfi-69 extras miscmacros type-checks type-errors)
    3845
     46;;;
     47
     48(define (alist-find proc al #!optional def)
     49        (let loop ((al al))
     50                (if (null? al) def
     51        (let* ((cell (car al))
     52               (val (cdr cell)))
     53            (if (proc (car cell) val) val
     54                (loop (cdr al)) ) ) ) ) )
     55
    3956;;; Variant Dictionary
    4057
    4158(define-record-type dict
    42         (make-dict data)
     59        (make-dictbase data)
    4360        dict?
    4461        (data dict-data-ref dict-data-set!)
     
    7188        dict )
    7289
    73 (define (dict-test dict) ((dict-test-ref dict) (dict-data-ref dict)))
    74 (define (dict->alist dict) ((dict->alist-ref dict) (dict-data-ref dict)))
    75 (define (dict-ref dict key def) ((dict-ref-ref dict) (dict-data-ref dict) key def))
    76 (define (dict-set! dict key val) ((dict-set-ref dict) (dict-data-ref dict) key val))
    77 (define (dict-delete! dict key) ((dict-delete-ref dict) (dict-data-ref dict) key))
    78 (define (dict-for-each dict proc) ((dict-for-each-ref dict) (dict-data-ref dict) proc))
    79 (define (dict-merge dict1 dict2) ((dict-merge-ref dict1) (dict-data-ref dict1) (dict-data-ref dict2)))
    80 (define (dict-search dict proc def) ((dict-search-ref dict) (dict-data-ref dict) proc def))
    81 (define (dict-count dict) ((dict-count-ref dict) (dict-data-ref dict)))
    82 (define (dict-keys dict) ((dict-keys-ref dict) (dict-data-ref dict)))
    83 (define (dict-values dict) ((dict-values-ref dict) (dict-data-ref dict)))
    84 (define (dict-exists? dict key) ((dict-exists-ref dict) (dict-data-ref dict) key))
     90(define (dictbase-test dict) ((dict-test-ref dict) (dict-data-ref dict)))
     91(define (dictbase->alist dict) ((dict->alist-ref dict) (dict-data-ref dict)))
     92(define (dictbase-ref dict key def) ((dict-ref-ref dict) (dict-data-ref dict) key def))
     93(define (dictbase-set! dict key val) ((dict-set-ref dict) (dict-data-ref dict) key val))
     94(define (dictbase-delete! dict key) ((dict-delete-ref dict) (dict-data-ref dict) key))
     95(define (dictbase-for-each dict proc) ((dict-for-each-ref dict) (dict-data-ref dict) proc))
     96(define (dictbase-merge dict1 dict2) ((dict-merge-ref dict1) (dict-data-ref dict1) (dict-data-ref dict2)))
     97(define (dictbase-search dict proc def) ((dict-search-ref dict) (dict-data-ref dict) proc def))
     98(define (dictbase-count dict) ((dict-count-ref dict) (dict-data-ref dict)))
     99(define (dictbase-keys dict) ((dict-keys-ref dict) (dict-data-ref dict)))
     100(define (dictbase-values dict) ((dict-values-ref dict) (dict-data-ref dict)))
     101(define (dictbase-exists? dict key) ((dict-exists-ref dict) (dict-data-ref dict) key))
    85102
    86103;; Association List
     
    133150  (eq? (dict-test-ref dict1) (dict-test-ref dict2)) )
    134151
    135 (define (dict-same-test? dict1 dict2) (eq? (dict-test dict1) (dict-test dict2)))
     152(define (dict-same-test? dict1 dict2) (eq? (dictbase-test dict1) (dictbase-test dict2)))
    136153
    137154(define (dict-bestfit dict)
    138         (if (fx< MAGIC-LIMIT (dict-count dict))
     155        (if (fx< MAGIC-LIMIT (dictbase-count dict))
    139156      (unless (htable-dict? dict) (become-htable-dict dict))
    140157      (unless (alist-dict? dict) (become-alist-dict dict)) ) )
     
    145162
    146163(define (check-value loc obj)
    147   (when (undefined-value? obj) (error-argument-type loc obj "non-undefined value")) )
    148 
    149 ;; Element count when hash-table faster
    150 ;; (your milage may vary)
    151 
    152 (define-constant MAGIC-LIMIT 12)
     164  (when (eq? (void) obj) (error-argument-type loc obj "non-undefined value")) )
    153165
    154166;;;
     
    181193
    182194(define (alist-dict-for-each data proc)
    183         (list-for-each/1 (lambda (cell) (proc (car cell) (cdr cell)))
     195        (for-each (lambda (cell) (proc (car cell) (cdr cell)))
    184196                          (alist-dict-alist data)) )
    185197
     
    187199        (let ((test (alist-dict-test data1))
    188200              (al (alist-dict-alist data1)))
    189                 (list-for-each/1 (lambda (cell)
     201                (for-each (lambda (cell)
    190202                                    (set! al (alist-update! (car cell) (cdr cell) al test)))
    191203                                        (alist-dict-alist data2))
     
    196208(define (alist-dict-count data) (length (alist-dict-alist data)))
    197209
    198 (define (alist-dict-keys data) (list-map/1 (lambda (x) (car x)) (alist-dict-alist data)))
    199 
    200 (define (alist-dict-values data) (list-map/1 (lambda (x) (cdr x)) (alist-dict-alist data)))
     210(define (alist-dict-keys data) (map (lambda (x) (car x)) (alist-dict-alist data)))
     211
     212(define (alist-dict-values data) (map (lambda (x) (cdr x)) (alist-dict-alist data)))
    201213
    202214(define (alist-dict-exists? data key)
    203         (not (undefined-value? (alist-dict-ref data key (undefined-value)))) )
     215        (not (eq? (void) (alist-dict-ref data key (void)))) )
    204216
    205217(define (make-alist-dict test al)
    206   (set-alist-dict-procs! (make-dict (make-alist-data test al))) )
    207 
    208 (define (alist-dict? dict) (eq? alist-dict-test-ref (dict-test dict)))
     218  (set-alist-dict-procs! (make-dictbase (make-alist-data test al))) )
     219
     220(define (alist-dict? dict) (eq? alist-dict-test-ref (dictbase-test dict)))
    209221
    210222(define (become-alist-dict dict)
    211         (dict-data-set! dict (make-alist-data (dict-test dict) (dict->alist dict)))
     223        (dict-data-set! dict (make-alist-data (dictbase-test dict) (dict->alist dict)))
    212224        (set-alist-dict-procs! dict) )
    213225
     
    252264
    253265(define (make-htable-dict test ht)
    254   (set-htable-dict-procs! (make-dict (make-htable-data test ht))) )
    255 
    256 (define (htable-dict? dict) (eq? htable-dict-test-ref (dict-test dict)))
     266  (set-htable-dict-procs! (make-dictbase (make-htable-data test ht))) )
     267
     268(define (htable-dict? dict) (eq? htable-dict-test-ref (dictbase-test dict)))
    257269
    258270(define (become-htable-dict dict)
    259         (let ((test (dict-test dict)))
     271        (let ((test (dictbase-test dict)))
    260272                (dict-data-set! dict (make-htable-data test (alist->hash-table (dict->alist dict) test))))
    261273        (set-htable-dict-procs! dict) )
     
    269281(define (*dict-print dict)
    270282  ((rec (print-node-table dict spcr)
    271     (dict-for-each dict
     283    (dictbase-for-each dict
    272284      (lambda (key val)
    273         (list-for-each/1 display spcr)
     285        (for-each display spcr)
    274286        (cond ((dict? val)
    275287                (write key) (display " :") (newline)
     
    281293(define (*dict-update! dict key valu-func updt-func curr loc)
    282294  (let ((val (updt-func
    283               (if (not (undefined-value? curr)) curr
     295              (if (not (eq? (void) curr)) curr
    284296                  (let ((val (valu-func)))
    285297                    (when *dict-safe-mode* (check-value loc val))
    286298                    val ) ) ) ) )
    287     (dict-set! dict key val)
     299    (dictbase-set! dict key val)
    288300    val ) )
    289301
     
    293305    (check-procedure loc valu-func)
    294306    (check-procedure loc updt-func) )
    295   (let* ((curr (dict-ref dict key (undefined-value)))
     307  (let* ((curr (dictbase-ref dict key (void)))
    296308         (updt (*dict-update! dict key valu-func updt-func curr loc)))
    297     (unless (undefined-value? curr) (dict-bestfit dict))
     309    (unless (eq? (void) curr) (dict-bestfit dict))
    298310    updt ) )
    299311
     
    323335(define (dict->alist dict)
    324336  (when *dict-safe-mode* (check-dict 'dict->alist dict))
    325         (dict->alist dict) )
     337        (dictbase->alist dict) )
    326338
    327339(define (dict-equivalence-function dict)
    328340  (when *dict-safe-mode* (check-dict 'dict-equivalence-function dict))
    329         (dict-test dict) )
     341        (dictbase-test dict) )
    330342
    331343(define (dict-count dict)
    332344  (when *dict-safe-mode* (check-dict 'dict-count dict))
    333         (dict-count dict) )
     345        (dictbase-count dict) )
    334346
    335347(define (dict-keys dict)
    336348  (when *dict-safe-mode* (check-dict 'dict-keys dict))
    337         (dict-keys dict) )
     349        (dictbase-keys dict) )
    338350
    339351(define (dict-values dict)
    340352  (when *dict-safe-mode* (check-dict 'dict-values dict))
    341         (dict-values dict) )
     353        (dictbase-values dict) )
    342354
    343355(define (dict-ref dict key #!optional def)
    344356  (when *dict-safe-mode* (check-dict 'dict-ref dict))
    345         (dict-ref dict key def) )
     357        (dictbase-ref dict key def) )
    346358
    347359(define (dict-set! dict key obj)
     
    349361    (check-value 'dict-set! obj)
    350362    (check-dict 'dict-set! dict) )
    351         (dict-set! dict key obj)
     363        (dictbase-set! dict key obj)
    352364        (dict-bestfit dict) )
    353365
    354366(define (dict-exists? dict key)
    355367  (when *dict-safe-mode* (check-dict 'dict-exists? dict))
    356   (dict-exists? dict key) )
     368  (dictbase-exists? dict key) )
    357369
    358370(define (dict-update! dict key valu-func #!optional (updt-func identity))
     
    367379(define (dict-delete! dict key)
    368380  (when *dict-safe-mode* (check-dict 'dict-delete! dict))
    369         (dict-delete! dict key)
     381        (dictbase-delete! dict key)
    370382        (dict-bestfit dict) )
    371383
     
    374386    (check-dict 'dict-for-each dict)
    375387    (check-procedure 'dict-for-each proc) )
    376         (dict-for-each dict proc) )
     388        (dictbase-for-each dict proc) )
    377389
    378390(define (dict-search dict proc #!optional def)
     
    380392    (check-dict 'dict-search dict)
    381393    (check-procedure 'dict-search proc) )
    382         (dict-search dict proc def) )
     394        (dictbase-search dict proc def) )
    383395
    384396(define (dict-merge! dict . dicts)
    385397  (when *dict-safe-mode* (check-dict 'dict-merge! dict))
    386         (list-for-each/1
     398        (for-each
    387399                (lambda (dictx)
    388400      (when *dict-safe-mode*
     
    390402        (unless (dict-same-test? dict dictx)
    391403          (error "cannot merge lookup-tables; incompatible test") ) )
    392                         (if (dict-same-kind? dict dictx) (dict-merge dict dictx)
    393           (dict-for-each dictx (cut dict-set! dict <> <>)) ) )
     404                        (if (dict-same-kind? dict dictx) (dictbase-merge dict dictx)
     405          (dictbase-for-each dictx (cut dict-set! dict <> <>)) ) )
    394406                dicts)
    395407        (dict-bestfit dict) )
  • release/4/lookup-table/trunk/tests/run.scm

    r14484 r14514  
    5555
    5656(dict-safe-mode #t)
    57 (dict-alist-test'((foo . bar) (baz . bop)) eq?)
     57(newline) (print "** Alist Test (Safe eq?) **") (newline)
     58(dict-alist-test '((foo . bar) (baz . bop)) eq?)
     59(newline) (print "** Alist Test (Safe equal?) **") (newline)
    5860(dict-alist-test '((foo . bar) (baz . bop) (2 . 3)) equal?)
     61(newline) (print "** HT Test (Safe) **") (newline)
    5962(dict-ht-test)
    6063
    6164(dict-safe-mode #f)
    62 (dict-alist-test'((foo . bar) (baz . bop)) eq?)
     65(newline) (print "** Alist Test (Unsafe eq?) **") (newline)
     66(dict-alist-test '((foo . bar) (baz . bop)) eq?)
     67(newline) (print "** Alist Test (Unsafe equal?) **") (newline)
    6368(dict-alist-test '((foo . bar) (baz . bop) (2 . 3)) equal?)
     69(newline) (print "** HT Test (Unsafe) **") (newline)
    6470(dict-ht-test)
Note: See TracChangeset for help on using the changeset viewer.