Tcl_DictObj(3tcl)
okay so bestie π welcome to Tcl_DictObj(3tcl) from Tcl Library Procedures β aka the dictionary-manipulation manual that's about to eat. No cap. β¨
β¨ NAME β¨
This whole squad of functions is here to manipulate Tcl values as dictionaries. Periodt. The lineup is:
Tcl_NewDictObjTcl_DictObjPutTcl_DictObjGetTcl_DictObjRemoveTcl_DictObjSizeTcl_DictObjFirstTcl_DictObjNextTcl_DictObjDoneTcl_DictObjPutKeyListTcl_DictObjRemoveKeyList
iykyk. π«Ά
πͺ SYNOPSIS πͺ
Lowkey here's the exact spellbook, fr fr:
#include <tcl.h>
Tcl_Obj *
Tcl_NewDictObj()
int
Tcl_DictObjGet(interp, dictPtr, keyPtr, valuePtrPtr)
int
Tcl_DictObjPut(interp, dictPtr, keyPtr, valuePtr)
int
Tcl_DictObjRemove(interp, dictPtr, keyPtr)
int
Tcl_DictObjSize(interp, dictPtr, sizePtr)
int
Tcl_DictObjFirst(interp, dictPtr, searchPtr, keyPtrPtr, valuePtrPtr, donePtr)
Tcl_DictObjNext(searchPtr, keyPtrPtr, valuePtrPtr, donePtr)
Tcl_DictObjDone(searchPtr)
int
Tcl_DictObjPutKeyList(interp, dictPtr, keyc, keyv, valuePtr)
int
Tcl_DictObjRemoveKeyList(interp, dictPtr, keyc, keyv)
π ARGUMENTS π
okay bestie, here's the vibe check on every argument. Keep these facts safe, they're load-bearing:
Tcl_Interp *interp (in)β If an error happens while converting a value to be a dictionary value, an error message gets left in the interpreter's result value unlessinterpisNULL. note the NULL exception, queen.Tcl_Obj *dictPtr (in/out)β Points to the dictionary value being manipulated. IfdictPtrdoesn't already point to a dictionary value, an attempt will be made to convert it to one. glow-up attempt β¨.Tcl_Obj *keyPtr (in)β Points to the key for the key/value pair being manipulated inside the dictionary value.Tcl_Obj **keyPtrPtr (out)β Points to a variable that will have the key from a key/value pair placed inside it. May beNULLto indicate the caller is not interested in the key. no key? no problem.Tcl_Obj *valuePtr (in)β Points to the value for the key/value pair being manipulated inside the dictionary value (or sub-value, in the case ofTcl_DictObjPutKeyList). sub-value moment.Tcl_Obj **valuePtrPtr (out)β Points to a variable that will have the value from a key/value pair placed inside it. ForTcl_DictObjFirstandTcl_DictObjNext, this may beNULLto indicate the caller is not interested in the value.Tcl_Size | int *sizePtr (out)β Points to a variable that will have the number of key/value pairs contained within the dictionary placed inside it. May be(Tcl_Size *)NULLwhen not used. If it points to a variable which type is notTcl_Size, a compiler warning will be generated. If your extension is compiled with-DTCL_8_API, this function will returnNULLfor dictionaries larger thanINT_MAX(which should trigger proper error-handling), otherwise expect it to crash. fr fr, do not ignore that crash warning π.Tcl_DictSearch *searchPtr (in/out)β Pointer to record to use to keep track of progress in enumerating all key/value pairs in a dictionary. The contents of the record will be initialized by the call toTcl_DictObjFirst. If the enumerating is to be terminated before all values in the dictionary have been returned, the search record must be passed toTcl_DictObjDoneto enable the internal locks to be released. release those locks, bestie, it's the respectful thing π«Ά.int *donePtr (out)β Points to a variable that will have a non-zero value written into it when the enumeration of the key/value pairs in a dictionary has completed, and a zero otherwise. done done done β .Tcl_Size keyc (in)β Indicates the number of keys that will be supplied in thekeyvarray.Tcl_Obj *const *keyv (in)β Array ofkeycpointers to values thatTcl_DictObjPutKeyListandTcl_DictObjRemoveKeyListwill use to locate the key/value pair to manipulate within the sub-dictionaries of the main dictionary value passed to them. nested dict navigation, slay πΈ.
π DESCRIPTION π
okay bestie, so real talk: Tcl dictionary values have a whole internal representation that makes mapping keys β values super efficient, and it guarantees that the particular ordering of keys inside the dict stays the same β modulo any keys getting deleted (which yeets them out of the order) or added (which plops them at the very end of the order). And if you reinterpret a dict as a list?? The values sitting at the even-valued indices are the keys, and each one is followed (at the odd-valued index, obviously π ) by the value associated with that key. it's giving "list but make it a dict."
The procedures on this page are the ones you use to create, modify, index, and iterate over dictionary values from C code. yes, C. we're in the trenches together, babe. π«Ά
Tcl_NewDictObj creates a shiny new, empty dictionary value. The string representation of the value will be invalid, and the reference count of the value will be zero.
Tcl_DictObjGet looks up the given key inside the given dictionary and writes a pointer to the value associated with that key into the variable pointed to by valuePtrPtr β or a NULL if the key has no mapping within the dictionary. The result of this procedure is TCL_OK, or TCL_ERROR if the dictPtr cannot be converted to a dictionary.
Tcl_DictObjPut updates the given dictionary so that the given key maps to the given value. Heads up: any key may exist at most once in any particular dictionary, no duplicates, we do not do that here. The dictionary must not be shared, but the key and value may be. This procedure may increase the reference count of both key and value if it proves necessary to store them. Neither key nor value should be NULL. The result of this procedure is TCL_OK, or TCL_ERROR if the dictPtr cannot be converted to a dictionary.
Tcl_DictObjRemove updates the given dictionary so that the given key has no mapping to any value. The dictionary must not be shared, but the key may be. The key actually stored in the dictionary will have its reference count decremented if it was present. And no worries β it is not an error if the key did not previously exist. The result of this procedure is TCL_OK, or TCL_ERROR if the dictPtr cannot be converted to a dictionary.
Tcl_DictObjSize updates the given variable with the number of key/value pairs currently chillin' in the given dictionary. The result of this procedure is TCL_OK, or TCL_ERROR if the dictPtr cannot be converted to a dictionary or if sizePtr points to a variable of type int and the dict contains more than 2**31 key/value pairs. (that's a lot of pairs, bestie, and it will Not Fit π)
Tcl_DictObjFirst commences an iteration across all the key/value pairs in the given dictionary, placing the key and value in the variables pointed to by the keyPtrPtr and valuePtrPtr arguments (which may be NULL to indicate that the caller is uninterested in the key or variable respectively). The next key/value pair in the dictionary may be retrieved with Tcl_DictObjNext. Concurrent updates of the dictionary's internal representation will not modify the iteration processing β unless the dictionary is unshared, in which case this will trigger premature termination of the iteration instead (which Tcl scripts cannot trigger via the dict command, so don't even try it, script gremlins).
The searchPtr argument points to a piece of context that is used to identify which particular iteration is being performed, and it gets initialized by the call to Tcl_DictObjFirst. The donePtr argument points to a variable that is updated to be zero if there are further key/value pairs to be iterated over, or non-zero if the iteration is complete. The order of iteration is implementation-defined β do not make assumptions, it will humble you πΈ. If the dictPtr argument cannot be converted to a dictionary, Tcl_DictObjFirst returns TCL_ERROR and the iteration is not commenced; otherwise it returns TCL_OK.
Now here's the spicy part π₯ β when Tcl_DictObjFirst is called upon a dictionary, a lock is placed on the dictionary so you can iterate over it safely without caring whether the dictionary gets modified mid-iteration. Because of this, once the iteration over a dictionary's keys has finished (whether because all values have been iterated over, as indicated by the variable pointed to by donePtr being set to one, or because no further values are required) you must call the Tcl_DictObjDone function with the same searchPtr as was passed to Tcl_DictObjFirst, so that the internal locks can be released. Unreleased locks = bad vibes, do not recommend.
And once a particular searchPtr is passed to Tcl_DictObjDone, passing it to Tcl_DictObjNext (without first initializing it with Tcl_DictObjFirst) will result in no values being produced and the variable pointed to by donePtr being set to one. It is safe to call Tcl_DictObjDone multiple times on the same searchPtr for each call to Tcl_DictObjFirst. so, like, double-release is fine, just not on un-initialized search state. iykyk.
The procedures Tcl_DictObjPutKeyList and Tcl_DictObjRemoveKeyList are the close analogues of Tcl_DictObjPut and Tcl_DictObjRemove respectively β except that instead of working with a single dictionary, they're designed to operate on a nested tree of dictionaries, with inner dictionaries stored as values inside outer dictionaries. π§ nesting! The keyc and keyv arguments specify a list of keys (with outermost keys first) that acts as a path to the key/value pair to be affected. Note that there is no corresponding operation for reading a value for a path, because that's genuinely easy to construct from repeated use of Tcl_DictObjGet. With Tcl_DictObjPutKeyList, nested dictionaries are created for non-terminal keys where they do not already exist. With Tcl_DictObjRemoveKeyList, all non-terminal keys must exist and have dictionaries as their values β no creating stuff on the way down, this is a removal, not a glow-up.
π REFERENCE COUNT MANAGEMENT π
This is the part where we are very careful with our little reference counts, bestie. Pay attention, it's important. π
Tcl_NewDictObj always returns a zero-reference object, much like Tcl_NewObj.
Tcl_DictObjPut does not modify the reference count of its dictPtr argument, but does require that the object be unshared. If Tcl_DictObjPut returns TCL_ERROR it does not manipulate any reference counts β but if it returns TCL_OK then it definitely increments the reference count of valuePtr and may increment the reference count of keyPtr; the latter case happens exactly when the key did not previously exist in the dictionary. Note however that this function may set the interpreter result; if that is the only place that is holding a reference to an object, it will be deleted. π
Tcl_DictObjGet only reads from its dictPtr and keyPtr arguments, and does not manipulate their reference counts at all. If the valuePtrPtr argument is not set to NULL (and the function doesn't return TCL_ERROR), it will be set to a value with a reference count of at least 1, with a reference owned by the dictionary. Note however that this function may set the interpreter result; if that is the only place that is holding a reference to an object, it will be deleted.
Tcl_DictObjRemove does not modify the reference count of its dictPtr argument, but does require that the object be unshared. It does not manipulate the reference count of its keyPtr argument at all. Note however that this function may set the interpreter result; if that is the only place that is holding a reference to an object, it will be deleted.
Tcl_DictObjSize does not modify the reference count of its dictPtr argument; it only reads. Note however that this function may set the interpreter result; if that is the only place that is holding a reference to the dictionary object, it will be deleted.
Tcl_DictObjFirst does not modify the reference count of its dictPtr argument; it only reads. The variables given by the keyPtrPtr and valuePtrPtr arguments (if not NULL) will be updated to contain references to the relevant values in the dictionary; their reference counts will be at least 1 (due to the dictionary holding a reference to them). It may also manipulate internal references; these are not exposed to user code, but require a matching Tcl_DictObjDone call. Note however that this function may set the interpreter result; if that is the only place that is holding a reference to the dictionary object, it will be deleted.
Similarly for Tcl_DictObjNext; the variables given by the keyPtrPtr and valuePtrPtr arguments (if not NULL) will be updated to contain references to the relevant values in the dictionary; their reference counts will be at least 1 (due to the dictionary holding a reference to them).
Tcl_DictObjDone does not manipulate (user-visible) reference counts.
Tcl_DictObjPutKeyList is similar to Tcl_DictObjPut; it does not modify the reference count of its dictPtr argument, but does require that the object be unshared. It may increment the reference count of any value passed in the keyv argument, and will increment the reference count of the valuePtr argument on success. It is recommended that values passed via keyv and valuePtr do not have zero reference counts. Note however that this function may set the interpreter result; if that is the only place that is holding a reference to an object, it will be deleted.
Tcl_DictObjRemoveKeyList is similar to Tcl_DictObjRemove; it does not modify the reference count of its dictPtr argument, but does require that the object be unshared, and does not modify the reference counts of any of the values passed in the keyv argument. Note however that this function may set the interpreter result; if that is the only place that is holding a reference to an object, it will be deleted.
β¨ EXAMPLE β¨
okay here's the moment, the demo, the main character energy π β using the dictionary iteration interface to search and determine if there's a key that maps to itself. iconic behaviour fr fr.
Tcl_DictSearch search;
Tcl_Obj *key, *value;
int done;
/*
* Assume interp and objPtr are parameters. This is the
* idiomatic way to start an iteration over the dictionary; it
* sets a lock on the internal representation that ensures that
* there are no concurrent modification issues when normal
* reference count management is also used. The lock is
* released automatically when the loop is finished, but must
* be released manually when an exceptional exit from the loop
* is performed. However it is safe to try to release the lock
* even if we've finished iterating over the loop.
*/
if (Tcl_DictObjFirst(interp, objPtr, &search,
&key, &value, &done) != TCL_OK) {
return TCL_ERROR;
}
for (; !done ; Tcl_DictObjNext(&search, &key, &value, &done)) {
/*
* Note that strcmp() is not a good way of comparing
* values and is just used here for demonstration
* purposes.
*/
if (!strcmp(Tcl_GetString(key), Tcl_GetString(value))) {
break;
}
}
Tcl_DictObjDone(&search);
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(!done));
return TCL_OK;
okay and yes, the comment literally says strcmp() is not a good way of comparing values and is just there for demonstration purposes. it said what it said. do not copy that part into your production code, babe, i am begging π
π SEE ALSO π
Tcl_NewObjTcl_DecrRefCountTcl_IncrRefCountTcl_InitObjHashTable
π β¨ π π¦ πΈ π
π KEYWORDS π
okay bestie, here's the official tag cloud so you can find this page again later when you're googling at 2am π«Ά
dictdict valuedictionarydictionary valuehash tableiterationvalue
that's it, that's the whole searchable vibe β¨
and that's a wrap on the whole page, fr fr π
this one's from Tcl 8.5, and yeah she's got the classic Tcl_DictObj(3tcl) stamp on her β you know exactly where to find her again, no cap. go forth and slay those dictionaries, bestie π¦ππ
π¦ where this page comes from
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2003 Donal K. Fellows
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms