| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <iterator> |
| 4 |
|
| 5 |
template <typename I> |
| 6 |
struct null_sentinel_sentinel |
| 7 |
{ |
| 8 |
}; |
| 9 |
|
| 10 |
template <typename I> |
| 11 |
class null_sentinel_iterator |
| 12 |
{ |
| 13 |
public: |
| 14 |
using iterator_category = std::forward_iterator_tag; |
| 15 |
using value_type = I; |
| 16 |
using difference_type = std::ptrdiff_t; |
| 17 |
using pointer = I *; |
| 18 |
using reference = I &; |
| 19 |
constexpr null_sentinel_iterator() = default; |
| 20 |
constexpr null_sentinel_iterator(const pointer i) : |
| 21 |
p(i) |
| 22 |
{ |
| 23 |
} |
| 24 |
pointer get() const |
| 25 |
{ |
| 26 |
return p; |
| 27 |
} |
| 28 |
reference operator*() const |
| 29 |
{ |
| 30 |
return *p; |
| 31 |
} |
| 32 |
null_sentinel_iterator &operator++() |
| 33 |
{ |
| 34 |
++p; |
| 35 |
return *this; |
| 36 |
} |
| 37 |
null_sentinel_iterator operator++(int) |
| 38 |
{ |
| 39 |
auto r = *this; |
| 40 |
++ *this; |
| 41 |
return r; |
| 42 |
} |
| 43 |
constexpr bool operator==(const null_sentinel_iterator &) const = default; |
| 44 |
constexpr bool operator==(null_sentinel_sentinel<I>) const |
| 45 |
{ |
| 46 |
return !**this; |
| 47 |
} |
| 48 |
private: |
| 49 |
pointer p{nullptr}; |
| 50 |
}; |
| 51 |
|
| 52 |
template <typename I> |
| 53 |
class null_sentinel_array |
| 54 |
{ |
| 55 |
I *const b; |
| 56 |
public: |
| 57 |
constexpr null_sentinel_array(I *const i) : |
| 58 |
b(i) |
| 59 |
{ |
| 60 |
} |
| 61 |
null_sentinel_iterator<I> begin() const |
| 62 |
{ |
| 63 |
return b; |
| 64 |
} |
| 65 |
null_sentinel_sentinel<I> end() const |
| 66 |
{ |
| 67 |
return {}; |
| 68 |
} |
| 69 |
}; |