DxLibEx
constant_range_loop.hpp
Go to the documentation of this file.
1 /*=============================================================================
2 Copyright (C) 2015-2017 DxLibEx project
3 https://github.com/Nagarei/DxLibEx/
4 
5 Distributed under the Boost Software License, Version 1.0.
6 (See http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 #ifndef DXLE_INC_UTILITY_CONSTANT_RANGE_LOOP_HPP_
9 #define DXLE_INC_UTILITY_CONSTANT_RANGE_LOOP_HPP_
10 
11 #include <cstddef>
12 #include <iterator>
13 #include "dxlibex/type_traits.hpp"
14 #include "dxlibex/config/defines.h"
15 
16 namespace dxle {
17  namespace utility {
18  template<typename T, enable_if_t<std::is_arithmetic<T>::value, nullptr_t> = nullptr>
19  class counter_iterator : std::iterator<std::random_access_iterator_tag, T> {
20  private:
21  T i;
22  typedef std::iterator<std::random_access_iterator_tag, T> my_t;
23  public:
24  typedef typename my_t::iterator_category iterator_category;
25  typedef typename my_t::value_type value_type;
26  typedef typename my_t::difference_type difference_type;
27  typedef typename my_t::pointer pointer;
28  typedef typename my_t::reference reference;
29 
33  i = o.i;
34  return *this;
35  }
36  DXLE_CONSTEXPR bool operator == (const counter_iterator & rhs) const DXLE_NOEXCEPT_OR_NOTHROW { return i == rhs.i; }
37  DXLE_CONSTEXPR bool operator != (const counter_iterator & rhs) const DXLE_NOEXCEPT_OR_NOTHROW { return i != rhs.i; }
38  DXLE_CONSTEXPR const T & operator * () const DXLE_NOEXCEPT_OR_NOTHROW { return i; }
39 #ifndef DXLE_SUPPORT_ONLY_CXX11_CONSTEXPR
41 #endif
42  DXLE_CONSTEXPR T operator[](difference_type n) const DXLE_NOEXCEPT_OR_NOTHROW { return i + n; }
43  DXLE_CONSTEXPR counter_iterator operator+(difference_type n) const DXLE_NOEXCEPT_OR_NOTHROW { return{ i + n }; }
44  DXLE_CONSTEXPR counter_iterator operator-(difference_type n) const DXLE_NOEXCEPT_OR_NOTHROW { return{ i - n }; }
45  DXLE_CONSTEXPR bool operator<(const counter_iterator& n) const DXLE_NOEXCEPT_OR_NOTHROW { return i < n.i; }
46  DXLE_CONSTEXPR bool operator<=(const counter_iterator& n) const DXLE_NOEXCEPT_OR_NOTHROW { return i <= n.i; }
47  DXLE_CONSTEXPR bool operator>(const counter_iterator& n) const DXLE_NOEXCEPT_OR_NOTHROW { return i > n.i; }
48  DXLE_CONSTEXPR bool operator>=(const counter_iterator& n) const DXLE_NOEXCEPT_OR_NOTHROW { return i >= n.i; }
50  i += it.i;
51  return *this;
52  }
54  i -= it.i;
55  return *this;
56  }
58  ++i;
59  return *this;
60  }
62  auto t = *this;
63  ++i;
64  return t;
65  }
67  --i;
68  return *this;
69  }
71  auto t = *this;
72  --i;
73  return t;
74  }
75  };
76  template<typename T>
78  template<typename T>
80  template<typename T>
82 
83  template<typename T, enable_if_t<std::is_arithmetic<T>::value, nullptr_t> = nullptr>
84  class nth_loop {
85  public:
86  using value_type = std::remove_cv_t<T>;
88  private:
89  value_type begin_, end_;
90  public:
91  DXLE_CONSTEXPR nth_loop(T n) : begin_(0), end_(n) {}
92  DXLE_CONSTEXPR nth_loop(T begin, T end) : begin_(begin), end_(end) {}
94  return{ begin_ };
95  }
97  return{ end_ };
98  }
99  };
100 
101  template<typename T, enable_if_t<std::is_arithmetic<T>::value, nullptr_t> = nullptr>
103  return{ n };
104  }
105  template<typename T, enable_if_t<std::is_arithmetic<T>::value, nullptr_t> = nullptr>
106  DXLE_CONSTEXPR nth_loop<T> rep(const T& begin, const T& end) DXLE_NOEXCEPT_OR_NOTHROW {
107  return{ begin, end };
108  }
109  }
110  using namespace utility;
111 }
112 namespace std {
113  template<typename T>
115  return it + n;
116  }
117 }
118 #endif //DXLE_INC_UTILITY_CONSTANT_RANGE_LOOP_HPP_
DXLE_CONSTEXPR nth_loop< T > rep(T n) DXLE_NOEXCEPT_OR_NOTHROW
#define DXLE_CONSTEXPR
Definition: suffix.hpp:21
DXLE_CONSTEXPR bool operator>(const counter_iterator &n) const DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CONSTEXPR bool operator!=(const counter_iterator &rhs) const DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CXX14_CONSTEXPR counter_iterator & operator=(const counter_iterator &o) DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CONSTEXPR bool operator<=(const counter_iterator &n) const DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CXX14_CONSTEXPR counter_iterator & operator+=(counter_iterator it) DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CONSTEXPR const T & operator*() const DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CXX14_CONSTEXPR counter_iterator & operator-=(counter_iterator it) DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CONSTEXPR iterator end() const DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CXX14_CONSTEXPR counter_iterator & operator--() DXLE_NOEXCEPT_OR_NOTHROW
my_t::iterator_category iterator_category
Definition: point2d.hpp:672
DXLE_CONSTEXPR nth_loop(T n)
Definition: cast_if.hpp:12
DXLE_CONSTEXPR bool operator<(const counter_iterator &n) const DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CONSTEXPR counter_iterator operator+(difference_type n) const DXLE_NOEXCEPT_OR_NOTHROW
#define DXLE_CXX14_CONSTEXPR
Definition: suffix.hpp:42
DXLE_CONSTEXPR counter_iterator< T > iterator_next(counter_iterator< T > it) DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CONSTEXPR iterator begin() const DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CONSTEXPR bool operator>=(const counter_iterator &n) const DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CXX14_CONSTEXPR counter_iterator & operator++() DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CONSTEXPR nth_loop(T begin, T end)
DXLE_CONSTEXPR T operator[](difference_type n) const DXLE_NOEXCEPT_OR_NOTHROW
#define DXLE_NOEXCEPT_OR_NOTHROW
Definition: suffix.hpp:94
DXLE_CONSTEXPR dxle::utility::counter_iterator< T > next(dxle::utility::counter_iterator< T > it, typename dxle::utility::counter_iterator< T >::difference_type n) DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CONSTEXPR bool operator==(const counter_iterator &rhs) const DXLE_NOEXCEPT_OR_NOTHROW
DXLE_CONSTEXPR counter_iterator operator-(difference_type n) const DXLE_NOEXCEPT_OR_NOTHROW
std::remove_cv_t< T > value_type