AlgoPlus v0.1.0
Loading...
Searching...
No Matches
mat_1d.h
1#ifndef MAT_1D_H
2#define MAT_1D_H
3
4#ifdef __cplusplus
5#include <cassert>
6#include <climits>
7#include <cstdint>
8#include <initializer_list>
9#include <iostream>
10#include <optional>
11#include <vector>
12#include "../algorithms/sorting/quick_sort.h"
13#endif
14
19template <typename T, size_t SIZE> class Mat1d {
20 private:
21 T* arr;
22 size_t _size;
23
24 public:
29 explicit Mat1d(std::vector<T> v = {}) : _size(SIZE) {
30 arr = new T[_size];
31 if (!v.empty()) {
32 try {
33 if (v.size() != _size) {
34 throw std::logic_error("Initializer array don't have the same size "
35 "as the constructed array");
36 } else {
37 for (size_t i = 0; i < _size; i++) {
38 arr[i] = v[i];
39 }
40 }
41 } catch (std::logic_error& e) {
42 std::cerr << e.what() << '\n';
43 }
44 }
45 }
46
51 explicit Mat1d(std::initializer_list<T> il) : _size(SIZE) {
52 arr = new T[_size];
53 if (il.size() != _size) {
54 throw std::logic_error("Initializer list doesn't have the same size as "
55 "the constructed array");
56 }
57 std::copy(il.begin(), il.end(), arr);
58 }
59
64 explicit Mat1d(const T val) noexcept : _size(SIZE) {
65 arr = new T[_size];
66 for (size_t i = 0; i < _size; ++i) {
67 arr[i] = val;
68 }
69 }
70
76 explicit Mat1d(Mat1d& mat) : _size(SIZE) {
77 try {
78 if (mat.size() != _size) {
79 throw std::logic_error("Tried to copy matrices with different sizes");
80 }
81 this->arr = new T[_size];
82 for (size_t i = 0; i < _size; i++) {
83 this->arr[i] = mat.arr[i];
84 }
85 } catch (std::logic_error& e) {
86 std::cerr << e.what() << '\n';
87 }
88 }
89
96 if (this == &mat) {
97 return *(this);
98 }
99 assert(mat.size() == this->_size);
100 for (size_t i = 0; i < _size; i++) {
101 this->arr[i] = mat.arr[i];
102 }
103 return *(this);
104 }
105
110 ~Mat1d() { delete[] arr; }
111
117 size_t size() const { return this->_size; }
118
123 typedef T* iterator;
124 T* begin() { return &arr[0]; }
125 // Iterator begin() { return Iterator(0, _size, arr); }
126
131 T* end() { return &arr[0] + SIZE; }
132 // Iterator end() { return Iterator(_size, _size, arr); }
133
138 T& operator[](const size_t index) {
139 assert(index < _size);
140 return arr[index];
141 }
142
149 bool operator==(const Mat1d<T, SIZE>& mat) const {
150 if (mat.size() != _size) {
151 return false;
152 }
153
154 for (size_t i = 0; i < _size; i++) {
155 if (mat.arr[i] != this->arr[i]) {
156 return false;
157 }
158 }
159 return true;
160 }
161
168 bool operator!=(const Mat1d<T, SIZE>& mat) const { return !(*this == mat); }
169
175 friend std::ostream& operator<<(std::ostream& out, const Mat1d& mat) {
176 out << '[';
177 for (size_t i = 0; i < mat.size(); i++) {
178 out << mat.arr[i];
179 if (i != mat.size() - 1) {
180 out << " ";
181 }
182 }
183 out << ']' << '\n';
184 return out;
185 }
186};
187
191template <typename T, size_t SIZE1, size_t SIZE2>
192bool operator==(const Mat1d<T, SIZE1>& mat1, const Mat1d<T, SIZE2>& mat2) {
193 return false; // Matrices of different sizes are not equal
194}
195
196template <typename T, size_t SIZE1, size_t SIZE2>
197bool operator!=(const Mat1d<T, SIZE1>& mat1, const Mat1d<T, SIZE2>& mat2) {
198 return !(mat1 == mat2); // Uses the == operator specialization
199}
200
201#endif
Class for 1-dimensional Matrix.
Definition mat_1d.h:19
T * iterator
Iterator begin for Mat1d class.
Definition mat_1d.h:123
Mat1d(std::vector< T > v={})
constructor for Mat1d class
Definition mat_1d.h:29
friend std::ostream & operator<<(std::ostream &out, const Mat1d &mat)
operator << for Mat1d class
Definition mat_1d.h:175
~Mat1d()
destructor of Mat1d class
Definition mat_1d.h:110
size_t size() const
size function
Definition mat_1d.h:117
Mat1d(Mat1d &mat)
copy constructor for Mat1d class
Definition mat_1d.h:76
Mat1d & operator=(Mat1d &mat)
copy constructor for Mat1d class
Definition mat_1d.h:95
Mat1d(std::initializer_list< T > il)
constructor for Mat1d class with initializer list
Definition mat_1d.h:51
T * end()
Iterator end for Mat1d class.
Definition mat_1d.h:131
Mat1d(const T val) noexcept
constructor for Mat1d class with input value
Definition mat_1d.h:64
bool operator!=(const Mat1d< T, SIZE > &mat) const
operator != for Mat1d class
Definition mat_1d.h:168
T & operator[](const size_t index)
operator[] for Mat1d class
Definition mat_1d.h:138
bool operator==(const Mat1d< T, SIZE > &mat) const
operator == for Mat1d class
Definition mat_1d.h:149