LAL: Linear Arrangement Library 21.07.01
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
data_array.hpp
1/*********************************************************************
2 *
3 * Linear Arrangement Library - A library that implements a collection
4 * algorithms for linear arrangments of graphs.
5 *
6 * Copyright (C) 2019 - 2021
7 *
8 * This file is part of Linear Arrangement Library. To see the full code
9 * visit the webpage:
10 * https://github.com/lluisalemanypuig/linear-arrangement-library.git
11 *
12 * Linear Arrangement Library is free software: you can redistribute it
13 * and/or modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * Linear Arrangement Library is distributed in the hope that it will be
18 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with Linear Arrangement Library. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * Contact:
26 *
27 * LluĂ­s Alemany Puig (lalemany@cs.upc.edu)
28 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
29 * CQL (Complexity and Quantitative Linguistics Lab)
30 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
31 * Webpage: https://cqllab.upc.edu/people/lalemany/
32 *
33 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
34 * LARCA (Laboratory for Relational Algorithmics, Complexity and Learning)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office S124, Omega building
37 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
38 * Webpage: https://cqllab.upc.edu/people/rferrericancho/
39 *
40 ********************************************************************/
41
42#pragma once
43
44// C++ includes
45#if defined DEBUG
46#include <cassert>
47#endif
48#include <algorithm>
49
50namespace lal {
51namespace internal {
52
53/*
54 * @brief Wrapper of an array for autmatic deallocation of memory.
55 *
56 * Automatically manage deallocation of memory via destructors.
57 * Objects of this class are not to be moved nor copied.
58 */
59template<typename T>
60struct data_array {
61public:
62 // the data of this array
63 T *data = nullptr;
64
65private:
66 // the size of this array in number of elements
67 std::size_t m_size;
68
69public:
70 // Constructor with size
71 data_array(const std::size_t n) noexcept : m_size(n) {
72 data = m_size == 0 ? nullptr : new T[m_size];
73 }
74 // Constructor with size and initial value
75 data_array(const std::size_t n, const T& v) noexcept : m_size(n) {
76 data = m_size == 0 ? nullptr : new T[m_size];
77 fill(v);
78 }
79 // Destructor
80 ~data_array() noexcept {
81 delete[] data;
82 // this is for those who like calling the destructor...
83 data = nullptr;
84 }
85
86 // Copy constructor
87 data_array(const data_array& d) : m_size(d.m_size) {
88 data = m_size == 0 ? nullptr : new T[m_size];
89 std::copy(d.begin(), d.end(), begin());
90 }
91 // Copy assignment operator
92 data_array& operator= (const data_array& d) {
93 if (m_size != d.m_size) {
94 delete[] data;
95 m_size = d.m_size;
96 data = new T[m_size];
97 }
98 std::copy(d.begin(), d.end(), begin());
99 return *this;
100 }
101
102 // Move constructor
103 data_array(data_array&& d) noexcept : m_size(d.m_size) {
104 // steal data
105 data = d.data;
106 // invalidate data
107 d.data = nullptr;
108 d.m_size = 0;
109 }
110 // Move assignment operator
111 data_array& operator= (data_array&& d) {
112 // free yourself
113 delete[] data;
114 // steal from others
115 data = d.data;
116 m_size = d.m_size;
117 // invalidate data
118 d.data = nullptr;
119 d.m_size = 0;
120 return *this;
121 }
122
123 // imitate the vector::size() method
124 [[nodiscard]]
125 inline constexpr std::size_t size() const noexcept { return m_size; }
126
127 // operator[]
128 [[nodiscard]] inline T& operator[] (const std::size_t i) noexcept {
129#if defined DEBUG
130 assert(i < size());
131#endif
132 return data[i];
133 }
134 [[nodiscard]]
135 inline const T& operator[] (const std::size_t i) const noexcept {
136#if defined DEBUG
137 assert(i < size());
138#endif
139 return data[i];
140 }
141
142 // assign the same value to every element in the data
143 inline void fill(const T& v) noexcept {
144 std::fill(&data[0], &data[m_size], v);
145 }
146
147 // pointer to non-constant first element and last+1 element
148 [[nodiscard]] inline T *begin() { return &data[0]; }
149 [[nodiscard]] inline T *end() { return &data[m_size]; }
150
151 // pointer to constant first element and last+1 element
152 [[nodiscard]] inline const T *begin() const { return &data[0]; }
153 [[nodiscard]] inline const T *end() const { return &data[m_size]; }
154};
155
156} // -- namespace internal
157} // -- namespace lal
Main namespace of the library.
Definition definitions.hpp:48