SIde-Channel Analysis toolKit (SICAK)
Software toolkit for side-channel analysis
types_basic.hpp
Go to the documentation of this file.
1 /*
2 * SICAK - SIde-Channel Analysis toolKit
3 * Copyright (C) 2018 Petr Socha, FIT, CTU in Prague
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18 
29 #ifndef TYPES_BASIC_HPP
30 #define TYPES_BASIC_HPP
31 
32 #include <algorithm>
33 #include <memory>
34 #include <utility>
35 #include "exceptions.hpp"
36 
44 class DataType {
45 
46 public:
47 
49  virtual ~DataType() {}
50 
51 protected:
52 
54  DataType() {}
55 
56 };
57 
58 
66 template <class T>
67 class ArrayType : public DataType {
68 
69 public:
70 
72  virtual ~ArrayType() {}
74  virtual size_t length() const = 0;
76  virtual size_t size() const = 0;
78  virtual void fill(T val) = 0;
80  virtual T * data() = 0;
82  virtual const T * data() const = 0;
83 
84 protected:
85 
87  ArrayType() {}
88 
89 };
90 
91 
99 template <class T>
100 class VectorType : public ArrayType<T> {
101 
102 public:
103 
105  virtual ~VectorType() {}
107  virtual void init(size_t length) = 0;
109  virtual void init(size_t length, T initVal) = 0;
110 
112  virtual T & operator() (size_t index) = 0;
114  virtual const T & operator() (size_t index) const = 0;
115 
116 protected:
117 
120 
121 };
122 
123 
131 template <class T>
132 class MatrixType : public ArrayType<T> {
133 
134 public:
135 
137  virtual ~MatrixType() {}
138 
140  virtual size_t cols() const = 0;
142  virtual size_t rows() const = 0;
143 
145  virtual void init(size_t cols, size_t rows) = 0;
147  virtual void init(size_t cols, size_t rows, T initVal) = 0;
148 
150  virtual T & operator() (size_t col, size_t row) = 0;
152  virtual const T & operator() (size_t col, size_t row) const = 0;
153 
154 protected:
155 
157  MatrixType() {};
158 
159 };
160 
161 
169 class StructuredType : public DataType {
170 
171 public:
172 
174  virtual ~StructuredType() {}
175 
176 protected:
177 
180 
181 };
182 
183 
191 template <class T>
193 
194 protected:
195 
198 
199 public:
200 
204  virtual void fill(T val) = 0;
205 
206 };
207 
208 
216 template <class T>
217 class Vector : public VectorType<T> {
218 
219 public:
220 
222  Vector() : m_data(nullptr), m_length(0), m_capacity(0) {}
224  Vector(size_t length) : m_data(nullptr), m_length(0), m_capacity(0) {
225  (*this).init(length);
226  }
228  Vector(size_t length, T initVal) : m_data(nullptr), m_length(0), m_capacity(0) {
229  (*this).init(length);
230  (*this).fill(initVal);
231  }
232 
234  Vector(Vector&& other) : m_data(std::move(other.m_data)), m_length(other.m_length) { }
236  Vector& operator=(Vector&& other) {
237  m_data = std::move(other.m_data);
238  m_length = other.m_length;
239  return (*this);
240  }
241 
243  virtual ~Vector() {}
244 
245  virtual size_t length() const { return m_length; }
246 
247  virtual size_t size() const { return m_length * sizeof(T); }
248 
249  virtual void init(size_t length) {
250 
251  // realloc if asking for more than I can take
252  if(length > m_capacity){
253 
254  try {
255 
256  m_data.reset(new T[length]);
257  m_capacity = length;
258 
259  } catch (std::bad_alloc & e) {
260  throw RuntimeException("Memory allocation failed");
261  }
262 
263  }
264 
265  m_length = length; //< set the length the user have asked for (array is at least this big)
266 
267  }
268 
269  virtual void init(size_t length, T initVal) {
270  (*this).init(length);
271  (*this).fill(initVal);
272  }
273 
274  virtual void fill(T val) {
275  std::fill_n(m_data.get(), m_length, val);
276  }
277 
278  virtual T * data() { return m_data.get(); }
279 
280  virtual const T * data() const { return m_data.get(); }
281 
282  T & operator() (size_t index) { return m_data[index]; }
283 
284  const T & operator() (size_t index) const { return m_data[index]; }
285 
286 protected:
287 
289  std::unique_ptr<T[]> m_data;
291  size_t m_length;
292  size_t m_capacity;
293 
294 };
295 
296 
304 template <class T>
305 class Matrix : public MatrixType<T> {
306 
307 public:
308 
310  Matrix() : m_vector(), m_cols(0), m_rows(0) {}
312  Matrix(size_t cols, size_t rows) : m_vector(cols * rows), m_cols(cols), m_rows(rows) {}
314  Matrix(size_t cols, size_t rows, T initVal) : m_vector(cols * rows, initVal), m_cols(cols), m_rows(rows) {}
315 
317  Matrix(Matrix&& other) : m_vector(std::move(other.m_vector)), m_cols(other.m_cols), m_rows(other.m_rows) {}
319  Matrix& operator=(Matrix&& other) {
320  m_vector = std::move(other.m_vector);
321  m_cols = other.m_cols;
322  m_rows = other.m_rows;
323  return (*this);
324  }
325 
327  virtual ~Matrix() {}
328 
329  virtual size_t cols() const { return m_cols; }
330  virtual size_t rows() const { return m_rows; }
331 
332  virtual void init(size_t cols, size_t rows) {
333  m_vector.init(cols * rows);
334  m_cols = cols;
335  m_rows = rows;
336  }
337  virtual void init(size_t cols, size_t rows, T initVal) {
338  m_vector.init(cols * rows, initVal);
339  m_cols = cols;
340  m_rows = rows;
341  }
342 
343  virtual T * data() { return m_vector.data(); }
344  virtual const T * data() const { return m_vector.data(); }
345 
346  virtual size_t length() const { return m_vector.length(); }
347  virtual size_t size() const { return m_vector.size(); }
348 
349  virtual void fill(T val) {
350  m_vector.fill(val);
351  }
352 
353  virtual T & operator() (size_t col, size_t row) { return m_vector(row * m_cols + col); }
354  virtual const T & operator() (size_t col, size_t row) const { return m_vector(row * m_cols + col); }
355 
356 protected:
357 
361  size_t m_cols;
363  size_t m_rows;
364 
365 };
366 
367 
368 #endif /* TYPES_BASIC_HPP */
virtual void init(size_t cols, size_t rows, T initVal)
Initializes the matrix with a specified number of cols and rows and fills it with 'val'.
Definition: types_basic.hpp:337
virtual ~VectorType()
Empty destructor.
Definition: types_basic.hpp:105
Vector< T > m_vector
A Matrix is composed using a large Vector.
Definition: types_basic.hpp:359
virtual T * data()
Returns a pointer to the contained data.
Definition: types_basic.hpp:343
VectorType()
Protected constructor.
Definition: types_basic.hpp:119
T & operator()(size_t index)
Accesses an element in the vector. Doesn't check for bounds.
Definition: types_basic.hpp:282
StructuredType()
Protected constructor.
Definition: types_basic.hpp:179
Matrix(size_t cols, size_t rows, T initVal)
Constructs a Matrix with 'cols' * 'rows' elements and fills it with 'initVal'.
Definition: types_basic.hpp:314
An abstract class, representing all the matrix-like data types.
Definition: types_basic.hpp:132
virtual size_t size() const =0
Returns the size of the contained data (i.e. length * sizeof(T))
Matrix(size_t cols, size_t rows)
Constructs a Matrix with 'cols' * 'rows' elements.
Definition: types_basic.hpp:312
A class representing a vector, stored in the machine's free space.
Definition: types_basic.hpp:217
virtual size_t rows() const
Returns number of rows.
Definition: types_basic.hpp:330
This header file contains exceptions.
virtual ~ArrayType()
Empty destructor.
Definition: types_basic.hpp:72
virtual T * data()=0
Returns a pointer to the contained data.
Matrix(Matrix &&other)
Move constructor.
Definition: types_basic.hpp:317
virtual const T * data() const
Returns a const pointer to the contained data.
Definition: types_basic.hpp:280
virtual ~Vector()
Empty destructor.
Definition: types_basic.hpp:243
ComputationalContext()
Protected constructor.
Definition: types_basic.hpp:197
Matrix & operator=(Matrix &&other)
Move assignment operator.
Definition: types_basic.hpp:319
A base abstract class, representing all the data types.
Definition: types_basic.hpp:44
virtual ~StructuredType()
Empty destructor.
Definition: types_basic.hpp:174
Vector(Vector &&other)
Move constructor.
Definition: types_basic.hpp:234
Vector & operator=(Vector &&other)
Move assignment operator.
Definition: types_basic.hpp:236
virtual void init(size_t length)=0
Initializes the vector with a specified number of elements.
An abstract class, representing all the structured data types.
Definition: types_basic.hpp:169
virtual ~ComputationalContext()
Empty destructor.
Definition: types_basic.hpp:202
Vector(size_t length)
Constructs a Vector with 'length' elements.
Definition: types_basic.hpp:224
virtual void init(size_t cols, size_t rows)=0
Initializes the matrix with a specified number of cols and rows.
virtual size_t length() const
Returns number of elements in the container.
Definition: types_basic.hpp:245
Matrix()
Constructs an empty Matrix with no elements. Needs to be initialized first (init).
Definition: types_basic.hpp:310
virtual size_t size() const
Returns the size of the contained data (i.e. length * sizeof(T))
Definition: types_basic.hpp:247
virtual ~DataType()
Empty destructor.
Definition: types_basic.hpp:49
virtual size_t length() const =0
Returns number of elements in the container.
virtual T * data()
Returns a pointer to the contained data.
Definition: types_basic.hpp:278
Vector()
Constructs an empty Vector with no elements. Needs to be initialized first (init).
Definition: types_basic.hpp:222
virtual ~MatrixType()
Empty destructor.
Definition: types_basic.hpp:137
size_t m_length
The number of elements in the vector.
Definition: types_basic.hpp:291
virtual T & operator()(size_t col, size_t row)
Accesses an element in the matrix. Doesn't check for bounds.
Definition: types_basic.hpp:353
virtual T & operator()(size_t col, size_t row)=0
Accesses an element in the matrix. Doesn't check for bounds.
virtual void fill(T val)=0
Fills the container with the 'val'.
virtual size_t rows() const =0
Returns number of rows.
virtual void init(size_t length)
Initializes the vector with a specified number of elements.
Definition: types_basic.hpp:249
virtual const T * data() const
Returns a const pointer to the contained data.
Definition: types_basic.hpp:344
MatrixType()
Protected constructor.
Definition: types_basic.hpp:157
A class representing a matrix, stored in the machine's free space.
Definition: types_basic.hpp:305
size_t m_rows
Number of rows.
Definition: types_basic.hpp:363
Vector(size_t length, T initVal)
Constructs a Vector with 'length' elements and fills it with 'initVal'.
Definition: types_basic.hpp:228
DataType()
Protected constructor.
Definition: types_basic.hpp:54
virtual size_t cols() const =0
Returns number of columns.
virtual void fill(T val)=0
Fills the context's containers (vectors, matrices,...) with the 'val'.
size_t m_cols
Number of columns.
Definition: types_basic.hpp:361
An abstract class, representing all the vector-like data types.
Definition: types_basic.hpp:100
virtual void init(size_t length, T initVal)
Initializes the vector with a specified number of elements and fills it with 'val'.
Definition: types_basic.hpp:269
virtual T & operator()(size_t index)=0
Accesses an element in the vector. Doesn't check for bounds.
An abstract class, representing all the computational contexts.
Definition: types_basic.hpp:192
virtual void fill(T val)
Fills the container with the 'val'.
Definition: types_basic.hpp:274
virtual void init(size_t cols, size_t rows)
Initializes the matrix with a specified number of cols and rows.
Definition: types_basic.hpp:332
ArrayType()
Protected constructor.
Definition: types_basic.hpp:87
An exception which cannot be directly influenced by the user, or predicted beforehand.
Definition: exceptions.hpp:76
virtual size_t length() const
Returns number of elements in the container.
Definition: types_basic.hpp:346
virtual void fill(T val)
Fills the container with the 'val'.
Definition: types_basic.hpp:349
std::unique_ptr< T[]> m_data
Unique_ptr holding the allocated space.
Definition: types_basic.hpp:289
virtual size_t size() const
Returns the size of the contained data (i.e. length * sizeof(T))
Definition: types_basic.hpp:347
virtual ~Matrix()
Empty destructor.
Definition: types_basic.hpp:327
virtual size_t cols() const
Returns number of columns.
Definition: types_basic.hpp:329
An abstract class, representing all the array-like data types.
Definition: types_basic.hpp:67