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
|
// strings/base.tcc - Inline functions for strings/base.hpp
//
// Copyright © 2013 Ben Longbons <b.r.longbons@gmail.com>
//
// This file is part of The Mana World (Athena server)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <cassert>
// simple pointer-wrapping iterator
template<class T>
class Slice<T>::iterator
{
typedef iterator X;
T *_ptr;
public:
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T *pointer;
typedef T& reference;
typedef std::random_access_iterator_tag iterator_category;
iterator(T *p=nullptr) : _ptr(p) {}
// iterator
reference operator *() const { return *_ptr; }
X& operator ++() { ++_ptr; return *this; }
// equality comparable
friend bool operator == (X l, X r) { return l._ptr == r._ptr; }
// input iterator
friend bool operator != (X l, X r) { return !(l == r); }
pointer operator->() const { return _ptr; }
X operator++ (int) { X out = *this; ++*this; return out; }
// forward iterator is mostly semantical, and the ctor is above
// bidirectional iterator
X& operator --() { --_ptr; return *this; }
X operator-- (int) { X out = *this; --*this; return out; }
// random access iterator
X& operator += (difference_type n) { _ptr += n; return *this; }
friend X operator + (X a, difference_type n) { return a += n; }
friend X operator + (difference_type n, X a) { return a += n; }
X& operator -= (difference_type n) { _ptr -= n; return *this; }
friend X operator - (X a, difference_type n) { return a -= n; }
friend difference_type operator - (X b, X a) { return b._ptr - a._ptr; }
reference operator[](difference_type n) const { return _ptr[n]; }
friend bool operator < (X a, X b) { return a._ptr < b._ptr; }
friend bool operator > (X a, X b) { return b < a; }
friend bool operator >= (X a, X b) { return !(a < b); }
friend bool operator <= (X a, X b) { return !(a > b); }
};
template<class T>
Slice<T>::Slice(std::nullptr_t) : _begin(nullptr), _end(nullptr)
{}
template<class T>
Slice<T>::Slice(T *b, T *e) : _begin(b), _end(e)
{}
template<class T>
Slice<T>::Slice(T *b, size_t l) : _begin(b), _end(b + l)
{}
template<class T>
template<class U, typename>
Slice<T>::Slice(Slice<U> o) : _begin(o.data()), _end(o.data() + o.size())
{}
template<class T>
template<size_t n, typename>
Slice<T>::Slice(T (&arr)[n]) : _begin(arr), _end(arr + n)
{}
template<class T>
Slice<T>::Slice(std::vector<T>& vec) : _begin(&*vec.begin()), _end(&*vec.end())
{}
template<class T>
typename Slice<T>::iterator Slice<T>::begin() const
{
return _begin;
}
template<class T>
typename Slice<T>::iterator Slice<T>::end() const
{
return _end;
}
template<class T>
T *Slice<T>::data() const
{
return _begin;
}
template<class T>
size_t Slice<T>::size() const
{
return _end - _begin;
}
template<class T>
Slice<T>::operator bool() const
{
return _begin != _end;
}
template<class T>
bool Slice<T>::operator not() const
{
return _begin == _end;
}
template<class T>
T& Slice<T>::front() const
{
return _begin[0];
}
template<class T>
T& Slice<T>::back() const
{
return _end[-1];
}
template<class T>
T& Slice<T>::pop_front()
{
++_begin;
return _begin[0 - 1];
}
template<class T>
T& Slice<T>::pop_back()
{
--_end;
return _end[-1 + 1];
}
template<class T>
T& Slice<T>::operator[](size_t o)
{
assert (o < size());
return _begin[o];
}
template<class T>
Slice<T> Slice<T>::slice_t(size_t o) const
{
return Slice(_begin + o, _end);
}
template<class T>
Slice<T> Slice<T>::slice_h(size_t o) const
{
return Slice(_begin, _begin + o);
}
template<class T>
Slice<T> Slice<T>::rslice_t(size_t no) const
{
return Slice(_end - no, _end);
}
template<class T>
Slice<T> Slice<T>::rslice_h(size_t no) const
{
return Slice(_begin, _end - no);
}
template<class T>
Slice<T> Slice<T>::islice_t(iterator it) const
{
return Slice(&*it, _end);
}
template<class T>
Slice<T> Slice<T>::islice_h(iterator it) const
{
return Slice(_begin, &*it);
}
template<class T>
Slice<T> Slice<T>::lslice(size_t o, size_t l) const
{
return Slice(_begin + o, _begin + o + l);
}
template<class T>
Slice<T> Slice<T>::pslice(size_t b, size_t e) const
{
return Slice(_begin + b, _begin + e);
}
template<class T>
Slice<T> Slice<T>::islice(iterator b, iterator e) const
{
return Slice(&*b, &*e);
}
|