-
Notifications
You must be signed in to change notification settings - Fork 8
/
arrays.c
111 lines (86 loc) · 2.88 KB
/
arrays.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
/***************************************************************************
SimpleMail - Copyright (C) 2000 Hynek Schlawack and Sebastian Bauer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
/**
* @file arrays.c
*/
#include "arrays.h"
#include <string.h>
#include <stdlib.h>
/*****************************************************************************/
int array_init(struct array *array)
{
memset(array,0,sizeof(*array));
return 1;
}
/*****************************************************************************/
void array_deinit(struct array *array)
{
free(array->els);
}
/*****************************************************************************/
int array_add(struct array *array, void *el)
{
if (array->num_el == array->num_el_allocated)
{
int new_num_el_allocated = array->num_el_allocated * 3 / 4 + 4;
void **new_els = (void **)realloc(array->els, new_num_el_allocated * sizeof(void*));
if (!new_els)
{
return -1;
}
array->els = new_els;
array->num_el_allocated = new_num_el_allocated;
}
array->els[array->num_el] = el;
return array->num_el++;
}
/*****************************************************************************/
void *array_get(struct array *array, int idx)
{
return array->els[idx];
}
/*****************************************************************************/
int iarray_init(struct iarray *array)
{
memset(array,0,sizeof(*array));
return 1;
}
/*****************************************************************************/
void iarray_deinit(struct iarray *array)
{
free(array->els);
}
/*****************************************************************************/
int iarray_add(struct iarray *array, int elm)
{
if (array->num_el == array->num_el_allocated)
{
int new_num_el_allocated = array->num_el_allocated * 3 / 4 + 4;
int *new_els = (int *)realloc(array->els, new_num_el_allocated * sizeof(int));
if (!new_els)
{
return -1;
}
array->els = new_els;
array->num_el_allocated = new_num_el_allocated;
}
array->els[array->num_el] = elm;
return array->num_el++;
}
/*****************************************************************************/
int iarray_get(struct iarray *array, int idx)
{
return array->els[idx];
}