-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.c
160 lines (122 loc) · 3.87 KB
/
vector.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
/**
The MIT License (MIT) Copyright (c) 2015 Giannis Vrentzos <gvre@gvre.gr>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "vector.h"
static void increase_capacity(vector *v);
static void increase_capacity(vector *v)
{
assert(v != NULL);
void **old_items = v->items;
size_t new_capacity = ceil(v->capacity_increase_factor * v->capacity);
v->items = realloc(v->items, new_capacity * sizeof *v->items);
if (v->items == NULL) {
v->items = old_items;
perror("Could not reallocate memory");
return;
}
v->capacity = new_capacity;
}
vector * vector_create(size_t capacity, void (*dealloc_fn)(void *it))
{
vector *v = malloc(sizeof *v);
if (v == NULL) {
perror("Could not allocate memory");
return NULL;
}
v->capacity = capacity;
v->size = 0;
v->capacity_increase_factor = 1.5;
v->items = malloc(capacity * sizeof *v->items);
v->dealloc_fn = dealloc_fn;
return v;
}
void vector_set_capacity_increase_factor(vector *v, float capacity_increase_factor)
{
assert(v != NULL);
if (capacity_increase_factor > 1)
v->capacity_increase_factor = capacity_increase_factor;
}
void vector_push_back(vector *v, void *value)
{
assert(v != NULL);
if (v->size == v->capacity)
increase_capacity(v);
v->items[v->size++] = value;
}
void vector_insert(vector *v, size_t pos, void *value)
{
assert(v != NULL);
assert(pos < v->size);
if (v->size == v->capacity)
increase_capacity(v);
memmove(v->items + pos + 1, v->items + pos, (v->size - pos) * sizeof *v->items);
v->items[pos] = value;
v->size++;
}
size_t vector_set(vector *v, size_t pos, void *value)
{
assert(v != NULL);
if (pos >= v->size)
return 0;
v->items[pos] = value;
return 1;
}
void *vector_get(vector *v, size_t pos)
{
assert(v != NULL);
if (pos >= v->size)
return NULL;
return v->items[pos];
}
size_t vector_size(vector *v)
{
assert(v != NULL);
return v->size;
}
int vector_empty(vector *v)
{
assert(v != NULL);
return v->size == 0;
}
size_t vector_capacity(vector *v)
{
assert(v != NULL);
return v->capacity;
}
void vector_erase(vector *v, size_t pos)
{
assert(v != NULL);
if (pos >= v->size)
return;
if (v->dealloc_fn != NULL) {
v->dealloc_fn(v->items[pos]);
v->size--;
if (pos < v->size)
memmove(v->items + pos, v->items + pos + 1, (v->size - pos) * sizeof *v->items);
}
}
void vector_clear(vector *v)
{
assert(v != NULL);
if (v->dealloc_fn != NULL) {
for (size_t i = 0; i < v->size; i++)
v->dealloc_fn(v->items[i]);
}
v->size = 0;
}
void vector_destroy(vector **v)
{
vector_clear(*v);
free((*v)->items);
free(*v);
*v = NULL;
}