| 1 |
'\" |
| 2 |
'\" Copyright (c) 2003 Donal K. Fellows |
| 3 |
'\" |
| 4 |
'\" See the file "license.terms" for information on usage and redistribution |
| 5 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 6 |
'\" |
| 7 |
.TH Tcl_DictObj 3 8.5 Tcl "Tcl Library Procedures" |
| 8 |
.so man.macros |
| 9 |
.BS |
| 10 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 11 |
.SH NAME |
| 12 |
Tcl_NewDictObj, Tcl_DictObjPut, Tcl_DictObjGet, Tcl_DictObjRemove, Tcl_DictObjSize, Tcl_DictObjFirst, Tcl_DictObjNext, Tcl_DictObjDone, Tcl_DictObjPutKeyList, Tcl_DictObjRemoveKeyList \- manipulate Tcl values as dictionaries |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB#include <tcl.h>\fR |
| 16 |
.sp |
| 17 |
Tcl_Obj * |
| 18 |
\fBTcl_NewDictObj\fR() |
| 19 |
.sp |
| 20 |
int |
| 21 |
\fBTcl_DictObjGet\fR(\fIinterp, dictPtr, keyPtr, valuePtrPtr\fR) |
| 22 |
.sp |
| 23 |
int |
| 24 |
\fBTcl_DictObjPut\fR(\fIinterp, dictPtr, keyPtr, valuePtr\fR) |
| 25 |
.sp |
| 26 |
int |
| 27 |
\fBTcl_DictObjRemove\fR(\fIinterp, dictPtr, keyPtr\fR) |
| 28 |
.sp |
| 29 |
int |
| 30 |
\fBTcl_DictObjSize\fR(\fIinterp, dictPtr, sizePtr\fR) |
| 31 |
.sp |
| 32 |
int |
| 33 |
\fBTcl_DictObjFirst\fR(\fIinterp, dictPtr, searchPtr, |
| 34 |
keyPtrPtr, valuePtrPtr, donePtr\fR) |
| 35 |
.sp |
| 36 |
void |
| 37 |
\fBTcl_DictObjNext\fR(\fIsearchPtr, keyPtrPtr, valuePtrPtr, donePtr\fR) |
| 38 |
.sp |
| 39 |
void |
| 40 |
\fBTcl_DictObjDone\fR(\fIsearchPtr\fR) |
| 41 |
.sp |
| 42 |
int |
| 43 |
\fBTcl_DictObjPutKeyList\fR(\fIinterp, dictPtr, keyc, keyv, valuePtr\fR) |
| 44 |
.sp |
| 45 |
int |
| 46 |
\fBTcl_DictObjRemoveKeyList\fR(\fIinterp, dictPtr, keyc, keyv\fR) |
| 47 |
.SH ARGUMENTS |
| 48 |
.AS Tcl_DictSearch "**valuePtrPtr" in/out |
| 49 |
.AP Tcl_Interp *interp in |
| 50 |
If an error occurs while converting a value to be a dictionary value, |
| 51 |
an error message is left in the interpreter's result value |
| 52 |
unless \fIinterp\fR is NULL. |
| 53 |
.AP Tcl_Obj *dictPtr in/out |
| 54 |
Points to the dictionary value to be manipulated. |
| 55 |
If \fIdictPtr\fR does not already point to a dictionary value, |
| 56 |
an attempt will be made to convert it to one. |
| 57 |
.AP Tcl_Obj *keyPtr in |
| 58 |
Points to the key for the key/value pair being manipulated within the |
| 59 |
dictionary value. |
| 60 |
.AP Tcl_Obj **keyPtrPtr out |
| 61 |
Points to a variable that will have the key from a key/value pair |
| 62 |
placed within it. May be NULL to indicate that the caller is not |
| 63 |
interested in the key. |
| 64 |
.AP Tcl_Obj *valuePtr in |
| 65 |
Points to the value for the key/value pair being manipulated within the |
| 66 |
dictionary value (or sub-value, in the case of |
| 67 |
\fBTcl_DictObjPutKeyList\fR.) |
| 68 |
.AP Tcl_Obj **valuePtrPtr out |
| 69 |
Points to a variable that will have the value from a key/value pair |
| 70 |
placed within it. For \fBTcl_DictObjFirst\fR and |
| 71 |
\fBTcl_DictObjNext\fR, this may be NULL to indicate that the caller is |
| 72 |
not interested in the value. |
| 73 |
.AP int *sizePtr out |
| 74 |
Points to a variable that will have the number of key/value pairs |
| 75 |
contained within the dictionary placed within it. |
| 76 |
.AP Tcl_DictSearch *searchPtr in/out |
| 77 |
Pointer to record to use to keep track of progress in enumerating all |
| 78 |
key/value pairs in a dictionary. The contents of the record will be |
| 79 |
initialized by the call to \fBTcl_DictObjFirst\fR. If the enumerating |
| 80 |
is to be terminated before all values in the dictionary have been |
| 81 |
returned, the search record \fImust\fR be passed to |
| 82 |
\fBTcl_DictObjDone\fR to enable the internal locks to be released. |
| 83 |
.AP int *donePtr out |
| 84 |
Points to a variable that will have a non-zero value written into it |
| 85 |
when the enumeration of the key/value pairs in a dictionary has |
| 86 |
completed, and a zero otherwise. |
| 87 |
.AP int keyc in |
| 88 |
Indicates the number of keys that will be supplied in the \fIkeyv\fR |
| 89 |
array. |
| 90 |
.AP "Tcl_Obj *const" *keyv in |
| 91 |
Array of \fIkeyc\fR pointers to values that |
| 92 |
\fBTcl_DictObjPutKeyList\fR and \fBTcl_DictObjRemoveKeyList\fR will |
| 93 |
use to locate the key/value pair to manipulate within the |
| 94 |
sub-dictionaries of the main dictionary value passed to them. |
| 95 |
.BE |
| 96 |
|
| 97 |
.SH DESCRIPTION |
| 98 |
.PP |
| 99 |
Tcl dictionary values have an internal representation that supports |
| 100 |
efficient mapping from keys to values and which guarantees that the |
| 101 |
particular ordering of keys within the dictionary remains the same |
| 102 |
modulo any keys being deleted (which removes them from the order) or |
| 103 |
added (which adds them to the end of the order). If reinterpreted as a |
| 104 |
list, the values at the even-valued indices in the list will be the |
| 105 |
keys of the dictionary, and each will be followed (in the odd-valued |
| 106 |
index) by the value associated with that key. |
| 107 |
.PP |
| 108 |
The procedures described in this man page are used to |
| 109 |
create, modify, index, and iterate over dictionary values from C code. |
| 110 |
.PP |
| 111 |
\fBTcl_NewDictObj\fR creates a new, empty dictionary value. The |
| 112 |
string representation of the value will be invalid, and the reference |
| 113 |
count of the value will be zero. |
| 114 |
.PP |
| 115 |
\fBTcl_DictObjGet\fR looks up the given key within the given |
| 116 |
dictionary and writes a pointer to the value associated with that key |
| 117 |
into the variable pointed to by \fIvaluePtrPtr\fR, or a NULL if the |
| 118 |
key has no mapping within the dictionary. The result of this |
| 119 |
procedure is \fBTCL_OK\fR, or \fBTCL_ERROR\fR if the \fIdictPtr\fR cannot be |
| 120 |
converted to a dictionary. |
| 121 |
.PP |
| 122 |
\fBTcl_DictObjPut\fR updates the given dictionary so that the given |
| 123 |
key maps to the given value; any key may exist at most once in any |
| 124 |
particular dictionary. The dictionary must not be shared, but the key |
| 125 |
and value may be. This procedure may increase the reference count of |
| 126 |
both key and value if it proves necessary to store them. Neither key |
| 127 |
nor value should be NULL. The result of this procedure is \fBTCL_OK\fR, or |
| 128 |
\fBTCL_ERROR\fR if the \fIdictPtr\fR cannot be converted to a dictionary. |
| 129 |
.PP |
| 130 |
\fBTcl_DictObjRemove\fR updates the given dictionary so that the given |
| 131 |
key has no mapping to any value. The dictionary must not be shared, |
| 132 |
but the key may be. The key actually stored in the dictionary will |
| 133 |
have its reference count decremented if it was present. It is not an |
| 134 |
error if the key did not previously exist. The result of this |
| 135 |
procedure is \fBTCL_OK\fR, or \fBTCL_ERROR\fR if the \fIdictPtr\fR cannot be |
| 136 |
converted to a dictionary. |
| 137 |
.PP |
| 138 |
\fBTcl_DictObjSize\fR updates the given variable with the number of |
| 139 |
key/value pairs currently in the given dictionary. The result of this |
| 140 |
procedure is \fBTCL_OK\fR, or \fBTCL_ERROR\fR if the \fIdictPtr\fR cannot be |
| 141 |
converted to a dictionary. |
| 142 |
.PP |
| 143 |
\fBTcl_DictObjFirst\fR commences an iteration across all the key/value |
| 144 |
pairs in the given dictionary, placing the key and value in the |
| 145 |
variables pointed to by the \fIkeyPtrPtr\fR and \fIvaluePtrPtr\fR |
| 146 |
arguments (which may be NULL to indicate that the caller is |
| 147 |
uninterested in they key or variable respectively.) The next |
| 148 |
key/value pair in the dictionary may be retrieved with |
| 149 |
\fBTcl_DictObjNext\fR. Concurrent updates of the dictionary's |
| 150 |
internal representation will not modify the iteration processing |
| 151 |
unless the dictionary is unshared, when this will trigger premature |
| 152 |
termination of the iteration instead (which Tcl scripts cannot trigger |
| 153 |
via the \fBdict\fR command.) The \fIsearchPtr\fR argument points to a |
| 154 |
piece of context that is used to identify which particular iteration |
| 155 |
is being performed, and is initialized by the call to |
| 156 |
\fBTcl_DictObjFirst\fR. The \fIdonePtr\fR argument points to a |
| 157 |
variable that is updated to be zero of there are further key/value |
| 158 |
pairs to be iterated over, or non-zero if the iteration is complete. |
| 159 |
The order of iteration is implementation-defined. If the |
| 160 |
\fIdictPtr\fR argument cannot be converted to a dictionary, |
| 161 |
\fBTcl_DictObjFirst\fR returns \fBTCL_ERROR\fR and the iteration is not |
| 162 |
commenced, and otherwise it returns \fBTCL_OK\fR. |
| 163 |
.PP |
| 164 |
When \fBTcl_DictObjFirst\fR is called upon a dictionary, a lock is placed on |
| 165 |
the dictionary to enable that dictionary to be iterated over safely without |
| 166 |
regard for whether the dictionary is modified during the iteration. Because of |
| 167 |
this, once the iteration over a dictionary's keys has finished (whether |
| 168 |
because all values have been iterated over as indicated by the variable |
| 169 |
indicated by the \fIdonePtr\fR argument being set to one, or because no |
| 170 |
further values are required) the \fBTcl_DictObjDone\fR function must be called |
| 171 |
with the same \fIsearchPtr\fR as was passed to \fBTcl_DictObjFirst\fR so that |
| 172 |
the internal locks can be released. Once a particular \fIsearchPtr\fR is |
| 173 |
passed to \fBTcl_DictObjDone\fR, passing it to \fBTcl_DictObjNext\fR (without |
| 174 |
first initializing it with \fBTcl_DictObjFirst\fR) will result in no values |
| 175 |
being produced and the variable pointed to by \fIdonePtr\fR being set to one. |
| 176 |
It is safe to call \fBTcl_DictObjDone\fR multiple times on the same |
| 177 |
\fIsearchPtr\fR for each call to \fBTcl_DictObjFirst\fR. |
| 178 |
.PP |
| 179 |
The procedures \fBTcl_DictObjPutKeyList\fR and |
| 180 |
\fBTcl_DictObjRemoveKeyList\fR are the close analogues of |
| 181 |
\fBTcl_DictObjPut\fR and \fBTcl_DictObjRemove\fR respectively, except |
| 182 |
that instead of working with a single dictionary, they are designed to |
| 183 |
operate on a nested tree of dictionaries, with inner dictionaries |
| 184 |
stored as values inside outer dictionaries. The \fIkeyc\fR and |
| 185 |
\fIkeyv\fR arguments specify a list of keys (with outermost keys |
| 186 |
first) that acts as a path to the key/value pair to be affected. Note |
| 187 |
that there is no corresponding operation for reading a value for a |
| 188 |
path as this is easy to construct from repeated use of |
| 189 |
\fBTcl_DictObjGet\fR. With \fBTcl_DictObjPutKeyList\fR, nested |
| 190 |
dictionaries are created for non-terminal keys where they do not |
| 191 |
already exist. With \fBTcl_DictObjRemoveKeyList\fR, all non-terminal |
| 192 |
keys must exist and have dictionaries as their values. |
| 193 |
.SH EXAMPLE |
| 194 |
Using the dictionary iteration interface to search determine if there |
| 195 |
is a key that maps to itself: |
| 196 |
.PP |
| 197 |
.CS |
| 198 |
Tcl_DictSearch search; |
| 199 |
Tcl_Obj *key, *value; |
| 200 |
int done; |
| 201 |
|
| 202 |
/* |
| 203 |
* Assume interp and objPtr are parameters. This is the |
| 204 |
* idiomatic way to start an iteration over the dictionary; it |
| 205 |
* sets a lock on the internal representation that ensures that |
| 206 |
* there are no concurrent modification issues when normal |
| 207 |
* reference count management is also used. The lock is |
| 208 |
* released automatically when the loop is finished, but must |
| 209 |
* be released manually when an exceptional exit from the loop |
| 210 |
* is performed. However it is safe to try to release the lock |
| 211 |
* even if we've finished iterating over the loop. |
| 212 |
*/ |
| 213 |
if (\fBTcl_DictObjFirst\fR(interp, objPtr, &search, |
| 214 |
&key, &value, &done) != TCL_OK) { |
| 215 |
return TCL_ERROR; |
| 216 |
} |
| 217 |
for (; !done ; \fBTcl_DictObjNext\fR(&search, &key, &value, &done)) { |
| 218 |
/* |
| 219 |
* Note that strcmp() is not a good way of comparing |
| 220 |
* values and is just used here for demonstration |
| 221 |
* purposes. |
| 222 |
*/ |
| 223 |
if (!strcmp(Tcl_GetString(key), Tcl_GetString(value))) { |
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
\fBTcl_DictObjDone\fR(&search); |
| 228 |
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(!done)); |
| 229 |
return TCL_OK; |
| 230 |
.CE |
| 231 |
.SH "SEE ALSO" |
| 232 |
Tcl_NewObj, Tcl_DecrRefCount, Tcl_IncrRefCount, Tcl_InitObjHashTable |
| 233 |
.SH KEYWORDS |
| 234 |
dict, dict value, dictionary, dictionary value, hash table, iteration, value |