-
Notifications
You must be signed in to change notification settings - Fork 0
/
sas.cpp
executable file
·74 lines (65 loc) · 1.35 KB
/
sas.cpp
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
#pragma once
#include "SA.h"
using namespace _SA;
using namespace std;
template <class T>
struct Pair{
int index;
T value;
};
template <class T>
inline ostream &operator<<(ostream &output, Pair<T> &out ){
output << "[" << out.index <<","<< out.value<<"]";
return output;
}
template<class T>
inline bool operator < (Pair<T> P1, Pair<T> P2){
return P1.value < P2.value;
}
template<class T>
class SAS : public SA< Pair<T> >{
public:
SAS( unsigned int mysize): SA< Pair<T> >( mysize ){
int i;
for(i=0; i< (this->size()); i++){
(this->at(i)).index = i;
}
}
inline int & index( int i){
return (this->at(i)).index;
}
inline float & f( int i){
return (this->at(i)).value;
}
void Sort(){
Pair<T> * adr = &(this->at(0));
sort( adr, adr+((this->size())));
int i;
for(i=1; i<(this->size()); i++){
T v1 = (this->at(i-1)).value;
T v2 = (this->at(i )).value;
if( v1 > v2){
printf("Error: sas.cpp sort failed.\n");
printf("Sort failed at position %d\n", i);
exit(1);
}
}
}
void reset(){
int i;
for(i=0; i< (this->size()); i++){
(this->at(i)).index = i;
}
}
};
/*
int test(int N){
int i;
SAS<float> X(N);
for(i = 0; i < N; i++) X[i].value = (float)rand() / (float)RAND_MAX;
cout << X <<endl;
X.Sort();
cout << X << endl;
return 0;
}
*/