summaryrefslogtreecommitdiff
path: root/test/raytracer/arrays.h
blob: fe65af0c44d685e6d3878528da9a3b45532d2331 (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
/* Resizable arrays */

struct array {
  int size;                     /* Number of elements in the array */
  int capacity;                 /* Max number of elements before resizing */
  void * data;                  /* Actual data -- variable sized */
};

struct array * alloc_array(int eltsize, int initialsize);

void grow_array(int eltsize, struct array * arr);

struct array * copy_array(int eltsize, struct array * arr, int extrasize);

#define new_array(ty,sz) alloc_array(sizeof(ty), sz)

#define data_array(ty,arr) ((ty *) (arr)->data)

#define get_array(ty,arr,idx) (data_array(ty,arr)[idx])

#define set_array(ty,arr,idx,newval) (data_array(ty,arr)[idx] = (newval))

#define extend_array(ty,arr) \
  if ((arr)->size >= (arr)->capacity) grow_array(sizeof(ty), (arr)); \
  (arr)->size++