-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStationBookmarks.cpp
132 lines (109 loc) · 2.59 KB
/
StationBookmarks.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
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
#include <iostream>
#include <fstream>
#include <algorithm>
#include "StationBookmarks.h"
StationBookmarks::StationBookmarks(std::string file) {
this->file_path = file;
};
StationBookmarks::~StationBookmarks() {
};
bool StationBookmarks::isMarked(int bmark) {
int idx = this->indexOf(bmark);
if (-1 != idx) {
return true;
} else {
return false;
}
};
void StationBookmarks::add(int bmark) {
if (this->isMarked(bmark)) {
return; // don't add it again.
}
this->bookmarks.push_back(bmark);
std::sort(this->bookmarks.begin(), this->bookmarks.end());
};
void StationBookmarks::remove(int bmark) {
int idx = this->indexOf(bmark);
if (-1 != idx) {
this->bookmarks.erase(this->bookmarks.begin() + idx);
}
};
int StationBookmarks::size() {
return this->bookmarks.size();
};
int StationBookmarks::indexOf(int bmark) {
for (int i = 0; i < this->bookmarks.size(); ++i) {
if (this->bookmarks[i] == bmark) {
return i;
}
}
return -1;
};
int StationBookmarks::get(int idx) {
return this->bookmarks[idx];
};
int StationBookmarks::next(int bmark) {
int idx = this->indexOf(bmark);
if (-1 == idx) {
// find closest
int shortest_dist = -1;
int shortest_mark = -1;
for (int i = 0; i < this->bookmarks.size(); ++i) {
int cur_dist = abs(this->bookmarks[i] - bmark);
if (cur_dist < shortest_dist || shortest_mark == -1) {
shortest_mark = this->bookmarks[i];
shortest_dist = cur_dist;
}
}
return shortest_mark;
}
++idx;
if (idx >= this->bookmarks.size()) {
idx = 0; // wrap around
}
return this->bookmarks[idx];
};
int StationBookmarks::prev(int bmark) {
int idx = this->indexOf(bmark);
if (-1 == idx) {
// find closest
int shortest_dist = -1;
int shortest_mark = -1;
for (int i = 0; i < this->bookmarks.size(); ++i) {
int cur_dist = abs(this->bookmarks[i] - bmark);
if (cur_dist < shortest_dist || shortest_mark == -1) {
shortest_mark = this->bookmarks[i];
shortest_dist = cur_dist;
}
}
return shortest_mark;
}
--idx;
if (idx < 0) {
idx = this->bookmarks.size() - 1; // wrap around
}
return this->bookmarks[idx];
};
void StationBookmarks::save() {
std::ofstream bmarksFile;
bmarksFile.open(this->file_path);
for (int i = 0; i < this->size(); ++i) {
bmarksFile << this->get(i) << std::endl;
}
bmarksFile.close();
};
bool StationBookmarks::load() {
std::ifstream bmarksFile;
bmarksFile.open(this->file_path);
if (bmarksFile.fail()) {
return false;
}
std::string line;
while (std::getline(bmarksFile, line)) {
this->add(std::stoi(line));
}
bmarksFile.close();
return true;
};
// std::vector<int> bookmarks;
// std::string file_path;