Debian Codegraph @

generic/bltArrayObj.c

raw symbols ◀ ▶ SCIP
2 Debian patches touch this file
1
2 /*
3 * bltArrayObj.c --
4 *
5 * Copyright 2000 Silicon Metrics, Inc.
6 *
7 * Permission to use, copy, modify, and distribute this software and
8 * its documentation for any purpose and without fee is hereby
9 * granted, provided that the above copyright notice appear in all
10 * copies and that both that the copyright notice and warranty
11 * disclaimer appear in supporting documentation, and that the names
12 * of Lucent Technologies or any of their entities not be used in
13 * advertising or publicity pertaining to distribution of the software
14 * without specific, written prior permission.
15 *
16 * Lucent Technologies disclaims all warranties with regard to this
17 * software, including all implied warranties of merchantability and
18 * fitness. In no event shall Lucent Technologies be liable for any
19 * special, indirect or consequential damages or any damages
20 * whatsoever resulting from loss of use, data or profits, whether in
21 * an action of contract, negligence or other tortuous action, arising
22 * out of or in connection with the use or performance of this
23 * software.
24 *
25 * The array Tcl object was created by George A. Howlett.
26 */
27
28 #include "bltInt.h"
29
30 #ifndef NO_ARRAY
31 #include "bltHash.h"
32
33 static Tcl_DupInternalRepProc DupArrayInternalRep;
34 static Tcl_FreeInternalRepProc FreeArrayInternalRep;
35 static Tcl_UpdateStringProc UpdateStringOfArray;
36 static Tcl_SetFromAnyProc SetArrayFromAny;
37
38 static Tcl_ObjType arrayObjType = {
39 "array",
40 FreeArrayInternalRep, /* Called when an object is freed. */
41 DupArrayInternalRep, /* Copies an internal representation
42 * from one object to another. */
43 UpdateStringOfArray, /* Creates string representation from
44 * an object's internal representation. */
45 SetArrayFromAny, /* Creates valid internal representation
46 * from an object's string representation. */
47 };
48
49 #if 1
50 static int
51 SetArrayFromAny(interp, objPtr)
52 Tcl_Interp *interp;
53 Tcl_Obj *objPtr;
54 {
55 Blt_HashEntry *hPtr;
56 Blt_HashTable *tablePtr;
57 Tcl_Obj *elemObjPtr, **vobjv;
•58 CONST Tcl_ObjType *oldTypePtr;
59 char *string;
60 int isNew;
61 int nElem;
62 register int i;
63
64 if (objPtr->typePtr == &arrayObjType) {
65 return TCL_OK;
66 }
67 if (Tcl_ListObjGetElements(interp, objPtr, &nElem, &vobjv) != TCL_OK) {
68 return TCL_ERROR;
69 }
70 if (nElem%2) {
71 if (interp != NULL) {
•72 string = Tcl_GetString(objPtr);
73 Tcl_AppendResult(interp, "odd length: ", string, 0);
74 }
75 return TCL_ERROR;
76 }
77 tablePtr = Blt_Malloc(sizeof(Blt_HashTable));
78 assert(tablePtr);
79 Blt_InitHashTable(tablePtr, BLT_STRING_KEYS);
80 for (i = 0; i < nElem; i += 2) {
81 hPtr = Blt_CreateHashEntry(tablePtr, Tcl_GetString(vobjv[i]), &isNew);
82 elemObjPtr = vobjv[i + 1];
83 Blt_SetHashValue(hPtr, elemObjPtr);
84
85 /* Make sure we increment the reference count */
86 Tcl_IncrRefCount(elemObjPtr);
87 }
88
89 oldTypePtr = objPtr->typePtr;
90 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
91 oldTypePtr->freeIntRepProc(objPtr);
92 }
93 objPtr->internalRep.otherValuePtr = (VOID *)tablePtr;
94 objPtr->typePtr = &arrayObjType;
95
96 return TCL_OK;
97 }
98 #else
99 static int
100 SetArrayFromAny(interp, objPtr)
101 Tcl_Interp *interp;
102 Tcl_Obj *objPtr;
103 {
104 Blt_HashEntry *hPtr;
105 Blt_HashTable *tablePtr;
106 Tcl_Obj *elemObjPtr;
107 Tcl_ObjType *oldTypePtr = objPtr->typePtr;
108 char **elemArr;
109 char *string;
110 int isNew;
111 int nElem;
112 register int i;
113
114 if (objPtr->typePtr == &arrayObjType) {
115 return TCL_OK;
116 }
117 /*
118 * Get the string representation. Make it up-to-date if necessary.
119 */
120 string = Tcl_GetString(objPtr);
121 if (Tcl_SplitList(interp, string, &nElem, &elemArr) != TCL_OK) {
122 return TCL_ERROR;
123 }
124 if (nElem%2) {
125 if (interp != NULL) {
126 Tcl_AppendResult(interp, "odd length: ", string, 0);
127 }
128 Blt_Free(elemArr);
129 return TCL_ERROR;
130 }
131 tablePtr = Blt_Malloc(sizeof(Blt_HashTable));
132 assert(tablePtr);
133 Blt_InitHashTable(tablePtr, BLT_STRING_KEYS);
134 for (i = 0; i < nElem; i += 2) {
135 hPtr = Blt_CreateHashEntry(tablePtr, elemArr[i], &isNew);
136 elemObjPtr = Tcl_NewStringObj(elemArr[i + 1], -1);
137 Blt_SetHashValue(hPtr, elemObjPtr);
138
139 /* Make sure we increment the reference count */
140 Tcl_IncrRefCount(elemObjPtr);
141 }
142
143 if ((oldTypePtr != NULL) && (oldTypePtr->freeIntRepProc != NULL)) {
144 oldTypePtr->freeIntRepProc(objPtr);
145 }
146 objPtr->internalRep.otherValuePtr = (VOID *)tablePtr;
147 objPtr->typePtr = &arrayObjType;
148 Blt_Free(elemArr);
149
150 return TCL_OK;
151 }
152 #endif
153
154 static void
155 DupArrayInternalRep(srcPtr, destPtr)
156 Tcl_Obj *srcPtr; /* Object with internal rep to copy. */
157 Tcl_Obj *destPtr; /* Object with internal rep to set. */
158 {
159 Blt_HashEntry *hPtr, *h2Ptr;
160 Blt_HashSearch cursor;
161 Blt_HashTable *srcTablePtr, *destTablePtr;
162 Tcl_Obj *valueObjPtr;
163 char *key;
164 int isNew;
165
166 srcTablePtr = (Blt_HashTable *)srcPtr->internalRep.otherValuePtr;
167 destTablePtr = Blt_Malloc(sizeof(Blt_HashTable));
168 assert(destTablePtr);
169 Blt_InitHashTable(destTablePtr, BLT_STRING_KEYS);
170 for (hPtr = Blt_FirstHashEntry(srcTablePtr, &cursor); hPtr != NULL;
171 hPtr = Blt_NextHashEntry(&cursor)) {
172 key = Blt_GetHashKey(srcTablePtr, hPtr);
173 h2Ptr = Blt_CreateHashEntry(destTablePtr, key, &isNew);
174 valueObjPtr = (Tcl_Obj *)Blt_GetHashValue(hPtr);
175 assert (valueObjPtr != NULL);
176 Blt_SetHashValue(h2Ptr, valueObjPtr );
177
178 /* Make sure we increment the reference count now that both
179 * array objects are using the same elements. */
180 Tcl_IncrRefCount(valueObjPtr);
181 }
182 Tcl_InvalidateStringRep(destPtr);
183 destPtr->internalRep.otherValuePtr = (VOID *)destTablePtr;
184 destPtr->typePtr = &arrayObjType;
185 }
186
187 static void
188 UpdateStringOfArray(objPtr)
189 Tcl_Obj *objPtr; /* Array object whose string rep to update. */
190 {
191 Tcl_DString dString;
192 Blt_HashTable *tablePtr;
193 Blt_HashEntry *hPtr;
194 Blt_HashSearch cursor;
195 Tcl_Obj *elemObjPtr;
196
197 tablePtr = (Blt_HashTable *)objPtr->internalRep.otherValuePtr;
198 Tcl_DStringInit(&dString);
199 for (hPtr = Blt_FirstHashEntry(tablePtr, &cursor); hPtr != NULL;
200 hPtr = Blt_NextHashEntry(&cursor)) {
201 elemObjPtr = (Tcl_Obj *)Blt_GetHashValue(hPtr);
202 Tcl_DStringAppendElement(&dString, Blt_GetHashKey(tablePtr, hPtr));
203 Tcl_DStringAppendElement(&dString, elemObjPtr == NULL?"":Tcl_GetString(elemObjPtr));
204 }
205 objPtr->bytes = Blt_Strdup(Tcl_DStringValue(&dString));
206 objPtr->length = strlen(Tcl_DStringValue(&dString));
207 Tcl_DStringFree(&dString);
208 }
209
210 static void
211 FreeArrayInternalRep(objPtr)
212 Tcl_Obj *objPtr; /* Array object to release. */
213 {
214 Blt_HashEntry *hPtr;
215 Blt_HashSearch cursor;
216 Blt_HashTable *tablePtr;
217 Tcl_Obj *elemObjPtr;
218
219 Tcl_InvalidateStringRep(objPtr);
220 tablePtr = (Blt_HashTable *)objPtr->internalRep.otherValuePtr;
221 for (hPtr = Blt_FirstHashEntry(tablePtr, &cursor); hPtr != NULL;
222 hPtr = Blt_NextHashEntry(&cursor)) {
223 elemObjPtr = (Tcl_Obj *)Blt_GetHashValue(hPtr);
224 Tcl_DecrRefCount(elemObjPtr);
225 }
226 Blt_DeleteHashTable(tablePtr);
227 Blt_Free(tablePtr);
228 }
229
230 int
231 Blt_GetArrayFromObj(interp, objPtr, tablePtrPtr)
232 Tcl_Interp *interp;
233 Tcl_Obj *objPtr;
234 Blt_HashTable **tablePtrPtr;
235 {
236 if (objPtr->typePtr == &arrayObjType) {
237 *tablePtrPtr = (Blt_HashTable *)objPtr->internalRep.otherValuePtr;
238 return TCL_OK;
239 }
240 if (SetArrayFromAny(interp, objPtr) == TCL_OK) {
241 *tablePtrPtr = (Blt_HashTable *)objPtr->internalRep.otherValuePtr;
242 return TCL_OK;
243 }
244 return TCL_ERROR;
245 }
246
247 Tcl_Obj *
248 Blt_NewArrayObj(objc, objv)
249 int objc;
250 Tcl_Obj *objv[];
251 {
252 Blt_HashEntry *hPtr;
253 Blt_HashTable *tablePtr;
254 Tcl_Obj *arrayObjPtr, *objPtr;
255 int isNew;
256 register int i;
257
258 if (objc % 2) {
259 return NULL;
260 }
261 tablePtr = Blt_Malloc(sizeof(Blt_HashTable));
262 assert(tablePtr);
263 Blt_InitHashTable(tablePtr, BLT_STRING_KEYS);
264
265 for (i = 0; i < objc; i += 2) {
266 hPtr = Blt_CreateHashEntry(tablePtr, Tcl_GetString(objv[i]), &isNew);
267 if ((i + 1) == objc) {
268 objPtr = Tcl_NewStringObj("",-1);
269 } else {
270 objPtr = objv[i+1];
271 }
272 Tcl_IncrRefCount(objPtr);
273 if (!isNew) {
274 Tcl_Obj *oldObjPtr;
275
276 oldObjPtr = Blt_GetHashValue(hPtr);
277 Tcl_DecrRefCount(oldObjPtr);
278 }
279 Blt_SetHashValue(hPtr, objPtr);
280 }
281 arrayObjPtr = Tcl_NewObj();
282 /*
283 * Reference counts for entry objects are initialized to 0. They
284 * are incremented as they are inserted into the tree via the
285 * Blt_TreeSetValue call.
286 */
287 arrayObjPtr->refCount = 0;
288 arrayObjPtr->internalRep.otherValuePtr = (VOID *)tablePtr;
289 arrayObjPtr->bytes = NULL;
290 arrayObjPtr->length = 0;
291 arrayObjPtr->typePtr = &arrayObjType;
292 return arrayObjPtr;
293 }
294
295 int
296 Blt_IsArrayObj(objPtr)
297 Tcl_Obj *objPtr;
298 {
299 return (objPtr->typePtr == &arrayObjType);
300 }
301
302 /*ARGSUSED*/
303 void
304 Blt_RegisterArrayObj(interp)
305 Tcl_Interp *interp; /* Not used. */
306 {
307 Tcl_RegisterObjType(&arrayObjType);
308 }
309 #endif /* NO_ARRAY */