2012-07-08 12:05:24 +00:00
|
|
|
#ifndef SPROUT_ITERATOR_SQUARE_ITERATOR_HPP
|
|
|
|
#define SPROUT_ITERATOR_SQUARE_ITERATOR_HPP
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
#include <iterator>
|
|
|
|
#include <utility>
|
|
|
|
#include <type_traits>
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
#include <sprout/iterator/next.hpp>
|
|
|
|
#include <sprout/iterator/prev.hpp>
|
|
|
|
#include <sprout/iterator/distance.hpp>
|
|
|
|
#include <sprout/numeric/dft/fixed/square.hpp>
|
2012-10-05 15:58:56 +00:00
|
|
|
#include <sprout/utility/swap.hpp>
|
2012-07-08 12:05:24 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
//
|
|
|
|
// square_iterator
|
|
|
|
//
|
|
|
|
template<typename Value>
|
|
|
|
class square_iterator
|
|
|
|
: public std::iterator<
|
|
|
|
std::random_access_iterator_tag,
|
|
|
|
Value,
|
|
|
|
std::ptrdiff_t,
|
|
|
|
Value*,
|
|
|
|
Value
|
|
|
|
>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef std::random_access_iterator_tag iterator_category;
|
|
|
|
typedef Value value_type;
|
|
|
|
typedef std::ptrdiff_t difference_type;
|
|
|
|
typedef value_type* pointer;
|
|
|
|
typedef value_type reference;
|
|
|
|
private:
|
|
|
|
difference_type index_;
|
|
|
|
value_type frequency_;
|
|
|
|
value_type amplitude_;
|
|
|
|
value_type phase_;
|
|
|
|
value_type duty_;
|
|
|
|
private:
|
|
|
|
explicit SPROUT_CONSTEXPR square_iterator(square_iterator const& other, difference_type index)
|
|
|
|
: index_(index)
|
|
|
|
, frequency_(other.frequency_)
|
|
|
|
, amplitude_(other.amplitude_)
|
|
|
|
, phase_(other.phase_)
|
|
|
|
, duty_(other.duty_)
|
|
|
|
{}
|
|
|
|
public:
|
|
|
|
SPROUT_CONSTEXPR square_iterator()
|
|
|
|
: index_()
|
|
|
|
, frequency_(1)
|
|
|
|
, amplitude_(1)
|
|
|
|
, phase_(0)
|
|
|
|
, duty_(0.5)
|
|
|
|
{}
|
|
|
|
square_iterator(square_iterator const&) = default;
|
|
|
|
explicit SPROUT_CONSTEXPR square_iterator(
|
|
|
|
difference_type index,
|
|
|
|
value_type const& frequency = 1,
|
|
|
|
value_type const& amplitude = 1,
|
|
|
|
value_type const& phase = 0,
|
|
|
|
value_type const& duty = 0.5
|
|
|
|
)
|
|
|
|
: index_(index)
|
|
|
|
, frequency_(frequency)
|
|
|
|
, amplitude_(amplitude)
|
|
|
|
, phase_(phase)
|
|
|
|
, duty_(duty)
|
|
|
|
{}
|
|
|
|
template<typename U>
|
|
|
|
SPROUT_CONSTEXPR square_iterator(square_iterator<U> const& it)
|
|
|
|
: index_(it.index_)
|
|
|
|
, frequency_(it.frequency_)
|
|
|
|
, amplitude_(it.amplitude_)
|
|
|
|
, phase_(it.phase_)
|
|
|
|
, duty_(it.duty_)
|
|
|
|
{}
|
|
|
|
template<typename U>
|
|
|
|
square_iterator& operator=(square_iterator<U> const& it) {
|
|
|
|
square_iterator temp(it);
|
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR difference_type index() const {
|
|
|
|
return index_;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR value_type const& frequency() const {
|
|
|
|
return frequency_;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR value_type const& amplitude() const {
|
|
|
|
return amplitude_;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR value_type const& phase() const {
|
|
|
|
return phase_;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR value_type const& duty() const {
|
|
|
|
return duty_;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR reference operator*() const {
|
2012-12-06 17:31:16 +00:00
|
|
|
return amplitude_ == 0 ? 0
|
|
|
|
: amplitude_ * sprout::fixed::detail::square_value(frequency_ * value_type(index_) + phase_, duty_)
|
|
|
|
;
|
2012-07-08 12:05:24 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR pointer operator->() const {
|
|
|
|
return &operator*()();
|
|
|
|
}
|
|
|
|
square_iterator& operator++() {
|
|
|
|
++index_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
square_iterator operator++(int) {
|
|
|
|
square_iterator result(*this);
|
|
|
|
++index_;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
square_iterator& operator--() {
|
|
|
|
--index_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
square_iterator operator--(int) {
|
|
|
|
square_iterator temp(*this);
|
|
|
|
--index_;
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR square_iterator operator+(difference_type n) const {
|
|
|
|
return square_iterator(*this, index_ + n);
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR square_iterator operator-(difference_type n) const {
|
|
|
|
return square_iterator(*this, index_ - n);
|
|
|
|
}
|
|
|
|
square_iterator& operator+=(difference_type n) {
|
|
|
|
square_iterator temp(*this, index_ + n);
|
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
square_iterator& operator-=(difference_type n) {
|
|
|
|
square_iterator temp(*this, index_ - n);
|
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
|
2012-12-06 17:31:16 +00:00
|
|
|
return amplitude_ == 0 ? 0
|
|
|
|
: amplitude_ * sprout::fixed::detail::square_value(frequency_ * value_type(index_ + n) + phase_, duty_)
|
|
|
|
;
|
2012-07-08 12:05:24 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR square_iterator next() const {
|
|
|
|
return square_iterator(*this, index_ + 1);
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR square_iterator prev() const {
|
|
|
|
return square_iterator(*this, index_ - 1);
|
|
|
|
}
|
2012-10-05 15:58:56 +00:00
|
|
|
void swap(square_iterator& other) SPROUT_NOEXCEPT {
|
|
|
|
sprout::swap(index_, other.index_);
|
|
|
|
sprout::swap(frequency_, other.frequency_);
|
|
|
|
sprout::swap(amplitude_, other.amplitude_);
|
|
|
|
sprout::swap(phase_, other.phase_);
|
|
|
|
sprout::swap(duty_, other.duty_);
|
2012-07-08 12:05:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Value1, typename Value2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator==(sprout::square_iterator<Value1> const& lhs, sprout::square_iterator<Value2> const& rhs) {
|
2012-07-08 12:05:24 +00:00
|
|
|
return lhs.index() == rhs.index();
|
|
|
|
}
|
|
|
|
template<typename Value1, typename Value2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator!=(sprout::square_iterator<Value1> const& lhs, sprout::square_iterator<Value2> const& rhs) {
|
2012-07-08 12:05:24 +00:00
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
template<typename Value1, typename Value2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator<(sprout::square_iterator<Value1> const& lhs, sprout::square_iterator<Value2> const& rhs) {
|
2012-07-08 12:05:24 +00:00
|
|
|
return lhs.index() < rhs.index();
|
|
|
|
}
|
|
|
|
template<typename Value1, typename Value2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator>(sprout::square_iterator<Value1> const& lhs, sprout::square_iterator<Value2> const& rhs) {
|
2012-07-08 12:05:24 +00:00
|
|
|
return rhs < lhs;
|
|
|
|
}
|
|
|
|
template<typename Value1, typename Value2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator<=(sprout::square_iterator<Value1> const& lhs, sprout::square_iterator<Value2> const& rhs) {
|
2012-07-08 12:05:24 +00:00
|
|
|
return !(rhs < lhs);
|
|
|
|
}
|
|
|
|
template<typename Value1, typename Value2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator>=(sprout::square_iterator<Value1> const& lhs, sprout::square_iterator<Value2> const& rhs) {
|
2012-07-08 12:05:24 +00:00
|
|
|
return !(lhs < rhs);
|
|
|
|
}
|
|
|
|
template<typename Value1, typename Value2>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR typename sprout::square_iterator<Value1>::difference_type
|
|
|
|
operator-(sprout::square_iterator<Value1> const& lhs, sprout::square_iterator<Value2> const& rhs) {
|
2012-07-08 12:05:24 +00:00
|
|
|
return lhs.index() - rhs.index();
|
|
|
|
}
|
|
|
|
template<typename Value>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::square_iterator<Value>
|
|
|
|
operator+(typename sprout::square_iterator<Value>::difference_type n, sprout::square_iterator<Value> const& it) {
|
2012-07-08 12:05:24 +00:00
|
|
|
return it + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// swap
|
|
|
|
//
|
|
|
|
template<typename Value>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline void
|
|
|
|
swap(sprout::square_iterator<Value>& lhs, sprout::square_iterator<Value>& rhs)
|
|
|
|
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs)))
|
|
|
|
{
|
2012-07-08 12:05:24 +00:00
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2012-09-26 09:42:43 +00:00
|
|
|
// iterator_distance
|
2012-09-26 07:31:11 +00:00
|
|
|
//
|
|
|
|
template<typename Value>
|
|
|
|
inline SPROUT_CONSTEXPR typename std::iterator_traits<sprout::square_iterator<Value> >::difference_type
|
2012-10-05 15:58:56 +00:00
|
|
|
iterator_distance(sprout::square_iterator<Value> first, sprout::square_iterator<Value> last) {
|
2012-09-26 07:31:11 +00:00
|
|
|
return last - first;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// iterator_next
|
2012-07-08 12:05:24 +00:00
|
|
|
//
|
|
|
|
template<typename Value>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::square_iterator<Value>
|
|
|
|
iterator_next(sprout::square_iterator<Value> const& it) {
|
2012-07-08 12:05:24 +00:00
|
|
|
return it.next();
|
|
|
|
}
|
|
|
|
template<typename Value>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::square_iterator<Value>
|
|
|
|
iterator_next(sprout::square_iterator<Value> const& it, typename sprout::square_iterator<Value>::difference_type n) {
|
2012-07-08 12:05:24 +00:00
|
|
|
return it + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2012-09-26 07:31:11 +00:00
|
|
|
// iterator_prev
|
2012-07-08 12:05:24 +00:00
|
|
|
//
|
|
|
|
template<typename Value>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::square_iterator<Value>
|
|
|
|
iterator_prev(sprout::square_iterator<Value> const& it) {
|
2012-07-08 12:05:24 +00:00
|
|
|
return it.prev();
|
|
|
|
}
|
|
|
|
template<typename Value>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::square_iterator<Value>
|
|
|
|
iterator_prev(sprout::square_iterator<Value> const& it, typename sprout::square_iterator<Value>::difference_type n) {
|
2012-07-08 12:05:24 +00:00
|
|
|
return it - n;
|
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ITERATOR_SQUARE_ITERATOR_HPP
|