summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/plib/parray.h
blob: a1e3c2c504aaca048e155bef6e5da7bdda1ce53c (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
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * parray.h
 *
 */

#ifndef PARRAY_H_
#define PARRAY_H_

#include "palloc.h"
#include "pconfig.h"
#include "pexception.h"
#include "pstrutil.h"

#include <array>
#include <memory>
#include <type_traits>
#include <utility>
#include <vector>

namespace plib {

	template <typename FT, int SIZE>
	struct sizeabs
	{
		static constexpr std::size_t ABS() { return (SIZE < 0) ? static_cast<std::size_t>(0 - SIZE) : static_cast<std::size_t>(SIZE); }
		using container = typename std::array<FT, ABS()> ;
	};

	template <typename FT>
	struct sizeabs<FT, 0>
	{
		static constexpr std::size_t ABS() { return 0; }
		//using container = typename std::vector<FT, arena_allocator<mempool, FT, 64>>;
		using container = typename std::vector<FT, aligned_allocator<FT, PALIGN_VECTOROPT>>;
	};

	/**
	 * \brief Array with preallocated or dynamic allocation
	 *
	 * Passing SIZE > 0 has the same functionality as a std::array.
	 * SIZE = 0 is pure dynamic allocation, the actual array size is passed to the
	 * constructor.
	 * SIZE < 0 reserves std::abs(SIZE) elements statically in place allocated. The
	 * actual size is passed in by the constructor.
	 * This array is purely intended for HPC application where depending on the
	 * architecture a preference dynamic/static has to be made.
	 *
	 * This struct is not intended to be a full replacement to std::array.
	 * It is a subset to enable switching between dynamic and static allocation.
	 * I consider > 10% performance difference to be a use case.
	 */

	template <typename FT, int SIZE>
	struct parray
	{
	public:
		static constexpr std::size_t SIZEABS() { return sizeabs<FT, SIZE>::ABS(); }

		using base_type = typename sizeabs<FT, SIZE>::container;
		using size_type = typename base_type::size_type;
		using reference = typename base_type::reference;
		using const_reference = typename base_type::const_reference;
		using value_type = typename base_type::value_type;

		template <int X = SIZE >
		parray(size_type size, typename std::enable_if<(X==0), int>::type = 0)
		: m_a(size), m_size(size)
		{
		}

		/* allow construction in fixed size arrays */
		parray()
		: m_size(SIZEABS())
		{
		}

		// osx clang doesn't like COPYASSIGNMOVE(parray, default)
		// it will generate some weird error messages about move assignment
		// constructor having a different noexcept status.

		parray(const parray &rhs) : m_a(rhs.m_a), m_size(rhs.m_size) {}
		parray(parray &&rhs) noexcept : m_a(std::move(rhs.m_a)), m_size(std::move(rhs.m_size)) {}
		parray &operator=(const parray &rhs) noexcept
		{
			if (this != &rhs)
			{
				m_a = rhs.m_a;
				m_size = rhs.m_size;
			}
			return *this;
		}

		parray &operator=(parray &&rhs) noexcept { std::swap(m_a,rhs.m_a); std::swap(m_size, rhs.m_size); return *this; }

		~parray() noexcept = default;

		template <int X = SIZE >
		parray(size_type size, typename std::enable_if<(X != 0), int>::type = 0)
		: m_size(size)
		{
			if ((SIZE < 0 && size > SIZEABS())
				|| (SIZE > 0 && size != SIZEABS()))
				throw plib::pexception("parray: size error " + plib::to_string(size) + ">" + plib::to_string(SIZE));
		}

		base_type &as_base() noexcept { return m_a; }

		inline size_type size() const noexcept { return SIZE <= 0 ? m_size : SIZEABS(); }

		constexpr size_type max_size() const noexcept { return base_type::max_size(); }

		bool empty() const noexcept { return size() == 0; }

		C14CONSTEXPR reference operator[](size_type i) noexcept
		{
			return assume_aligned_ptr<FT, PALIGN_VECTOROPT>(&m_a[0])[i];
		}
		constexpr const_reference operator[](size_type i) const noexcept
		{
			return assume_aligned_ptr<FT, PALIGN_VECTOROPT>(&m_a[0])[i];
		}

		FT * data() noexcept { return assume_aligned_ptr<FT, PALIGN_VECTOROPT>(m_a.data()); }
		const FT * data() const noexcept { return assume_aligned_ptr<FT, PALIGN_VECTOROPT>(m_a.data()); }

	private:
		PALIGNAS_VECTOROPT()
		base_type               m_a;
		PALIGNAS_CACHELINE()
		size_type               m_size;
	};

	template <typename FT, int SIZE1, int SIZE2>
	struct parray2D : public parray<parray<FT, SIZE2>, SIZE1>
	{
	public:

		using size_type = std::size_t;

		parray2D(size_type size1, size_type size2)
		: parray<parray<FT, SIZE2>, SIZE1>(size1)
		{
			if (SIZE2 <= 0)
			{
				for (size_type i=0; i < this->size(); i++)
					(*this)[i] = parray<FT, SIZE2>(size2);
			}
		}

		COPYASSIGNMOVE(parray2D, default)

		~parray2D() noexcept = default;

	};

} // namespace plib



#endif /* PARRAY_H_ */