Skip to content
Myr

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

5 files+603 added−0 removed

added:subprojects/tllist/LICENSE+21 added
1MIT License23Copyright (c) 2018 Daniel Eklöf45Permission is hereby granted, free of charge, to any person obtaining a copy6of this software and associated documentation files (the "Software"), to deal7in the Software without restriction, including without limitation the rights8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9copies of the Software, and to permit persons to whom the Software is10furnished to do so, subject to the following conditions:1112The above copyright notice and this permission notice shall be included in all13copies or substantial portions of the Software.1415THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21SOFTWARE.
added:subprojects/tllist/meson.build+5 added
1project('tllist', 'c', version: '1.0.0', license: 'MIT', meson_version: '>=0.50.0')2tllist = declare_dependency(include_directories: '.')34unittest = executable('unittest', 'test.c', dependencies: [tllist])5test('unittest', unittest)
added:subprojects/tllist/README.md+301 added
1# tllist23**tllist** is a *T*yped *L*inked *L*ist C header file only4library implemented using pre-processor macros.5671. [Description](#description)81. [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)181. [Integrating](#integrating)19 1. [Meson](#meson)201. [API](#api)21 1. [Cheat sheet](#cheat-sheet)222324## Description2526Most C implementations of linked list are untyped. That is, their data27carriers are typically `void *`. This is error prone since your28compiler will not be able to help you correct your mistakes (_oh, was29it pointer-to-a-pointer... I though it was just a pointer..._).3031**tllist** addresses this by using pre-processor macros to implement32dynamic types, where the data carrier is typed to whatever you want;33both **primitive** data types are supported as well as aggregated ones34such as **structs**, **enums** and **unions**.3536Being a double-linked list, most operations are constant in time37(including pushing and popping both to/from front and back).3839The memory overhead is fairly small; each item carries, besides its40data, a _prev_ and _next_ pointer (i.e. a constant 16 byte overhead41per item on 64-bit architectures).4243The list itself has two _head_ and _tail_ pointers, plus a _length_44variable (typically 8 bytes on 64-bit architectures) to make list45length lookup constant in time.4647Thus, assuming 64-bit pointers (and a 64-bit `size_t` type), the total48overhead is `3*8 + n*2*8` bytes.495051## Usage5253### Declaring a variable54551. **Declare a variable**5657 ```c58 /* Declare a variable using an anonymous type */59 tll(int) an_integer_list = tll_init();60 ```6162632. **Typedef**6465 ```c66 /* First typedef the list type */67 typedef tll(int) an_integer_list_t;6869 /* Then declare a variable using that typedef */70 an_integer_list_t an_integer_list = tll_init();71 ```72733. **Named struct**7475 ```c76 /* First declare named struct */77 tll(int, an_integer_list);7879 /* Then declare a variable using that named struct */80 struct an_integer_list an_integer_list = tll_init();81 ```8283### Adding items - basic8485Use `tll_push_back()` or `tll_push_front()` to add elements to the86back or front of the list.8788```c89tll_push_back(an_integer_list, 4711);90tll_push_front(an_integer_list, 1234);91```929394### List length9596`tll_length()` returns the length (number of items) in a list.9798```c99tll_push_back(an_integer_list, 1234);100tll_push_back(an_integer_list, 5678);101printf("length: %zu\n", tll_length(an_integer_list));102```103104Outputs:105106 length: 2107108109### Accessing items110111For the front and back items, you can use `tll_front()` and112`tll_back()` respectively. For any other item in the list, you need to113iterate the list and find the item yourself.114115```c116tll_push_back(an_integer_list, 1234);117tll_push_back(an_integer_list, 5555);118tll_push_back(an_integer_list, 6789);119120printf("front: %d\n", tll_front(an_integer_list));121printf("back: %d\n", tll_back(an_integer_list));122```123124Outputs:125126 front: 1234127 back: 6789128129130### Iterating131132You can iterate the list either forward or backwards, using133`tll_foreach()` and `tll_rforeach()` respectively.134135The `it` variable should be treated as an opaque iterator type, where136`it->item` is the item.137138In reality, it is simply a pointer to the linked list entry, and since139tllist is a header-only implementation, you do have access to e.g. the140next/prev pointers. There should not be any need to access anything141except `item` however.142143Note that `it` can be named anything.144145```c146tll_push_back(an_integer_list, 1);147tll_push_back(an_integer_list, 2);148149tll_foreach(an_integer_list, it) {150 printf("forward: %d\n", it->item);151}152153tll_rforeach(an_integer_list, it) {154 printf("reverse: %d\n", it->item);155}156```157158Outputs:159160 forward: 1161 forward: 2162 reverse: 2163 reverse: 1164165166### Removing items - basic167168`tll_pop_front()` and `tll_pop_back()` removes the front/back item and169returns it.170171```c172tll_push_back(an_integer_list, 1234);173tll_push_back(an_integer_list, 5678);174175printf("front: %d\n", tll_pop_front(an_integer_list));176printf("back: %d\n", tll_pop_back(an_integer_list));177printf("length: %zu\n", tll_length(an_integer_list));178```179180Outputs:181182 front: 1234183 back: 5678184 length: 0185186187### Adding items - advanced188189Given an iterator, you can insert new items before or after that190iterator, using `tll_insert_before()` and `tll_insert_after()`.191192```c193tll_foreach(an_integer_list, it) {194 if (it->item == 1234) {195 tll_insert_before(an_integer_list, it, 7777);196 break;197 }198}199```200201Q: Why do I have to pass **both** the _list_ and the _iterator_ to202 `tll_insert_before()`?203204A: If not, **each** element in the list would have to contain a205 pointer to the owning list, which would significantly increase the206 overhead.207208209### Removing items - advanced210211Similar to how you can add items while iterating a list, you can also212remove them.213214Note that the `*foreach()` functions are **safe** in this regard - it215is perfectly OK to remove the "current" item.216217```c218tll_foreach(an_integer_list, it) {219 if (it->item.delete_me)220 tll_remove(an_integer_list, it);221}222```223224To make it slightly easier to handle cases where the item _itself_225must be free:d as well, there is also `tll_remove_and_free()`. It226works just like `tll_remove()`, but takes an additional argument; a227callback that will be called for each item.228229```c230tll(int *) int_p_list = tll_init();231232int *a = malloc(sizeof(*a));233int *b = malloc(sizeof(*b));234235*a = 1234;236*b = 5678;237238tll_push_back(int_p_list, a);239tll_push_back(int_p_list, b);240241tll_foreach(int_p_list, it) {242 tll_remove_and_free(int_p_list, it, free);243}244```245246247### Freeing248249To remove **all** items, use `tll_free()`, or250`tll_free_and_free()`. These are just convenience functions and251calling these are equivalent to:252253```c254tll_foreach(an_integer_list, it) {255 tll_remove(an_integer_list, it);256}257```258259Note that there is no need to call `tll_free()` on an empty260(`tll_length(list) == 0`) list.261262263## Integrating264265The easiest way may be to simply copy `tllist.h` into your266project. But see sections below for other ways.267268269### Meson270271You can use tllist as a subproject. In your main project's272`meson.build`, to something like:273274```meson275tllist = subproject('tllist').get_variable('tllist')276executable('you-executable', ..., dependencies: [tllist])277```278279280## API281282### Cheat sheet283284| 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>67#include <tllist.h>89int10main(int argc, const char *const *argv)11{12 tll(int) l = tll_init();13 assert(tll_length(l) == 0);1415 /* 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);1920 assert(tll_front(l) == 123);21 assert(tll_back(l) == 789);2223 /* push front */24 tll_push_front(l, 0xabc); assert(tll_length(l) == 4);2526 assert(tll_front(l) == 0xabc);27 assert(tll_back(l) == 789);2829 /* Pop back */30 assert(tll_pop_back(l) == 789);31 assert(tll_back(l) == 456);3233 /* Pop front */34 assert(tll_pop_front(l) == 0xabc);35 assert(tll_front(l) == 123);3637 /* foreach */38 assert(tll_length(l) == 2);3940 int seen[tll_length(l)];41 memset(seen, 0, tll_length(l) * sizeof(seen[0]));4243 size_t count = 0;44 tll_foreach(l, it)45 seen[count++] = it->item;4647 assert(count == tll_length(l));48 assert(seen[0] == 123);49 assert(seen[1] == 456);5051 /* rforeach */52 memset(seen, 0, tll_length(l) * sizeof(seen[0]));53 count = 0;54 tll_rforeach(l, it)55 seen[count++] = it->item;5657 assert(count == tll_length(l));58 assert(seen[0] == 456);59 assert(seen[1] == 123);6061 /* 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);7071 /* 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);7980 /* 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);8889 /* free */90 tll_free(l);91 assert(tll_length(l) == 0);92 assert(l.head == NULL);93 assert(l.tail == NULL);9495 return EXIT_SUCCESS;96}
added:subprojects/tllist/tllist.h+180 added
1#pragma once23#include <stdlib.h>4#include <stddef.h>5#include <assert.h>67#define TLL_PASTE2( a, b) a##b8#define TLL_PASTE( a, b) TLL_PASTE2( a, b)910/* 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;1718/*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 }3334/* Initializer: tll(int) my_list = tll_init(); */35#define tll_init() {.head = NULL, .tail = NULL, .length = 0}3637/* Length/size of list: printf("size: %zu\n", tll_length(my_list)); */38#define tll_length(list) (list).length3940/* 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)4748/* 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)5556/*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)7374/* 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)8182/*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)101102/*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)121122/*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)142143/* 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)149150#define tll_front(list) (list).head->item151#define tll_back(list) (list).tail->item152153/*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 })163164/* 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 })171172/* 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)176177/* 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)