diff options
author | Viktor Söderqvist <viktor.soderqvist@est.tech> | 2021-09-14 16:48:06 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-14 17:48:06 +0300 |
commit | ea36d4de17101f05b03d267a4afbae0f7b33a27c (patch) | |
tree | c51e89d23d286f768314a9f9e4a3cdc27c3098c7 /src/quicklist.c | |
parent | 1376d83363cf0e9c9f872762854518b16d8cedef (diff) | |
download | redis-ea36d4de17101f05b03d267a4afbae0f7b33a27c.tar.gz |
Modules: Add remaining list API functions (#8439)
List functions operating on elements by index:
* RM_ListGet
* RM_ListSet
* RM_ListInsert
* RM_ListDelete
Iteration is done using a simple for loop over indices.
The index based functions use an internal iterator as an optimization.
This is explained in the docs:
```
* Many of the list functions access elements by index. Since a list is in
* essence a doubly-linked list, accessing elements by index is generally an
* O(N) operation. However, if elements are accessed sequentially or with
* indices close together, the functions are optimized to seek the index from
* the previous index, rather than seeking from the ends of the list.
*
* This enables iteration to be done efficiently using a simple for loop:
*
* long n = RM_ValueLength(key);
* for (long i = 0; i < n; i++) {
* RedisModuleString *elem = RedisModule_ListGet(key, i);
* // Do stuff...
* }
```
Diffstat (limited to 'src/quicklist.c')
-rw-r--r-- | src/quicklist.c | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/src/quicklist.c b/src/quicklist.c index ec5409533..c63b35468 100644 --- a/src/quicklist.c +++ b/src/quicklist.c @@ -673,6 +673,15 @@ void quicklistDelEntry(quicklistIter *iter, quicklistEntry *entry) { * quicklistNext() will jump to the next node. */ } +/* Replace quicklist entry by 'data' with length 'sz'. */ +void quicklistReplaceEntry(quicklist *quicklist, quicklistEntry *entry, + void *data, int sz) { + /* quicklistNext() and quicklistIndex() provide an uncompressed node */ + entry->node->zl = ziplistReplace(entry->node->zl, entry->zi, data, sz); + quicklistNodeUpdateSz(entry->node); + quicklistCompress(quicklist, entry->node); +} + /* Replace quicklist entry at offset 'index' by 'data' with length 'sz'. * * Returns 1 if replace happened. @@ -681,10 +690,7 @@ int quicklistReplaceAtIndex(quicklist *quicklist, long index, void *data, int sz) { quicklistEntry entry; if (likely(quicklistIndex(quicklist, index, &entry))) { - /* quicklistIndex provides an uncompressed node */ - entry.node->zl = ziplistReplace(entry.node->zl, entry.zi, data, sz); - quicklistNodeUpdateSz(entry.node); - quicklistCompress(quicklist, entry.node); + quicklistReplaceEntry(quicklist, &entry, data, sz); return 1; } else { return 0; @@ -1189,6 +1195,11 @@ int quicklistNext(quicklistIter *iter, quicklistEntry *entry) { } } +/* Sets the direction of a quicklist iterator. */ +void quicklistSetDirection(quicklistIter *iter, int direction) { + iter->direction = direction; +} + /* Duplicate the quicklist. * On success a copy of the original quicklist is returned. * |