libsheepy

C lib for handling text files, strings and json like data structure with an object oriented system
git clone https://spartatek.se/git/libsheepy.git
Log | Files | Refs | README | LICENSE

commit 1f0e478b035fcef8a231cbd3eb1caed5ea8a097a
parent 01669a08f2bd05b4459e1b36ff9c84b4561d3617
Author: Remy Noulin <loader2x@gmail.com>
Date:   Sat, 19 Sep 2020 21:27:47 +0200

add forEach element and enumerate for slice, staticSlice, vector, dVector, staticArray, dArray, slab and C arrays

completion.txt      |  16 ++++
documentation.md    | 184 +++++++++++++++++++++++++++++++++++++++++++
release/libsheepy.h | 220 +++++++++++++++++++++++++++++++++++++++++++++++++++-
src/libsheepy.c     |  39 +++++++++-
src/libsheepy.h     | 220 +++++++++++++++++++++++++++++++++++++++++++++++++++-
5 files changed, 674 insertions(+), 5 deletions(-)

Diffstat:
Mcompletion.txt | 16++++++++++++++++
Mdocumentation.md | 184+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mrelease/libsheepy.h | 220++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/libsheepy.c | 39++++++++++++++++++++++++++++++++++++---
Msrc/libsheepy.h | 220++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
5 files changed, 674 insertions(+), 5 deletions(-)

diff --git a/completion.txt b/completion.txt @@ -840,6 +840,8 @@ loopDownTo(maxCount, to) loopFrom(from, maxCount) loopStep(maxCount, step) loopFromStep(from, maxCount, step) +aForEach(array, element) +aEnumerate(name, index, element) forEachCharP(list, element) forEachS(list, element) forEachType(type, list, element) @@ -1009,6 +1011,8 @@ sliceWrite(name, file) sliceReadFilename(name, filename) sliceRead(name, file) forEachSc(name, index) +sliceForEach(name, element) +sliceEnumerate(name, index, element) sliceInject(name, index) slicePrepend(name, v) sliceDequeue(name) @@ -1061,6 +1065,8 @@ staticSliceWrite staticSliceReadFilename(name, filename) staticSliceRead(name, file) forEachSSc +staticSliceForEach(name, element) +staticSliceEnumerate(name, index, element) staticSliceInject(name, index) staticSlicePrepend(name, v) staticSliceDequeue @@ -1131,6 +1137,8 @@ vectorWrite vectorReadFilename(name, filename) vectorRead(name, file) forEachV +vectorForEach(name, element) +vectorEnumerate(name, index, element) vectorInject(name, index) vectorPrepend(name, v) vectorDequeue @@ -1183,6 +1191,8 @@ dVectorWriteFilename(a, filename) dVectorWrite(a, file) dVectorReadFilename(a, filename) dVectorRead(a, file) +dVectorForEach(name, element) +dVectorEnumerate(name, index, element) typedef struct { } staticArrayBase staticArrayT(typeName, element, MAXCOUNT) @@ -1213,6 +1223,8 @@ staticArrayWriteFilename(name, filename) staticArrayWrite(name, file) staticArrayReadFilename(name, filename) staticArrayRead(name, file) +staticArrayForEach(name, element) +staticArrayEnumerate(name, index, element) indexer indexerT(typeName, INT_TYPE) createIndexer(typeName, name, maxCount) @@ -1315,6 +1327,8 @@ dArrayWriteFilename(a, filename) dArrayWrite(a, file) dArrayReadFilename(a, filename) dArrayRead(a, file) +dArrayForEach(name, element) +dArrayEnumerate(name, index, element) slabSz slabT(typeName, elementType) createSlab(typeName, name) @@ -1349,6 +1363,8 @@ slabWriteFilename(a, filename) slabWrite(a, file) slabReadFilename(a, filename) slabRead(a, file) +slabForEach(name, element) +slabEnumerate(name, index, element) staticBitsetT(typeName, containerType, count) staticBitsetInit staticBitsetCount(name) diff --git a/documentation.md b/documentation.md @@ -871,6 +871,8 @@ loopDownTo(maxCount, to) loopFrom(from, maxCount) loopStep(maxCount, step) loopFromStep(from, maxCount, step) +aForEach(array, element) +aEnumerate(name, index, element) forEachCharP(list, element) forEachS(list, element) forEachType(type, list, element) @@ -1040,6 +1042,8 @@ sliceWrite(name, file) sliceReadFilename(name, filename) sliceRead(name, file) forEachSc(name, index) +sliceForEach(name, element) +sliceEnumerate(name, index, element) sliceInject(name, index) slicePrepend(name, v) sliceDequeue(name) @@ -1092,6 +1096,8 @@ staticSliceWrite staticSliceReadFilename(name, filename) staticSliceRead(name, file) forEachSSc +staticSliceForEach(name, element) +staticSliceEnumerate(name, index, element) staticSliceInject(name, index) staticSlicePrepend(name, v) staticSliceDequeue @@ -1162,6 +1168,8 @@ vectorWrite vectorReadFilename(name, filename) vectorRead(name, file) forEachV +vectorForEach(name, element) +vectorEnumerate(name, index, element) vectorInject(name, index) vectorPrepend(name, v) vectorDequeue @@ -1214,6 +1222,8 @@ dVectorWriteFilename(a, filename) dVectorWrite(a, file) dVectorReadFilename(a, filename) dVectorRead(a, file) +dVectorForEach(name, element) +dVectorEnumerate(name, index, element) typedef struct { } staticArrayBase staticArrayT(typeName, element, MAXCOUNT) @@ -1244,6 +1254,8 @@ staticArrayWriteFilename(name, filename) staticArrayWrite(name, file) staticArrayReadFilename(name, filename) staticArrayRead(name, file) +staticArrayForEach(name, element) +staticArrayEnumerate(name, index, element) indexer indexerT(typeName, INT_TYPE) createIndexer(typeName, name, maxCount) @@ -1346,6 +1358,8 @@ dArrayWriteFilename(a, filename) dArrayWrite(a, file) dArrayReadFilename(a, filename) dArrayRead(a, file) +dArrayForEach(name, element) +dArrayEnumerate(name, index, element) slabSz slabT(typeName, elementType) createSlab(typeName, name) @@ -1380,6 +1394,8 @@ slabWriteFilename(a, filename) slabWrite(a, file) slabReadFilename(a, filename) slabRead(a, file) +slabForEach(name, element) +slabEnumerate(name, index, element) staticBitsetT(typeName, containerType, count) staticBitsetInit staticBitsetCount(name) @@ -3402,6 +3418,27 @@ int listPrintS(char **list); */ #define loopFromStep(from, maxCount, step) \ /* + * loop on array elements + * element is a pointer to a value in the array + * + * aForEach(array, e) { + * e->x = 0; + * } + */ +#define aForEach(array, element) \ +/* + * enumerate array elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * aEnumerate(array, i, e) { + * e->x = 0; + * printf("aEnumerate %d\n", i); + * } + */ +#define aEnumerate(name, index, element) \ +/* * forEach - loop macro on list indexes * to access the element in the loop, use *element */ @@ -3974,6 +4011,27 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); */ #define forEachSc(name, index) range(index, (name)->count) /* + * loop on slice elements + * element is a pointer to a value in the array + * + * sliceForEach(&slc, e) { + * e->x = 0; + * } + */ +#define sliceForEach(name, element) \ +/* + * enumerate slice elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * sliceEnumerate(&slc, i, e) { + * e->x = 0; + * printf("sliceEnumerate %d\n", i); + * } + */ +#define sliceEnumerate(name, index, element) \ +/* * insert an element at index * the data is moved to create space for the new element * to set data at the new element, use sliceAt(name, index) = value; @@ -4355,6 +4413,27 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); */ #define forEachSSc forEachSc /* + * loop on slice elements + * element is a pointer to a value in the array + * + * staticSliceForEach(&slc, e) { + * e->x = 0; + * } + */ +#define staticSliceForEach(name, element) \ +/* + * enumerate slice elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * sliceEnumerate(&slc, i, e) { + * e->x = 0; + * printf("sliceEnumerate %d\n", i); + * } + */ +#define staticSliceEnumerate(name, index, element) \ +/* * insert an element at index * the data is moved to create space for the new element * to set data at the new element, use staticSliceAt(name, index) = value; @@ -4811,6 +4890,27 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); */ #define forEachV forEachSc /* + * loop on vector elements + * element is a pointer to a value in the array + * + * vectorForEach(&slc, e) { + * e->x = 0; + * } + */ +#define vectorForEach(name, element) \ +/* + * enumerate vector elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * vectorEnumerate(&slc, i, e) { + * e->x = 0; + * printf("vectorEnumerate %d\n", i); + * } + */ +#define vectorEnumerate(name, index, element) \ +/* * insert an element at index * the data is moved to create space for the new element * to set data at the new element, use vectorAt(name, index) = value; @@ -5197,6 +5297,27 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); */ #define dVectorRead(a, file) do {\ /* + * loop on dVector elements + * element is a pointer to a value in the array + * + * dVectorForEach(&vec, e) { + * e->x = 0; + * } + */ +#define dVectorForEach(name, element) \ +/* + * enumerate dVector elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * dVectorEnumerate(&vec, i, e) { + * e->x = 0; + * printf("dVectorEnumerate %d\n", i); + * } + */ +#define dVectorEnumerate(name, index, element) \ +/* * end dynamic segmented vector */ /* @@ -5376,6 +5497,27 @@ typedef struct { */ #define staticArrayRead(name, file) do {\ /* + * loop on staticArray elements + * element is a pointer to a value in the array + * + * staticArrayForEach(sa, e) { + * e->x = 0; + * } + */ +#define staticArrayForEach(name, element) \ +/* + * enumerate staticArray elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * staticArrayEnumerate(sa, i, e) { + * e->x = 0; + * printf("staticArrayEnumerate %d\n", i); + * } + */ +#define staticArrayEnumerate(name, index, element) \ +/* * indexer is a staticArray without the list * It indexes an independent array * @@ -6024,6 +6166,27 @@ bool fiberPrepend(void *ctx, int thisSlot, fiberFT func); */ #define dArrayRead(a, file) do {\ /* + * loop on dArray elements + * element is a pointer to a value in the array + * + * dArrayForEach(&a, e) { + * e->x = 0; + * } + */ +#define dArrayForEach(name, element) \ +/* + * enumerate dArray elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * dArrayEnumerate(&a, i, e) { + * e->x = 0; + * printf("dArrayEnumerate %d\n", i); + * } + */ +#define dArrayEnumerate(name, index, element) \ +/* * end dynamic array */ /* @@ -6286,6 +6449,27 @@ bool fiberPrepend(void *ctx, int thisSlot, fiberFT func); */ #define slabRead(a, file) do {\ /* + * loop on slab elements + * element is a pointer to a value in the array + * + * slabForEach(&a, e) { + * e->x = 0; + * } + */ +#define slabForEach(name, element) \ +/* + * enumerate slab elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * slabEnumerate(&a, i, e) { + * e->x = 0; + * printf("slabEnumerate %d\n", i); + * } + */ +#define slabEnumerate(name, index, element) \ +/* * end slab */ /* diff --git a/release/libsheepy.h b/release/libsheepy.h @@ -98,7 +98,7 @@ // version accoring to the version package: Release.Major.minor.patch // https://noulin.net/version/file/README.md.html -#define LIBSHEEPY_VERSION "1.2.3.3" +#define LIBSHEEPY_VERSION "1.2.4" #ifndef SH_PREFIX #define SH_PREFIX(NAME) NAME @@ -2794,6 +2794,33 @@ int listPrintS(char **list); /** + * loop on array elements + * element is a pointer to a value in the array + * + * aForEach(array, e) { + * e->x = 0; + * } + */ +#define aForEach(array, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + for (typeof(&array[0]) element = &array[0] ; UNIQVAR(libsheepyInternalIndex) < ARRAY_SIZE(array) ; UNIQVAR(libsheepyInternalIndex)++, element = &array[UNIQVAR(libsheepyInternalIndex)]) + +/** + * enumerate array elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * aEnumerate(array, i, e) { + * e->x = 0; + * printf("aEnumerate %d\n", i); + * } + */ +#define aEnumerate(name, index, element) \ + ; size_t index = 0 ; \ + for (typeof(&array[0]) element = &array[0] ; index < ARRAY_SIZE(array) ; index++, element = &array[index]]) + +/** * forEach - loop macro on list indexes * to access the element in the loop, use *element */ @@ -3263,6 +3290,7 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); */ #define sliceInitCount(name, countInt) do{\ var UNIQVAR(c) = countInt;\ + /* TODO check if countInt is 0 then +1 */\ (name)->array = malloc(UNIQVAR(c) * sizeof (name)->array[0]);\ (name)->count = 0;\ } while(0) @@ -3693,6 +3721,33 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); #define forEachSc(name, index) range(index, (name)->count) /** + * loop on slice elements + * element is a pointer to a value in the array + * + * sliceForEach(&slc, e) { + * e->x = 0; + * } + */ +#define sliceForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + for (sliceElemPtrType(name) element = slicePtr(name, 0) ; UNIQVAR(libsheepyInternalIndex) < (name)->count ; UNIQVAR(libsheepyInternalIndex)++, element = slicePtr(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate slice elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * sliceEnumerate(&slc, i, e) { + * e->x = 0; + * printf("sliceEnumerate %d\n", i); + * } + */ +#define sliceEnumerate(name, index, element) \ + ; size_t index = 0 ; \ + for (sliceElemPtrType(name) element = slicePtr(name, 0) ; index < (name)->count ; index++, element = slicePtr(name, index)) + +/** * insert an element at index * the data is moved to create space for the new element * to set data at the new element, use sliceAt(name, index) = value; @@ -4366,6 +4421,33 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); #define forEachSSc forEachSc /** + * loop on slice elements + * element is a pointer to a value in the array + * + * staticSliceForEach(&slc, e) { + * e->x = 0; + * } + */ +#define staticSliceForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + for (staticSliceElemPtrType(name) element = staticSlicePtr(name, 0) ; UNIQVAR(libsheepyInternalIndex) < (name)->count ; UNIQVAR(libsheepyInternalIndex)++, element = staticSlicePtr(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate slice elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * sliceEnumerate(&slc, i, e) { + * e->x = 0; + * printf("sliceEnumerate %d\n", i); + * } + */ +#define staticSliceEnumerate(name, index, element) \ + ; size_t index = 0 ; \ + for (staticSliceElemPtrType(name) element = staticSlicePtr(name, 0) ; index < (name)->count ; index++, element = staticSlicePtr(name, index)) + +/** * insert an element at index * the data is moved to create space for the new element * to set data at the new element, use staticSliceAt(name, index) = value; @@ -5096,6 +5178,33 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); #define forEachV forEachSc /** + * loop on vector elements + * element is a pointer to a value in the array + * + * vectorForEach(&slc, e) { + * e->x = 0; + * } + */ +#define vectorForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + for (vectorElemPtrType(name) element = vectorPtr(name, 0) ; UNIQVAR(libsheepyInternalIndex) < (name)->count ; UNIQVAR(libsheepyInternalIndex)++, element = vectorPtr(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate vector elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * vectorEnumerate(&slc, i, e) { + * e->x = 0; + * printf("vectorEnumerate %d\n", i); + * } + */ +#define vectorEnumerate(name, index, element) \ + ; size_t index = 0 ; \ + for (vectorElemPtrType(name) element = vectorPtr(name, 0) ; index < (name)->count ; index++, element = vectorPtr(name, index)) + +/** * insert an element at index * the data is moved to create space for the new element * to set data at the new element, use vectorAt(name, index) = value; @@ -5712,6 +5821,33 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); }\ } while(0) +/** + * loop on dVector elements + * element is a pointer to a value in the array + * + * dVectorForEach(&vec, e) { + * e->x = 0; + * } + */ +#define dVectorForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + for (dVectorElemPtrType(name) element = dVectorPtr(name, 0) ; UNIQVAR(libsheepyInternalIndex) < (name)->count ; UNIQVAR(libsheepyInternalIndex)++, element = dVectorPtr(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate dVector elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * dVectorEnumerate(&vec, i, e) { + * e->x = 0; + * printf("dVectorEnumerate %d\n", i); + * } + */ +#define dVectorEnumerate(name, index, element) \ + ; size_t index = 0 ; \ + for (dVectorElemPtrType(name) element = dVectorPtr(name, 0) ; index < (name)->count ; index++, element = dVectorPtr(name, index)) + /* @@ -6044,6 +6180,34 @@ typedef struct { } while(0) /** + * loop on staticArray elements + * element is a pointer to a value in the array + * + * staticArrayForEach(sa, e) { + * e->x = 0; + * } + */ +#define staticArrayForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + for (staticArrayElemPtrType(name) element = &staticArrayRef(name, 0) ; UNIQVAR(libsheepyInternalIndex) < staticArrayCount(name) ; UNIQVAR(libsheepyInternalIndex)++, element = &staticArrayRef(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate staticArray elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * staticArrayEnumerate(sa, i, e) { + * e->x = 0; + * printf("staticArrayEnumerate %d\n", i); + * } + */ +#define staticArrayEnumerate(name, index, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + ; size_t index = name.head ; \ + for (staticArrayElemPtrType(name) element = &staticArrayRef(name, 0) ; UNIQVAR(libsheepyInternalIndex) < staticArrayCount(name) ; UNIQVAR(libsheepyInternalIndex)++, index = (name.head + UNIQVAR(libsheepyInternalIndex)) % name.maxCount, element = &staticArrayRef(name, index)) + +/** * indexer is a staticArray without the list * It indexes an independent array * @@ -6984,6 +7148,33 @@ bool fiberPrepend(void *ctx, int thisSlot, fiberFT func); }\ } while(0) +/** + * loop on dArray elements + * element is a pointer to a value in the array + * + * dArrayForEach(&a, e) { + * e->x = 0; + * } + */ +#define dArrayForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = (name)->head; \ + for (dArrayElemPtrType(name) element = dArrayPtr(name, (name)->head) ; UNIQVAR(libsheepyInternalIndex) < (name)->last + 1 ; UNIQVAR(libsheepyInternalIndex)++, element = dArrayPtr(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate dArray elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * dArrayEnumerate(&a, i, e) { + * e->x = 0; + * printf("dArrayEnumerate %d\n", i); + * } + */ +#define dArrayEnumerate(name, index, element) \ + ; size_t index = (name)->head; \ + for (dArrayElemPtrType(name) element = dArrayPtr(name, (name)->head) ; index < (name)->last + 1 ; index++, element = dArrayPtr(name, index)) + /* @@ -7357,6 +7548,33 @@ bool fiberPrepend(void *ctx, int thisSlot, fiberFT func); }\ } while(0) +/** + * loop on slab elements + * element is a pointer to a value in the array + * + * slabForEach(&a, e) { + * e->x = 0; + * } + */ +#define slabForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = (name)->head; \ + for (slabElemPtrType(name) element = slabPtr(name, (name)->head) ; UNIQVAR(libsheepyInternalIndex) < (name)->last + 1 ; UNIQVAR(libsheepyInternalIndex)++, element = slabPtr(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate slab elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * slabEnumerate(&a, i, e) { + * e->x = 0; + * printf("slabEnumerate %d\n", i); + * } + */ +#define slabEnumerate(name, index, element) \ + ; size_t index = (name)->head; \ + for (slabElemPtrType(name) element = slabPtr(name, (name)->head) ; index < (name)->last + 1 ; index++, element = slabPtr(name, index)) + /* * end slab diff --git a/src/libsheepy.c b/src/libsheepy.c @@ -55279,7 +55279,22 @@ void bttt(void) { int MAIN(int ARGC, char** ARGV) { char **l UNUSED = NULL; - argc = ARGC; argv = ARGV;;// list + argc = ARGC; argv = ARGV; + typ struct {int a; int b;} structuret;; + + sliceT(slStruct, structuret); + slStruct slc; + sliceInit(&slc); + slicePush(&slc); + slicePush(&slc); + sliceEnumerate(&slc, i, elm) { + elm->a = 0;; + elm->b = 0;; + printf("sliceEnumerate %d\n", i); + } + sliceFree(&slc); + + // list /* i8 a = 0 */ /* i16 s = 0 */ @@ -55375,9 +55390,13 @@ int MAIN(int ARGC, char** ARGV) { sliceInit(&slce); //sliceElemType(&slce) slEl; - staticSliceT(sslicet, char*, 1); + staticSliceT(sslicet, char*, 2); sslicet sslce; staticSliceInit(&sslce); + staticSlicePush(&sslce); + staticSliceForEach(&sslce, S) S = NULL;; + staticSliceEnumerate(&sslce, iS, S) S = NULL;; + //staticSliceElemType(&sslce) sslEl; puts(getCurrentDate()); @@ -55390,12 +55409,16 @@ int MAIN(int ARGC, char** ARGV) { vectorInitCount(&vec, 17); + logVar(vectorMaxCount(&vec), "zu"); logVar(vectorCount(&vec), "zu"); vectorAppend(&vec, "qwe"); vectorAppend(&vec, "2345"); + vectorForEach(&vec, S) S = NULL;; + vectorEnumerate(&vec, iv, S) S = NULL;; + logVar(vectorMaxCount(&vec), "zu"); logVar(vectorCount(&vec), "zu"); @@ -55446,6 +55469,8 @@ int MAIN(int ARGC, char** ARGV) { dVectorInit(&dvec); //dVectorElemType(&dvec) dvecE; + dVectorForEach(&dvec, S) S = NULL;; + dVectorEnumerate(&dvec, idv, S) S = NULL;; XSUCCESS @@ -55627,6 +55652,9 @@ int MAIN(int ARGC, char** ARGV) { staticArrayLast($) = i;; } + staticArrayForEach($, S) S = 0;; + staticArrayEnumerate($, isa, S) S = 0;; + rangeInf(i) { puts("infinity"); if (i == 5) { @@ -55681,6 +55709,9 @@ int MAIN(int ARGC, char** ARGV) { slabAppend(&b, 1); slabAppend(&b, 2); + slabForEach(&b, S) S = 0;; + slabEnumerate(&b, isl, S) S = 0;; + printf("size %d\n", slabSz); slabAt(&b, 1) = 3; @@ -55722,7 +55753,9 @@ int MAIN(int ARGC, char** ARGV) { timeUs(loghex(a, 10)); - //intDynt bbb; + intDynt bbb; + dArrayForEach(&bbb, S) S = 0;; + dArrayEnumerate(&bbb, ida, S) S = 0;; //dArrayElemType(&bbb) bbbE; /* intDynt b; diff --git a/src/libsheepy.h b/src/libsheepy.h @@ -98,7 +98,7 @@ // version accoring to the version package: Release.Major.minor.patch // https://noulin.net/version/file/README.md.html -#define LIBSHEEPY_VERSION "1.2.3.3" +#define LIBSHEEPY_VERSION "1.2.4" #ifndef SH_PREFIX #define SH_PREFIX(NAME) NAME @@ -2794,6 +2794,33 @@ int listPrintS(char **list); /** + * loop on array elements + * element is a pointer to a value in the array + * + * aForEach(array, e) { + * e->x = 0; + * } + */ +#define aForEach(array, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + for (typeof(&array[0]) element = &array[0] ; UNIQVAR(libsheepyInternalIndex) < ARRAY_SIZE(array) ; UNIQVAR(libsheepyInternalIndex)++, element = &array[UNIQVAR(libsheepyInternalIndex)]) + +/** + * enumerate array elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * aEnumerate(array, i, e) { + * e->x = 0; + * printf("aEnumerate %d\n", i); + * } + */ +#define aEnumerate(name, index, element) \ + ; size_t index = 0 ; \ + for (typeof(&array[0]) element = &array[0] ; index < ARRAY_SIZE(array) ; index++, element = &array[index]]) + +/** * forEach - loop macro on list indexes * to access the element in the loop, use *element */ @@ -3263,6 +3290,7 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); */ #define sliceInitCount(name, countInt) do{\ var UNIQVAR(c) = countInt;\ + /* TODO check if countInt is 0 then +1 */\ (name)->array = malloc(UNIQVAR(c) * sizeof (name)->array[0]);\ (name)->count = 0;\ } while(0) @@ -3693,6 +3721,33 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); #define forEachSc(name, index) range(index, (name)->count) /** + * loop on slice elements + * element is a pointer to a value in the array + * + * sliceForEach(&slc, e) { + * e->x = 0; + * } + */ +#define sliceForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + for (sliceElemPtrType(name) element = slicePtr(name, 0) ; UNIQVAR(libsheepyInternalIndex) < (name)->count ; UNIQVAR(libsheepyInternalIndex)++, element = slicePtr(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate slice elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * sliceEnumerate(&slc, i, e) { + * e->x = 0; + * printf("sliceEnumerate %d\n", i); + * } + */ +#define sliceEnumerate(name, index, element) \ + ; size_t index = 0 ; \ + for (sliceElemPtrType(name) element = slicePtr(name, 0) ; index < (name)->count ; index++, element = slicePtr(name, index)) + +/** * insert an element at index * the data is moved to create space for the new element * to set data at the new element, use sliceAt(name, index) = value; @@ -4366,6 +4421,33 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); #define forEachSSc forEachSc /** + * loop on slice elements + * element is a pointer to a value in the array + * + * staticSliceForEach(&slc, e) { + * e->x = 0; + * } + */ +#define staticSliceForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + for (staticSliceElemPtrType(name) element = staticSlicePtr(name, 0) ; UNIQVAR(libsheepyInternalIndex) < (name)->count ; UNIQVAR(libsheepyInternalIndex)++, element = staticSlicePtr(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate slice elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * sliceEnumerate(&slc, i, e) { + * e->x = 0; + * printf("sliceEnumerate %d\n", i); + * } + */ +#define staticSliceEnumerate(name, index, element) \ + ; size_t index = 0 ; \ + for (staticSliceElemPtrType(name) element = staticSlicePtr(name, 0) ; index < (name)->count ; index++, element = staticSlicePtr(name, index)) + +/** * insert an element at index * the data is moved to create space for the new element * to set data at the new element, use staticSliceAt(name, index) = value; @@ -5096,6 +5178,33 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); #define forEachV forEachSc /** + * loop on vector elements + * element is a pointer to a value in the array + * + * vectorForEach(&slc, e) { + * e->x = 0; + * } + */ +#define vectorForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + for (vectorElemPtrType(name) element = vectorPtr(name, 0) ; UNIQVAR(libsheepyInternalIndex) < (name)->count ; UNIQVAR(libsheepyInternalIndex)++, element = vectorPtr(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate vector elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * vectorEnumerate(&slc, i, e) { + * e->x = 0; + * printf("vectorEnumerate %d\n", i); + * } + */ +#define vectorEnumerate(name, index, element) \ + ; size_t index = 0 ; \ + for (vectorElemPtrType(name) element = vectorPtr(name, 0) ; index < (name)->count ; index++, element = vectorPtr(name, index)) + +/** * insert an element at index * the data is moved to create space for the new element * to set data at the new element, use vectorAt(name, index) = value; @@ -5712,6 +5821,33 @@ void **iListDel(void ***list, intmax_t start, intmax_t end); }\ } while(0) +/** + * loop on dVector elements + * element is a pointer to a value in the array + * + * dVectorForEach(&vec, e) { + * e->x = 0; + * } + */ +#define dVectorForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + for (dVectorElemPtrType(name) element = dVectorPtr(name, 0) ; UNIQVAR(libsheepyInternalIndex) < (name)->count ; UNIQVAR(libsheepyInternalIndex)++, element = dVectorPtr(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate dVector elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * dVectorEnumerate(&vec, i, e) { + * e->x = 0; + * printf("dVectorEnumerate %d\n", i); + * } + */ +#define dVectorEnumerate(name, index, element) \ + ; size_t index = 0 ; \ + for (dVectorElemPtrType(name) element = dVectorPtr(name, 0) ; index < (name)->count ; index++, element = dVectorPtr(name, index)) + /* @@ -6044,6 +6180,34 @@ typedef struct { } while(0) /** + * loop on staticArray elements + * element is a pointer to a value in the array + * + * staticArrayForEach(sa, e) { + * e->x = 0; + * } + */ +#define staticArrayForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + for (staticArrayElemPtrType(name) element = &staticArrayRef(name, 0) ; UNIQVAR(libsheepyInternalIndex) < staticArrayCount(name) ; UNIQVAR(libsheepyInternalIndex)++, element = &staticArrayRef(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate staticArray elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * staticArrayEnumerate(sa, i, e) { + * e->x = 0; + * printf("staticArrayEnumerate %d\n", i); + * } + */ +#define staticArrayEnumerate(name, index, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = 0; \ + ; size_t index = name.head ; \ + for (staticArrayElemPtrType(name) element = &staticArrayRef(name, 0) ; UNIQVAR(libsheepyInternalIndex) < staticArrayCount(name) ; UNIQVAR(libsheepyInternalIndex)++, index = (name.head + UNIQVAR(libsheepyInternalIndex)) % name.maxCount, element = &staticArrayRef(name, index)) + +/** * indexer is a staticArray without the list * It indexes an independent array * @@ -6984,6 +7148,33 @@ bool fiberPrepend(void *ctx, int thisSlot, fiberFT func); }\ } while(0) +/** + * loop on dArray elements + * element is a pointer to a value in the array + * + * dArrayForEach(&a, e) { + * e->x = 0; + * } + */ +#define dArrayForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = (name)->head; \ + for (dArrayElemPtrType(name) element = dArrayPtr(name, (name)->head) ; UNIQVAR(libsheepyInternalIndex) < (name)->last + 1 ; UNIQVAR(libsheepyInternalIndex)++, element = dArrayPtr(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate dArray elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * dArrayEnumerate(&a, i, e) { + * e->x = 0; + * printf("dArrayEnumerate %d\n", i); + * } + */ +#define dArrayEnumerate(name, index, element) \ + ; size_t index = (name)->head; \ + for (dArrayElemPtrType(name) element = dArrayPtr(name, (name)->head) ; index < (name)->last + 1 ; index++, element = dArrayPtr(name, index)) + /* @@ -7357,6 +7548,33 @@ bool fiberPrepend(void *ctx, int thisSlot, fiberFT func); }\ } while(0) +/** + * loop on slab elements + * element is a pointer to a value in the array + * + * slabForEach(&a, e) { + * e->x = 0; + * } + */ +#define slabForEach(name, element) \ + ;size_t UNIQVAR(libsheepyInternalIndex) = (name)->head; \ + for (slabElemPtrType(name) element = slabPtr(name, (name)->head) ; UNIQVAR(libsheepyInternalIndex) < (name)->last + 1 ; UNIQVAR(libsheepyInternalIndex)++, element = slabPtr(name, UNIQVAR(libsheepyInternalIndex))) + +/** + * enumerate slab elements + * index is the position of the element in the array + * index is declared as size_t and is available after the loop + * element is a pointer to a value in the array + * + * slabEnumerate(&a, i, e) { + * e->x = 0; + * printf("slabEnumerate %d\n", i); + * } + */ +#define slabEnumerate(name, index, element) \ + ; size_t index = (name)->head; \ + for (slabElemPtrType(name) element = slabPtr(name, (name)->head) ; index < (name)->last + 1 ; index++, element = slabPtr(name, index)) + /* * end slab