| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- /***************************************************************************
- * Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
- * Copyright (c) QuantStack *
- * *
- * Distributed under the terms of the BSD 3-Clause License. *
- * *
- * The full license is in the file LICENSE, distributed with this software. *
- ****************************************************************************/
- #ifndef XTL_XCOMPLEX_SEQUENCE_HPP
- #define XTL_XCOMPLEX_SEQUENCE_HPP
- #include <array>
- #include <vector>
- #include <algorithm>
- #include "xclosure.hpp"
- #include "xcomplex.hpp"
- #include "xiterator_base.hpp"
- #include "xsequence.hpp"
- namespace xtl
- {
- /************************************
- * Optimized 1-D xcomplex container *
- ************************************/
- template <class IT, bool ieee_compliant>
- class xcomplex_iterator;
- template <class C, bool ieee_compliant>
- class xcomplex_sequence
- {
- public:
- using container_type = C;
- using cvt = typename C::value_type;
- using value_type = xcomplex<cvt, cvt, ieee_compliant>;
- using reference = xcomplex<cvt&, cvt&, ieee_compliant>;
- using const_reference = xcomplex<const cvt&, const cvt&, ieee_compliant>;
- using pointer = xclosure_pointer<reference>;
- using const_pointer = xclosure_pointer<const_reference>;
- using size_type = typename container_type::size_type;
- using difference_type = typename container_type::difference_type;
- using iterator = xcomplex_iterator<typename C::iterator, ieee_compliant>;
- using const_iterator = xcomplex_iterator<typename C::const_iterator, ieee_compliant>;
- using reverse_iterator = xcomplex_iterator<typename C::reverse_iterator, ieee_compliant>;
- using const_reverse_iterator = xcomplex_iterator<typename C::const_reverse_iterator, ieee_compliant>;
- bool empty() const noexcept;
- size_type size() const noexcept;
- size_type max_size() const noexcept;
- reference at(size_type i);
- const_reference at(size_type i) const;
- reference operator[](size_type i);
- const_reference operator[](size_type i) const;
- reference front();
- const_reference front() const;
- reference back();
- const_reference back() const;
- iterator begin() noexcept;
- iterator end() noexcept;
- const_iterator begin() const noexcept;
- const_iterator end() const noexcept;
- const_iterator cbegin() const noexcept;
- const_iterator cend() const noexcept;
- reverse_iterator rbegin() noexcept;
- reverse_iterator rend() noexcept;
- const_reverse_iterator rbegin() const noexcept;
- const_reverse_iterator rend() const noexcept;
- const_reverse_iterator crbegin() const noexcept;
- const_reverse_iterator crend() const noexcept;
- container_type real() && noexcept;
- container_type& real() & noexcept;
- const container_type& real() const & noexcept;
- container_type imag() && noexcept;
- container_type& imag() & noexcept;
- const container_type& imag() const & noexcept;
- protected:
- xcomplex_sequence() = default;
- xcomplex_sequence(size_type s);
- xcomplex_sequence(size_type s, const value_type& v);
- template <class TR, class TC, bool B>
- xcomplex_sequence(size_type s, const xcomplex<TR, TC, B>& v);
- xcomplex_sequence(std::initializer_list<value_type> init);
- ~xcomplex_sequence() = default;
- xcomplex_sequence(const xcomplex_sequence&) = default;
- xcomplex_sequence& operator=(const xcomplex_sequence&) = default;
- xcomplex_sequence(xcomplex_sequence&&) = default;
- xcomplex_sequence& operator=(xcomplex_sequence&&) = default;
- container_type m_real;
- container_type m_imag;
- };
- template <class C, bool B>
- bool operator==(const xcomplex_sequence<C, B>& lhs, const xcomplex_sequence<C, B>& rhs);
- template <class C, bool B>
- bool operator!=(const xcomplex_sequence<C, B>& lhs, const xcomplex_sequence<C, B>& rhs);
- /******************
- * xcomplex_array *
- ******************/
- template <class T, std::size_t N, bool ieee_compliant = false>
- class xcomplex_array : public xcomplex_sequence<std::array<T, N>, ieee_compliant>
- {
- public:
- using base_type = xcomplex_sequence<std::array<T, N>, ieee_compliant>;
- using value_type = typename base_type::value_type;
- using size_type = typename base_type::size_type;
- xcomplex_array() = default;
- xcomplex_array(size_type s);
- xcomplex_array(size_type s, const value_type& v);
- template <class TR, class TI, bool B>
- xcomplex_array(size_type s, const xcomplex<TR, TI, B>& v);
- };
- /*******************
- * xcomplex_vector *
- *******************/
- template <class T, bool ieee_compliant = false, class A = std::allocator<T>>
- class xcomplex_vector : public xcomplex_sequence<std::vector<T, A>, ieee_compliant>
- {
- public:
- using base_type = xcomplex_sequence<std::vector<T, A>, ieee_compliant>;
- using value_type = typename base_type::value_type;
- using size_type = typename base_type::size_type;
- xcomplex_vector() = default;
- xcomplex_vector(size_type s);
- xcomplex_vector(size_type s, const value_type& v);
- xcomplex_vector(std::initializer_list<value_type> init);
- template <class TR, class TI, bool B>
- xcomplex_vector(size_type s, const xcomplex<TR, TI, B>& v);
- void resize(size_type);
- void resize(size_type, const value_type&);
- template <class TR, class TI, bool B>
- void resize(size_type s, const xcomplex<TR, TI, B>& v);
- };
- /*********************
- * xcomplex_iterator *
- *********************/
- template <class IT, bool ieee_compliant>
- struct xcomplex_iterator_traits
- {
- using iterator_type = xcomplex_iterator<IT, ieee_compliant>;
- using value_type = xcomplex<typename IT::value_type, typename IT::value_type, ieee_compliant>;
- using reference = xcomplex<typename IT::reference, typename IT::reference, ieee_compliant>;
- using pointer = xclosure_pointer<reference>;
- using difference_type = typename IT::difference_type;
- };
- template <class IT, bool B>
- class xcomplex_iterator : public xrandom_access_iterator_base2<xcomplex_iterator_traits<IT, B>>
- {
- public:
- using self_type = xcomplex_iterator<IT, B>;
- using base_type = xrandom_access_iterator_base2<xcomplex_iterator_traits<IT, B>>;
- using value_type = typename base_type::value_type;
- using reference = typename base_type::reference;
- using pointer = typename base_type::pointer;
- using difference_type = typename base_type::difference_type;
- xcomplex_iterator() = default;
- xcomplex_iterator(IT it_real, IT it_imag);
- self_type& operator++();
- self_type& operator--();
- self_type& operator+=(difference_type n);
- self_type& operator-=(difference_type n);
- difference_type operator-(const self_type& rhs) const;
- reference operator*() const;
- pointer operator->() const;
- bool operator==(const self_type& rhs) const;
- private:
- IT m_it_real;
- IT m_it_imag;
- };
- /************************************
- * xcomplex_sequence implementation *
- ************************************/
- template <class C, bool B>
- inline xcomplex_sequence<C, B>::xcomplex_sequence(size_type s)
- : m_real(make_sequence<container_type>(s)),
- m_imag(make_sequence<container_type>(s))
- {
- }
- template <class C, bool B>
- inline xcomplex_sequence<C, B>::xcomplex_sequence(size_type s, const value_type& v)
- : m_real(make_sequence<container_type>(s, v.real())),
- m_imag(make_sequence<container_type>(s, v.imag()))
- {
- }
- template <class C, bool B>
- template <class TR, class TC, bool B2>
- inline xcomplex_sequence<C, B>::xcomplex_sequence(size_type s, const xcomplex<TR, TC, B2>& v)
- : m_real(make_sequence<container_type>(s, v.real())),
- m_imag(make_sequence<container_type>(s, v.imag()))
- {
- }
- template <class C, bool B>
- inline xcomplex_sequence<C, B>::xcomplex_sequence(std::initializer_list<value_type> init)
- : m_real(make_sequence<container_type>(init.size())),
- m_imag(make_sequence<container_type>(init.size()))
- {
- std::transform(init.begin(), init.end(), m_real.begin(), [](const auto& v) { return v.real(); });
- std::transform(init.begin(), init.end(), m_imag.begin(), [](const auto& v) { return v.imag(); });
- }
- template <class C, bool B>
- inline bool xcomplex_sequence<C, B>::empty() const noexcept
- {
- return m_real.empty();
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::size() const noexcept -> size_type
- {
- return m_real.size();
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::max_size() const noexcept -> size_type
- {
- return m_real.max_size();
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::at(size_type i) -> reference
- {
- return reference(m_real.at(i), m_imag.at(i));
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::at(size_type i) const -> const_reference
- {
- return const_reference(m_real.at(i), m_imag.at(i));
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::operator[](size_type i) -> reference
- {
- return reference(m_real[i], m_imag[i]);
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::operator[](size_type i) const -> const_reference
- {
- return const_reference(m_real[i], m_imag[i]);
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::front() -> reference
- {
- return reference(m_real.front(), m_imag.front());
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::front() const -> const_reference
- {
- return const_reference(m_real.front(), m_imag.front());
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::back() -> reference
- {
- return reference(m_real.back(), m_imag.back());
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::back() const -> const_reference
- {
- return const_reference(m_real.back(), m_imag.back());
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::begin() noexcept -> iterator
- {
- return iterator(m_real.begin(), m_imag.begin());
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::end() noexcept -> iterator
- {
- return iterator(m_real.end(), m_imag.end());
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::begin() const noexcept -> const_iterator
- {
- return cbegin();
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::end() const noexcept -> const_iterator
- {
- return cend();
- }
-
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::cbegin() const noexcept -> const_iterator
- {
- return const_iterator(m_real.cbegin(), m_imag.cbegin());
- }
-
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::cend() const noexcept -> const_iterator
- {
- return const_iterator(m_real.cend(), m_imag.cend());
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::rbegin() noexcept -> reverse_iterator
- {
- return reverse_iterator(m_real.rbegin(), m_imag.rbegin());
- }
-
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::rend() noexcept -> reverse_iterator
- {
- return reverse_iterator(m_real.rend(), m_imag.rend());
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::rbegin() const noexcept -> const_reverse_iterator
- {
- return crbegin();
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::rend() const noexcept -> const_reverse_iterator
- {
- return crend();
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::crbegin() const noexcept -> const_reverse_iterator
- {
- return const_reverse_iterator(m_real.crbegin(), m_imag.crbegin());
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::crend() const noexcept -> const_reverse_iterator
- {
- return const_reverse_iterator(m_real.crend(), m_imag.crend());
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::real() && noexcept -> container_type
- {
- return m_real;
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::real() & noexcept -> container_type&
- {
- return m_real;
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::real() const & noexcept -> const container_type&
- {
- return m_real;
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::imag() && noexcept -> container_type
- {
- return m_imag;
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::imag() & noexcept -> container_type&
- {
- return m_imag;
- }
- template <class C, bool B>
- inline auto xcomplex_sequence<C, B>::imag() const & noexcept -> const container_type&
- {
- return m_imag;
- }
- template <class C, bool B>
- inline bool operator==(const xcomplex_sequence<C, B>& lhs, const xcomplex_sequence<C, B>& rhs)
- {
- return lhs.real() == rhs.real() && lhs.imag() == rhs.imag();
- }
- template <class C, bool B>
- inline bool operator!=(const xcomplex_sequence<C, B>& lhs, const xcomplex_sequence<C, B>& rhs)
- {
- return !(lhs == rhs);
- }
- /*********************************
- * xcomplex_array implementation *
- *********************************/
- template <class T, std::size_t N, bool B>
- inline xcomplex_array<T, N, B>::xcomplex_array(size_type s)
- : base_type(s)
- {
- }
-
- template <class T, std::size_t N, bool B>
- inline xcomplex_array<T, N, B>::xcomplex_array(size_type s, const value_type& v)
- : base_type(s, v)
- {
- }
- template <class T, std::size_t N, bool B>
- template <class TR, class TI, bool B2>
- inline xcomplex_array<T, N, B>::xcomplex_array(size_type s, const xcomplex<TR, TI, B2>& v)
- : base_type(s, v)
- {
- }
- /**********************************
- * xcomplex_vector implementation *
- **********************************/
- template <class T, bool B, class A>
- inline xcomplex_vector<T, B, A>::xcomplex_vector(size_type s)
- : base_type(s)
- {
- }
- template <class T, bool B, class A>
- inline xcomplex_vector<T, B, A>::xcomplex_vector(size_type s, const value_type& v)
- : base_type(s, v)
- {
- }
- template <class T, bool B, class A>
- template <class TR, class TI, bool B2>
- inline xcomplex_vector<T, B, A>::xcomplex_vector(size_type s, const xcomplex<TR, TI, B2>& v)
- : base_type(s, v)
- {
- }
- template <class T, bool B, class A>
- inline xcomplex_vector<T, B, A>::xcomplex_vector(std::initializer_list<value_type> init)
- : base_type(init)
- {
- }
- template <class T, bool B, class A>
- void xcomplex_vector<T, B, A>::resize(size_type s)
- {
- this->m_real.resize(s);
- this->m_imag.resize(s);
- }
- template <class T, bool B, class A>
- void xcomplex_vector<T, B, A>::resize(size_type s, const value_type& v)
- {
- this->m_real.resize(s, v.real());
- this->m_imag.resize(s, v.imag());
- }
- template <class T, bool B, class A>
- template <class TR, class TI, bool B2>
- inline void xcomplex_vector<T, B, A>::resize(size_type s, const xcomplex<TR, TI, B2>& v)
- {
- this->m_real.resize(s, v.real());
- this->m_imag.resize(s, v.imag());
- }
- /************************************
- * xcomplex_iterator implementation *
- ************************************/
- template <class IT, bool B>
- inline xcomplex_iterator<IT, B>::xcomplex_iterator(IT it_real, IT it_imag)
- : m_it_real(it_real), m_it_imag(it_imag)
- {
- }
- template <class IT, bool B>
- inline auto xcomplex_iterator<IT, B>::operator++() -> self_type&
- {
- ++m_it_real;
- ++m_it_imag;
- return *this;
- }
- template <class IT, bool B>
- inline auto xcomplex_iterator<IT, B>::operator--() -> self_type&
- {
- --m_it_real;
- --m_it_imag;
- return *this;
- }
- template <class IT, bool B>
- inline auto xcomplex_iterator<IT, B>::operator+=(difference_type n) -> self_type&
- {
- m_it_real += n;
- m_it_imag += n;
- return *this;
- }
- template <class IT, bool B>
- inline auto xcomplex_iterator<IT, B>::operator-=(difference_type n) -> self_type&
- {
- m_it_real -= n;
- m_it_imag -= n;
- return *this;
- }
-
- template <class IT, bool B>
- inline auto xcomplex_iterator<IT, B>::operator-(const self_type& rhs) const -> difference_type
- {
- return m_it_real - rhs.m_it_real;
- }
- template <class IT, bool B>
- inline auto xcomplex_iterator<IT, B>::operator*() const -> reference
- {
- return reference(*m_it_real, *m_it_imag);
- }
- template <class IT, bool B>
- inline auto xcomplex_iterator<IT, B>::operator->() const -> pointer
- {
- return pointer(operator*());
- }
- template <class IT, bool B>
- inline bool xcomplex_iterator<IT, B>::operator==(const self_type& rhs) const
- {
- return m_it_real == rhs.m_it_real && m_it_imag == rhs.m_it_imag;
- }
- }
- #endif
|