Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
| Comment: | Add an abstract list type for reversing lists |
|---|---|
| Timelines: | family | ancestors | descendants | both | apn-tip636-appl |
| Files: | files | file ages | folders |
| SHA3-256: |
224c5670f04f357f956f22fc0bc5922b |
| User & Date: | apnadkarni 2025-04-19 11:41:18.743 |
Context
|
2025-04-20
| ||
| 07:49 | Add abstract list type for lrepeat check-in: bacff30db2 user: apnadkarni tags: apn-tip636-appl | |
|
2025-04-19
| ||
| 11:41 | Add an abstract list type for reversing lists check-in: 224c5670f0 user: apnadkarni tags: apn-tip636-appl | |
|
2025-04-17
| ||
| 20:53 | Fix warning: variables 'ui' and 'maxSizeDecade' used in loop condition not modified in loop body [-W... check-in: ab0977d71d user: jan.nijtmans tags: core-9-0-branch | |
Changes
Changes to generic/tclCmdIL.c.
| ︙ | ︙ | |||
3153 3154 3155 3156 3157 3158 3159 |
int
Tcl_LreverseObjCmd(
TCL_UNUSED(void *),
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument values. */
{
| < < < < < < < < | < | < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < < | < < < < < < < < < < < < < < < < | 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 |
int
Tcl_LreverseObjCmd(
TCL_UNUSED(void *),
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument values. */
{
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, "list");
return TCL_ERROR;
}
Tcl_Obj *resultObj = NULL;
if (Tcl_ListObjReverse(interp, objv[1], &resultObj) != TCL_OK) {
return TCL_ERROR;
}
Tcl_SetObjResult(interp, resultObj);
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
* Tcl_LsearchObjCmd --
|
| ︙ | ︙ |
Changes to generic/tclInt.h.
| ︙ | ︙ | |||
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 |
Tcl_Obj *valueObj,
Tcl_Obj *listObj,
int *boolResult)
{
Tcl_ObjTypeInOperatorProc *proc = TclObjTypeHasProc(listObj, inOperProc);
return proc(interp, valueObj, listObj, boolResult);
}
#endif /* TCL_MAJOR_VERSION > 8 */
/*
* The structure below defines an entry in the assocData hash table which is
* associated with an interpreter. The entry contains a pointer to a function
* to call when the interpreter is deleted, and a pointer to a user-defined
* piece of data.
| > > > > > | 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 |
Tcl_Obj *valueObj,
Tcl_Obj *listObj,
int *boolResult)
{
Tcl_ObjTypeInOperatorProc *proc = TclObjTypeHasProc(listObj, inOperProc);
return proc(interp, valueObj, listObj, boolResult);
}
/* Function related to abstract list implementations */
MODULE_SCOPE int
Tcl_ListObjReverse(Tcl_Interp *interp, Tcl_Obj *objPtr, Tcl_Obj **reversedPtrPtr);
#endif /* TCL_MAJOR_VERSION > 8 */
/*
* The structure below defines an entry in the assocData hash table which is
* associated with an interpreter. The entry contains a pointer to a function
* to call when the interpreter is deleted, and a pointer to a user-defined
* piece of data.
|
| ︙ | ︙ |
Added generic/tclListTypes.c.
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
/*
* tclListTypes.c --
*
* This file contains functions that implement the Tcl abstract list
* object types.
*
* Copyright © 2025 Ashok P. Nadkarni. All rights reserved.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include <assert.h>
#include "tclInt.h"
/*
*------------------------------------------------------------------------
*
* TclAbstractListUpdateString --
*
* Common function to update the string representation of an abstract list
* type. Adapted from UpdateStringOfList in tclListObj.c.
* Assumes no prior string representation exists.
*
* Results:
* None.
*
* Side effects:
* The string representation of the object is updated.
*
*------------------------------------------------------------------------
*/
static void TclAbstractListUpdateString (Tcl_Obj *objPtr)
{
#define LOCAL_SIZE 64
int localFlags[LOCAL_SIZE], *flagPtr = NULL;
Tcl_Size numElems, i, length;
size_t bytesNeeded = 0;
const char *elem;
char *start, *dst;
int ret;
ret = Tcl_ListObjLength(NULL, objPtr, &numElems);
assert(ret == TCL_OK); // Should only be called for lists
(void) ret; // Avoid compiler warning
/* Handle empty list case first, so rest of the routine is simpler. */
if (numElems == 0) {
objPtr->bytes = (char *)Tcl_Alloc(1);
objPtr->bytes[0] = '\0';
objPtr->length = 0;
return;
}
/* Pass 1: estimate space, gather flags. */
if (numElems <= LOCAL_SIZE) {
flagPtr = localFlags;
}
else {
flagPtr = (int *)Tcl_Alloc(numElems);
}
for (i = 0; i < numElems; i++) {
Tcl_Obj *elemObj;
flagPtr[i] = (i ? TCL_DONT_QUOTE_HASH : 0);
ret = Tcl_ListObjIndex(NULL, objPtr, i, &elemObj);
assert(ret == TCL_OK);
elem = Tcl_GetStringFromObj(elemObj, &length);
bytesNeeded += Tcl_ScanCountedElement(elem, length, flagPtr + i);
if (bytesNeeded > SIZE_MAX - numElems) {
Tcl_Panic("max size for a Tcl value (%" TCL_Z_MODIFIER
"u bytes) exceeded",
SIZE_MAX);
}
#if TCL_MAJOR_VERSION > 8
Tcl_BounceRefCount(elemObj);
#endif
}
bytesNeeded += numElems - 1;
/*
* Pass 2: copy into string rep buffer.
*/
start = dst = (char *) Tcl_Alloc(bytesNeeded);
for (i = 0; i < numElems; i++) {
Tcl_Obj *elemObj;
flagPtr[i] |= (i ? TCL_DONT_QUOTE_HASH : 0);
ret = Tcl_ListObjIndex(NULL, objPtr, i, &elemObj);
assert(ret == TCL_OK);
elem = Tcl_GetStringFromObj(elemObj, &length);
dst += Tcl_ConvertCountedElement(elem, length, dst, flagPtr[i]);
*dst++ = ' ';
}
dst[-1] = '\0'; // Overwrite last space
size_t finalLen = dst - start;
/* If we are wasting "too many" bytes, attempt a reallocation */
if (bytesNeeded > 1000 && (bytesNeeded-finalLen) > (bytesNeeded/4)) {
char *newBytes = (char *)Tcl_Realloc(start, finalLen);
if (newBytes != NULL) {
start = newBytes;
}
}
objPtr->bytes = start;
objPtr->length = finalLen-1; // Exclude the trailing null
if (flagPtr != localFlags) {
Tcl_Free(flagPtr);
}
}
/*
* ------------------------------------------------------------------------
* lreverseType is an abstract list type that contains the same elements as a
* given list but in reverse order. Implementation is straightforward with the
* target list stored in ptrAndSize.ptr field. Indexing is then just a question
* of mapping index of the reversed list to that of the original target.
* The ptrAndSize.size field is used as a length cache.
* ------------------------------------------------------------------------
*/
static void LreverseFreeIntrep(Tcl_Obj *objPtr);
static void LreverseDupIntrep(Tcl_Obj *srcObj, Tcl_Obj *dupObj);
static Tcl_ObjTypeLengthProc LreverseTypeLength;
static Tcl_ObjTypeIndexProc LreverseTypeIndex;
static Tcl_ObjTypeReverseProc LreverseTypeReverse;
static const Tcl_ObjType lreverseType = {
"lreverse", /* name */
LreverseFreeIntrep, /* freeIntRepProc */
LreverseDupIntrep, /* dupIntRepProc */
TclAbstractListUpdateString, /* updateStringProc */
NULL, /* setFromAnyProc */
TCL_OBJTYPE_V2(LreverseTypeLength, /* lengthProc */
LreverseTypeIndex, /* indexProc */
NULL, /* sliceProc */
LreverseTypeReverse, /* reverseProc */
NULL, /* getElementsProc */
NULL, /* setElementProc - TODO */
NULL, /* replaceProc - TODO */
NULL) /* inOperProc - TODO */
};
void
LreverseFreeIntrep(Tcl_Obj *objPtr)
{
Tcl_DecrRefCount((Tcl_Obj *)objPtr->internalRep.ptrAndSize.ptr);
}
void
LreverseDupIntrep(Tcl_Obj *srcObj, Tcl_Obj *dupObj)
{
Tcl_Obj *targetObj = (Tcl_Obj *)srcObj->internalRep.ptrAndSize.ptr;
Tcl_IncrRefCount(targetObj);
dupObj->internalRep.ptrAndSize.ptr = targetObj;
dupObj->internalRep.ptrAndSize.size = srcObj->internalRep.ptrAndSize.size;
dupObj->typePtr = srcObj->typePtr;
}
/* Implementation of Tcl_ObjType.lengthProc for lreverseType */
Tcl_Size
LreverseTypeLength(Tcl_Obj *objPtr)
{
return objPtr->internalRep.ptrAndSize.size;
}
/* Implementation of Tcl_ObjType.indexProc for lreverseType */
int
LreverseTypeIndex(Tcl_Interp *interp,
Tcl_Obj *objPtr, /* Source list */
Tcl_Size index, /* Element index */
Tcl_Obj **elemPtrPtr) /* Returned element */
{
Tcl_Obj *targetPtr = (Tcl_Obj *)objPtr->internalRep.ptrAndSize.ptr;
Tcl_Size len = objPtr->internalRep.ptrAndSize.size;
if (index < 0 || index >= len) {
*elemPtrPtr = NULL;
return TCL_OK;
}
index = len - index - 1; /* Reverse the index */
return Tcl_ListObjIndex(interp, targetPtr, index, elemPtrPtr);
}
/* Implementation of Tcl_ObjType.reverseProc for lreverseType */
int
LreverseTypeReverse(Tcl_Interp *interp,
Tcl_Obj *objPtr, /* Operand */
Tcl_Obj **reversedPtrPtr) /* Result */
{
(void)interp; /* Unused */
/* Simple return the original */
*reversedPtrPtr = (Tcl_Obj *) objPtr->internalRep.ptrAndSize.ptr;
return TCL_OK;
}
/*
*------------------------------------------------------------------------
*
* Tcl_ListObjReverse --
*
* Returns a Tcl_Obj containing a list with the same elements as the
* source list with elements in reverse order.
*
* Results:
* Standard Tcl result.
*
* Side effects:
* Stores the result in *reversedPtrPtr. This may be the same as objPtr,
* a new allocation, or a pointer to an internally stored object. In
* all cases, the reference count of the returned object is not
* incremented to account for the returned reference to it.
*
*------------------------------------------------------------------------
*/
int
Tcl_ListObjReverse(
Tcl_Interp *interp,
Tcl_Obj *objPtr, /* Source whose elements are to be reversed */
Tcl_Obj **reversedPtrPtr) /* Location to store result object */
{
/* If the list is an AbstractList with a specialized reverse, use it. */
if (TclObjTypeHasProc(objPtr, reverseProc)) {
if (TclObjTypeReverse(interp, objPtr, reversedPtrPtr) == TCL_OK) {
return TCL_OK;
}
/* Specialization does not work for this case. Try default path */
}
Tcl_Size elemc;
/* Verify target is a list or can be converted to one */
if (TclObjTypeHasProc(objPtr, lengthProc)) {
elemc = TclObjTypeLength(objPtr);
} else {
if (TclListObjLength(interp, objPtr, &elemc) != TCL_OK) {
return TCL_ERROR;
}
}
/* If the list is empty, just return it. [Bug 1876793] */
if (elemc == 0) {
*reversedPtrPtr = objPtr;
return TCL_OK;
}
Tcl_Obj *resultPtr = Tcl_NewObj();
Tcl_InvalidateStringRep(resultPtr);
Tcl_IncrRefCount(objPtr);
resultPtr->internalRep.ptrAndSize.ptr = objPtr;
resultPtr->internalRep.ptrAndSize.size = elemc;
resultPtr->typePtr = &lreverseType;
*reversedPtrPtr = resultPtr;
return TCL_OK;
}
|
Changes to unix/Makefile.in.
| ︙ | ︙ | |||
305 306 307 308 309 310 311 | tclCompCmds.o tclCompCmdsGR.o tclCompCmdsSZ.o tclCompExpr.o \ tclCompile.o tclConfig.o tclDate.o tclDictObj.o tclDisassemble.o \ tclEncoding.o tclEnsemble.o \ tclEnv.o tclEvent.o tclExecute.o tclFCmd.o tclFileName.o tclGet.o \ tclHash.o tclHistory.o \ tclIcu.o tclIndexObj.o tclInterp.o tclIO.o tclIOCmd.o \ tclIORChan.o tclIORTrans.o tclIOGT.o tclIOSock.o tclIOUtil.o \ | | | 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | tclCompCmds.o tclCompCmdsGR.o tclCompCmdsSZ.o tclCompExpr.o \ tclCompile.o tclConfig.o tclDate.o tclDictObj.o tclDisassemble.o \ tclEncoding.o tclEnsemble.o \ tclEnv.o tclEvent.o tclExecute.o tclFCmd.o tclFileName.o tclGet.o \ tclHash.o tclHistory.o \ tclIcu.o tclIndexObj.o tclInterp.o tclIO.o tclIOCmd.o \ tclIORChan.o tclIORTrans.o tclIOGT.o tclIOSock.o tclIOUtil.o \ tclLink.o tclListObj.o tclListTypes.o \ tclLiteral.o tclLoad.o tclMain.o tclNamesp.o tclNotify.o \ tclObj.o tclOptimize.o tclPanic.o tclParse.o tclPathObj.o tclPipe.o \ tclPkg.o tclPkgConfig.o tclPosixStr.o \ tclPreserve.o tclProc.o tclProcess.o tclRegexp.o \ tclResolve.o tclResult.o tclScan.o tclStringObj.o tclStrIdxTree.o \ tclStrToD.o tclThread.o \ tclThreadAlloc.o tclThreadJoin.o tclThreadStorage.o tclStubInit.o \ |
| ︙ | ︙ | |||
443 444 445 446 447 448 449 450 451 452 453 454 455 456 | $(GENERIC_DIR)/tclIOGT.c \ $(GENERIC_DIR)/tclIOSock.c \ $(GENERIC_DIR)/tclIOUtil.c \ $(GENERIC_DIR)/tclIORChan.c \ $(GENERIC_DIR)/tclIORTrans.c \ $(GENERIC_DIR)/tclLink.c \ $(GENERIC_DIR)/tclListObj.c \ $(GENERIC_DIR)/tclLiteral.c \ $(GENERIC_DIR)/tclLoad.c \ $(GENERIC_DIR)/tclMain.c \ $(GENERIC_DIR)/tclNamesp.c \ $(GENERIC_DIR)/tclNotify.c \ $(GENERIC_DIR)/tclObj.c \ $(GENERIC_DIR)/tclOptimize.c \ | > | 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 | $(GENERIC_DIR)/tclIOGT.c \ $(GENERIC_DIR)/tclIOSock.c \ $(GENERIC_DIR)/tclIOUtil.c \ $(GENERIC_DIR)/tclIORChan.c \ $(GENERIC_DIR)/tclIORTrans.c \ $(GENERIC_DIR)/tclLink.c \ $(GENERIC_DIR)/tclListObj.c \ $(GENERIC_DIR)/tclListTypes.c \ $(GENERIC_DIR)/tclLiteral.c \ $(GENERIC_DIR)/tclLoad.c \ $(GENERIC_DIR)/tclMain.c \ $(GENERIC_DIR)/tclNamesp.c \ $(GENERIC_DIR)/tclNotify.c \ $(GENERIC_DIR)/tclObj.c \ $(GENERIC_DIR)/tclOptimize.c \ |
| ︙ | ︙ | |||
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 | tclLink.o: $(GENERIC_DIR)/tclLink.c $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclLink.c tclListObj.o: $(GENERIC_DIR)/tclListObj.c $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclListObj.c tclLiteral.o: $(GENERIC_DIR)/tclLiteral.c $(COMPILEHDR) $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclLiteral.c tclObj.o: $(GENERIC_DIR)/tclObj.c $(COMPILEHDR) $(MATHHDRS) $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclObj.c tclOptimize.o: $(GENERIC_DIR)/tclOptimize.c $(COMPILEHDR) | > > > | 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 | tclLink.o: $(GENERIC_DIR)/tclLink.c $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclLink.c tclListObj.o: $(GENERIC_DIR)/tclListObj.c $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclListObj.c tclListTypes.o: $(GENERIC_DIR)/tclListTypes.c $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclListTypes.c tclLiteral.o: $(GENERIC_DIR)/tclLiteral.c $(COMPILEHDR) $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclLiteral.c tclObj.o: $(GENERIC_DIR)/tclObj.c $(COMPILEHDR) $(MATHHDRS) $(CC) -c $(CC_SWITCHES) $(GENERIC_DIR)/tclObj.c tclOptimize.o: $(GENERIC_DIR)/tclOptimize.c $(COMPILEHDR) |
| ︙ | ︙ |
Changes to win/Makefile.in.
| ︙ | ︙ | |||
330 331 332 333 334 335 336 337 338 339 340 341 342 343 | tclIORChan.$(OBJEXT) \ tclIORTrans.$(OBJEXT) \ tclIOSock.$(OBJEXT) \ tclIOUtil.$(OBJEXT) \ tclLink.$(OBJEXT) \ tclLiteral.$(OBJEXT) \ tclListObj.$(OBJEXT) \ tclLoad.$(OBJEXT) \ tclMainW.$(OBJEXT) \ tclMain.$(OBJEXT) \ tclNamesp.$(OBJEXT) \ tclNotify.$(OBJEXT) \ tclOO.$(OBJEXT) \ tclOOBasic.$(OBJEXT) \ | > | 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | tclIORChan.$(OBJEXT) \ tclIORTrans.$(OBJEXT) \ tclIOSock.$(OBJEXT) \ tclIOUtil.$(OBJEXT) \ tclLink.$(OBJEXT) \ tclLiteral.$(OBJEXT) \ tclListObj.$(OBJEXT) \ tclListTypes.$(OBJEXT) \ tclLoad.$(OBJEXT) \ tclMainW.$(OBJEXT) \ tclMain.$(OBJEXT) \ tclNamesp.$(OBJEXT) \ tclNotify.$(OBJEXT) \ tclOO.$(OBJEXT) \ tclOOBasic.$(OBJEXT) \ |
| ︙ | ︙ |
Changes to win/makefile.vc.
| ︙ | ︙ | |||
285 286 287 288 289 290 291 292 293 294 295 296 297 298 | $(TMP_DIR)\tclIOGT.obj \ $(TMP_DIR)\tclIOSock.obj \ $(TMP_DIR)\tclIOUtil.obj \ $(TMP_DIR)\tclIORChan.obj \ $(TMP_DIR)\tclIORTrans.obj \ $(TMP_DIR)\tclLink.obj \ $(TMP_DIR)\tclListObj.obj \ $(TMP_DIR)\tclLiteral.obj \ $(TMP_DIR)\tclLoad.obj \ $(TMP_DIR)\tclMainW.obj \ $(TMP_DIR)\tclMain.obj \ $(TMP_DIR)\tclNamesp.obj \ $(TMP_DIR)\tclNotify.obj \ $(TMP_DIR)\tclOO.obj \ | > | 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | $(TMP_DIR)\tclIOGT.obj \ $(TMP_DIR)\tclIOSock.obj \ $(TMP_DIR)\tclIOUtil.obj \ $(TMP_DIR)\tclIORChan.obj \ $(TMP_DIR)\tclIORTrans.obj \ $(TMP_DIR)\tclLink.obj \ $(TMP_DIR)\tclListObj.obj \ $(TMP_DIR)\tclListTypes.obj \ $(TMP_DIR)\tclLiteral.obj \ $(TMP_DIR)\tclLoad.obj \ $(TMP_DIR)\tclMainW.obj \ $(TMP_DIR)\tclMain.obj \ $(TMP_DIR)\tclNamesp.obj \ $(TMP_DIR)\tclNotify.obj \ $(TMP_DIR)\tclOO.obj \ |
| ︙ | ︙ |