2016-05-17 03:04:11 +12:00
|
|
|
#pragma once
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
#ifndef TCG_CYCLIC_H
|
|
|
|
#define TCG_CYCLIC_H
|
|
|
|
|
|
|
|
// STD includes
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
// tcg includes
|
|
|
|
#include "numeric_ops.h"
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace tcg {
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//************************************************************************
|
|
|
|
// Cyclic helpers
|
|
|
|
//************************************************************************
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
namespace cyclic_ops {
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
template <typename Scalar>
|
2016-06-15 18:43:10 +12:00
|
|
|
inline bool ll(Scalar a, Scalar b, Scalar c) {
|
|
|
|
return a <= c ? a < b && b < c : c > b || b > a;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
template <typename Scalar>
|
2016-06-15 18:43:10 +12:00
|
|
|
inline bool lel(Scalar a, Scalar b, Scalar c) {
|
|
|
|
return a <= c ? a <= b && b < c : c > b || b >= a;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
template <typename Scalar>
|
2016-06-15 18:43:10 +12:00
|
|
|
inline bool lle(Scalar a, Scalar b, Scalar c) {
|
|
|
|
return a <= c ? a < b && b <= c : c >= b || b > a;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
template <typename Scalar>
|
2016-06-15 18:43:10 +12:00
|
|
|
inline bool lele(Scalar a, Scalar b, Scalar c) {
|
|
|
|
return a <= c ? a <= b && b <= c : c >= b || b >= a;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#ifdef min
|
|
|
|
#undef min
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <typename Scalar>
|
2016-06-15 18:43:10 +12:00
|
|
|
Scalar min(Scalar a, Scalar b, Scalar ref) {
|
|
|
|
return lel(ref, a, b) ? a : b;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef max
|
|
|
|
#undef max
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template <typename Scalar>
|
2016-06-15 18:43:10 +12:00
|
|
|
Scalar max(Scalar a, Scalar b, Scalar ref) {
|
|
|
|
return lel(ref, a, b) ? a : b;
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <typename value_type, typename diff_type>
|
2016-06-15 18:43:10 +12:00
|
|
|
value_type increased(const value_type &val, const value_type &start,
|
|
|
|
diff_type length) {
|
|
|
|
return start + numeric_ops::mod(val - start + 1, length);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <typename value_type, typename diff_type>
|
2016-06-15 18:43:10 +12:00
|
|
|
value_type increased(const value_type &val, const value_type &start,
|
|
|
|
diff_type length, diff_type add) {
|
|
|
|
return start + numeric_ops::mod(val - start + add, length);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <typename value_type, typename diff_type>
|
2016-06-15 18:43:10 +12:00
|
|
|
value_type decreased(const value_type &val, const value_type &start,
|
|
|
|
diff_type length) {
|
|
|
|
return start + numeric_ops::mod(val - start - 1, length);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <typename value_type, typename diff_type>
|
2016-06-15 18:43:10 +12:00
|
|
|
value_type decreased(const value_type &val, const value_type &start,
|
|
|
|
diff_type length, diff_type add) {
|
|
|
|
return start + numeric_ops::mod(val - start - add, length);
|
2016-03-19 06:57:51 +13:00
|
|
|
}
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // namespace cyclic_ops
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
//************************************************************************
|
|
|
|
// Cyclic Iterators
|
|
|
|
//************************************************************************
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
template <typename It,
|
|
|
|
typename Cat = typename std::iterator_traits<It>::iterator_category>
|
2016-03-19 06:57:51 +13:00
|
|
|
class cyclic_iterator;
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <typename It>
|
|
|
|
class cyclic_iterator<It, std::forward_iterator_tag>
|
2016-06-15 18:43:10 +12:00
|
|
|
: public std::iterator<std::forward_iterator_tag,
|
|
|
|
typename std::iterator_traits<It>::value_type,
|
|
|
|
typename std::iterator_traits<It>::difference_type,
|
|
|
|
typename std::iterator_traits<It>::pointer,
|
|
|
|
typename std::iterator_traits<It>::reference> {
|
2016-03-19 06:57:51 +13:00
|
|
|
protected:
|
2016-06-15 18:43:10 +12:00
|
|
|
It m_it, m_begin, m_end;
|
|
|
|
int m_lap;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
cyclic_iterator() : m_lap(0) {}
|
|
|
|
cyclic_iterator(const It &it, const It &begin, const It &end, int lap = 0)
|
|
|
|
: m_it(it), m_begin(begin), m_end(end), m_lap(lap) {}
|
|
|
|
|
|
|
|
cyclic_iterator &operator++() {
|
|
|
|
if (++m_it == m_end) ++m_lap, m_it = m_begin;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
cyclic_iterator operator++(int) {
|
|
|
|
cyclic_iterator it(*this);
|
|
|
|
operator++();
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
typename cyclic_iterator::reference operator*() { return *m_it; }
|
|
|
|
typename cyclic_iterator::pointer operator->() { return m_it.operator->(); }
|
|
|
|
|
|
|
|
bool operator==(const cyclic_iterator &it) const {
|
|
|
|
return m_it == it.m_it && m_lap == it.m_lap;
|
|
|
|
}
|
|
|
|
bool operator!=(const cyclic_iterator &it) const { return !operator==(it); }
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <typename It>
|
|
|
|
class cyclic_iterator<It, std::bidirectional_iterator_tag>
|
2016-06-15 18:43:10 +12:00
|
|
|
: public std::iterator<std::bidirectional_iterator_tag,
|
|
|
|
typename std::iterator_traits<It>::value_type,
|
|
|
|
typename std::iterator_traits<It>::difference_type,
|
|
|
|
typename std::iterator_traits<It>::pointer,
|
|
|
|
typename std::iterator_traits<It>::reference> {
|
2016-03-19 06:57:51 +13:00
|
|
|
protected:
|
2016-06-15 18:43:10 +12:00
|
|
|
It m_it, m_begin, m_end;
|
|
|
|
int m_lap;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
cyclic_iterator() : m_lap(0) {}
|
|
|
|
cyclic_iterator(const It &it, const It &begin, const It &end, int lap = 0)
|
|
|
|
: m_it(it), m_begin(begin), m_end(end), m_lap(lap) {}
|
|
|
|
|
|
|
|
cyclic_iterator &operator++() {
|
|
|
|
if (++m_it == m_end) ++m_lap, m_it = m_begin;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
cyclic_iterator &operator--() {
|
|
|
|
if (m_it == m_begin) --m_lap, m_it = m_end;
|
|
|
|
--m_it;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
cyclic_iterator operator++(int) {
|
|
|
|
cyclic_iterator it(*this);
|
|
|
|
operator++();
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
cyclic_iterator operator--(int) {
|
|
|
|
cyclic_iterator it(*this);
|
|
|
|
operator--();
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
typename cyclic_iterator::reference operator*() { return *m_it; }
|
|
|
|
typename cyclic_iterator::pointer operator->() { return m_it.operator->(); }
|
|
|
|
|
|
|
|
bool operator==(const cyclic_iterator &it) const {
|
|
|
|
return m_it == it.m_it && m_lap == it.m_lap;
|
|
|
|
}
|
|
|
|
bool operator!=(const cyclic_iterator &it) const { return !operator==(it); }
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <typename It>
|
|
|
|
class cyclic_iterator<It, std::random_access_iterator_tag>
|
2016-06-15 18:43:10 +12:00
|
|
|
: public std::iterator<std::random_access_iterator_tag,
|
|
|
|
typename std::iterator_traits<It>::value_type,
|
|
|
|
typename std::iterator_traits<It>::difference_type,
|
|
|
|
typename std::iterator_traits<It>::pointer,
|
|
|
|
typename std::iterator_traits<It>::reference> {
|
2016-03-19 06:57:51 +13:00
|
|
|
protected:
|
2016-06-15 18:43:10 +12:00
|
|
|
It m_it, m_begin, m_end;
|
|
|
|
int m_lap;
|
|
|
|
typename cyclic_iterator::difference_type m_count;
|
2016-03-19 06:57:51 +13:00
|
|
|
|
|
|
|
public:
|
2016-06-15 18:43:10 +12:00
|
|
|
cyclic_iterator() : m_lap(0), m_count(0) {}
|
|
|
|
cyclic_iterator(const It &it, const It &begin, const It &end, int lap = 0)
|
|
|
|
: m_it(it)
|
|
|
|
, m_begin(begin)
|
|
|
|
, m_end(end)
|
|
|
|
, m_lap(lap)
|
|
|
|
, m_count(m_end - m_begin) {}
|
|
|
|
|
|
|
|
cyclic_iterator &operator++() {
|
|
|
|
if (++m_it == m_end) ++m_lap, m_it = m_begin;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
cyclic_iterator &operator--() {
|
|
|
|
if (m_it == m_begin) --m_lap, m_it = m_end;
|
|
|
|
--m_it;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
cyclic_iterator operator++(int) {
|
|
|
|
cyclic_iterator it(*this);
|
|
|
|
operator++();
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
cyclic_iterator operator--(int) {
|
|
|
|
cyclic_iterator it(*this);
|
|
|
|
operator--();
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
cyclic_iterator &operator+=(
|
|
|
|
const typename cyclic_iterator::difference_type &val) {
|
|
|
|
m_lap += (val + (m_it - m_begin)) / m_count;
|
|
|
|
m_it = cyclic_ops::increased(m_it, m_begin, m_count, val);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
cyclic_iterator &operator-=(
|
|
|
|
const typename cyclic_iterator::difference_type &val) {
|
|
|
|
m_lap -= (val + (m_end - m_it)) / m_count;
|
|
|
|
m_it = cyclic_ops::decreased(m_it, m_begin, m_count, val);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
typename cyclic_iterator::difference_type operator-(
|
|
|
|
const cyclic_iterator &it) const {
|
|
|
|
assert(m_begin == it.m_begin && m_end == it.m_end);
|
|
|
|
return m_it - it.m_it + m_lap * m_count - it.m_lap * it.m_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
cyclic_iterator operator+(
|
|
|
|
const typename cyclic_iterator::difference_type &val) const {
|
|
|
|
cyclic_iterator it(*this);
|
|
|
|
it += val;
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
cyclic_iterator operator-(
|
|
|
|
const typename cyclic_iterator::difference_type &val) const {
|
|
|
|
cyclic_iterator it(*this);
|
|
|
|
it -= val;
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
typename cyclic_iterator::reference operator*() const { return *m_it; }
|
|
|
|
typename cyclic_iterator::pointer operator->() const {
|
|
|
|
return m_it.operator->();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const cyclic_iterator &it) const {
|
|
|
|
return m_it == it.m_it && m_lap == it.m_lap;
|
|
|
|
}
|
|
|
|
bool operator!=(const cyclic_iterator &it) const { return !operator==(it); }
|
|
|
|
|
|
|
|
bool operator<(const cyclic_iterator &it) const {
|
|
|
|
return m_lap < it.m_lap || m_it < it.m_it;
|
|
|
|
}
|
|
|
|
bool operator>(const cyclic_iterator &it) const {
|
|
|
|
return m_lap > it.m_lap || m_it > it.m_it;
|
|
|
|
}
|
|
|
|
bool operator<=(const cyclic_iterator &it) const {
|
|
|
|
return m_lap <= it.m_lap || m_it <= it.m_it;
|
|
|
|
}
|
|
|
|
bool operator>=(const cyclic_iterator &it) const {
|
|
|
|
return m_lap >= it.m_lap || m_it >= it.m_it;
|
|
|
|
}
|
2016-03-19 06:57:51 +13:00
|
|
|
};
|
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
} // namespace tcg
|
2016-03-19 06:57:51 +13:00
|
|
|
|
2016-06-15 18:43:10 +12:00
|
|
|
#endif // TCG_CYCLIC_H
|