-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSchedule.cc
197 lines (156 loc) · 4.5 KB
/
Schedule.cc
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
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iterator>
#include "Schedule.h"
using namespace std;
//Schedule implementation
Schedule::Schedule() {}
Schedule::Schedule(const string& filename){
ifstream file(filename);
if(file.is_open()){
read(file);
}
}
Schedule::Schedule(istream& is){
read(is);
}
Schedule::Schedule(const Schedule& other):events_(other.events_) {}
Schedule& Schedule::operator=(const Schedule& other){
events_ = other.events_;
return *this;
}
Schedule::~Schedule() {}
void Schedule::read(istream& is){
string line;
while (getline(is, line)) {
istringstream iss(line);
vector<string> words;
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter(words));
for (const auto& word : words) {
events_.emplace_back(word.c_str());
sort();
}
}
}
void Schedule::clear(){
events_.clear();
}
size_t Schedule::size() const {
return events_.size();
}
bool Schedule::empty() const {
return events_.empty();
}
void Schedule::sort(){
std::sort(events_.begin(), events_.end(), [](const Event& e1, const Event& e2){
if(e1.year() < e2.year()){
return true;
}else if(e1.year() > e2.year()){
return false;
}else if(e1.month() < e2.month()){
return true;
}else if(e1.month() > e2.month()){
return false;
}else{
return e1.day() < e2.day();
}
});
}
const Event& Schedule::operator[](size_t index) const {
return events_.at(index);
}
ostream& operator<<(ostream& os, const Schedule& schedule) {
for (const auto& event : schedule.events_) {
os << event << '\n';
}
return os;
}
//Schedule iterator implementation
Schedule::iterator::iterator():schedule_(nullptr), index_(0) {}
Schedule::iterator::iterator(const iterator& other):
schedule_(other.schedule_), index_(other.index_) {}
Schedule::iterator::iterator(Schedule* schedule, std::size_t index) :
schedule_(schedule), index_(index) {}
// Increment operators
Schedule::iterator& Schedule::iterator::operator++() {
index_++;
return *this;
}
Schedule::iterator Schedule::iterator::operator++(int) {
iterator tmp(*this);
operator++();
return tmp;
}
// Decrement operators
Schedule::iterator& Schedule::iterator::operator--() {
index_--;
return *this;
}
Schedule::iterator Schedule::iterator::operator--(int) {
iterator tmp(*this);
operator--();
return tmp;
}
// Dereference operator
const Event& Schedule::iterator::operator*() const {
return schedule_->events_[index_];
}
// Comparison operators
bool Schedule::iterator::operator==(const iterator& other) const {
return schedule_ == other.schedule_ && index_ == other.index_;
}
bool Schedule::iterator::operator!=(const iterator& other) const {
return !(*this == other);
}
Schedule::iterator Schedule::begin() {
return Schedule::iterator(this, 0);
}
Schedule::iterator Schedule::end() {
return Schedule::iterator(this, events_.size());
}
//const iterator implementation
Schedule::const_iterator::const_iterator()
: schedule_(nullptr), index_(0) {}
Schedule::const_iterator::const_iterator(const const_iterator& other)
: schedule_(other.schedule_), index_(other.index_) {}
Schedule::const_iterator::const_iterator(const Schedule* schedule, std::size_t index)
: schedule_(schedule), index_(index) {}
Schedule::const_iterator& Schedule::const_iterator::operator++() {
++index_;
return *this;
}
Schedule::const_iterator Schedule::const_iterator::operator++(int) {
const_iterator result(*this);
++(*this);
return result;
}
Schedule::const_iterator& Schedule::const_iterator::operator--() {
--index_;
return *this;
}
Schedule::const_iterator Schedule::const_iterator::operator--(int) {
const_iterator result(*this);
--(*this);
return result;
}
const Event& Schedule::const_iterator::operator*() const {
return schedule_->events_[index_];
}
bool Schedule::const_iterator::operator==(const const_iterator& other) const {
return schedule_ == other.schedule_ && index_ == other.index_;
}
bool Schedule::const_iterator::operator!=(const const_iterator& other) const {
return !(*this == other);
}
Schedule::const_iterator Schedule::begin() const {
return Schedule::const_iterator(this, 0);
}
Schedule::const_iterator Schedule::end() const {
return Schedule::const_iterator(this, events_.size());
}
Schedule::const_iterator::operator iterator() const {
return Schedule::iterator(const_cast<Schedule*>(schedule_), index_);
}