[go: up one dir, main page]

Menu

[r377]: / libpetey / kselect.cc  Maximize  Restore  History

Download this file

451 lines (393 with data), 10.2 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#include <stdint.h>
#include <stdio.h>
#include <algorithm>
#include "randomize.h"
#include "kselect.h"
namespace libpetey {
//lets have a showdown:
template <class type>
kselect_base<type>::kselect_base(long k1) {
k=k1;
}
template <class type>
kselect_base<type>::kselect_base() {}
template <class type>
kselect_base<type>::~kselect_base() {}
template <class type>
kselect_naive<type>::kselect_naive(long k1) {
select=new type[k1];
this->k=k1;
ncur=0;
}
template <class type>
kselect_naive<type>::~kselect_naive() {
delete [] select;
}
template <class type>
long kselect_naive<type>::add(type val) {
long gind=-1;
if (ncur<this->k) {
select[ncur]=val;
ncur++;
} else {
for (long i=0; i<this->k; i++) {
if (gind!=-1) {
if (select[i]>gind) gind=i;
} else if (val<select[i]) {
gind=i;
}
}
if (gind!=-1) select[gind]=val;
}
return ncur;
}
template <class type>
void kselect_naive<type>::get(type *kleast) {
for (long i=0; i<this->k; i++) kleast[i]=select[i];
}
template <class type>
kselect_tree<type>::kselect_tree(long k1) {
this->k=k1;
}
template <class type>
kselect_tree<type>::~kselect_tree() {
}
template <class type>
long kselect_tree<type>::add(type val) {
long n;
n=data.add(val);
if (n>this->k) {
n=data.delete_greatest();
}
return n;
}
template <class type>
void kselect_tree<type>::get(type *kleast) {
data.decompose(kleast, this->k);
}
template <class type>
kselect_heap<type>::kselect_heap(long k1) {
this->k=k1;
data=new vector<type>(0);
}
template <class type>
kselect_heap<type>::~kselect_heap() {
delete data;
}
template <class type>
long kselect_heap<type>::add(type val) {
long n;
n=data->size();
data->push_back(val);
if (n>this->k) {
(*data)[n]=val;
push_heap(data->begin(), data->end());
pop_heap(data->begin(), data->end());
data->pop_back();
} else if (n==this->k) {
make_heap(data->begin(), data->end());
data->push_back(val);
}
return n;
}
template <class type>
void kselect_heap<type>::get(type *kleast) {
sort_heap(data->begin(), data->end());
for (long i=0; i<this->k; i++) kleast[i]=(*data)[i];
}
template <class type>
kselect_quick<type>::kselect_quick(long k1) {
this->k=k1;
data=new vector_s<type>(this->k);
ncur=0;
}
template <class type>
kselect_quick<type>::kselect_quick(long k1, long n) {
this->k=k1;
data=new vector_s<type>(n);
ncur=0;
}
template <class type>
kselect_quick<type>::~kselect_quick() {
delete data;
}
template <class type>
long kselect_quick<type>::add(type val) {
(*data)[int(ncur)]=val;
ncur++;
//printf("kselect_quick: adding value; current list:\n");
//data->print(stdout);
return ncur;
}
template <class type>
void kselect_quick<type>::get(type *kleast) {
type *d2;
long n;
d2=(type *) *data;
n=data->size();
kleast_quick(d2, n, this->k);
for (long i=0; i<this->k; i++) kleast[i]=d2[i];
}
template <class type>
kiselect_base<type>::kiselect_base(long k1) {
k=k1;
ncur=0;
}
template <class type>
kiselect_base<type>::kiselect_base() {}
template <class type>
kiselect_base<type>::~kiselect_base() {}
//trying to move towards more of a test-driven development:
template <class type>
int kiselect_base<type>::test(long n) {
int err=0;
type list[n];
type kleast[k];
long ind[k];
long lind; //index of largest of k-least
int flag[n];
//generate a list of random numbers and apply k-least algo to it:
for (long i=0; i<n; i++) {
list[i]=ranu();
add(list[i]);
}
get(kleast, ind);
//find the largest of the k-least:
lind=0;
for (long i=1; i<k; i++) {
if (kleast[i]>kleast[lind]) lind=i;
}
//set flags to exclude all k-least from the comparison:
for (long i=0; i<n; i++) flag[i]=1;
for (long i=0; i<k; i++) flag[ind[i]]=0;
//largest of k-least must be smaller than all others in the list:
for (long i=0; i<n; i++) {
if (flag[i] && kleast[lind]>list[i]) {
err=-1;
break;
}
if (err!=0) break;
}
if (err!=0) {
for (long i=0; i<n; i++) printf("%g ", list[i]);
printf("\n");
for (long i=0; i<k; i++) printf("%g ", kleast[i]);
printf("\n");
}
return err;
}
template <class type>
kiselect_tree<type>::kiselect_tree(long k1) {
this->k=k1;
this->ncur=0;
}
template <class type>
kiselect_tree<type>::~kiselect_tree() {
}
template <class type>
kiselect_naive<type>::kiselect_naive(long k1) {
select=new type[k1];
ind=new long [k1];
this->k=k1;
this->ncur=0;
}
template <class type>
kiselect_naive<type>::~kiselect_naive() {
delete [] select;
delete [] ind;
}
template <class type>
long kiselect_naive<type>::add(type val) {
long gind=-1;
if (this->ncur<this->k) {
select[this->ncur]=val;
ind[this->ncur]=this->ncur;
} else {
for (long i=0; i<this->k; i++) {
if (gind!=-1) {
if (select[i]>select[gind]) gind=i;
} else if (val<select[i]) {
gind=i;
}
}
if (gind!=-1) {
select[gind]=val;
ind[gind]=this->ncur;
}
}
this->ncur++;
return this->ncur;
}
template <class type>
long kiselect_naive<type>::add(type val, long i) {
if (this->ncur<this->k) {
select[this->ncur]=val;
ind[this->ncur]=i;
} else {
for (long j=0; j<this->k; j++) {
if (val<select[i]) {
select[j]=val;
ind[j]=i;
break;
}
}
}
this->ncur++;
return this->ncur;
}
template <class type>
void kiselect_naive<type>::get(type *kleast, long *j) {
for (long i=0; i<this->k; i++) {
kleast[i]=select[i];
j[i]=ind[i];
}
}
template <class type>
long kiselect_tree<type>::add(type val) {
long n;
n=data.add(val, this->ncur);
this->ncur++;
if (n>this->k) {
n=data.delete_greatest();
}
return n;
}
template <class type>
long kiselect_tree<type>::add(type val, long ind) {
long n;
n=data.add(val, ind);
this->ncur++;
if (n>this->k) {
n=data.delete_greatest();
}
return n;
}
template <class type>
void kiselect_tree<type>::get(type *kleast, long *ind) {
data.decompose(kleast, ind, this->k);
}
template <class type>
int heap_ind_el_comp(const heap_ind_el<type> &v1, heap_ind_el<type> &v2) {
if (v1.data<v2.data) return 1;
return 0;
}
template <class type>
kiselect_heap<type>::kiselect_heap(long k1) {
this->k=k1;
data=new vector<heap_ind_el<type> >(0);
this->ncur=0;
}
template <class type>
kiselect_heap<type>::~kiselect_heap() {
delete data;
}
template <class type>
long kiselect_heap<type>::add(type val) {
long n;
heap_ind_el<type> v1;
v1.data=val;
v1.ind=this->ncur;
this->ncur++;
data->push_back(v1);
n=data->size();
if (n>this->k) {
push_heap(data->begin(), data->end(), &heap_ind_el_comp<type>);
pop_heap(data->begin(), data->end(), &heap_ind_el_comp<type>);
data->pop_back();
} else if (n==this->k) {
make_heap(data->begin(), data->end(), &heap_ind_el_comp<type>);
}
return n;
}
template <class type>
long kiselect_heap<type>::add(type val, long ind) {
long n;
heap_ind_el<type> v1;
v1.data=val;
v1.ind=ind;
this->ncur++;
data->push_back(v1);
n=data->size();
if (n>this->k) {
push_heap(data->begin(), data->end(), &heap_ind_el_comp<type>);
pop_heap(data->begin(), data->end(), &heap_ind_el_comp<type>);
data->pop_back();
} else if (n==this->k) {
make_heap(data->begin(), data->end(), &heap_ind_el_comp<type>);
}
return n;
}
template <class type>
void kiselect_heap<type>::get(type *kleast, long *ind) {
sort_heap(data->begin(), data->end(), &heap_ind_el_comp<type>);
for (long i=0; i<this->k; i++) {
kleast[i]=(*data)[i].data;
ind[i]=(*data)[i].ind;
}
}
template <class type>
kiselect_quick<type>::kiselect_quick(long k1) {
this->k=k1;
data=new vector_s<type>(this->k);
ind=new vector_s<long>(this->k);
this->ncur=0;
}
template <class type>
kiselect_quick<type>::kiselect_quick(long k1, long n) {
this->k=k1;
data=new vector_s<type>(n);
ind=new vector_s<long>(n);
this->ncur=0;
}
template <class type>
kiselect_quick<type>::~kiselect_quick() {
delete data;
}
template <class type>
long kiselect_quick<type>::add(type val) {
(*data)[this->ncur]=val;
(*ind)[this->ncur]=this->ncur;
this->ncur++;
return data->size();
}
template <class type>
long kiselect_quick<type>::add(type val, long ind2) {
(*data)[this->ncur]=val;
(*ind)[this->ncur]=ind2;
this->ncur++;
return data->size();
}
template <class type>
void kiselect_quick<type>::get(type *kleast, long *ind2) {
long n;
type *d2;
long *ind1;
n=data->size();
d2=(type *) *data;
ind1=(long *) *ind;
n=data->size();
kleast_quick(d2, n, this->k, ind1, 0, n-1);
for (long i=0; i<this->k; i++) {
kleast[i]=d2[ind1[i]];
ind2[i]=ind1[i];
}
}
template class kselect_naive<float>;
template class kselect_tree<float>;
template class kselect_heap<float>;
template class kselect_quick<float>;
template class kselect_naive<double>;
template class kselect_tree<double>;
template class kselect_heap<double>;
template class kselect_quick<double>;
template class kiselect_base<float>;
template class kiselect_naive<float>;
template class kiselect_tree<float>;
template class kiselect_heap<float>;
template class kiselect_quick<float>;
template class kiselect_base<double>;
template class kiselect_naive<double>;
template class kiselect_tree<double>;
template class kiselect_heap<double>;
template class kiselect_quick<double>;
} //end namespace libpetey