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
- Perfroms 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
- (make-dict [EQUALITY eq? [ESTIMATE <small-number>]]) -> dict procedure
Returns a dictionary using the supplied EQUALITY test, optimized for the number of elements ESTIMATE.
alist->dict
- (alist->dict ALIST [EQUALITY eq?] [ESTIMATE 0]) -> dict procedure
Returns a dictionary constructed from ALIST using the supplied EQUALITY test, and optional ESTIMATE.
dict?
- (dict? OBJECT) -> boolean procedure
Is the OBJECT a dictionary?
dict-equivalence-function
- (dict-equivalence-function DICT) -> (procedure (object object) boolean) procedure
Returns the equality test predicate procedure for DICT.
dict-count
- (dict-count DICT) -> integer procedure
Returns the number of items in the DICT.
dict-keys
- (dict-keys DICT) -> list procedure
Returns the keys in the DICT.
dict-values
- (dict-values DICT) -> list procedure
Returns the values in the DICT.
dict->alist
- (dict->alist DICT) => list procedure
Returns the DICT as an association list. The result may not be mutated!
dict-ref
- (dict-ref DICT KEY [DEFAULT #f]) => * procedure
Returns the value associated with KEY in the DICT, otherwise DEFAULT.
dict-Idempotent-ref!
- (dict-Idempotent-ref! DICT KEY FUNC [DEFAULT #f]) => * procedure
Should a value for KEY exist in DICT it is returned. Otherwise FUNC is invoked on DEFAULT. Any result other than DEFAULT is the value for the KEY and that value is returned. Otherwise returns DEFAULT.
dict-exists?
- (dict-exists? DICT KEY) => boolean procedure
Does an entry with KEY exist in the DICT ?
dict-set!
- (dict-set! DICT KEY VALUE) procedure
Associate VALUE with KEY in the DICT.
VALUE must not be (void)!
dict-update!
- (dict-update! DICT KEY DEFAULT-VALUE-PROCEDURE [FUNC identity]) procedure
Invokes FUNC on either the existing value for KEY in the DICT, or the result of the DEFAULT-VALUE-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.
DEFAULT-VALUE-PROCEDURE must not return (void)!
dict-update-list!
- (dict-update-list! DICT KEY [OBJECT]...) procedure
Updates the value for KEY in the DICT with a list of OBJECT.
dict-update-dict!
- (dict-update-dict! DICT KEY [EQUALITY eq? [ESTIMATE <small-number>]]) procedure
Updates the value for KEY in the DICT with a dict.
EQUALITY and ESTIMATE as for make-dict.
dict-delete!
- (dict-delete! DICT KEY) procedure
Removes any association of KEY in the DICT.
dict-for-each
- (dict-for-each DICT (PROCEDURE (-> KEY VALUE <ignored>))) procedure
Invokes the supplied PROCEDURE with each association in the DICT.
dict-merge!
- (dict-merge! DICT [DICT1...]) => dict procedure
Returns the DICT as the (union DICT DICT1 ...) using overwrite semantics.
Tables must have the same equality predicate.
dict-search
- (dict-search DICT PROCEDURE [DEFAULT #f]) => * procedure
Returns the first entry value matched by the PROCEDURE. Otherwise the DEFAULT value is returned.)
PROCEDURE is a (procedure (object object) boolean) where the first argument is the key and the second is the value.
dict-print
- (dict-print DICT [PORT (current-output-port)]) procedure
Pretty-print DICT to 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
Author
Version history
- 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-2010 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.