common: list: an interface to a static list which can have elements added and removed...
[avr_work.git] / common / list.h
blobbaf9391a1aaeab0cdd97c33d9434d2a027ad86e1
1 /*
2 * list.h
3 */
4 #ifndef _LIST_H_
5 #define _LIST_H_
6 #include <stdint.h>
7 #include <stdbool.h>
9 typedef char list_base_t; // allowed to use char* to access any type of mem.
10 typedef uint8_t list_index_t;
12 typedef struct {
13 LIST_BASE_T * buffer; // buffer
14 LIST_INDEX_T first; // position of first element
15 LIST_INDEX_T end; // position of last element +1
16 LIST_INDEX_T ct; // number of elements in the list
17 const LIST_INDEX_T sz; // size of the buffer
18 } list_t;
20 #define LIST_DEF(buff) {\
21 .buffer = (buff), .first = 0, .last = 0, .ct = 0,\
22 .sz = sizeof(buff) / sizeof(list_base_t) \
25 // add to front (push)
26 #define list_push list_push_front
27 int8_t list_push_front(list_t *list, list_base_t x);
28 int8_t list_push_front_o(list_t *list, list_base_t x);
30 // add to back (append)
31 #define list_append list_push_back
32 list_error_t list_push_back(list_t *list, list_base_t x);
33 list_error_t list_push_back_o(list_t *list, list_base_t x);
35 // remove
36 #define list_pop list_pop_front
37 list_base_t list_pop_front(list_t *list); // (pop)
38 #define list_take list_pop_back
39 list_base_t list_pop_back(list_t *list); // (take)
41 // peek
42 list_base_t list_peek_front(list_t *list);
43 list_base_t list_peek_back(list_t *list);
44 list_base_t list_peek(list_t *list, list_index_t index);
46 void list_flush(list_t *list);
47 bool list_empty(list_t *list);
48 bool list_full(list_t *list);
50 #endif