Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/tcl8.5.2/doc/DictObj.3 @ 25

Last change on this file since 25 was 25, checked in by landauf, 16 years ago

added tcl to libs

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