-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice.cpp
More file actions
46 lines (35 loc) · 1.12 KB
/
slice.cpp
File metadata and controls
46 lines (35 loc) · 1.12 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
#include <string>
using std::string;
#ifndef SLICE_CPP
#define SLICE_CPP
//both ends inclusive!
class Slice {
public:
long start, end;
inline long size() const {
return end - start + 1;
}
string apply(string s) const {
return s.substr(start, size());
}
bool is_inbounds(int max) const {
return start <= end && end <= max;
}
bool is_invalid() const {
return start == -1 && end == -1;
}
bool operator>(const Slice& other) const {
if (is_invalid()) return false;
if (other.is_invalid()) return true;
return (size() > other.size()) || (size() == other.size() && start > other.start);
}
bool operator==(const Slice& other) const {
return start == other.start && end == other.end;
}
};
//for every substring, there is a corresponding substring in the reverse of the string
//for a substring containing the characters with indicies 0 and 1, the corresponding one will have the last 2 indicies
Slice reverse_slice(Slice slice, long string_length) {
return {string_length-slice.end-1, string_length-slice.start-1};
}
#endif