This repository has been archived by the owner on May 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsequence.h
229 lines (166 loc) · 5.27 KB
/
sequence.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
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
#include <stdexcept>
template <typename T> class Sequence{
public:
virtual T getFirst() const = 0;
virtual T getLast() const = 0;
virtual T get(int index) const = 0;
virtual int getSize() const = 0;
virtual Sequence<T>* getSubsequence(int start, int end) const = 0; //end excluding
virtual void set(const T &item, int index) = 0;
virtual void append(const T &item) = 0;
virtual void prepend(const T &item) = 0;
virtual void insertAt(const T &item, int index) = 0;
virtual Sequence<T>* concat(const Sequence<T>& seq) const = 0;
virtual bool operator==(const Sequence<T> &seq) const {
if(this->getSize() != seq.getSize()) return false;
for(int i = 0; i < this->getSize(); i++){
if(this->get(i) != seq.get(i)) return false;
}
return true;
}
};
template <typename T> class ArraySequence : public Sequence<T>{
protected:
DynamicArray<T> *array;
public:
ArraySequence(){
this->array = new DynamicArray<T>();
}
ArraySequence(const ArraySequence<T> &seq){
this->array = new DynamicArray<T>(*seq.array);
}
ArraySequence(DynamicArray<T>* array){
this->array = array;
}
ArraySequence(T *items, int size){
this->array = new DynamicArray<T>(items, size);
}
ArraySequence(int size){
this->array = new DynamicArray<T>(size);
}
virtual ~ArraySequence(){
delete this->array;
}
virtual T getFirst() const override {
return this->array->get(0);
}
virtual T getLast() const override {
return this->array->get(this->getSize() - 1);
}
virtual T get(int index) const override {
return this->array->get(index);
}
virtual int getSize() const override {
return this->array->getSize();
}
virtual void set(const T &item, int index) override {
if(index < 0 || index >= this->getSize()) throw std::out_of_range(INDEX_OUT_OF_RANGE_MESSAGE);
this->array->set(item, index);
}
virtual ArraySequence<T>* getSubsequence(int start, int end) const override {
if(start < 0 || start >= this->getSize()) throw std::out_of_range(INDEX_OUT_OF_RANGE_MESSAGE);
if(end < 0 || end > this->getSize()) throw std::out_of_range(INDEX_OUT_OF_RANGE_MESSAGE);
if(start > end) throw std::logic_error("end must be not less than start");
T subArray[end - start];
for(int i = 0; i < end - start; i++)
subArray[i] = this->array->get(i + start);
ArraySequence<T> *subSequence = new ArraySequence<T>(subArray, end - start);
return subSequence;
}
virtual void append(const T &item) override {
this->array->resize(this->getSize() + 1);
this->array->set(item, this->getSize() - 1);
}
virtual void prepend(const T &item) override {
this->array->resize(this->getSize() + 1);
T t1 = this->array->get(0);
T t2;
for(int i = 0; i < this->getSize() - 1; i++){
t2 = t1;
t1 = this->array->get(i + 1);
this->array->set(t2, i + 1);
}
this->array->set(item, 0);
}
virtual void insertAt(const T &item, int index) override {
if(index < 0 || index > this->getSize()) throw std::out_of_range(INDEX_OUT_OF_RANGE_MESSAGE);
this->array->resize(this->getSize() + 1);
T t1 = this->array->get(index);
T t2;
for(int i = index; i < this->getSize() - 1; i++){
t2 = t1;
t1 = this->array->get(i + 1);
this->array->set(t2, i + 1);
}
this->array->set(item, index);
}
virtual ArraySequence<T>* concat(const Sequence<T>& seq) const override {
DynamicArray<T> *array = new DynamicArray<T>(this->getSize() + seq.getSize());
ArraySequence<T> *newSequence = new ArraySequence<T>(array);
for(int i = 0; i < this->getSize(); i++)
newSequence->set(this->get(i), i);
for(int i = 0; i < seq.getSize(); i++)
newSequence->set(seq.get(i), i + this->getSize());
return newSequence;
}
};
template <typename T> class ListSequence : public Sequence<T>{
protected:
LinkedList<T> *list;
public:
ListSequence(){
this->list = new LinkedList<T>();
}
ListSequence(const ListSequence<T> &seq){
this->list = new LinkedList<T>(*seq.list);
}
ListSequence(LinkedList<T> *list){
this->list = list;
}
ListSequence(T *items, int size){
this->list = new LinkedList<T>(items, size);
}
ListSequence(int size){
this->list = new LinkedList<T>(size);
}
virtual ~ListSequence(){
delete this->list;
}
virtual T getFirst() const override {
return this->list->getFirst();
}
virtual T getLast() const override {
return this->list->getLast();
}
virtual T get(int index) const override {
return this->list->get(index);
}
virtual int getSize() const override {
return this->list->getSize();
}
virtual ListSequence<T>* getSubsequence(int start, int end) const override {
LinkedList<T> *subList = this->list->getSublist(start, end);
ListSequence<T> *seq = new ListSequence<T>(subList);
return seq;
}
virtual void set(const T &item, int index) override {
this->list->set(item, index);
}
virtual void append(const T &item) override {
this->list->append(item);
}
virtual void prepend(const T &item) override {
this->list->prepend(item);
}
virtual void insertAt(const T &item, int index) override {
this->list->insertAt(item, index);
}
virtual ListSequence<T>* concat(const Sequence<T>& seq) const override {
ListSequence<T> *newSequence = new ListSequence<T>();
for(int i = 0; i < this->getSize(); i++)
newSequence->append(this->get(i));
for(int i = 0; i < seq.getSize(); i++)
newSequence->append(seq.get(i));
return newSequence;
}
};