4.15 Dictionaries
A dictionary is an instance of a datatype that maps keys to values. The following datatypes are all dictionaries:
vectors (using only exact integers as keys);
lists of pairs (an association list using equal? to compare keys); and
structures whose types implement the gen:dict generic interface.
(require racket/dict) | package: base |
4.15.1 Dictionary Predicates and Contracts
Beware that dict? is not a constant-time test on pairs, since checking that v is an association list may require traversing the list.
Examples: | ||||||||
|
procedure
(dict-implements? d sym ...) → boolean?
d : dict? sym : symbol?
Examples: | ||||||||||||
|
procedure
(dict-implements/c sym ...) → flat-contract?
sym : symbol?
procedure
(dict-mutable? d) → boolean?
d : dict?
Equivalent to (dict-implements? d 'dict-set!).
Examples: | ||||||||||
|
procedure
d : dict?
Equivalent to (or (dict-implements? d 'dict-remove!) (dict-implements? d 'dict-remove)).
Examples: | ||||||
|
procedure
d : dict?
Equivalent to (dict-implements? d 'dict-set).
Examples: | ||||||||
|
4.15.2 Generic Dictionary Interface
syntax
Examples: | ||||||||||||||||||||||||||
|
value
dict-set!, or #f if unsupported
dict-set, or #f if unsupported
dict-remove!, or #f if unsupported
dict-remove, or #f if unsupported
4.15.2.1 Primitive Dictionary Methods
These methods of gen:dict have no fallback implementations; they are only supported for dictionary types that directly implement them.
procedure
dict : dict? key : any/c
failure-result : any/c = (lambda () (raise (make-exn:fail ....)))
If failure-result is a procedure, it is called (through a tail call) with no arguments to produce the result.
Otherwise, failure-result is returned as the result.
Examples: | |||||||||||||||||||||||||
|
Examples: | ||||||||||||||||
|
procedure
(dict-set dict key v) → (and/c dict? immutable?)
dict : (and/c dict? immutable?) key : any/c v : any/c
Examples: | ||||||||
|
procedure
(dict-remove! dict key) → void?
dict : (and/c dict? (not/c immutable?)) key : any/c
Examples: | |||||||||||||
|
procedure
(dict-remove dict key) → (and/c dict? immutable?)
dict : (and/c dict? immutable?) key : any/c
Examples: | ||||||||||||||||
|
procedure
(dict-iterate-first dict) → any/c
dict : dict?
Examples: | ||||||||
|
procedure
(dict-iterate-next dict pos) → any/c
dict : dict? pos : any/c
Examples: | ||||||||||||
|
procedure
(dict-iterate-key dict pos) → any
dict : dict? pos : any/c
Examples: | ||||||||||
|
procedure
(dict-iterate-value dict pos) → any
dict : dict? pos : any/c
Examples: | ||||||||||
|
4.15.2.2 Derived Dictionary Methods
These methods of gen:dict have fallback implementations in terms of the other methods; they may be supported even by dictionary types that do not directly implement them.
procedure
(dict-has-key? dict key) → boolean?
dict : dict? key : any/c
Supported for any dict that implements dict-ref.
Examples: | ||||||||||||
|
procedure
(dict-set*! dict key v ... ...) → void?
dict : (and/c dict? (not/c immutable?)) key : any/c v : any/c
Supported for any dict that implements dict-set!.
Examples: | ||||||||||||||||||||||||
|
procedure
(dict-set* dict key v ... ...) → (and/c dict? immutable?)
dict : (and/c dict? immutable?) key : any/c v : any/c
Supported for any dict that implements dict-set.
Examples: | ||||||||||
|
Supported for any dict that implements dict-ref and dict-set!.
Examples: | |||||||||||||||||||||
|
procedure
(dict-update! dict key updater [ failure-result]) → void? dict : (and/c dict? (not/c immutable?)) key : any/c updater : (any/c . -> . any/c)
failure-result : any/c = (lambda () (raise (make-exn:fail ....)))
Supported for any dict that implements dict-ref and dict-set!.
Examples: | ||||||||||||||||||
|
procedure
(dict-update dict key updater [failure-result])
→ (and/c dict? immutable?) dict : dict? key : any/c updater : (any/c . -> . any/c)
failure-result : any/c = (lambda () (raise (make-exn:fail ....)))
Supported for any dict that implements dict-ref and dict-set.
Examples: | ||||||
|
Supported for any dict that implements dict-iterate-first, dict-iterate-next, dict-iterate-key, and dict-iterate-value.
Example: | ||
|
Supported for any dict that implements dict-iterate-first, dict-iterate-next, dict-iterate-key, and dict-iterate-value.
Example: | |||||||||
|
procedure
(dict-empty? dict) → boolean?
dict : dict?
Supported for any dict that implements dict-iterate-first.
Examples: | ||||
|
procedure
(dict-count dict) → exact-nonnegative-integer?
dict : dict?
Supported for any dict that implements dict-iterate-first and dict-iterate-next.
Examples: | ||||
|
Supported for any dict that implements dict-clear, dict-set!, dict-iterate-first, dict-iterate-next, dict-iterate-key, and dict-iterate-value.
Examples: | |||||||||||||||||
|
procedure
(dict-clear dict) → dict?
dict : dict?
Supported for any dict that supports dict-remove, dict-iterate-first, dict-iterate-next, and dict-iterate-key.
Examples: | ||||
|
procedure
(dict-clear! dict) → dict?
dict : dict?
Supported for any dict that supports dict-remove!, dict-iterate-first, and dict-iterate-key.
Examples: | ||||||||||||||||
|
Supported for any dict that implements dict-iterate-first, dict-iterate-next, and dict-iterate-key.
Examples: | |||||
|
procedure
(dict-values dict) → list?
dict : dict?
Supported for any dict that implements dict-iterate-first, dict-iterate-next, and dict-iterate-value.
Examples: | |||||
|
procedure
(dict->list dict) → list?
dict : dict?
Supported for any dict that implements dict-iterate-first, dict-iterate-next, dict-iterate-key, and dict-iterate-value.
Examples: | |||||
|
4.15.3 Dictionary Sequences
Supported for any dict that implements dict-iterate-first, dict-iterate-next, dict-iterate-key, and dict-iterate-value.
Examples: | |||||||
|
procedure
(in-dict-keys dict) → sequence?
dict : dict?
Supported for any dict that implements dict-iterate-first, dict-iterate-next, and dict-iterate-key.
Examples: | |||||||
|
procedure
(in-dict-values dict) → sequence?
dict : dict?
Supported for any dict that implements dict-iterate-first, dict-iterate-next, and dict-iterate-value.
Examples: | |||||||
|
procedure
(in-dict-pairs dict) → sequence?
dict : dict?
Supported for any dict that implements dict-iterate-first, dict-iterate-next, dict-iterate-key, and dict-iterate-value.
Examples: | |||||||
|
4.15.4 Contracted Dictionaries
(list dict-vector (vector type-key-contract type-value-contract type-iter-contract instance-key-contract instance-value-contract instance-iter-contract))
The first vector must be a vector of 10 procedures which match the gen:dict generic interface (in addition, it must be an immutable vector). The second vector must contain six elements; each of the first three is a contract for the dictionary type’s keys, values, and positions, respectively. Each of the second three is either #f or a procedure used to extract the contract from a dictionary instance.
procedure
(dict-key-contract d) → contract?
d : dict?
procedure
(dict-value-contract d) → contract?
d : dict?
procedure
(dict-iter-contract d) → contract?
d : dict?
4.15.5 Custom Hash Tables
syntax
(define-custom-hash-types name optional-predicate comparison-expr optional-hash-functions)
optional-predicate =
| #:key? predicate-expr optional-hash-functions =
| hash1-expr | hash1-expr hash2-expr
Defines seven names:
name? recognizes instances of the new type,
immutable-name? recognizes immutable instances of the new type,
mutable-name? recognizes mutable instances of the new type with strongly-held keys,
weak-name? recognizes mutable instances of the new type with weakly-held keys,
make-immutable-name constructs immutable instances of the new type,
make-mutable-name constructs mutable instances of the new type with strongly-held keys, and
make-weak-name constructs mutable instances of the new type with weakly-held keys.
The constructors all accept a dictionary as an optional argument, providing initial key/value pairs.
Examples: | ||||||||||||||||||||||||||||||||||||||||||||||
|
procedure
(make-custom-hash-types eql? [ hash1 hash2 #:key? key? #:name name #:for who]) →
(any/c . -> . boolean?) (any/c . -> . boolean?) (any/c . -> . boolean?) (any/c . -> . boolean?) (->* [] [dict?] dict?) (->* [] [dict?] dict?) (->* [] [dict?] dict?)
eql? :
(or/c (any/c any/c . -> . any/c) (any/c any/c (any/c any/c . -> . any/c) . -> . any/c))
hash1 :
(or/c (any/c . -> . exact-integer?) (any/c (any/c . -> . exact-integer?) . -> . exact-integer?)) = (const 1)
hash2 :
(or/c (any/c . -> . exact-integer?) (any/c (any/c . -> . exact-integer?) . -> . exact-integer?)) = (const 1) key? : (any/c . -> . boolean?) = (const #true) name : symbol? = 'custom-hash who : symbol? = 'make-custom-hash-types
The comparison function eql? may accept 2 or 3 arguments. If it accepts 2 arguments, it given two keys to compare them. If it accepts 3 arguments and does not accept 2 arguments, it is also given a recursive comparison function that handles data cycles when comparing sub-parts of the keys.
The hash functions hash1 and hash2 may accept 1 or 2 arguments. If either hash function accepts 1 argument, it is applied to a key to compute the corresponding hash value. If either hash function accepts 2 arguments and does not accept 1 argument, it is also given a recursive hash function that handles data cycles when computing hash values of sub-parts of the keys.
The predicate key? must accept 1 argument and is used to recognize valid keys for the new dictionary type.
Produces seven values:
a predicate recognizing all instances of the new dictionary type,
a predicate recognizing immutable instances,
a predicate recognizing mutable instances,
a predicate recognizing weak instances,
a constructor for immutable instances,
a constructor for mutable instances, and
a constructor for weak instances.
See define-custom-hash-types for an example.
These procedures are deprecated; use define-custom-hash-types instead.