-
Notifications
You must be signed in to change notification settings - Fork 1
/
c_vector_iterate.c
79 lines (70 loc) · 1.98 KB
/
c_vector_iterate.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* c_vector_iterate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aaubin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/02/13 21:48:38 by aaubin #+# #+# */
/* Updated: 2014/02/15 05:23:55 by aleger ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "c_vector.h"
void *c_vector_each(t_vector *self)
{
int pos;
if (self->cursor >= self->count)
{
self->cursor = 0;
return (NULL);
}
pos = self->cursor;
self->cursor++;
return (self->data(self, pos));
}
void c_vector_map(t_vector *self, void (*fct)(void *elt))
{
int pos;
pos = 0;
while (pos < (int)self->count)
{
fct(self->data(self, pos));
pos++;
}
}
void vector_pop_front(void *elem, t_vector *self)
{
static int index = 0;
if (index >= (int)self->count)
{
index = 0;
return ;
}
vector_memcpy(elem, VECTOR_INDEX(index), self->elt_size);
++index;
}
int vector_push_front(void *elem, t_vector *self)
{
void *content_tmp;
void *target;
int index;
index = self->count;
content_tmp = malloc(self->elt_size);
if (content_tmp)
{
if (!VECTOR_SPACE(self))
vector_extend(0, self);
self->count++;
while (index > 0)
{
c_vector_memmove(VECTOR_INDEX(index), VECTOR_INDEX(index - 1),
self->elt_size);
index--;
}
target = VECTOR_INDEX(0);
vector_memcpy(target, elem, self->elt_size);
return (1);
}
return (0);
}