Outdated egg!
This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.
If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.
lookup-table
Provides a Dictionary abstraction.
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:
- dict? is the name of the safe & unsafe variant's predicate, while dict?/synch is the safe synchronized and unsafe synchronized variant's name.
- dict-count is the name of the safe & unsafe variant's operation, while dict-count/synch is the safe synchronized name and dict-count/%synch the unsafe synchronized name.
make-dict
[procedure] (make-dict [EQUALITY [ESTIMATE]]) -> dictReturns 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]) -> dictReturns a dictionary constructed from ALIST using the supplied EQUALITY test, and optional ESTIMATE.
- EQUALITY
- procedure ; eq?
- ESTIMATE
- fixnum ; 0
dict?
[procedure] (dict? OBJ) -> booleanIs 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) -> fixnumReturns the number of items in the DICT.
dict-keys
[procedure] (dict-keys DICT) -> listReturns the keys in the DICT.
dict-values
[procedure] (dict-values DICT) -> listReturns the values in the DICT.
dict->alist
[procedure] (dict->alist DICT) -> listReturns 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) -> booleanDoes 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 ...) -> dictReturns the DICT as the (union DICT DICT1 ...) using overwrite semantics.
Tables must have the same equality predicate!
dict-search
[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
- Safe variant
(require-extension lookup-table)
- Synchronized safe variant
(require-extension lookup-table-synch)
- Unsafe variant
(require-extension lookup-table-unsafe)
- Synchronized unsafe variant
(require-extension lookup-table-unsafe-synch)
Requirements
miscmacros check-errors record-variants synch dsssl-utils
Author
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.