diff --git a/srclib/libhelper/include/libhelper/lib.hpp b/srclib/libhelper/include/libhelper/lib.hpp index 261dc3a..d2c1b1b 100644 --- a/srclib/libhelper/include/libhelper/lib.hpp +++ b/srclib/libhelper/include/libhelper/lib.hpp @@ -165,10 +165,10 @@ private: Data *data = new Data[capacity]; for (size_t i = 0; i < count; i++) - data[i] = d[i]; + data[i] = tuple_data[i]; - delete[] d; - d = data; + delete[] tuple_data; + tuple_data = data; } } @@ -178,13 +178,6 @@ public: _Type2 second; _Type3 third; - friend std::ostream &operator<<(std::ostream &os, - const Data &data) noexcept { - os << "(" << data.first << ", " << data.second << ", " << data.third - << ")"; - return os; - } - bool operator==(const std::tuple<_Type1, _Type2, _Type3> &t) const noexcept { return first == std::get<0>(t) && second == std::get<1>(t) && @@ -220,26 +213,26 @@ public: } }; - Data *d = nullptr; + Data *tuple_data = nullptr; size_t capacity{}, count{}; - PureTuple() : d(new Data[20]), capacity(20), count(0) {} - ~PureTuple() { delete[] d; } + PureTuple() : tuple_data(new Data[20]), capacity(20), count(0) {} + ~PureTuple() { delete[] tuple_data; } PureTuple(std::initializer_list val) - : d(new Data[20]), capacity(20), count(0) { + : tuple_data(new Data[20]), capacity(20), count(0) { for (const auto &v : val) insert(v); } PureTuple(PureTuple &other) - : d(new Data[other.capacity]), capacity(other.capacity), + : tuple_data(new Data[other.capacity]), capacity(other.capacity), count(other.count) { - std::copy(other.d, other.d + count, d); + std::copy(other.tuple_data, other.tuple_data + count, tuple_data); } PureTuple(PureTuple &&other) noexcept - : d(new Data[other.capacity]), capacity(other.capacity), + : tuple_data(new Data[other.capacity]), capacity(other.capacity), count(other.count) { - std::copy(other.d, other.d + count, d); + std::copy(other.tuple_data, other.tuple_data + count, tuple_data); other.clear(); } @@ -305,7 +298,7 @@ public: bool find(const Data &data) const noexcept { for (size_t i = 0; i < count; i++) - if (data == d[i]) return true; + if (data == tuple_data[i]) return true; return false; } @@ -314,7 +307,7 @@ public: std::enable_if_t>, bool> find(const std::tuple<_Type1, _Type2, _Type3> &t) const noexcept { for (size_t i = 0; i < count; i++) - if (d[i] == t) return true; + if (tuple_data[i] == t) return true; return false; } @@ -322,14 +315,14 @@ public: bool find(const _Type1 &val, const _Type2 &val2, const _Type3 &val3) const noexcept { for (size_t i = 0; i < count; i++) - if (d[i] == std::make_tuple(val, val2, val3)) return true; + if (tuple_data[i] == std::make_tuple(val, val2, val3)) return true; return false; } void insert(const Data &val) noexcept { expand_if_needed(); - if (!find(val)) d[count++] = val; + if (!find(val)) tuple_data[count++] = val; } template > @@ -337,13 +330,14 @@ public: insert(const std::tuple<_Type1, _Type2, _Type3> &t) noexcept { expand_if_needed(); if (!find(t)) - d[count++] = Data{std::get<0>(t), std::get<1>(t), std::get<2>(t)}; + tuple_data[count++] = + Data{std::get<0>(t), std::get<1>(t), std::get<2>(t)}; } void insert(const _Type1 &val, const _Type2 &val2, const _Type3 &val3) noexcept { expand_if_needed(); - if (!find(val, val2, val3)) d[count++] = Data{val, val2, val3}; + if (!find(val, val2, val3)) tuple_data[count++] = Data{val, val2, val3}; } void merge(const PureTuple &other) noexcept { @@ -357,9 +351,9 @@ public: void pop(const Data &data) noexcept { for (size_t i = 0; i < count; i++) { - if (d[i] == data) { + if (tuple_data[i] == data) { for (size_t j = i; j < count - 1; j++) - d[j] = d[j + 1]; + tuple_data[j] = tuple_data[j + 1]; --count; break; } @@ -371,7 +365,7 @@ public: for (size_t x = 0; x < count; x++) { if (i == x) { for (size_t j = i; j < count - 1; j++) - d[j] = d[j + 1]; + tuple_data[j] = tuple_data[j + 1]; --count; break; } @@ -380,9 +374,9 @@ public: void pop(const _Type1 &val, const _Type2 &val2, const _Type3 &val3) noexcept { for (size_t i = 0; i < count; i++) { - if (d[i] == std::make_tuple(val, val2, val3)) { + if (tuple_data[i] == std::make_tuple(val, val2, val3)) { for (size_t j = i; j < count - 1; j++) - d[j] = d[j + 1]; + tuple_data[j] = tuple_data[j + 1]; --count; break; } @@ -393,9 +387,9 @@ public: std::enable_if_t>, void> pop(const std::tuple<_Type1, _Type2, _Type3> &t) noexcept { for (size_t i = 0; i < count; i++) { - if (d[i] == t) { + if (tuple_data[i] == t) { for (size_t j = i; j < count - 1; j++) - d[j] = d[j + 1]; + tuple_data[j] = tuple_data[j + 1]; --count; break; } @@ -403,35 +397,38 @@ public: } void clear() noexcept { - delete[] d; + delete[] tuple_data; count = 0; capacity = 20; - d = new Data[capacity]; + tuple_data = new Data[capacity]; } - Data back() const noexcept { return (count > 0) ? d[count - 1] : Data{}; } - Data top() const noexcept { return (count > 0) ? d[0] : Data{}; } + Data back() const noexcept { + return (count > 0) ? tuple_data[count - 1] : Data{}; + } + Data top() const noexcept { return (count > 0) ? tuple_data[0] : Data{}; } Data at(size_t i) const noexcept { if (i >= count) return Data{}; - return d[i]; + return tuple_data[i]; } void foreach (std::function func) { for (size_t i = 0; i < count; i++) - func(d[i].first, d[i].second, d[i].third); + func(tuple_data[i].first, tuple_data[i].second, tuple_data[i].third); } void foreach (std::function)> func) { for (size_t i = 0; i < count; i++) - func(std::make_tuple(d[i].first, d[i].second, d[i].third)); + func(std::make_tuple(tuple_data[i].first, tuple_data[i].second, + tuple_data[i].third)); } [[nodiscard]] size_t size() const noexcept { return count; } [[nodiscard]] bool empty() const noexcept { return count == 0; } - iterator begin() const noexcept { return iterator(d); } - iterator end() const noexcept { return iterator(d + count); } + iterator begin() const noexcept { return iterator(tuple_data); } + iterator end() const noexcept { return iterator(tuple_data + count); } explicit operator bool() const noexcept { return count > 0; } bool operator!() const noexcept { return count == 0; } @@ -441,7 +438,7 @@ public: return false; for (size_t i = 0; i < this->count; i++) - if (d[i] != other.d[i]) return false; + if (tuple_data[i] != other.tuple_data[i]) return false; return true; } @@ -451,34 +448,27 @@ public: Data operator[](size_t i) const noexcept { if (i >= count) return Data{}; - return d[i]; + return tuple_data[i]; } explicit operator int() const noexcept { return count; } PureTuple &operator=(const PureTuple &other) { if (this != &other) { - delete[] d; + delete[] tuple_data; capacity = other.capacity; count = other.count; - d = new Data[capacity]; + tuple_data = new Data[capacity]; - std::copy(other.d, other.d + count, d); + std::copy(other.tuple_data, other.tuple_data + count, tuple_data); } return *this; } - friend std::ostream &operator<<(std::ostream &os, - const PureTuple &tuple) noexcept { - os << "["; - for (size_t i = 0; i < tuple.count; i++) { - os << tuple.d[i]; - if (i + 1 < tuple.count) os << ", "; - } - - os << "]"; - return os; + PureTuple &operator<<(const std::tuple<_Type1, _Type2, _Type3> &t) noexcept { + insert(t); + return *this; } friend PureTuple &operator>>(const std::tuple<_Type1, _Type2, _Type3> &t, @@ -486,11 +476,6 @@ public: tuple.insert(t); return tuple; } - - PureTuple &operator<<(const std::tuple<_Type1, _Type2, _Type3> &t) noexcept { - insert(t); - return *this; - } }; namespace LoggingProperties { diff --git a/srclib/libhelper/tests/test.cpp b/srclib/libhelper/tests/test.cpp index cf5cc1d..adb7c39 100644 --- a/srclib/libhelper/tests/test.cpp +++ b/srclib/libhelper/tests/test.cpp @@ -130,7 +130,10 @@ int main(int argc, char **argv) { std::cout << "pure tuple test: " << std::boolalpha << static_cast(values.at(0)) << std::endl; - std::cout << std::boolalpha << values << std::endl; + for (const auto &[x, y, z] : values) { + std::cout << std::boolalpha << "(" << x << ", " << y << ", " << z << ")" + << std::endl; + } std::cout << Helper::getLibVersion() << std::endl;