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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
#ifndef CORE_ITERATOR_HPP
#define CORE_ITERATOR_HPP
#include <functional>
#include <iterator>
#include <ostream>
#include <core/type_traits.hpp>
#include <core/utility.hpp>
namespace core {
inline namespace v2 {
/* capacity */
template <class Container>
constexpr auto size (Container const& container) noexcept -> decltype(
container.size()
) { return container.size(); }
template <class T, ::std::size_t N>
constexpr ::std::size_t size (T const (&)[N]) noexcept { return N; }
template <class Container>
constexpr bool empty (Container const& container) noexcept {
return container.empty();
}
template <class T, std::size_t N>
constexpr bool empty (T const (&)[N]) noexcept { return false; }
/* element access */
template <class Container>
constexpr auto front (Container const& container) -> decltype(
container.front()
) { return container.front(); }
template <class Container>
constexpr auto front (Container& container) -> decltype(container.front()) {
return container.front();
}
template <class T, ::std::size_t N>
constexpr T const& front (T const (&array)[N]) noexcept { return array[0]; }
template <class T, ::std::size_t N>
constexpr T& front (T (&array)[N]) noexcept { return array[0]; }
template <class Container>
constexpr auto back (Container const& container) -> decltype(
container.back()
) { return container.back(); }
template <class Container>
constexpr auto back (Container& container) -> decltype(container.back()) {
return container.back();
}
template <class T, ::std::size_t N>
constexpr T const& back (T const (&array)[N]) noexcept { return array[N - 1]; }
template <class T, ::std::size_t N>
constexpr T& back (T (&array)[N]) noexcept { return array[N - 1]; }
/* data access */
template <class Container>
constexpr auto data (Container const& container) noexcept -> decltype(
container.data()
) { return container.data(); }
template <class Container>
constexpr auto data (Container& container) noexcept -> decltype(
container.data()
) { return container.data(); }
template <class T, ::std::size_t N>
constexpr T const* data (T const (&array)[N]) noexcept { return array; }
template <class T, ::std::size_t N>
constexpr T* data (T (&array)[N]) noexcept { return array; }
/* iteration */
template <class Container>
auto cbegin (Container const& container) -> decltype(::std::begin(container)) {
return ::std::begin(container);
}
template <class Container>
auto cend (Container const& container) -> decltype(::std::end(container)) {
return ::std::end(container);
}
template <class Container>
auto rbegin (Container const& container) -> decltype(container.rbegin()) {
return container.rbegin();
}
template <class Container>
auto rbegin (Container& container) -> decltype(container.rbegin()) {
return container.rbegin();
}
template <class Container>
auto crbegin (Container const& container) -> decltype(rbegin(container)) {
return rbegin(container);
}
template <class Container>
auto rend (Container const& container) -> decltype(container.rend()) {
return container.rend();
}
template <class Container>
auto rend (Container& container) -> decltype(container.rend()) {
return container.rend();
}
template <class Container>
auto crend (Container const& container) -> decltype(rend(container)) {
return rend(container);
}
template <class Iterator>
::std::reverse_iterator<Iterator> make_reverse_iterator (Iterator iter) {
return ::std::reverse_iterator<Iterator>(iter);
}
template <
class DelimT,
class CharT=char,
class Traits=::std::char_traits<CharT>
> struct ostream_joiner final : ::std::iterator<
::std::output_iterator_tag,
void,
void,
void,
void
> {
using delimiter_type = DelimT;
using ostream_type = ::std::basic_ostream<CharT, Traits>;
using traits_type = Traits;
using char_type = CharT;
ostream_joiner (ostream_type& stream, delimiter_type const& delimiter) :
stream(stream),
delimiter { delimiter }
{ }
ostream_joiner (ostream_type& stream, delimiter_type&& delimiter) :
stream(stream),
delimiter { ::core::move(delimiter) },
first { true }
{ }
template <class T>
ostream_joiner& operator = (T const& item) {
if (not first and delimiter) { this->stream << delimiter; }
this->stream << item;
this->first = false;
return *this;
}
ostream_joiner& operator ++ (int) noexcept { return *this; }
ostream_joiner& operator ++ () noexcept { return *this; }
ostream_joiner& operator * () noexcept { return *this; }
private:
ostream_type& stream;
delimiter_type delimiter;
bool first;
};
template <class T>
struct number_iterator {
using iterator_category = ::std::bidirectional_iterator_tag;
using difference_type = T;
using value_type = T;
using reference = add_lvalue_reference_t<T>;
using pointer = add_pointer_t<T>;
static_assert(::std::is_integral<value_type>::value, "");
explicit number_iterator (value_type value, value_type step=1) noexcept :
value { value },
step { step }
{ }
number_iterator (number_iterator const&) noexcept = default;
number_iterator () noexcept = default;
~number_iterator () noexcept = default;
number_iterator& operator = (number_iterator const&) noexcept = default;
void swap (number_iterator& that) noexcept {
::std::swap(this->value, that.value);
::std::swap(this->step, that.step);
}
reference operator * () noexcept { return this->value; }
number_iterator& operator ++ () noexcept {
this->value += this->step;
return *this;
}
number_iterator& operator -- () noexcept {
this->value -= this->step;
return *this;
}
number_iterator operator ++ (int) const noexcept {
return number_iterator { this->value + this->step };
}
number_iterator operator -- (int) const noexcept {
return number_iterator { this->value - this->step };
}
bool operator == (number_iterator const& that) const noexcept {
return this->value == that.value and this->step == that.step;
}
bool operator != (number_iterator const& that) const noexcept {
return this->value != that.value and this->step == that.step;
}
private:
value_type value { };
value_type step { static_cast<value_type>(1) };
};
template <class T>
void swap (number_iterator<T>& lhs, number_iterator<T>& rhs) noexcept {
lhs.swap(rhs);
}
template <class CharT, class Traits, class DelimT>
ostream_joiner<decay_t<DelimT>, CharT, Traits> make_ostream_joiner (
::std::basic_ostream<CharT, Traits>& stream,
DelimT&& delimiter
) {
return ostream_joiner<decay_t<DelimT>, CharT, Traits> {
stream,
::core::forward<DelimT>(delimiter)
};
}
template <class T>
number_iterator<T> make_number_iterator (T value, T step) noexcept {
return number_iterator<T> { value, step };
}
template <class T>
number_iterator<T> make_number_iterator (T value) noexcept {
return number_iterator<T> { value };
}
}} /* namespace core::v2 */
#endif /* CORE_ITERATOR_HPP */
|