diff options
Diffstat (limited to 'src/lib/netlist/plib/pmatrix2d.h')
-rwxr-xr-x[-rw-r--r--] | src/lib/netlist/plib/pmatrix2d.h | 93 |
1 files changed, 64 insertions, 29 deletions
diff --git a/src/lib/netlist/plib/pmatrix2d.h b/src/lib/netlist/plib/pmatrix2d.h index 33e3169c64a..b9a2ebee116 100644..100755 --- a/src/lib/netlist/plib/pmatrix2d.h +++ b/src/lib/netlist/plib/pmatrix2d.h @@ -17,70 +17,102 @@ namespace plib { - template<typename T, typename A = aligned_allocator<T>> + template<typename T, typename A = aligned_arena> class pmatrix2d { public: using size_type = std::size_t; + using arena_type = A; + using allocator_type = typename A::template allocator_type<T>; + + static constexpr const size_type align_size = align_traits<allocator_type>::align_size; + static constexpr const size_type stride_size = align_traits<allocator_type>::stride_size; + using value_type = T; - using allocator_type = A; + using reference = T &; + using const_reference = const T &; + + using pointer = T *; + using const_pointer = const T *; - static constexpr const size_type align_size = align_traits<A>::align_size; - static constexpr const size_type stride_size = align_traits<A>::stride_size; pmatrix2d() noexcept - : m_N(0), m_M(0), m_stride(8), m_v() + : m_N(0), m_M(0), m_stride(8), m_v(nullptr) { } pmatrix2d(size_type N, size_type M) : m_N(N), m_M(M), m_v() { + gsl_Expects(N>0); + gsl_Expects(M>0); m_stride = ((M + stride_size-1) / stride_size) * stride_size; - m_v.resize(N * m_stride); + m_v = m_a.allocate(N * m_stride); + for (std::size_t i = 0; i < N * m_stride; i++) + ::new(&m_v[i]) T(); + } + + ~pmatrix2d() + { + if (m_v != nullptr) + { + for (std::size_t i = 0; i < m_N * m_stride; i++) + (&m_v[i])->~T(); + m_a.deallocate(m_v, m_N * m_stride); + } } void resize(size_type N, size_type M) { + gsl_Expects(N>0); + gsl_Expects(M>0); + if (m_v != nullptr) + { + for (std::size_t i = 0; i < N * m_stride; i++) + (&m_v[i])->~T(); + m_a.deallocate(m_v, N * m_stride); + } m_N = N; m_M = M; m_stride = ((M + stride_size-1) / stride_size) * stride_size; - m_v.resize(N * m_stride); + m_v = m_a.allocate(N * m_stride); + for (std::size_t i = 0; i < N * m_stride; i++) + ::new(&m_v[i]) T(); } - constexpr T * operator[] (size_type row) noexcept + constexpr pointer operator[] (size_type row) noexcept { - return assume_aligned_ptr<T, align_size>(&m_v[m_stride * row]); + return &m_v[m_stride * row]; } - constexpr const T * operator[] (size_type row) const noexcept + constexpr const_pointer operator[] (size_type row) const noexcept { - return assume_aligned_ptr<T, align_size>(&m_v[m_stride * row]); + return &m_v[m_stride * row]; } - T & operator()(size_type r, size_type c) noexcept + reference operator()(size_type r, size_type c) noexcept { return (*this)[r][c]; } - const T & operator()(size_type r, size_type c) const noexcept + const_reference operator()(size_type r, size_type c) const noexcept { return (*this)[r][c]; } // for compatibility with vrl variant - void set(size_type r, size_type c, const T &v) noexcept + void set(size_type r, size_type c, const value_type &v) noexcept { (*this)[r][c] = v; } - T * data() noexcept + pointer data() noexcept { - return m_v.data(); + return m_v; } - const T * data() const noexcept + const_pointer data() const noexcept { - return m_v.data(); + return m_v; } size_type didx(size_type r, size_type c) const noexcept @@ -93,20 +125,23 @@ namespace plib size_type m_M; size_type m_stride; - std::vector<T, A> m_v; + T * __restrict m_v; + + allocator_type m_a; }; // variable row length matrix - template<typename T, typename A = aligned_allocator<T>> + template<typename T, typename A = aligned_arena> class pmatrix2d_vrl { public: using size_type = std::size_t; using value_type = T; - using allocator_type = A; + using arena_type = A; + using allocator_type = typename A::template allocator_type<T>; - static constexpr const size_type align_size = align_traits<A>::align_size; - static constexpr const size_type stride_size = align_traits<A>::stride_size; + static constexpr const size_type align_size = align_traits<allocator_type>::align_size; + static constexpr const size_type stride_size = align_traits<allocator_type>::stride_size; pmatrix2d_vrl() noexcept : m_N(0), m_M(0), m_v() { @@ -139,24 +174,24 @@ namespace plib return &(m_v[m_row[row]]); } -#if 0 + //FIXME: no check! T & operator()(size_type r, size_type c) noexcept { return (*this)[r][c]; } -#else + void set(size_type r, size_type c, const T &v) noexcept { if (c + m_row[r] >= m_row[r + 1]) { - m_v.insert(m_v.begin() + m_row[r+1], v); + m_v.insert(m_v.begin() + narrow_cast<std::ptrdiff_t>(m_row[r+1]), v); for (size_type i = r + 1; i <= m_N; i++) m_row[i] = m_row[i] + 1; } else (*this)[r][c] = v; } -#endif + //FIXME: no check! const T & operator()(size_type r, size_type c) const noexcept { @@ -184,8 +219,8 @@ namespace plib size_type m_N; size_type m_M; - std::vector<size_type, A> m_row; - std::vector<T, A> m_v; + std::vector<size_type, typename A::template allocator_type<size_type>> m_row; + std::vector<T, allocator_type> m_v; }; |