aboutsummaryrefslogtreecommitdiff
path: root/src/node_vec.c
blob: 47bcdeb0c70f1256cf8eaaf485b71622d70a6a81 (plain) (blame)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "all.h"
#include <stdio.h>
#include <stdlib.h>

void node_vec_free(node_vec *v) {
  free(v->_data);
  v->_data = NULL;
  v->_cap = 0;
  v->len = 0;
}

static void _node_vec_init(node_vec *v) {
  if (v->_data)
    return;

  v->_cap = 16;
  v->len = 0;

  v->_data = malloc(v->_cap * sizeof(ast_node_t *));
  if (!v->_data) {
    perror("_node_vec_init.malloc");
    abort();
  }
}

static void _node_vec_ensure(node_vec *v) {
  _node_vec_init(v);

  if (v->len >= v->_cap) {
    v->_cap = (v->_cap == 0) ? 16 : v->_cap * 2;

    ast_node_t **tmp = realloc(v->_data, v->_cap * sizeof(ast_node_t *));
    if (!tmp) {
      perror("_node_vec_ensure.realloc");
      abort();
    }

    v->_data = tmp;
  }
}

size_t node_vec_push(node_vec *v, ast_node_t *data) {
  _node_vec_ensure(v);

  v->_data[v->len++] = data;
  return v->len - 1;
}

ast_node_t *node_vec_get(node_vec *v, size_t idx) {
  _node_vec_init(v);

  if (idx < v->len)
    return v->_data[idx];

  return NULL;
}

ast_node_t *node_vec_rem_shift(node_vec *v, size_t idx) {
  _node_vec_init(v);

  if (idx >= v->len)
    return NULL;

  ast_node_t *old = v->_data[idx];

  for (size_t j = idx; j + 1 < v->len; j++) {
    v->_data[j] = v->_data[j + 1];
  }

  v->len--;
  return old;
}

ast_node_t *node_vec_rem(node_vec *v, size_t idx) {
  _node_vec_init(v);

  if (idx >= v->len)
    return NULL;

  ast_node_t *old = v->_data[idx];
  v->_data[idx] = v->_data[--v->len];
  return old;
}

ast_node_t *node_vec_has(node_vec *v, ast_node_t *x,
                         int (*comp)(const ast_node_t *, const ast_node_t *)) {
  _node_vec_init(v);

  if (!comp)
    return NULL;

  for (size_t i = 0; i < v->len; i++) {
    if (comp(v->_data[i], x) == 0)
      return v->_data[i];
  }

  return NULL;
}

void node_vec_shrink(node_vec *v) {
  _node_vec_init(v);

  size_t new_cap = v->len ? v->len : 1;

  ast_node_t **tmp = realloc(v->_data, new_cap * sizeof(ast_node_t *));
  if (!tmp) {
    perror("node_vec_shrink.realloc");
    abort();
  }

  v->_cap = new_cap;
  v->_data = tmp;
}