tllist: import static copy under subprojects
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Daniel Eklöf · committed by Daniel Eklöf · parent 0990cc5 · Browse files · 5 files·+603 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+603 added−0 removed
- other:+603 added−0 removed
Provenance
- Commit
f75cbf4- Tree
ade768b- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
0990cc5
5 files changed+603 added−0 removed
added:subprojects/tllist/LICENSE+21 added
1+MIT License2+3+Copyright (c) 2018 Daniel Eklöf4+5+Permission is hereby granted, free of charge, to any person obtaining a copy6+of this software and associated documentation files (the "Software"), to deal7+in the Software without restriction, including without limitation the rights8+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9+copies of the Software, and to permit persons to whom the Software is10+furnished to do so, subject to the following conditions:11+12+The above copyright notice and this permission notice shall be included in all13+copies or substantial portions of the Software.14+15+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21+SOFTWARE.added:subprojects/tllist/meson.build+5 added
1+project('tllist', 'c', version: '1.0.0', license: 'MIT', meson_version: '>=0.50.0')2+tllist = declare_dependency(include_directories: '.')3+4+unittest = executable('unittest', 'test.c', dependencies: [tllist])5+test('unittest', unittest)added:subprojects/tllist/README.md+301 added
1+# tllist2+3+**tllist** is a *T*yped *L*inked *L*ist C header file only4+library implemented using pre-processor macros.5+6+7+1. [Description](#description)8+1. [Usage](#usage)9+ 1. [Declaring a variable](#declaring-a-variable)10+ 1. [Adding items - basic](#adding-items-basic)11+ 1. [List length](#list-length)12+ 1. [Accessing items](#accessing-items)13+ 1. [Iterating](#iterating)14+ 1. [Removing items - basic](#removing-items-basic)15+ 1. [Adding items - advanced](#adding-items-advanced)16+ 1. [Removing items - advanced](#removing-items-advanced)17+ 1. [Freeing](#freeing)18+1. [Integrating](#integrating)19+ 1. [Meson](#meson)20+1. [API](#api)21+ 1. [Cheat sheet](#cheat-sheet)22+23+24+## Description25+26+Most C implementations of linked list are untyped. That is, their data27+carriers are typically `void *`. This is error prone since your28+compiler will not be able to help you correct your mistakes (_oh, was29+it pointer-to-a-pointer... I though it was just a pointer..._).30+31+**tllist** addresses this by using pre-processor macros to implement32+dynamic types, where the data carrier is typed to whatever you want;33+both **primitive** data types are supported as well as aggregated ones34+such as **structs**, **enums** and **unions**.35+36+Being a double-linked list, most operations are constant in time37+(including pushing and popping both to/from front and back).38+39+The memory overhead is fairly small; each item carries, besides its40+data, a _prev_ and _next_ pointer (i.e. a constant 16 byte overhead41+per item on 64-bit architectures).42+43+The list itself has two _head_ and _tail_ pointers, plus a _length_44+variable (typically 8 bytes on 64-bit architectures) to make list45+length lookup constant in time.46+47+Thus, assuming 64-bit pointers (and a 64-bit `size_t` type), the total48+overhead is `3*8 + n*2*8` bytes.49+50+51+## Usage52+53+### Declaring a variable54+55+1. **Declare a variable**56+57+ ```c58+ /* Declare a variable using an anonymous type */59+ tll(int) an_integer_list = tll_init();60+ ```61+62+63+2. **Typedef**64+65+ ```c66+ /* First typedef the list type */67+ typedef tll(int) an_integer_list_t;68+69+ /* Then declare a variable using that typedef */70+ an_integer_list_t an_integer_list = tll_init();71+ ```72+73+3. **Named struct**74+75+ ```c76+ /* First declare named struct */77+ tll(int, an_integer_list);78+79+ /* Then declare a variable using that named struct */80+ struct an_integer_list an_integer_list = tll_init();81+ ```82+83+### Adding items - basic84+85+Use `tll_push_back()` or `tll_push_front()` to add elements to the86+back or front of the list.87+88+```c89+tll_push_back(an_integer_list, 4711);90+tll_push_front(an_integer_list, 1234);91+```92+93+94+### List length95+96+`tll_length()` returns the length (number of items) in a list.97+98+```c99+tll_push_back(an_integer_list, 1234);100+tll_push_back(an_integer_list, 5678);101+printf("length: %zu\n", tll_length(an_integer_list));102+```103+104+Outputs:105+106+ length: 2107+108+109+### Accessing items110+111+For the front and back items, you can use `tll_front()` and112+`tll_back()` respectively. For any other item in the list, you need to113+iterate the list and find the item yourself.114+115+```c116+tll_push_back(an_integer_list, 1234);117+tll_push_back(an_integer_list, 5555);118+tll_push_back(an_integer_list, 6789);119+120+printf("front: %d\n", tll_front(an_integer_list));121+printf("back: %d\n", tll_back(an_integer_list));122+```123+124+Outputs:125+126+ front: 1234127+ back: 6789128+129+130+### Iterating131+132+You can iterate the list either forward or backwards, using133+`tll_foreach()` and `tll_rforeach()` respectively.134+135+The `it` variable should be treated as an opaque iterator type, where136+`it->item` is the item.137+138+In reality, it is simply a pointer to the linked list entry, and since139+tllist is a header-only implementation, you do have access to e.g. the140+next/prev pointers. There should not be any need to access anything141+except `item` however.142+143+Note that `it` can be named anything.144+145+```c146+tll_push_back(an_integer_list, 1);147+tll_push_back(an_integer_list, 2);148+149+tll_foreach(an_integer_list, it) {150+ printf("forward: %d\n", it->item);151+}152+153+tll_rforeach(an_integer_list, it) {154+ printf("reverse: %d\n", it->item);155+}156+```157+158+Outputs:159+160+ forward: 1161+ forward: 2162+ reverse: 2163+ reverse: 1164+165+166+### Removing items - basic167+168+`tll_pop_front()` and `tll_pop_back()` removes the front/back item and169+returns it.170+171+```c172+tll_push_back(an_integer_list, 1234);173+tll_push_back(an_integer_list, 5678);174+175+printf("front: %d\n", tll_pop_front(an_integer_list));176+printf("back: %d\n", tll_pop_back(an_integer_list));177+printf("length: %zu\n", tll_length(an_integer_list));178+```179+180+Outputs:181+182+ front: 1234183+ back: 5678184+ length: 0185+186+187+### Adding items - advanced188+189+Given an iterator, you can insert new items before or after that190+iterator, using `tll_insert_before()` and `tll_insert_after()`.191+192+```c193+tll_foreach(an_integer_list, it) {194+ if (it->item == 1234) {195+ tll_insert_before(an_integer_list, it, 7777);196+ break;197+ }198+}199+```200+201+Q: Why do I have to pass **both** the _list_ and the _iterator_ to202+ `tll_insert_before()`?203+204+A: If not, **each** element in the list would have to contain a205+ pointer to the owning list, which would significantly increase the206+ overhead.207+208+209+### Removing items - advanced210+211+Similar to how you can add items while iterating a list, you can also212+remove them.213+214+Note that the `*foreach()` functions are **safe** in this regard - it215+is perfectly OK to remove the "current" item.216+217+```c218+tll_foreach(an_integer_list, it) {219+ if (it->item.delete_me)220+ tll_remove(an_integer_list, it);221+}222+```223+224+To make it slightly easier to handle cases where the item _itself_225+must be free:d as well, there is also `tll_remove_and_free()`. It226+works just like `tll_remove()`, but takes an additional argument; a227+callback that will be called for each item.228+229+```c230+tll(int *) int_p_list = tll_init();231+232+int *a = malloc(sizeof(*a));233+int *b = malloc(sizeof(*b));234+235+*a = 1234;236+*b = 5678;237+238+tll_push_back(int_p_list, a);239+tll_push_back(int_p_list, b);240+241+tll_foreach(int_p_list, it) {242+ tll_remove_and_free(int_p_list, it, free);243+}244+```245+246+247+### Freeing248+249+To remove **all** items, use `tll_free()`, or250+`tll_free_and_free()`. These are just convenience functions and251+calling these are equivalent to:252+253+```c254+tll_foreach(an_integer_list, it) {255+ tll_remove(an_integer_list, it);256+}257+```258+259+Note that there is no need to call `tll_free()` on an empty260+(`tll_length(list) == 0`) list.261+262+263+## Integrating264+265+The easiest way may be to simply copy `tllist.h` into your266+project. But see sections below for other ways.267+268+269+### Meson270+271+You can use tllist as a subproject. In your main project's272+`meson.build`, to something like:273+274+```meson275+tllist = subproject('tllist').get_variable('tllist')276+executable('you-executable', ..., dependencies: [tllist])277+```278+279+280+## API281+282+### Cheat sheet283+284+| Function | Description | Context | Complexity |285+|-------------------------------------|-------------------------------------------------------|--------------------|-----------:|286+| `list = tll_init()` | initialize a new tllist variable to an empty list | Variable init | O(1) |287+| `tll_length(list)` | returns the length (number of items) of a list | | O(1) |288+| `tll_push_front(list, item)` | inserts _item_ at the beginning of the list | | O(1) |289+| `tll_push_back(list, item)` | inserts _item_ at the end of the list | | O(1) |290+| `tll_front(list)` | returns the first item in the list | | O(1) |291+| `tll_back(list)` | returns the last item in the list | | O(1) |292+| `tll_pop_front(list)` | removes and returns the first item in the list | | O(1) |293+| `tll_pop_back(list)` | removes and returns the last item in the list | | O(1) |294+| `tll_foreach(list, it)` | iterates the list from the beginning to the end | | O(n) |295+| `tll_rforeach(list, it)` | iterates the list from the end to the beginning | | O(n) |296+| `tll_insert_before(list, it, item)` | inserts _item_ before _it_. | `tll_(r)foreach()` | O(1) |297+| `tll_insert_after(list, it, item)` | inserts _item_ after _it_. | `tll_(r)foreach()` | O(1) |298+| `tll_remove(list, it)` | removes _it_ from the list. | `tll_(r)foreach()` | O(1) |299+| `tll_remove_and_free(list, it, cb)` | removes _it_ from the list, and calls `cb(it->item)`. | `tll_(r)foreach()` | O(1) |300+| `tll_free(list)` | removes **all** items from the list | | O(n) |301+| `tll_free_and_free(list, cb)` | removes **all** items from the list, and calls `cb(it->item)` for each item. | | O(n) |added:subprojects/tllist/test.c+96 added
1+#undef NDEBUG2+#include <stdlib.h>3+#include <string.h>4+#include <stdbool.h>5+#include <assert.h>6+7+#include <tllist.h>8+9+int10+main(int argc, const char *const *argv)11+{12+ tll(int) l = tll_init();13+ assert(tll_length(l) == 0);14+15+ /* push back */16+ tll_push_back(l, 123); assert(tll_length(l) == 1);17+ tll_push_back(l, 456); assert(tll_length(l) == 2);18+ tll_push_back(l, 789); assert(tll_length(l) == 3);19+20+ assert(tll_front(l) == 123);21+ assert(tll_back(l) == 789);22+23+ /* push front */24+ tll_push_front(l, 0xabc); assert(tll_length(l) == 4);25+26+ assert(tll_front(l) == 0xabc);27+ assert(tll_back(l) == 789);28+29+ /* Pop back */30+ assert(tll_pop_back(l) == 789);31+ assert(tll_back(l) == 456);32+33+ /* Pop front */34+ assert(tll_pop_front(l) == 0xabc);35+ assert(tll_front(l) == 123);36+37+ /* foreach */38+ assert(tll_length(l) == 2);39+40+ int seen[tll_length(l)];41+ memset(seen, 0, tll_length(l) * sizeof(seen[0]));42+43+ size_t count = 0;44+ tll_foreach(l, it)45+ seen[count++] = it->item;46+47+ assert(count == tll_length(l));48+ assert(seen[0] == 123);49+ assert(seen[1] == 456);50+51+ /* rforeach */52+ memset(seen, 0, tll_length(l) * sizeof(seen[0]));53+ count = 0;54+ tll_rforeach(l, it)55+ seen[count++] = it->item;56+57+ assert(count == tll_length(l));58+ assert(seen[0] == 456);59+ assert(seen[1] == 123);60+61+ /* remove */62+ tll_push_back(l, 789);63+ tll_foreach(l, it) {64+ if (it->item > 123 && it->item < 789)65+ tll_remove(l, it);66+ }67+ assert(tll_length(l) == 2);68+ assert(tll_front(l) == 123);69+ assert(tll_back(l) == 789);70+71+ /* insert before */72+ tll_foreach(l, it) {73+ if (it->item == 123)74+ tll_insert_before(l, it, 0xabc);75+ }76+ assert(tll_length(l) == 3);77+ assert(tll_front(l) == 0xabc);78+ assert(tll_back(l) == 789);79+80+ /* insert after */81+ tll_foreach(l, it) {82+ if (it->item == 789)83+ tll_insert_after(l, it, 999);84+ }85+ assert(tll_length(l) == 4);86+ assert(tll_front(l) == 0xabc);87+ assert(tll_back(l) == 999);88+89+ /* free */90+ tll_free(l);91+ assert(tll_length(l) == 0);92+ assert(l.head == NULL);93+ assert(l.tail == NULL);94+95+ return EXIT_SUCCESS;96+}added:subprojects/tllist/tllist.h+180 added
1+#pragma once2+3+#include <stdlib.h>4+#include <stddef.h>5+#include <assert.h>6+7+#define TLL_PASTE2( a, b) a##b8+#define TLL_PASTE( a, b) TLL_PASTE2( a, b)9+10+/* Utility macro to generate a list element struct with a unique struct tag */11+#define TLL_UNIQUE_INNER_STRUCT(TYPE, ID) \12+ struct TLL_PASTE(__tllist_ , ID) { \13+ TYPE item; \14+ struct TLL_PASTE(__tllist_, ID) *prev; \15+ struct TLL_PASTE(__tllist_, ID) *next; \16+ } *head, *tail;17+18+/*19+ * Defines a new typed-list type, or directly instantiate a typed-list variable20+ *21+ * Example a, declare a variable (list of integers):22+ * tll(int) my_list;23+ *24+ * Example b, declare a type, and then use the type:25+ * tll(int, my_list_type);26+ * struct my_list_type my_list;27+ */28+#define tll(TYPE, ...) \29+ struct __VA_ARGS__ { \30+ TLL_UNIQUE_INNER_STRUCT(TYPE, __COUNTER__) \31+ size_t length; \32+ }33+34+/* Initializer: tll(int) my_list = tll_init(); */35+#define tll_init() {.head = NULL, .tail = NULL, .length = 0}36+37+/* Length/size of list: printf("size: %zu\n", tll_length(my_list)); */38+#define tll_length(list) (list).length39+40+/* Adds a new item to the back of the list */41+#define tll_push_back(list, new_item) \42+ do { \43+ tll_insert_after(list, (list).tail, new_item); \44+ if ((list).head == NULL) \45+ (list).head = (list).tail; \46+ } while (0)47+48+/* Adds a new item to the front of the list */49+#define tll_push_front(list, new_item) \50+ do { \51+ tll_insert_before(list, (list).head, new_item); \52+ if ((list).tail == NULL) \53+ (list).tail = (list).head; \54+ } while (0)55+56+/*57+ * Iterates the list. <it> is an iterator pointer. You can access the58+ * list item with ->item:59+ *60+ * tll(int) my_list = vinit();61+ * tll_push_back(my_list, 5);62+ *63+ * tll_foreach(my_list i) {64+ * printf("%d\n", i->item);65+ * }66+*/67+#define tll_foreach(list, it) \68+ for (__typeof__(*(list).head) *it = (list).head, \69+ *it_next = it != NULL ? it->next : NULL; \70+ it != NULL; \71+ it = it_next, \72+ it_next = it_next != NULL ? it_next->next : NULL)73+74+/* Same as tll_foreach(), but iterates backwards */75+#define tll_rforeach(list, it) \76+ for (__typeof__(*(list).tail) *it = (list).tail, \77+ *it_prev = it != NULL ? it->prev : NULL; \78+ it != NULL; \79+ it = it_prev, \80+ it_prev = it_prev != NULL ? it_prev->prev : NULL)81+82+/*83+ * Inserts a new item after <it>, which is an iterator. I.e. you can84+ * only call this from inside a tll_foreach() or tll_rforeach() loop.85+ */86+#define tll_insert_after(list, it, new_item) \87+ do { \88+ __typeof__((list).head) __e = malloc(sizeof(*__e)); \89+ __e->item = (new_item); \90+ __e->prev = (it); \91+ __e->next = (it) != NULL ? (it)->next : NULL; \92+ if ((it) != NULL) { \93+ if ((it)->next != NULL) \94+ (it)->next->prev = __e; \95+ (it)->next = __e; \96+ } \97+ if ((it) == (list).tail) \98+ (list).tail = __e; \99+ (list).length++; \100+ } while (0)101+102+/*103+ * Inserts a new item before <it>, which is an iterator. I.e. you can104+ * only call this from inside a tll_foreach() or tll_rforeach() loop.105+ */106+#define tll_insert_before(list, it, new_item) \107+ do { \108+ __typeof__((list).head) __e = malloc(sizeof(*__e)); \109+ __e->item = (new_item); \110+ __e->prev = (it) != NULL ? (it)->prev : NULL; \111+ __e->next = (it); \112+ if ((it) != NULL) { \113+ if ((it)->prev != NULL) \114+ (it)->prev->next = __e; \115+ (it)->prev = __e; \116+ } \117+ if ((it) == (list).head) \118+ (list).head = __e; \119+ (list).length++; \120+ } while (0)121+122+/*123+ * Removes an entry from the list. <it> is an iterator. I.e. you can124+ * only call this from inside a tll_foreach() or tll_rforeach() loop.125+ */126+#define tll_remove(list, it) \127+ do { \128+ assert((list).length > 0); \129+ __typeof__((list).head) __prev = it->prev; \130+ __typeof__((list).head) __next = it->next; \131+ if (__prev != NULL) \132+ __prev->next = __next; \133+ else \134+ (list).head = __next; \135+ if (__next != NULL) \136+ __next->prev = __prev; \137+ else \138+ (list).tail = __prev; \139+ free(it); \140+ (list).length--; \141+ } while (0)142+143+/* Same as tll_remove(), but calls free_callback(it->item) */144+#define tll_remove_and_free(list, it, free_callback) \145+ do { \146+ free_callback((it)->item); \147+ tll_remove((list), (it)); \148+ } while (0)149+150+#define tll_front(list) (list).head->item151+#define tll_back(list) (list).tail->item152+153+/*154+ * Removes the first element from the list, and returns it (note:155+ * returns the *actual* item, not an iterator.156+ */157+#define tll_pop_front(list) \158+ ({__typeof__((list).head) it = (list).head; \159+ __typeof__((list).head->item) __ret = it->item; \160+ tll_remove((list), it); \161+ __ret; \162+ })163+164+/* Same as tll_pop_front(), but returns/removes the *last* element */165+#define tll_pop_back(list) \166+ ({__typeof__((list).tail) it = (list).tail; \167+ __typeof__((list).tail->item) __ret = it->item; \168+ tll_remove((list), it); \169+ __ret; \170+ })171+172+/* Frees the list. This call is *not* needed if the list is already empty. */173+#define tll_free(list) \174+ tll_foreach(list, __it) \175+ tll_remove(list, __it)176+177+/* Same as tll_free(), but also calls free_callback(item) for every item */178+#define tll_free_and_free(list, free_callback) \179+ tll_foreach(list, __it) \180+ tll_remove_and_free(list, __it, free_callback)