-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
160 lines (141 loc) · 3.75 KB
/
list.c
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "list.h"
#include "micc.h"
void print_list(var_t v)
{
static int nest_level = 0;
nest_level++;
list_t* list = get_ref(v);
printf("%16.16llX => [\n",v);
for (size_t i = 0; i < list->len; i++)
{
for (size_t n = 0;n < nest_level;n++)
printf("\t");
Print(list->elems[i]);
}
for (size_t n = 0;n < nest_level;n++)
printf("\t");
printf("]\n");
nest_level--;
}
var_t set_empty_list()
{
list_t *raw = create_list(0);
return build_var(raw, T_LIST);
}
var_t set_list_from_array(size_t len, var_t v_arr[])
{
list_t *raw = create_list(len);
for (size_t i = 0; i < len; i++) {
raw->elems[i] = Clone(v_arr[i]);
}
return build_var(raw, T_LIST);
}
var_t set_list_from_args(var_t v_first,...)
{
size_t len = 0;
va_list v_rest;
va_start(v_rest, v_first);
va_list v_rest_copy;
va_copy(v_rest_copy, v_rest);
for (var_t v = v_first; v != VAR_END; v = va_arg(v_rest, var_t))
len++;
va_end(v_rest);
list_t* raw = create_list(len);
size_t i = 0;
for (var_t v = v_first ; v!=VAR_END; v=va_arg(v_rest_copy, var_t))
raw->elems[i++] = Clone(v);
va_end(v_rest_copy);
return build_var(raw, T_LIST);
}
var_t set_list_from_len_args(size_t len, ...)
{
list_t *raw = create_list(len);
va_list v_args;
va_start(v_args, len);
for (size_t i = 0; i < len; i++) {
var_t v = va_arg(v_args, var_t);
raw->elems[i] = Clone(v);
}
va_end(v_args);
return build_var(raw, T_LIST);
}
size_t size_list(var_t v)
{
list_t* list = get_ref(v);
size_t size_elems = 0;
for (size_t i = 0; i < list->len; i++)
size_elems += Size(list->elems[i]);
return size_heap_var(v) + size_elems + (list->alloc - list->len) * sizeof(var_t);
}
var_t clone_list(var_t v)
{
list_t* list = get_ref(v);
list_t* raw = _new_heap_obj();
raw->len = list->len;
raw->alloc = list->alloc;
raw->elems = checked_malloc(raw->alloc * sizeof(var_t));
for (size_t i = 0; i < raw->len; i++)
raw->elems[i] = Clone(list->elems[i]);
return build_var(raw, T_LIST);
}
void delete_list(var_t* v_ref)
{
list_t* list = get_ref(*v_ref);
for (size_t i = 0; i < list->len; i++)
Delete(&(list->elems[i]));
free(list->elems);
free(list);
*v_ref = VAR_NULL;
}
void list_add(var_t v, var_t v_add)
{
list_t* list = get_ref(v);
if (++list->len > list->alloc)
{
size_t new_alloc = (size_t)(list->alloc * LIST_INCR_FACTOR);
list->elems = checked_realloc(list->elems, new_alloc);
list->alloc = new_alloc;
}
list->elems[list->len-1] = Clone(v_add);
}
var_t list_get(var_t v, size_t index)
{
list_t* list = get_ref(v);
if (index > list->len - 1) list_index_error(index, list->len);
return Clone(list->elems[index]);
}
void list_update(var_t v, size_t index, var_t v_update)
{
list_t* list = get_ref(v);
if (index > list->len - 1) list_index_error(index, list->len);
Delete(&(list->elems[index]));
list->elems[index] = Clone(v_update);
}
void list_remove(var_t v, size_t index)
{
list_t* list = get_ref(v);
if (index > list->len - 1) list_index_error(index, list->len);
Delete(&(list->elems[index]));
for (size_t i = index;i < list->len - 1;i++)
list->elems[i] = list->elems[i + 1];
list->len--;
}
var_t list_pop(var_t v)
{
list_t* list = get_ref(v);
if (list->len == 0) list_index_error(0, 0);
return list->elems[(list->len--) - 1];
}
void list_clear(var_t v)
{
list_t* list = get_ref(v);
for (size_t i = 0; i < list->len; i++) {
Delete(&(list->elems[i]));
}
list->len = 0;
}
bool list_empty(var_t v)
{
list_t* list = get_ref(v);
return list->len > 0 ? false : true;
}