summaryrefslogtreecommitdiff
path: root/inc/macros.h
blob: 841084dcb92e9b349defca4d9fb620c74806431c (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
36
37
38
39
40
41
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

#ifndef MACROS_H
#define MACROS_H

#define DEFINE_ARRAY(TYPE)                                                     \
  typedef struct {                                                             \
    TYPE *data;                                                                \
    size_t size;                                                               \
  } Array_##TYPE

#define InitArray(TYPE, ...)                                                   \
  ({                                                                           \
    TYPE temp[] = __VA_ARGS__;                                                 \
    Array_##TYPE *arr = malloc(sizeof(Array_##TYPE));                          \
    arr->size = sizeof(temp) / sizeof(temp[0]);                                \
    arr->data = malloc(arr->size * sizeof(TYPE));                              \
    if (arr->data) {                                                           \
      memcpy(arr->data, temp, arr->size * sizeof(TYPE));                       \
    }                                                                          \
    arr;                                                                       \
  })

#define InitArrayWithSize(TYPE, SIZE, INIT_VALUE)                              \
  ({                                                                           \
    Array_##TYPE *arr = malloc(sizeof(Array_##TYPE));                          \
    arr->size = SIZE;                                                          \
    arr->data = malloc(arr->size * sizeof(TYPE));                              \
    if (arr->data) {                                                           \
      for (size_t i = 0; i < arr->size; i++)                                   \
        arr->data[i] = INIT_VALUE;                                             \
    }                                                                          \
    arr;                                                                       \
  })

#define c_max(x, y) (((x) >= (y)) ? (x) : (y))
#define c_min(x, y) (((x) <= (y)) ? (x) : (y))

#endif // MACROS_H