summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/pmatrix2d.h
blob: efa70da5b2a4a7c2b5c980101f5d8d0772a2cde9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// license:GPL-2.0+
// copyright-holders:Couriersud

#ifndef PMATRIX2D_H_
#define PMATRIX2D_H_

///
/// \file pmatrix2d.h
///

#include "palloc.h"

#include <algorithm>
#include <type_traits>
#include <vector>

namespace plib
{


	template<typename T, typename A = aligned_allocator<T>>
	class pmatrix2d
	{
	public:
		using value_type = T;
		using allocator_type = A;

		static constexpr const std::size_t align_size = align_traits<A>::align_size;
		static constexpr const std::size_t stride_size = align_traits<A>::stride_size;
		pmatrix2d()
		: m_N(0), m_M(0), m_stride(8), m_v()
		{
		}

		pmatrix2d(std::size_t N, std::size_t M)
		: m_N(N), m_M(M), m_v()
		{
			m_stride = ((M + stride_size-1) / stride_size) * stride_size;
			m_v.resize(N * m_stride);
		}

		void resize(std::size_t N, std::size_t M)
		{
			m_N = N;
			m_M = M;
			m_stride = ((M + stride_size-1) / stride_size) * stride_size;
			m_v.resize(N * m_stride);
		}

		C14CONSTEXPR T * operator[] (std::size_t row) noexcept
		{
			return assume_aligned_ptr<T, align_size>(&m_v[m_stride * row]);
		}

		constexpr const T * operator[] (std::size_t row) const noexcept
		{
			return assume_aligned_ptr<T, align_size>(&m_v[m_stride * row]);
		}

		T & operator()(std::size_t r, std::size_t c) noexcept
		{
			return (*this)[r][c];
		}

		const T & operator()(std::size_t r, std::size_t c) const noexcept
		{
			return (*this)[r][c];
		}

	private:

		std::size_t m_N;
		std::size_t m_M;
		std::size_t m_stride;

		std::vector<T, A> m_v;
	};

} // namespace plib

#endif // PMATRIX2D_H_