-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotarray.h
More file actions
executable file
·149 lines (115 loc) · 2.39 KB
/
Rotarray.h
File metadata and controls
executable file
·149 lines (115 loc) · 2.39 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
#ifndef _ROTARRAY_H
#define _ROTARRAY_H
#include <iostream>
template<typename T>
class Rotarray
{
private:
T*data;
int startIndex;
int size;
public:
int getSize()const{
return size;
}
Rotarray(int _size):size(_size),startIndex(0){
data=new T[size];
}
void copy(const Rotarray& right){
if(data){
delete[] data;
}
size=right.size;
startIndex=right.startIndex;
data=new T[size];
for(int i=0;i<size;i++){
(*this)[i]=right[i];
}
}
Rotarray(const Rotarray& right):data(NULL){
//copy constructor
copy(right);
}
const Rotarray& operator=(const Rotarray& right){
copy(right);
return *this;
}
T& operator [](int _offset){
return data[(_offset+startIndex)%size];
}
const T& operator [](int _offset)const{
return data[(_offset+startIndex)%size];
}
~Rotarray(){
if(data)
delete[] data;
}
bool operator>(const Rotarray& right)const{
int sizeCompare=min(size,right.size);
for(int i=0;i<sizeCompare;i++){
if((*this)[i]!=right[i]){
return (*this)[i]>right[i];
}
}
//every has been equal till now, so compare sizes;
return size>right.size;
}
bool operator<(const Rotarray& right) const{
int sizeCompare=min(size,right.size);
for(int i=0;i<sizeCompare;i++){
if((*this)[i]!=right[i]){
return (*this)[i]<right[i];
}
}
return size<right.size;
}
bool operator==(const Rotarray& right) const{
if(size!=right.size)
return false;
for(int i=0;i<size;i++){
if((*this)[i]!=right[i]){
return false;
}
}
return true;
}
bool operator!=(const Rotarray& right) const{
return !(*this==right);
}
bool operator<=(const Rotarray& right) const{
return !(this>right);
}
bool operator>=(const Rotarray& right) const{
return !(this<right);
}
void shiftRight(){
startIndex++;
if(startIndex>=size){
startIndex=0;
}
}
void shiftLeft(){
startIndex--;
if(startIndex<0){
startIndex=size-1;
}
}
void reset(){
startIndex=0;
}
void setStartIndex(int _newStartIndex){
startIndex=_newStartIndex%size;
}
};
template<typename T>
ostream& operator<<(ostream& os,const Rotarray<T>& obj){
if(obj.getSize()<1)
return os;
os<<"["<<obj[0];
for(int i=1;i<obj.getSize();i++){
os<<","<<obj[i];
}
os<<"]";
return os;
}
#endif