手写C版本的vector

记录一个小demo,留着以后批判。

实现了除了运算符重载以外的所有功能(大概),quicksort有问题,前期设计导致的,不能从0,1开始…贼麻烦,不建议用。其他的都没什么问题。vector使用的是const static VectorFuntion vector **类似于C++的namespace**中的通用接口。

用到的宏

1
2
#define INT_MIN -2147483648
#define TYPE int//勉强范型吧

data定义

1
2
3
4
5
typedef struct VectorData{
unsigned int size;
unsigned int space;
TYPE data[];
}VectorData;

funtion定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
typedef struct VectorFuntion{
VectorData* (*initVectorData)(unsigned int);
TYPE (*front)(VectorData* this);
TYPE (*back)(VectorData* this);
TYPE*(*begin)(VectorData* this);
TYPE*(*end)(VectorData* this);
char (*empty)(VectorData* this);
void (*capacity)(VectorData** this,unsigned int space);
unsigned int(*size)(VectorData* this);
unsigned int(*max_size)(void);
void (*shrink_to_fit)(VectorData** this);
char (*clean)(VectorData** this);
char (*insert)(VectorData* this,unsigned int pos,TYPE value);
TYPE (*push_back)(VectorData* this);
char (*pop_back)(VectorData* this);
char (*resize)(VectorData* this,unsigned int size);
char (*swap)(VectorData* this,unsigned int pos1,unsigned int pos2);
char (*erase)(VectorData* this,TYPE val);
void (*PrintValue)(VectorData* this);
void (*sort)(VectorData* this,unsigned int,unsigned int);
}VectorFuntion;

vector初始化

1
2
3
4
5
6
const static VectorFuntion vector = {
initVectorData,front,back,begin,end,empty,
capacity,size,max_size,shrink_to_fit,
clean,insert,push_back,pop_back,
resize,swap,Vectorerase,PrintVectorData,VectorQuickSort
};

测试函数

测试用例基本通过,quickSort有点小问题,我记不清开始下标了…

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
void test(){
VectorData *t = initVectorData(100);
int arr[1000];
for(int i = 0; i < 10; i++){
arr[i] = i;
}
initVectorDataVal(t, arr, 10);
vector.insert(t,5,20);//ok
TYPE front = vector.front(t);//ok
printf("front = %d\n",front);
vector.capacity(&t,1000);//ok
TYPE* q = vector.begin(t);
printf("q = %d\n",*q);
TYPE* p = vector.end(t);
printf("*p = %d\n",*p);
vector.shrink_to_fit(&t);//ok
printf("size = %d\n",vector.size(t));
vector.erase(t,4);//ok
vector.resize(t,50);//ok
printf("size = %d \n",vector.size(t));//ok
vector.swap(t,5,9);//ok
// vector.clean(&t);//ok
printf("max size = %d\n",vector.max_size());//ok
vector.PrintValue(t);
vector.sort(t,2,9);//some problems
vector.PrintValue(t);
}

完整代码

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//
// vector.c
// COOP
//
// Created by echo on 2021/2/15.
// Copyright © 2021 echo. All rights reserved.
//
//#include "OPP.h"
/*
author:echo
time:2021,02,15 09:53
实现了除了运算符重载以外的所有功能(大概),quicksort有问题,前期设计导致的,不能从0,1开始...贼麻烦,不建议用。其他的都没什么问题。

*/
#include "vector.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>



VectorData* initVectorData(unsigned int space){
VectorData* this = (VectorData*)malloc(sizeof(VectorData) + sizeof(TYPE) * space);
this->size = 0;
this->space = space;
return this;
}


TYPE frontV(VectorData* this){
if(this->size > 1)return this->data[1];
else return INT_MIN;
}

TYPE backV(VectorData* this){
if(this->size > 1)return this->data[this->size-1];
else return INT_MIN;
}

TYPE* beginV(VectorData* this){
if(this->size > 1)return &this->data[1];
else return NULL;
}

TYPE* endV(VectorData* this){
if(this->size > 1)return &this->data[this->size-1];
else return NULL;
}

char emptyV(VectorData* this){
return this->size == 0 ? 1 : 0;
}

void capacityV(VectorData** this,unsigned int space){
VectorData* tmp = *this;
*this = (VectorData*)malloc(sizeof(VectorData) + sizeof(TYPE)*space);
int index = 0;
while(index < tmp->size){
(*this)->data[index] = tmp->data[index];
index++;
}
(*this)->size = tmp->size;
(*this)->space = space;
free(tmp);
tmp = NULL;
// return this;
}

unsigned int sizeV(VectorData* this){
return this->size;
}

unsigned int max_sizeV(void){
return (-INT_MIN) -1;
}

void shrink_to_fitV(VectorData** this){
VectorData* tmp = *this;
printf("--------\n");
*this = (VectorData*)malloc(sizeof(VectorData)+sizeof(TYPE)*tmp->size);
int index = 0;
while(index < tmp->size){
(*this)->data[index] = tmp->data[index];
index++;
}
(*this)->size = tmp->size;
(*this)->space = tmp->space;
free(tmp);
tmp = NULL;
// return this;
}

char cleanV(VectorData** this){
free(*this);
*this = NULL;
return 1;
}

char insertV(VectorData* this,unsigned int pos,TYPE val){
if(this->size + 1 >= this->space){
capacityV(&this,this->space);
}
int index = this->size;
for(; index > pos; index--){
this->data[pos-1] = this->data[pos];
}
this->data[pos] = val;
return 1;
}
TYPE push_backV(VectorData* this,TYPE value){
if(this->size < this->space){
this->data[this->size] = value;
this->size++;
return value;
}
else{
capacityV(&this, this->space*2);
}
return value;
}
char pop_backV(VectorData* this){
if(this->size > 1){
this->size--;
return 1;
}
return 0;
}
char resizeV(VectorData* this,unsigned int size){
if(size > this->space){
capacityV(&this, size);

return 1;
}
else{
this->space = size;
return 1;
}
}
char swapV(VectorData* this,unsigned int pos1,unsigned int pos2){
if(pos1 < this->size && pos2 < this->size){
TYPE tmp = this->data[pos1];
this->data[pos1] = this->data[pos2];
this->data[pos2] = tmp;
return 1;
}
return 0;
}
char VectoreraseV(VectorData* this,TYPE val){
int index = 0;
for(; index < this->size; index++){
if(val == this->data[index]){
for(;index < this->size-1; index++){
this->data[index] = this->data[index+1];
}
return 1;
}
}
return 0;
}
void PrintVectorDataV(VectorData* this){
if(this == NULL){
printf("the object if free!\n");
return;
}
int index = 0;
for(; index < this->size; index++){
printf("%d\t",this->data[index]);
}
printf("\n");
}
void QuickSortSwapV(TYPE* a,TYPE* b){
TYPE tmp = *a;
*a = *b;
*b = tmp;
}
void QuickSortV(TYPE* arr,unsigned int left,unsigned int right){
if(left >= right) return;
unsigned int i = left -1, j = right+1;
TYPE x = arr[((right+left) >> 1)];
while(i < j){
do j--;while(arr[j] > x);
do i++;while(arr[i] < x);
if(i < j)QuickSortSwapV(&arr[i], &arr[j]);
else {(void)(QuickSortV(arr, j+1, right));QuickSortV(arr, left, j);}
}
}
void VectorQuickSortV(VectorData* this,unsigned int Start,unsigned int end){
QuickSortV(this->data, Start, end);
}
void initVectorDataValV(VectorData* this,TYPE* val,unsigned int size){
unsigned int index = 1;
for(;index < size && index < this->space; index++){
this->data[index] = val[index];
printf("%d\t",this->data[index]);
}
printf("\n");
this->size = size;
}


const VectorFuntion vector = {
initVectorData,frontV,backV,beginV,endV,emptyV,
capacityV,sizeV,max_sizeV,shrink_to_fitV,
cleanV,insertV,push_backV,pop_backV,
resizeV,swapV,VectoreraseV,PrintVectorDataV,VectorQuickSortV
};


void test(){
VectorData *t = initVectorData(100);
int arr[1000];
for(int i = 0; i < 10; i++){
arr[i] = i;
}
initVectorDataValV(t, arr, 10);
vector.insert(t,5,20);//ok
TYPE front = vector.front(t);//ok
printf("front = %d\n",front);
vector.capacity(&t,1000);//ok
TYPE* q = vector.begin(t);
printf("q = %d\n",*q);
TYPE* p = vector.end(t);
printf("*p = %d\n",*p);
vector.shrink_to_fit(&t);//ok
printf("size = %d\n",vector.size(t));
vector.erase(t,4);//ok
vector.resize(t,50);//ok
printf("size = %d \n",vector.size(t));//ok
vector.swap(t,5,9);//ok
// vector.clean(&t);//ok
printf("max size = %d\n",vector.max_size());//ok
vector.PrintValue(t);
vector.sort(t,2,9);//some problems
vector.PrintValue(t);
}

Vector.h

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
//
// vector.h
// COOP
//
// Created by echo on 2021/2/15.
// Copyright © 2021 echo. All rights reserved.
//

#ifndef vector_h
#define vector_h

#include <stdio.h>
#define INT_MIN -2147483648
#define TYPE int
typedef struct VectorData{
unsigned int size;
unsigned int space;
TYPE data[];
}VectorData;

typedef struct VectorFuntion{
VectorData* (*initVectorData)(unsigned int);
TYPE (*front)(VectorData* this);
TYPE (*back)(VectorData* this);
TYPE*(*begin)(VectorData* this);
TYPE*(*end)(VectorData* this);
char (*empty)(VectorData* this);
void (*capacity)(VectorData** this,unsigned int space);
unsigned int(*size)(VectorData* this);
unsigned int(*max_size)(void);
void (*shrink_to_fit)(VectorData** this);
char (*clean)(VectorData** this);
char (*insert)(VectorData* this,unsigned int pos,TYPE value);
TYPE (*push_back)(VectorData* this,TYPE);
char (*pop_back)(VectorData* this);
char (*resize)(VectorData* this,unsigned int size);
char (*swap)(VectorData* this,unsigned int pos1,unsigned int pos2);
char (*erase)(VectorData* this,TYPE val);
void (*PrintValue)(VectorData* this);
void (*sort)(VectorData* this,unsigned int,unsigned int);
}VectorFuntion;

#endif /* vector_h */

Main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "vector.h"


extern const struct VectorFuntion vector;
//貌似sort有点小问题,排序貌似不支持01开始

void VectorTest(){
VectorData* tmp = vector.initVectorData(10);
vector.push_back(tmp,10);
vector.PrintValue(tmp);
}


int main(){
VectorTest()
return 0;
}