-
Notifications
You must be signed in to change notification settings - Fork 1
/
array.c
131 lines (100 loc) · 2.79 KB
/
array.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
/**
=========================================================================
Author: findstr
Email: findstr@sina.com
File Name: array.c
Description: (C) 2014-04 findstr
Edit History:
2014-04-27 File created.
=========================================================================
**/
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "array.h"
struct array {
int em_size;
int em_cnt;
int buff_size;
void *buff;
};
struct array *array_create(int em_size)
{
struct array *p = (struct array *)malloc(sizeof(struct array));
if (p == NULL)
return NULL;
assert(em_size > 0);
memset(p, 0, sizeof(struct array));
p->em_size = em_size;
return p;
}
int array_release(struct array *arr)
{
assert(arr);
if (arr->buff)
free(arr->buff);
free(arr);
return 0;
}
int array_reserve(struct array *arr, int em_cnt)
{
assert(arr);
if (arr->em_cnt > em_cnt)
return 0;
arr->buff_size = em_cnt * arr->em_size;
arr->buff = realloc(arr->buff, arr->buff_size);
return 0;
}
int array_get(struct array *arr, int index, void *em)
{
assert(arr);
assert(em);
assert(index < arr->em_cnt);
memcpy(em, (unsigned char *)arr->buff + index * arr->em_size, arr->em_size);
return 0;
}
void *array_ptr(struct array *arr, int em_index)
{
assert(arr);
assert(em_index < arr->em_cnt);
return (unsigned char *)arr->buff + em_index * arr->em_size;
}
int array_push(struct array *arr, const void *em)
{
int new_size;
assert(arr);
assert(em);
new_size = arr->em_size * (arr->em_cnt + 1);
if (new_size > arr->buff_size) {
arr->buff_size = new_size * 2;
arr->buff = realloc(arr->buff, arr->buff_size);
}
if (arr->buff == NULL)
return -1;
memcpy((unsigned char *)arr->buff + arr->em_cnt * arr->em_size, em, arr->em_size);
arr->em_cnt++;
//printf("--buff_size:%d, elm_cnt:%d\n", arr->buff_size, arr->em_cnt);
return 0;
}
int array_erase(struct array *arr, int em_index)
{
assert(arr);
assert(em_index < arr->em_cnt);
memcpy((unsigned char *)arr->buff + em_index * arr->em_size, (unsigned char *)arr->buff + (em_index + 1) * arr->em_size, arr->em_size * (arr->em_cnt - em_index - 1));
arr->em_cnt--;
return 0;
}
int array_size(struct array *arr)
{
if (arr == NULL)
return 0;
assert(arr);
return arr->em_cnt;
}
int array_clear(struct array *arr)
{
assert(arr);
arr->em_cnt = 0;
return 0;
}