]> Cypherpunks repositories - keks.git/commitdiff
Rename "pool" to "items"
authorSergey Matveev <stargrave@stargrave.org>
Mon, 14 Oct 2024 07:42:28 +0000 (10:42 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Mon, 14 Oct 2024 17:27:33 +0000 (20:27 +0300)
cyac/README
cyac/cmd/.gitignore
cyac/cmd/all.do
cyac/cmd/clean
cyac/cmd/default.do
cyac/cmd/print-items.c [moved from cyac/cmd/print-pooled.c with 85% similarity]
cyac/dec.h
cyac/items.c [moved from cyac/pool.c with 64% similarity]
cyac/items.h [moved from cyac/pool.h with 80% similarity]
cyac/o.list

index db5310b37a0a5402f9c4cd46ec0402bff86df48e72aaf07c2c7b84e851620b7d..c303e893a59eb7d9c0cfcb86d82d171c29a5bdb8369b3f39c3e39abc85b5a937 100644 (file)
@@ -12,7 +12,7 @@ decoded atom.
 dectai.* contains converter from TAI64 to UTC.
 leapsecs.* contains the leap seconds database itself.
 iter.* contains helpers that may pass over the iterables.
-pool.* contains a non-streamable parser.
+items.* contains a non-streamable parser.
 
 enc.* contains encoders for various atoms. Containers and blobs must
 be made manually, by finishing them with proper EOC/BIN and sorting the
index f5062f208cdcfbbd2479fa0475351b6596992e9da1f0447e40e46f79f16891f6..9caf01a1f16122aefbce8735574cf20bc236bfa7d6893ff27cc5f8403fe0c8e1 100644 (file)
@@ -1,3 +1,3 @@
+/print-items
 /print-itered
-/print-pooled
 /test-vector
index f14d17cb8d00384a933755d7396f3f72aae3ce0a41dde64cebe557e41cf0834b..1cf8db8d0afb91ab991854632ba8bc700eb22db2b8a5f4f4e8e68b632a63ff39 100644 (file)
@@ -1 +1 @@
-redo-ifchange print-itered print-pooled test-vector
+redo-ifchange print-items print-itered test-vector
index dcefe26772cae9ca42d87f03396060492eaf67d9bdec2edce421059b2fbf6bcf..defcb85251b0380ce9310c92a2a845e7a1d77cb4048e5fd1767ab38eb239d6f4 100755 (executable)
@@ -1,3 +1,3 @@
 #!/bin/sh -e
 
-exec rm -f print-itered print-pooled test-vector
+exec rm -f print-items print-itered test-vector
index e8eac11b8d56ea1d0ac59fca4542c114baf4ebdd1c3984d39c057123d07c44b8..2b6e469220d51dbffc28b030d4428db5f9b04967813bccc8b55228f26d45f82d 100644 (file)
@@ -3,4 +3,4 @@ read CC <../conf/cc
 CFLAGS=$(cat ../conf/cflags)
 LDFLAGS=$(cat ../conf/ldflags)
 read PREFIX <../conf/prefix
-$CC $CFLAGS -I$PREFIX/include -o $3 $2.c $LDFLAGS -L$PREFIX/lib -lyac
+$CC $CFLAGS -I$PREFIX/include -o $3 $2.c $LDFLAGS -L$PREFIX/lib -lyac -lm
similarity index 85%
rename from cyac/cmd/print-pooled.c
rename to cyac/cmd/print-items.c
index dc273aae83b3b0e37620190c93743612233a92a9ec81bca2f27f7939e5b1c794..7811e298fde80733b33b0b469dedd971a51df91e5022c31cdc8b09f286407c13 100644 (file)
@@ -16,6 +16,7 @@
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <math.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -28,7 +29,7 @@
 #include <yac/dec.h>
 #include <yac/dectai.h>
 #include <yac/err.h>
-#include <yac/pool.h>
+#include <yac/items.h>
 
 #include "hex.c.in"
 
@@ -40,16 +41,19 @@ printIndent(const ptrdiff_t indent)
     }
 }
 
+static int OffDigits = 0;
+static char OffFmt[10] = {0};
+
 static enum YACErr
 printer(
-    const struct YACItemPool *pool,
+    const struct YACItems *items,
     ptrdiff_t idx,
     ptrdiff_t indent,
     ptrdiff_t listIdx,
     const char *mapKey)
 {
-    struct YACItem *item = &(pool->list[idx]);
-    printf("%04zd ", item->off);
+    struct YACItem *item = &(items->list[idx]);
+    printf(OffFmt, item->off);
     if (item->atom.typ == YACItemEOC) {
         indent--;
         assert(indent >= 0);
@@ -105,14 +109,16 @@ printer(
         idx = item->atom.val.first;
         listIdx = 0;
         while (idx != -1) {
-            err = printer(pool, idx, indent, listIdx, NULL);
+            err = printer(items, idx, indent, listIdx, NULL);
             if (err != YACErrNo) {
                 return err;
             }
-            idx = pool->list[idx].next;
+            idx = items->list[idx].next;
             listIdx++;
         }
-        fputs("     ", stdout);
+        for (int i = 0; i < OffDigits + 1; i++) {
+            fputs(" ", stdout);
+        }
         indent--;
         printIndent(indent);
         fputs("]\n", stdout);
@@ -124,16 +130,19 @@ printer(
         idx = item->atom.val.first;
         while (idx != -1) {
             str = strndup(
-                (const char *)(pool->list[idx].atom.val.buf), pool->list[idx].atom.len);
-            idx = pool->list[idx].next;
-            err = printer(pool, idx, indent, -1, str);
+                (const char *)(items->list[idx].atom.val.buf),
+                items->list[idx].atom.len);
+            idx = items->list[idx].next;
+            err = printer(items, idx, indent, -1, str);
             free(str);
             if (err != YACErrNo) {
                 return err;
             }
-            idx = pool->list[idx].next;
+            idx = items->list[idx].next;
+        }
+        for (int i = 0; i < OffDigits + 1; i++) {
+            fputs(" ", stdout);
         }
-        fputs("     ", stdout);
         indent--;
         printIndent(indent);
         fputs("}\n", stdout);
@@ -145,14 +154,16 @@ printer(
         idx++;
         listIdx = 0;
         while (idx != -1) {
-            err = printer(pool, idx, indent, listIdx, NULL);
+            err = printer(items, idx, indent, listIdx, NULL);
             if (err != YACErrNo) {
                 return err;
             }
-            idx = pool->list[idx].next;
+            idx = items->list[idx].next;
             listIdx++;
         }
-        fputs("     ", stdout);
+        for (int i = 0; i < OffDigits + 1; i++) {
+            fputs(" ", stdout);
+        }
         indent--;
         printIndent(indent);
         fputs("]\n", stdout);
@@ -256,15 +267,18 @@ main(int argc, char **argv)
         buf = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
     }
 
-    struct YACItemPool pool;
-    YACItemPoolInit(&pool);
+    OffDigits = (int)(1 + floor(log10((double)len)));
+    snprintf(OffFmt, sizeof OffFmt, "%%0%dzd ", OffDigits);
+
+    struct YACItems items;
+    YACItemsInit(&items);
     ptrdiff_t off = 0;
-    enum YACErr err = YACItemPoolParse(&pool, &off, buf, len);
+    enum YACErr err = YACItemsParse(&items, &off, buf, len);
     if (err != YACErrNo) {
         fprintf(stderr, "err: %d\n", err);
         return EXIT_FAILURE;
     }
-    err = printer(&pool, 0, 0, -1, NULL);
+    err = printer(&items, 0, 0, -1, NULL);
     if (err != YACErrNo) {
         fprintf(stderr, "err: %d\n", err);
         return EXIT_FAILURE;
index e8cb55072fe3be49b5f4cfb86f69fb248b1bb7dd9cee80b5587b0c0544669d03..839421ef5d8eba806f7f281dc1bc4fc5c7ba68607a4008ed70678e7ed5563b2c 100644 (file)
@@ -47,25 +47,25 @@ enum YACItemType {
 // @item YACItemList
 //     No additional fields are used, if parsed through
 //     @code{YACAtomDecode()}.
-//     If parsed through the @code{YACItemPoolParse()}, then
+//     If parsed through the @code{YACItemsParse()}, then
 //     @code{.len} contains the number of elements in a list,
-//     @code{.val.first} is the pool index of the first element.
+//     @code{.val.first} is the items pool index of the first element.
 //     It equals to -1, if list is empty.
 // @item YACItemMap
 //     No additional fields are used, if parsed through
 //     @code{YACAtomDecode()}.
-//     If parsed through the @code{YACItemPoolParse()}, then
+//     If parsed through the @code{YACItemsParse()}, then
 //     @code{.len} contains the number of elements in a map,
-//     @code{.val.first} is the pool index of the first element's key.
-//     Key's item @code{.next} points to the value, that points to the
-//     next key, and so on.
+//     @code{.val.first} is the items pool index of the first element's
+//     key. Key's item @code{.next} points to the value, that points to
+//     the next key, and so on.
 //     @code{.val.first} equals to -1, if map is empty.
 // @item YACItemBlob
 //     @code{.val.uint} contains the length of the chunk.
-//     If parsed through the @code{YACItemPoolParse()}, then
+//     If parsed through the @code{YACItemsParse()}, then
 //     @code{.len} contains the number of chunks, including the
 //     terminating binary string, that may be empty.
-//     @code{.val.first} is the pool index of the first chunk.
+//     @code{.val.first} is the items pool index of the first chunk.
 // @item YACItemFloat
 //     @code{.val.TODO} contains float's value.
 // @item YACItemTAI64
similarity index 64%
rename from cyac/pool.c
rename to cyac/items.c
index 7464ef92385c4a6ef37a43e60092f44e648ccdeed5cbd694cc324ec736fd353f..b2ddfd997871cabe155ca004c377e0cc1a710cfd4df86879f6b0306c467cc49a 100644 (file)
 
 #include "dec.h"
 #include "err.h"
-#include "pool.h"
+#include "items.h"
 
-static const ptrdiff_t yacPoolChunkLen = 32;
+static const ptrdiff_t yacItemsPoolGrowLen = 32;
 
 enum YACErr
-YACItemPoolInit(struct YACItemPool *pool)
+YACItemsInit(struct YACItems *items)
 {
-    pool->len = 0;
-    pool->cap = yacPoolChunkLen;
-    pool->list = calloc((size_t)(pool->cap), sizeof(struct YACItem));
-    if (pool->list == NULL) {
+    items->len = 0;
+    items->cap = yacItemsPoolGrowLen;
+    items->list = calloc((size_t)(items->cap), sizeof(struct YACItem));
+    if (items->list == NULL) {
         return YACErrNoMem;
     }
     return YACErrNo;
 }
 
 enum YACErr
-YACItemPoolGrow(struct YACItemPool *pool)
+yacItemsGrow(struct YACItems *items)
 {
-    pool->cap += yacPoolChunkLen;
-    if (pool->cap <= 0) {
+    items->cap += yacItemsPoolGrowLen;
+    if (items->cap <= 0) {
         return YACErrNoMem;
     }
-    const size_t size = (size_t)(pool->cap) * sizeof(struct YACItem);
-    pool->list = realloc(pool->list, size);
-    if (pool->list == NULL) {
+    const size_t size = (size_t)(items->cap) * sizeof(struct YACItem);
+    items->list = realloc(items->list, size);
+    if (items->list == NULL) {
         return YACErrNoMem;
     }
-    const size_t begin = (size_t)(pool->len) * sizeof(struct YACItem);
-    memset((unsigned char *)(pool->list) + begin, 0, size - begin);
+    const size_t begin = (size_t)(items->len) * sizeof(struct YACItem);
+    memset((unsigned char *)(items->list) + begin, 0, size - begin);
     return YACErrNo;
 }
 
 static enum YACErr
-yacItemPoolAdd(
-    struct YACItemPool *pool,
+yacItemsAdd(
+    struct YACItems *items,
     ptrdiff_t *off,
     const unsigned char *buf,
     const size_t len)
 {
     enum YACErr err = YACErrInvalid;
-    if (pool->len == pool->cap) {
-        err = YACItemPoolGrow(pool);
+    if (items->len == items->cap) {
+        err = yacItemsGrow(items);
         if (err != YACErrNo) {
             return err;
         }
     }
-    struct YACItem *item = &(pool->list[pool->len]);
+    struct YACItem *item = &(items->list[items->len]);
     item->next = -1;
     item->off = *off;
     err = YACAtomDecode(&(item->atom), buf + *off, len - (size_t)(*off));
@@ -75,70 +75,70 @@ yacItemPoolAdd(
         return err;
     }
     (*off) += item->atom.off;
-    pool->len++;
-    if (pool->len <= 0) {
+    items->len++;
+    if (items->len <= 0) {
         return YACErrNoMem;
     }
     return YACErrNo;
 }
 
 enum YACErr
-YACItemPoolParse(
-    struct YACItemPool *pool,
+YACItemsParse(
+    struct YACItems *items,
     ptrdiff_t *off,
     const unsigned char *buf,
     const size_t len)
 {
-    ptrdiff_t item = pool->len;
-    enum YACErr err = yacItemPoolAdd(pool, off, buf, len);
+    ptrdiff_t item = items->len;
+    enum YACErr err = yacItemsAdd(items, off, buf, len);
     if (err != YACErrNo) {
         return err;
     }
-    switch (pool->list[item].atom.typ) {
+    switch (items->list[item].atom.typ) {
     case YACItemList: {
-        pool->list[item].atom.val.first = item + 1;
-        pool->list[item].atom.len = 0;
+        items->list[item].atom.val.first = item + 1;
+        items->list[item].atom.len = 0;
         ptrdiff_t prev = -1;
         ptrdiff_t cur = -1;
         ptrdiff_t idx = item;
         for (;;) {
-            err = YACItemPoolParse(pool, off, buf, len);
+            err = YACItemsParse(items, off, buf, len);
             if (err != YACErrNo) {
                 return err;
             }
             cur = idx + 1;
-            if (pool->list[cur].atom.typ == YACItemEOC) {
-                if (pool->list[item].atom.len == 0) {
-                    pool->list[item].atom.val.first = -1;
+            if (items->list[cur].atom.typ == YACItemEOC) {
+                if (items->list[item].atom.len == 0) {
+                    items->list[item].atom.val.first = -1;
                 }
                 return YACErrNo;
             }
             if (prev != -1) {
-                pool->list[prev].next = cur;
+                items->list[prev].next = cur;
             }
             prev = cur;
-            idx = (pool->len) - 1;
-            pool->list[item].atom.len++;
+            idx = (items->len) - 1;
+            items->list[item].atom.len++;
         }
     }
     case YACItemMap: {
-        pool->list[item].atom.val.first = item + 1;
-        pool->list[item].atom.len = 0;
+        items->list[item].atom.val.first = item + 1;
+        items->list[item].atom.len = 0;
         ptrdiff_t idx = item;
         ptrdiff_t prev = -1;
         ptrdiff_t cur = -1;
         size_t prevKeyLen = 0;
         const unsigned char *prevKey = NULL;
         for (;;) {
-            err = YACItemPoolParse(pool, off, buf, len);
+            err = YACItemsParse(items, off, buf, len);
             if (err != YACErrNo) {
                 return err;
             }
             cur = idx + 1;
-            switch (pool->list[cur].atom.typ) {
+            switch (items->list[cur].atom.typ) {
             case YACItemEOC:
-                if (pool->list[item].atom.len == 0) {
-                    pool->list[item].atom.val.first = -1;
+                if (items->list[item].atom.len == 0) {
+                    items->list[item].atom.val.first = -1;
                 }
                 return YACErrNo;
             case YACItemStr:
@@ -159,51 +159,51 @@ YACItemPoolParse(
             default:
                 return YACErrMapBadKey;
             }
-            if (pool->list[cur].atom.len == 0) {
+            if (items->list[cur].atom.len == 0) {
                 return YACErrMapBadKey;
             }
-            if (pool->list[cur].atom.len < prevKeyLen) {
+            if (items->list[cur].atom.len < prevKeyLen) {
                 return YACErrMapUnordered;
             }
-            if ((pool->list[cur].atom.len == prevKeyLen) &&
-                (memcmp(prevKey, pool->list[cur].atom.val.buf, prevKeyLen) >= 0)) {
+            if ((items->list[cur].atom.len == prevKeyLen) &&
+                (memcmp(prevKey, items->list[cur].atom.val.buf, prevKeyLen) >= 0)) {
                 return YACErrMapUnordered;
             }
-            prevKeyLen = pool->list[cur].atom.len;
-            prevKey = pool->list[cur].atom.val.buf;
+            prevKeyLen = items->list[cur].atom.len;
+            prevKey = items->list[cur].atom.val.buf;
             if (prev != -1) {
-                pool->list[prev].next = cur;
+                items->list[prev].next = cur;
             }
             prev = cur;
-            idx = (pool->len) - 1;
-            err = YACItemPoolParse(pool, off, buf, len);
+            idx = (items->len) - 1;
+            err = YACItemsParse(items, off, buf, len);
             if (err != YACErrNo) {
                 return err;
             }
             cur = idx + 1;
-            if (pool->list[cur].atom.typ == YACItemEOC) {
+            if (items->list[cur].atom.typ == YACItemEOC) {
                 return YACErrMapNoVal;
             }
-            pool->list[prev].next = cur;
+            items->list[prev].next = cur;
             prev = cur;
-            idx = (pool->len) - 1;
-            pool->list[item].atom.len++;
+            idx = (items->len) - 1;
+            items->list[item].atom.len++;
         }
     }
     case YACItemBlob: {
-        pool->list[item].atom.len = 0;
-        const ptrdiff_t chunkLen = (ptrdiff_t)(pool->list[item].atom.val.uint);
+        items->list[item].atom.len = 0;
+        const ptrdiff_t chunkLen = (ptrdiff_t)(items->list[item].atom.val.uint);
         ptrdiff_t idx = item;
         ptrdiff_t prev = -1;
         ptrdiff_t cur = -1;
         bool eoc = false;
         while (!eoc) {
-            err = YACItemPoolParse(pool, off, buf, len);
+            err = YACItemsParse(items, off, buf, len);
             if (err != YACErrNo) {
                 return err;
             }
             cur = idx + 1;
-            struct YACAtom *atom = &(pool->list[cur].atom);
+            struct YACAtom *atom = &(items->list[cur].atom);
             switch (atom->typ) {
             case YACItemNIL:
                 if (((ptrdiff_t)len - *off) <= chunkLen) {
@@ -238,11 +238,11 @@ YACItemPoolParse(
                 return YACErrBlobBadAtom;
             }
             if (prev != -1) {
-                pool->list[prev].next = cur;
+                items->list[prev].next = cur;
             }
             prev = cur;
-            idx = (pool->len) - 1;
-            pool->list[item].atom.len++;
+            idx = (items->len) - 1;
+            items->list[item].atom.len++;
         }
         break;
     }
similarity index 80%
rename from cyac/pool.h
rename to cyac/items.h
index 04b3927be0a1499e198d56525abed88e2870e4f7753d945083c5aa503d17e8ed..18c26d8c6e21ae08a087412a5a99963afbf3017718b1933e96e78daedbcd9d2b 100644 (file)
@@ -25,28 +25,28 @@ struct YACItem {
     struct YACAtom atom;
 };
 
-// @deftypevar struct YACItemPool
-// Pool contains concatenated @code{YACItem}s. Item's @{.next} can be
-// used as an index in that pool: @code{pool->list[item.next]}.
+// @deftypevar struct YACItems
+// Items pool contains concatenated @code{YACItem}s. Item's @{.next} can
+// be used as an index in that pool: @code{items->list[item.next]}.
 // @strong{Remember} that if there is not enough room for the next item,
 // then @code{.list} is reallocated, so previous pointers to the items
 // may become invalid! Using their indices will be safer.
 // @end deftypevar
-struct YACItemPool {
+struct YACItems {
     struct YACItem *list;
     ptrdiff_t len;
     ptrdiff_t cap;
 };
 
 enum YACErr
-YACItemPoolInit(struct YACItemPool *);
+YACItemsInit(struct YACItems *);
 
 enum YACErr
-YACItemPoolGrow(struct YACItemPool *);
+yacItemsGrow(struct YACItems *);
 
 enum YACErr
-YACItemPoolParse(
-    struct YACItemPool *,
+YACItemsParse(
+    struct YACItems *,
     ptrdiff_t *off,
     const unsigned char *buf,
     const size_t len);
index 078f6594d9d64fd9cabfd26f22c035a16a3f2a4cebe9b106f91a85373e9639f8..d732c982d2a068691ef5a6c36f03445828caf6ce77e28e1e0f3a2003ddd1e3b7 100644 (file)
@@ -3,8 +3,8 @@ dectai.o
 enc.o
 enctai.o
 frombe.o
+items.o
 iter.o
 leapsecs.o
-pool.o
 tobe.o
 utf8.o