-
Notifications
You must be signed in to change notification settings - Fork 2
/
array_iterable.h
37 lines (28 loc) · 1.66 KB
/
array_iterable.h
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
36
37
#ifndef IT_ARR_ITRBLE_H
#define IT_ARR_ITRBLE_H
#include "func_iter.h"
#include <stdlib.h>
#define ArrIter(ElmntType) ElmntType##ArrIter
#define DefineArrIterOf(T) \
typedef struct \
{ \
size_t i; \
size_t const size; \
T const* const arr; \
} ArrIter(T)
/* Macro to consistently name the arriter -> iterable functions based on element type */
#define prep_arriter_of(T) prep_##T##arr_itr
/*
Take in a source array, its size and its element type, build an `ArrIter` from it, and call the wrapper function to
turn it into an `Iterable`
*/
#define arr_into_iter(srcarr, sz, T) prep_arriter_of(T)(&(ArrIter(T)){.i = 0, .size = sz, .arr = srcarr})
/* Define `ArrIter` struct for int arrays */
DefineArrIterOf(int);
/* Define `ArrIter` struct for char* arrays */
DefineArrIterOf(string);
/* Convert a pointer to an `ArrIter(int)` to an `Iterable(int)` */
Iterable(int) prep_arriter_of(int)(ArrIter(int) * x);
/* Convert a pointer to an `ArrIter(string)` to an `Iterable(string)` */
Iterable(string) prep_arriter_of(string)(ArrIter(string) * x);
#endif /* !IT_ARR_ITRBLE_H */