From d043bad63ded27b71bf2762f6c07ca1186058cf2 Mon Sep 17 00:00:00 2001 From: June McEnroe Date: Sun, 11 Sep 2022 17:28:19 -0400 Subject: Set pointer to Entry in Cursor This feels a little redundant but the API makes sense, I think? --- cache.c | 79 +++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 31 deletions(-) (limited to 'cache.c') diff --git a/cache.c b/cache.c index 30ebef4..970bd9c 100644 --- a/cache.c +++ b/cache.c @@ -107,15 +107,15 @@ static struct Node *insert(bool touch, uint id, const char *key) { } } -struct Entry *cacheInsert(bool touch, uint id, const char *key) { - return &insert(touch, id, key)->entry; -} - const struct Entry *cacheGet(uint id, const char *key) { struct Node *node = find(id, key); return (node ? &node->entry : &DefaultEntry); } +struct Entry *cacheInsert(bool touch, uint id, const char *key) { + return &insert(touch, id, key)->entry; +} + void cacheReplace(bool touch, const char *old, const char *new) { struct Node *next = NULL; for (struct Node *node = head; node; node = next) { @@ -128,6 +128,32 @@ void cacheReplace(bool touch, const char *old, const char *new) { } } +void cacheRemove(uint id, const char *key) { + gen++; + struct Node *next = NULL; + for (struct Node *node = head; node; node = next) { + next = node->next; + if (id && node->id != id) continue; + if (strcmp(node->key, key)) continue; + detach(node); + free(node->key); + free(node); + if (id) break; + } +} + +void cacheClear(uint id) { + gen++; + struct Node *next = NULL; + for (struct Node *node = head; node; node = next) { + next = node->next; + if (node->id != id) continue; + detach(node); + free(node->key); + free(node); + } +} + const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix) { size_t len = strlen(prefix); if (curs->gen != gen) curs->node = NULL; @@ -138,6 +164,7 @@ const char *cacheComplete(struct Cursor *curs, uint id, const char *prefix) { ) { if (curs->node->id && curs->node->id != id) continue; if (strncasecmp(curs->node->key, prefix, len)) continue; + curs->entry = &curs->node->entry; return curs->node->key; } return NULL; @@ -152,12 +179,27 @@ const char *cacheSearch(struct Cursor *curs, uint id, const char *substr) { ) { if (curs->node->id && curs->node->id != id) continue; if (!strstr(curs->node->key, substr)) continue; + curs->entry = &curs->node->entry; return curs->node->key; } return NULL; } -uint cacheID(struct Cursor *curs, const char *key) { +const char *cacheNextKey(struct Cursor *curs, uint id) { + if (curs->gen != gen) curs->node = NULL; + for ( + curs->gen = gen, curs->node = (curs->node ? curs->node->next : head); + curs->node; + curs->node = curs->node->next + ) { + if (curs->node->id != id) continue; + curs->entry = &curs->node->entry; + return curs->node->key; + } + return NULL; +} + +uint cacheNextID(struct Cursor *curs, const char *key) { if (curs->gen != gen) curs->node = NULL; for ( curs->gen = gen, curs->node = (curs->node ? curs->node->next : head); @@ -166,6 +208,7 @@ uint cacheID(struct Cursor *curs, const char *key) { ) { if (!curs->node->id) continue; if (strcmp(curs->node->key, key)) continue; + curs->entry = &curs->node->entry; return curs->node->id; } return None; @@ -181,29 +224,3 @@ void cacheAccept(struct Cursor *curs) { void cacheReject(struct Cursor *curs) { curs->node = NULL; } - -void cacheRemove(uint id, const char *key) { - gen++; - struct Node *next = NULL; - for (struct Node *node = head; node; node = next) { - next = node->next; - if (id && node->id != id) continue; - if (strcmp(node->key, key)) continue; - detach(node); - free(node->key); - free(node); - if (id) break; - } -} - -void cacheClear(uint id) { - gen++; - struct Node *next = NULL; - for (struct Node *node = head; node; node = next) { - next = node->next; - if (node->id != id) continue; - detach(node); - free(node->key); - free(node); - } -} -- cgit 1.4.1-2-gfad0