summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/pmatrix2d.h
blob: 33e3169c64ad99dccc4f4f2205225467072cf9ff (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// 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 size_type = std::size_t;
		using value_type = T;
		using allocator_type = A;

		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()
		{
		}

		pmatrix2d(size_type N, size_type 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(size_type N, size_type M)
		{
			m_N = N;
			m_M = M;
			m_stride = ((M + stride_size-1) / stride_size) * stride_size;
			m_v.resize(N * m_stride);
		}

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

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

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

		const T & 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
		{
			(*this)[r][c] = v;
		}

		T * data() noexcept
		{
			return m_v.data();
		}

		const T * data() const noexcept
		{
			return m_v.data();
		}

		size_type didx(size_type r, size_type c) const noexcept
		{
			return m_stride * r + c;
		}
	private:

		size_type m_N;
		size_type m_M;
		size_type m_stride;

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

	// variable row length matrix
	template<typename T, typename A = aligned_allocator<T>>
	class pmatrix2d_vrl
	{
	public:
		using size_type = std::size_t;
		using value_type = T;
		using allocator_type = A;

		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_vrl() noexcept
		: m_N(0), m_M(0), m_v()
		{
		}

		pmatrix2d_vrl(size_type N, size_type M)
		: m_N(N), m_M(M), m_v()
		{
			m_row.resize(N + 1, 0);
			m_v.resize(N); //FIXME
		}

		void resize(size_type N, size_type M)
		{
			m_N = N;
			m_M = M;
			m_row.resize(N + 1);
			for (std::size_t i = 0; i < m_N; i++)
				m_row[i] = 0;
			m_v.resize(N); //FIXME
		}

		constexpr T * operator[] (size_type row) noexcept
		{
			return &(m_v[m_row[row]]);
		}

		constexpr const T * operator[] (size_type row) const noexcept
		{
			return &(m_v[m_row[row]]);
		}

#if 0
		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);
				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
		{
			return (*this)[r][c];
		}

		T * data() noexcept
		{
			return m_v.data();
		}

		const T * data() const noexcept
		{
			return m_v.data();
		}

		//FIXME: no check!
		size_type didx(size_type r, size_type c) const noexcept
		{
			return m_row[r] + c;
		}

		std::size_t tx() const { return m_v.size(); }
	private:

		size_type m_N;
		size_type m_M;
		std::vector<size_type, A> m_row;
		std::vector<T, A> m_v;
	};


} // namespace plib

#endif // PMATRIX2D_H_