-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.c
151 lines (135 loc) · 2.58 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
#include <stdio.h>
#include <stdlib.h>
#include "vector.h"
int vec_init(struct VECTOR* p, rank cap)
{
if (cap < 1)
{
printf("invalid parameter\n");
return -2;
}
p->_deconstruct(p);
p->_elem = (T*)malloc(sizeof(T) * cap);
if (p->_elem == NULL)
{
perror("no enough mem");
return -1;
}
p->_size = 0;
p->_capacity = cap;
return 0;
}
int copyform_init(struct VECTOR* p, T* const A, rank lo, rank hi, const int lenA)
{
if (lo<0 || hi>lenA)
{
printf("invalid parameters\n");
return -2;
}
p->_deconstruct(p);
p->_elem = (T*)malloc(sizeof(T) * (p->_capacity = 2 * (hi - lo)));
if (p->_elem == NULL)
{
return -1;
}
p->_size = 0;
while (lo < hi)
{
p->_elem[p->_size++] = A[lo++];
}
return 0;
}
void expand(struct VECTOR *p)
{
if(p->_size<p->_capacity)
return;
T* oldelem = p->_elem;
p->_elem = (T*)malloc(sizeof(T) * (p->_capacity <<= 1));
if (p->_elem == NULL)
perror("no enough memmer");
for (rank i = 0; i < p->_size; i++)
p->_elem[i] = oldelem[i];
free((void*)oldelem);
}
int insert(struct VECTOR *p, rank r, T val)
{
if (r > p->_size)
{
printf("out of memory boundary\n");
return -1;
}
p->_expand(p);
for (rank i = p->_size; i > r; i--)
p->_elem[i] = p->_elem[i - 1];
p->_elem[r] = val; p->_size++;
return r;
}
int check(struct VECTOR *p, T val)
{
int lo = 0, hi = p->_size;
while ((hi-- > lo) && (val != p->_elem[hi])) ;
return hi;
}
int rmelem(struct VECTOR *p, rank lo, rank hi)
{
if (lo<0 || hi>p->_size)
{
perror("operate memory where beyond allocated ");
//printf("strerror:%s\n", strerror(errno));
return -1;
}
while (hi < p->_size) p->_elem[lo++] = p->_elem[hi++];
p->_size = lo;
return hi-lo;
}
int rm_single(struct VECTOR* p, rank r)
{
if (p->_rmelem(p, r, r + 1) == -1)
{
perror("overflow");
//printf("strerror:%s\n", strerror(errno));
return -1;
}
return 0;
}
void display (struct VECTOR *p)
{
printf("size:%d\n", p->_size);
for (rank i = 0; i < p->_size; i++)
{
printf("%d ", p->_elem[i]);
}
printf("\n");
}
void deconstruct(struct VECTOR *p)
{
if (p->_elem != NULL)
{
free((void*)p->_elem);
p->_elem = NULL;
}
return;
}
void insert_tail(struct VECTOR* p, T val)
{
p->_expand(p);
p->_elem[p->_size++] = val;
}
void construct(struct VECTOR* p)
{
p->_elem = NULL;
p->_capacity = 0;
p->_size = 0;
p->_check = check;
p->_rmelem = rmelem;
p->_display = display;
p->_insert = insert;
p->_empty_init = vec_init;
p->_deconstruct = deconstruct;
p->_copyform_init = copyform_init;
p->_expand = expand;
p->_insert_tail = insert_tail;
p->_rm_single = rm_single;
p->_empty_init(p, 1);
}