summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/pmatrix2d.h
blob: 4591d00b6f6148c5c142a397cc32b7c569ec354c (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// 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_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 reference = T &;
		using const_reference = const T &;

		using pointer = T *;
		using const_pointer = const T *;

		pmatrix2d() noexcept
		: 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 = 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 = m_a.allocate(N * m_stride);
			for (std::size_t i = 0; i < N * m_stride; i++)
				::new(&m_v[i]) T();
		}

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

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

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

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

		pointer data() noexcept
		{
			return m_v;
		}

		const_pointer data() const noexcept
		{
			return m_v;
		}

		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;

		T * __restrict m_v;

		allocator_type m_a;
	};

	// variable row length matrix
	template<typename T, typename A = aligned_arena>
	class pmatrix2d_vrl
	{
	public:
		using size_type = std::size_t;
		using value_type = 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;
		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
		}

		PCOPYASSIGNMOVE(pmatrix2d_vrl, default)

		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]]);
		}

		//FIXME: no check!
		T & operator()(size_type r, size_type c) noexcept
		{
			return (*this)[r][c];
		}

		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() + 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;
		}

		//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;
		}

		size_type colcount(size_type row) const noexcept
		{
			return m_row[row + 1] - m_row[row];
		}

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

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


} // namespace plib

#endif // PMATRIX2D_H_