aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy_list.h
blob: 6d6e5363bd0e0694e0f75901931ea2b10c3ad104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef TEENSY_LIST_H_
#define TEENSY_LIST_H_

#include "teensy_common.h"
#include "teensy_mem.h"

#define TY_LIST_MIN_CAP 8
#define TY_LIST_GROW_RATE 2

#define ty_list_last(arr) ((arr)->items[(arr)->len - 1])

#define ty_list_reserve(arr, amt) \
    do { \
        if ((amt) > (arr)->cap) { \
            (arr)->cap = ty_max(TY_LIST_MIN_CAP, (arr)->cap); \
            while ((arr)->cap < (amt)) \
                (arr)->cap *= TY_LIST_GROW_RATE; \
            \
            (arr)->items = ty_realloc( \
                (arr)->items, \
                sizeof(*(arr)->items) * (arr)->cap \
            ); \
        } \
    } while (0)

#define ty_list_append(arr, elem) \
    do { \
        ty_list_reserve(arr, (arr)->len + 1); \
        (arr)->items[(arr)->len++] = (elem); \
    } while (0)

#define ty_list_clear(arr) ((arr)->len = 0)
#define ty_list_free(arr) ty_free((arr)->items)

#endif // TEENSY_LIST_H_