lookup-table

Provides a Dictionary abstraction.

  1. lookup-table
  2. Documentation
    1. make-dict
    2. alist->dict
    3. dict?
    4. dict-equivalence-function
    5. dict-count
    6. dict-keys
    7. dict-values
    8. dict->alist
    9. dict-ref
    10. dict-indempotent-ref!
    11. dict-exists?
    12. dict-set!
    13. dict-update!
    14. dict-update-list!
    15. dict-update-dict!
    16. dict-delete!
    17. dict-for-each
    18. dict-merge!
    19. dict-search
    20. dict-print
  3. Usage
  4. Requirements
  5. Author
  6. Version history
  7. License

Documentation

Purports to be a simple key -> value lookup table. Known as a 'Dictionary' in some circles. It attempts to optimize for speed of lookup by choosing different storage models.

Four variants of the API are available:

safe
Performs argument and other constraint checks.
unsafe
Performs no constraint checks.
safe synchronized
Suffixes each procedure name with "/synch". All access to the dict object is synchronized.
unsafe synchronized
Suffixes each procedure name with "/%synch", except constructors & predicates which use "/synch".

Example:

make-dict

[procedure] (make-dict [EQUALITY [ESTIMATE]]) -> dict

Returns a dictionary using the supplied EQUALITY test, optimized for the number of elements ESTIMATE.

EQUALITY
procedure ; eq?
ESTIMATE
fixnum

alist->dict

[procedure] (alist->dict ALIST EQUALITY [ESTIMATE]) -> dict

Returns a dictionary constructed from ALIST using the supplied EQUALITY test, and optional ESTIMATE.

EQUALITY
procedure ; eq?
ESTIMATE
fixnum ; 0

dict?

[procedure] (dict? OBJ) -> boolean

Is the OBJ a dictionary?

dict-equivalence-function

[procedure] (dict-equivalence-function DICT) -> (* * --> boolean)

Returns the equality test predicate procedure for DICT.

dict-count

[procedure] (dict-count DICT) -> fixnum

Returns the number of items in the DICT.

dict-keys

[procedure] (dict-keys DICT) -> list

Returns the keys in the DICT.

dict-values

[procedure] (dict-values DICT) -> list

Returns the values in the DICT.

dict->alist

[procedure] (dict->alist DICT) -> list

Returns the DICT as an association list. The result may not be mutated!

dict-ref

[procedure] (dict-ref DICT KEY [DEF]) -> *

Returns the value associated with KEY in the DICT, otherwise DEF.

DEF
* ; #f

dict-indempotent-ref!

[procedure] (dict-indempotent-ref! DICT KEY FUNC [DEF]) -> *

Should a value for KEY exist in DICT it is returned. Otherwise FUNC is invoked on DEF. Any result other than DEF is the value for the KEY and that value is returned. Otherwise returns DEF.

DEF
* ; #f

dict-exists?

[procedure] (dict-exists? DICT KEY) -> boolean

Does an entry with KEY exist in the DICT ?

dict-set!

[procedure] (dict-set! DICT KEY VAL)

Associate VAL with KEY in the DICT.

VAL must not be (void)!

dict-update!

[procedure] (dict-update! DICT KEY DEF-VAL-PROCEDURE [FUNC])

Invokes FUNC on either the existing value for KEY in the DICT, or the result of the DEF-VAL-PROCEDURE when no existing value. The result then becomes the value for KEY in the DICT.

Returns the updated value for KEY in the DICT.

FUNC
(* -> *) ; identity

DEF-VAL-PROCEDURE must not return (void)!

dict-update-list!

[procedure] (dict-update-list! DICT KEY OBJ ...)

Updates the value for KEY in the DICT with the OBJ list.

OBJ ...
(list-of *)

dict-update-dict!

[procedure] (dict-update-dict! DICT KEY [EQUALITY [ESTIMATE]])

Updates the value for KEY in the DICT with a dict.

EQUALITY
procedure ; eq?; ESTIMATE : fixnum

dict-delete!

[procedure] (dict-delete! DICT KEY)

Removes any association of KEY in the DICT.

dict-for-each

[procedure] (dict-for-each DICT PROC))

Invokes the supplied PROC with each association in the DICT.

PROC
(KEY VAL -> void)

dict-merge!

[procedure] (dict-merge! DICT DICT1 ...) -> dict

Returns the DICT as the (union DICT DICT1 ...) using overwrite semantics.

Tables must have the same equality predicate!

[procedure] (dict-search DICT PRED [DEF]) -> *

Returns the first entry value matched by the PRED. Otherwise the DEF value is returned.)

PRED
(KEY VAL --> boolean)
DEF
* ; #f

dict-print

[procedure] (dict-print DICT [PORT])

Pretty-print DICT to PORT.

PORT
output-port ; (current-output-port)

Usage

(require-extension lookup-table)
(require-extension lookup-table-synch)
(require-extension lookup-table-unsafe)
(require-extension lookup-table-unsafe-synch)

Requirements

miscmacros check-errors record-variants synch dsssl-utils

setup-helper

test

Author

Kon Lovett

Version history

1.14.1
Uses typed-define.
1.14.0
Add types.
1.13.3
Fix unsafe versions (not actually "unsafe").
1.13.2
Fix for backwards incompatible use of module body string feature.
1.13.1
Made dict-safe-mode a plain procedure since it is a no-op.
1.13.0
Uses record-variants.
1.12.0
Added 'make-dict' parameters to 'dict-update-dict!'.
1.11.0
Added 'dict-Idempotent-ref!' and 'synch' versions.
1.10.0
Split into safe & unsafe
1.9.1
Fix for strange compile output (C procedures being redefined).
1.9.0
Use of primitives.
1.8.0
Chicken 4 release.

License

Copyright (C) 2009-2018 Kon Lovett. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.