-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatterns.h
More file actions
148 lines (139 loc) · 3.82 KB
/
patterns.h
File metadata and controls
148 lines (139 loc) · 3.82 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
#ifndef PATTERNS_H_INCLUDED
#define PATTERNS_H_INCLUDED
#include <vector>
using namespace std;
const size_t MaxSize = 100;
template<typename T>
class Iterator {
protected:
Iterator() {}
public:
virtual ~Iterator() {}
virtual void First() = 0;
virtual void Next() = 0;
virtual bool IsDone() const = 0;
virtual T GetCurrent() const = 0;
};
template <typename T>
class Containers {
public:
virtual ~Containers() {}
virtual bool IsEmpty() const = 0;
virtual size_t Size() const = 0;
virtual T GetElementByIndex(size_t index) const = 0;
virtual Iterator<T>* GetIterator() = 0;
};
template<typename T>
class StackIterator : public Iterator<T> {
private:
const Containers<T>* StackContainer;
size_t Pos;
public:
StackIterator(const Containers<T>* container)
: StackContainer(container), Pos(0) {}
void First() override { Pos = 0; }
void Next() override { Pos++; }
bool IsDone() const override { return (Pos >= StackContainer->Size()); }
T GetCurrent() const override { return StackContainer->GetElementByIndex(Pos); }
};
template<typename T>
class Stack : public Containers<T> {
private:
T Bottles[MaxSize];
size_t Border;
public:
bool IsEmpty() const override {
return (Border == 0);
}
size_t Size() const override {
return Border;
}
void Push(T newBottle) {
Bottles[Border++] = newBottle;
}
T Pop() {
return Bottles[--Border];
}
T GetElementByIndex(size_t index) const override {
return Bottles[index];
}
Iterator<T>* GetIterator() override {
return new StackIterator<T>(this);
}
Stack() : Border(0) {}
};
template<typename T>
class VectorIterator : public Iterator<T> {
private:
const Containers<T>* VectorContainer;
size_t Pos;
public:
VectorIterator(const Containers<T>* container)
: VectorContainer(container), Pos(0) {}
void First() override { Pos = 0; }
void Next() override { Pos++; }
bool IsDone() const override { return (Pos >= VectorContainer->Size()); }
T GetCurrent() const override { return VectorContainer->GetElementByIndex(Pos); }
};
template<typename T>
class Vector : public Containers<T> {
private:
vector<T> elements;
public:
Vector() = default;
bool IsEmpty() const override {
return elements.empty();
}
size_t Size() const override {
return elements.size();
}
void Push(const T& newElement) {
elements.push_back(newElement);
}
T Top() const {
return elements.back();
}
T Pop() {
T last = elements.back();
elements.pop_back();
return last;
}
T GetElementByIndex(size_t index) const override {
return elements[index];
}
Iterator<T>* GetIterator() override {
return new VectorIterator<T>(this);
}
};
template<typename T>
class IteratorDecorator : public Iterator<T>
{
protected:
Iterator<T> *It;
public:
IteratorDecorator(Iterator<T> *it) : It(it) {}
virtual ~IteratorDecorator() { delete It; }
virtual void First() { It->First(); }
virtual void Next() { It->Next(); }
virtual bool IsDone() const { return It->IsDone(); }
virtual T GetCurrent() const { return It->GetCurrent(); }
};
template<typename ContainerType, typename ItemType>
class ConstIteratorAdapter : public Iterator<ItemType>
{
protected:
ContainerType *Container;
typename ContainerType::const_iterator It;
public:
ConstIteratorAdapter(ContainerType *container)
: Container(container)
{
It = Container->begin();
}
virtual ~ConstIteratorAdapter() {}
virtual void First() { It = Container->begin(); }
virtual void Next() { It++; }
virtual bool IsDone() const { return (It == Container->end()); }
virtual ItemType GetCurrent() const { return *It; }
};
#endif // PATTERNS_H_INCLUDED