-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVec.h
More file actions
244 lines (224 loc) · 4.9 KB
/
Vec.h
File metadata and controls
244 lines (224 loc) · 4.9 KB
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
237
238
239
240
241
242
#ifndef _VECTOR_
#define _VECTOR_
#endif
#ifndef _ZERO_
#define _ZERO_ 0.00001
#endif
#ifndef _MATH_
#include <math.h>
#define _MATH_
#endif
class Vector{
public :
int size;
double *value;
Vector(){
size=0;
value=NULL;
}
Vector(int size){
this->size=size;
value=new double[size];
}
Vector(int start,int end,int interval=1){
int i;
int num= (end-start)/interval+1;
size=num;
value=new double[num];
for(i=1;i<=num;++i)
{
value[i-1]=(i-1)*interval+start;
}
}
double& operator()(int i){
return value[i-1];
}
void disp(){
int i;
Vector vec=*this;
for(i=1;i<=size;++i)
printf("%f ",vec(i));
printf("\n");
}
void deleteData(){
delete []value;
}
void swap(int i,int j){
Vector vec=*this;
double a=vec(i);
vec(i)=vec(j);
vec(j)=a;
}
Vector& init(int size){
delete []value;
this->size=size;
value=new double[size];
}
Vector copy(){
Vector newVec(size);
Vector oldVec=*this;
int i;
for(i=1;i<=size;++i){
newVec(i)=oldVec(i);
}
return newVec;
}
void copy(Vector oldVec){
size=oldVec.size;
Vector newVec=*this;
if (size!=newVec.size) return;//只能拷贝相同维度的向量
int i;
for(i=1;i<=size;++i){
newVec(i)=oldVec(i);
}
return;
}
~Vector(){
if(!size){
delete []value;
size=0;
}
}
};
class IntVector{
public :
int size;
int *value;
IntVector(){
size=0;
value=NULL;
}
IntVector(int size){
this->size=size;
value=new int[size];
}
IntVector(int start,int end,int interval=1){
int i;
int num= (end-start)/interval+1;
size=num;
value=new int[num];
for(i=1;i<=num;++i)
{
value[i-1]=(i-1)*interval+start;
}
}
IntVector& toOnes(){
IntVector& vec=*this;
int i;
for(i=1;i<=size;++i) vec(i)=1;
}
int& operator()(int i){
return value[i-1];
}
void disp(){
int i;
IntVector vec=*this;
for(i=1;i<=size;++i)
printf("%d ",vec(i));
printf("\n");
}
void deleteData(){
delete []value;
}
void swap(int i,int j){
IntVector vec=*this;
int a=vec(i);
vec(i)=vec(j);
vec(j)=a;
}
IntVector& init(int size){
delete []value;
this->size=size;
value=new int[size];
}
IntVector copy(){
IntVector newVec(size);
IntVector oldVec=*this;
int i;
for(i=1;i<=size;++i){
newVec(i)=oldVec(i);
}
return newVec;
}
void copy(IntVector oldVec){
size=oldVec.size;
IntVector newVec=*this;
if (size!=newVec.size) return;//只能拷贝相同维度的向量
int i;
for(i=1;i<=size;++i){
newVec(i)=oldVec(i);
}
return;
}
void toRandom(){
int i,j,k;
int n=size*3;
for(i=1;i<=n;++i)
{
k=rand()%(size)+1;
j=rand()%(size)+1;
swap(k,j);
}
}
~IntVector(){
if(!size){
delete []value;
size=0;
}
}
};
// void sort(Vector valueSeq){
// int head=1,tail=valueSeq.size;
// sort(valueSeq,head,tail);
// }
// void sort(Vector valueSeq,int head,int tail){
// int index,cake=head;
// if (tail==head){
// return ;
// }
// for(index=head+1;index<=tail;++index)
// if(valueSeq(index)<valueSeq(head))
// {
// ++cake;
// valueSeq.swap(index,cake);
// }
// valueSeq.swap(head,cake);
// sort(valueSeq,head,cake-1);
// sort(valueSeq,cake+1,tail);
// }
void sort(IntVector &valueSeq,int head,int tail){
int index,cake=head;
if (tail<=head){
return ;
}
for(index=head+1;index<=tail;++index)
if(valueSeq(index)<valueSeq(head))
{
++cake;
valueSeq.swap(index,cake);
}
valueSeq.swap(head,cake);
sort(valueSeq,head,cake-1);
sort(valueSeq,cake+1,tail);
}
void sort(IntVector &valueSeq){
int head=1,tail=valueSeq.size;
sort(valueSeq,head,tail);
}
void argsort(Vector valueSeq,Vector indexSeq,int head,int tail){
int index,cake=head;
if (tail==head){
return ;
}
for(index=head+1;index<=tail;++index)
if(valueSeq(index)<valueSeq(head))
{
++cake;
valueSeq.swap(index,cake);
indexSeq.swap(index,cake);
}
valueSeq.swap(head,cake);
indexSeq.swap(head,cake);
argsort(valueSeq,indexSeq,head,cake-1);
argsort(valueSeq,indexSeq,cake+1,tail);
}