NeTrainSim 0.1.1 beta
The Open-Source Network Trains Simulator
 
Loading...
Searching...
No Matches
vector.h
Go to the documentation of this file.
1
6#ifndef VECTOR_H
7#define VECTOR_H
8
9#include <iostream>
10#include <vector>
11#include <algorithm>
12#include <numeric>
13#include <sstream>
14
23template <typename T>
24class Vector : public std::vector<T> {
25public:
27 using std::vector<T>::vector;
28
37 [[nodiscard]] auto argmin() const {
38 return std::distance(std::cbegin(*this),
39 std::min_element(std::cbegin(*this), std::cend(*this)));
40 }
41
50 [[nodiscard]] auto argmax() const {
51 return std::distance(std::cbegin(*this),
52 std::max_element(std::cbegin(*this), std::cend(*this)));
53 }
54
61 void sort() {
62 std::sort(std::begin(*this), std::end(*this));
63 }
64
73 [[nodiscard]] T min() const
74 {
75 return *std::min_element(std::cbegin(*this), std::cend(*this));
76 }
77
86 [[nodiscard]] T max() const
87 {
88 return *std::max_element(std::cbegin(*this), std::cend(*this));
89 }
90
99 [[nodiscard]] T sum() const
100 {
101 return std::accumulate(std::cbegin(*this), std::cend(*this), T{});
102 }
103
112 [[nodiscard]] int index(const T& element) const
113 {
114 auto it = std::find(std::cbegin(*this), std::cend(*this), element);
115 return it != std::cend(*this) ? std::distance(std::cbegin(*this), it) : -1;
116 }
117
128 [[nodiscard]] bool exist(const T& element) const
129 {
130 return std::find(std::cbegin(*this), std::cend(*this), element) != std::cend(*this);
131 }
132
138 [[nodiscard]] bool hasCommonElement(const Vector<T>& other) const {
139 for (const auto& elem : other) {
140 if (std::find(this->begin(), this->end(), elem) != this->end()) {
141 return true;
142 }
143 }
144 return false;
145 }
146
147
153 bool isSubsetOf(const Vector<T>& other) const {
154 for (const auto& elem : *this) {
155 if (std::find(other.begin(), other.end(), elem) == other.end()) {
156 return false;
157 }
158 }
159 return true;
160 }
161
170 void insertToEnd(const std::vector<T>& other_vector) {
171 if (other_vector.empty()) {
172 return;
173 }
174 this->reserve(this->size() + other_vector.size());
175 this->insert(this->end(), other_vector.begin(), other_vector.end());
176 }
177
188 bool removeValue(const T& value) {
189 auto iter = std::find(this->begin(), this->end(), value);
190 if (iter != this->end()) {
191 this->erase(iter);
192 return true;
193 }
194 return false;
195 }
196
205 std::string toString() const {
206 std::stringstream ss = std::stringstream("");
207 bool first = true;
208 ss << "[ ";
209 for (const auto& elem : *this) {
210 if (!first) {
211 ss << ", ";
212 }
213 ss << elem;
214 first = false;
215 }
216 ss << "]";
217 return ss.str();
218 }
219
220 std::string toNotFormattedString() const {
221 std::stringstream ss = std::stringstream("");
222 bool first = true;
223 for (const auto& elem : *this) {
224 if (!first) {
225 ss << ",";
226 }
227 ss << elem;
228 first = false;
229 }
230 return ss.str();
231 }
232};
233
234#endif // VECTOR_H
A vector.
Definition vector.h:24
bool removeValue(const T &value)
Removes the value described by value.
Definition vector.h:188
bool isSubsetOf(const Vector< T > &other) const
check if the other vector is a subset of the current vector
Definition vector.h:153
std::string toNotFormattedString() const
Definition vector.h:220
std::string toString() const
Convert this object into a string representation.
Definition vector.h:205
auto argmax() const
Argmax function returns the index of the largest element in the vector.
Definition vector.h:50
T sum() const
Sum function returns the sum of all elements in the vector.
Definition vector.h:99
T min() const
min function returns the smalled element in the vector
Definition vector.h:73
bool hasCommonElement(const Vector< T > &other) const
check if the other vector contains any value in the current vector
Definition vector.h:138
bool exist(const T &element) const
check if the element exists in the vector
Definition vector.h:128
T max() const
max function returns the largest element in the vector
Definition vector.h:86
void insertToEnd(const std::vector< T > &other_vector)
insert another vector to the end of this vector
Definition vector.h:170
auto argmin() const
Argmin function returns the index of the smallest element in the vector.
Definition vector.h:37
int index(const T &element) const
get the index of an element in the vector
Definition vector.h:112
void sort()
Sort function sorts the elements in the vector in ascending order.
Definition vector.h:61