-
Notifications
You must be signed in to change notification settings - Fork 1
/
c_vector_ops.c
73 lines (64 loc) · 2.16 KB
/
c_vector_ops.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* c_vector_ops.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aaubin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/02/12 22:11:51 by aaubin #+# #+# */
/* Updated: 2014/02/15 05:09:19 by aleger ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "c_vector.h"
void vector_extend(size_t size, t_vector *self)
{
size_t old_c;
old_c = self->capacity;
if (size > self->capacity)
self->capacity = size;
else
self->capacity *= 2;
self->content = vector_realloc(self->content, old_c * self->elt_size,
self->elt_size * self->capacity);
}
static int vector_insert(void *elem, size_t index, t_vector *self)
{
void *target;
if ((int)index > -1)
{
if (!VECTOR_INBOUNDS(index))
return (0);
target = VECTOR_INDEX(index);
}
else
{
if (!VECTOR_SPACE(self))
vector_extend(0, self);
target = VECTOR_INDEX(self->count);
self->count++;
}
vector_memcpy(target, elem, self->elt_size);
return (1);
}
int vector_push(void *elem, t_vector *self)
{
return (vector_insert(elem, -1, self));
}
void vector_pop(void *elem, t_vector *self)
{
vector_memcpy(elem, VECTOR_INDEX(self->count - 1), self->elt_size);
self->count--;
}
void vector_swap(t_vector *self, int index_1, int index_2)
{
void *content_tmp;
content_tmp = malloc(self->elt_size);
if (content_tmp)
{
vector_memcpy(content_tmp, VECTOR_INDEX(index_1), self->elt_size);
c_vector_memmove(VECTOR_INDEX(index_1), VECTOR_INDEX(index_2),
self->elt_size);
vector_memcpy(VECTOR_INDEX(index_2), content_tmp, self->elt_size);
}
}