pmt: improve PureTuple class.

This commit is contained in:
2025-09-22 20:16:18 +03:00
parent 17f2fb5660
commit b60c5c023f
2 changed files with 49 additions and 61 deletions

View File

@@ -165,10 +165,10 @@ private:
Data *data = new Data[capacity]; Data *data = new Data[capacity];
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++)
data[i] = d[i]; data[i] = tuple_data[i];
delete[] d; delete[] tuple_data;
d = data; tuple_data = data;
} }
} }
@@ -178,13 +178,6 @@ public:
_Type2 second; _Type2 second;
_Type3 third; _Type3 third;
friend std::ostream &operator<<(std::ostream &os,
const Data &data) noexcept {
os << "(" << data.first << ", " << data.second << ", " << data.third
<< ")";
return os;
}
bool bool
operator==(const std::tuple<_Type1, _Type2, _Type3> &t) const noexcept { operator==(const std::tuple<_Type1, _Type2, _Type3> &t) const noexcept {
return first == std::get<0>(t) && second == std::get<1>(t) && 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{}; size_t capacity{}, count{};
PureTuple() : d(new Data[20]), capacity(20), count(0) {} PureTuple() : tuple_data(new Data[20]), capacity(20), count(0) {}
~PureTuple() { delete[] d; } ~PureTuple() { delete[] tuple_data; }
PureTuple(std::initializer_list<Data> val) PureTuple(std::initializer_list<Data> val)
: d(new Data[20]), capacity(20), count(0) { : tuple_data(new Data[20]), capacity(20), count(0) {
for (const auto &v : val) for (const auto &v : val)
insert(v); insert(v);
} }
PureTuple(PureTuple &other) PureTuple(PureTuple &other)
: d(new Data[other.capacity]), capacity(other.capacity), : tuple_data(new Data[other.capacity]), capacity(other.capacity),
count(other.count) { 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 PureTuple(PureTuple &&other) noexcept
: d(new Data[other.capacity]), capacity(other.capacity), : tuple_data(new Data[other.capacity]), capacity(other.capacity),
count(other.count) { count(other.count) {
std::copy(other.d, other.d + count, d); std::copy(other.tuple_data, other.tuple_data + count, tuple_data);
other.clear(); other.clear();
} }
@@ -305,7 +298,7 @@ public:
bool find(const Data &data) const noexcept { bool find(const Data &data) const noexcept {
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++)
if (data == d[i]) return true; if (data == tuple_data[i]) return true;
return false; return false;
} }
@@ -314,7 +307,7 @@ public:
std::enable_if_t<std::is_same_v<T, std::tuple<_Type1, _Type2, _Type3>>, bool> std::enable_if_t<std::is_same_v<T, std::tuple<_Type1, _Type2, _Type3>>, bool>
find(const std::tuple<_Type1, _Type2, _Type3> &t) const noexcept { find(const std::tuple<_Type1, _Type2, _Type3> &t) const noexcept {
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++)
if (d[i] == t) return true; if (tuple_data[i] == t) return true;
return false; return false;
} }
@@ -322,14 +315,14 @@ public:
bool find(const _Type1 &val, const _Type2 &val2, bool find(const _Type1 &val, const _Type2 &val2,
const _Type3 &val3) const noexcept { const _Type3 &val3) const noexcept {
for (size_t i = 0; i < count; i++) 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; return false;
} }
void insert(const Data &val) noexcept { void insert(const Data &val) noexcept {
expand_if_needed(); expand_if_needed();
if (!find(val)) d[count++] = val; if (!find(val)) tuple_data[count++] = val;
} }
template <typename T = std::tuple<_Type1, _Type2, _Type3>> template <typename T = std::tuple<_Type1, _Type2, _Type3>>
@@ -337,13 +330,14 @@ public:
insert(const std::tuple<_Type1, _Type2, _Type3> &t) noexcept { insert(const std::tuple<_Type1, _Type2, _Type3> &t) noexcept {
expand_if_needed(); expand_if_needed();
if (!find(t)) 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, void insert(const _Type1 &val, const _Type2 &val2,
const _Type3 &val3) noexcept { const _Type3 &val3) noexcept {
expand_if_needed(); 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 { void merge(const PureTuple &other) noexcept {
@@ -357,9 +351,9 @@ public:
void pop(const Data &data) noexcept { void pop(const Data &data) noexcept {
for (size_t i = 0; i < count; i++) { 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++) for (size_t j = i; j < count - 1; j++)
d[j] = d[j + 1]; tuple_data[j] = tuple_data[j + 1];
--count; --count;
break; break;
} }
@@ -371,7 +365,7 @@ public:
for (size_t x = 0; x < count; x++) { for (size_t x = 0; x < count; x++) {
if (i == x) { if (i == x) {
for (size_t j = i; j < count - 1; j++) for (size_t j = i; j < count - 1; j++)
d[j] = d[j + 1]; tuple_data[j] = tuple_data[j + 1];
--count; --count;
break; break;
} }
@@ -380,9 +374,9 @@ public:
void pop(const _Type1 &val, const _Type2 &val2, const _Type3 &val3) noexcept { void pop(const _Type1 &val, const _Type2 &val2, const _Type3 &val3) noexcept {
for (size_t i = 0; i < count; i++) { 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++) for (size_t j = i; j < count - 1; j++)
d[j] = d[j + 1]; tuple_data[j] = tuple_data[j + 1];
--count; --count;
break; break;
} }
@@ -393,9 +387,9 @@ public:
std::enable_if_t<std::is_same_v<T, std::tuple<_Type1, _Type2, _Type3>>, void> std::enable_if_t<std::is_same_v<T, std::tuple<_Type1, _Type2, _Type3>>, void>
pop(const std::tuple<_Type1, _Type2, _Type3> &t) noexcept { pop(const std::tuple<_Type1, _Type2, _Type3> &t) noexcept {
for (size_t i = 0; i < count; i++) { 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++) for (size_t j = i; j < count - 1; j++)
d[j] = d[j + 1]; tuple_data[j] = tuple_data[j + 1];
--count; --count;
break; break;
} }
@@ -403,35 +397,38 @@ public:
} }
void clear() noexcept { void clear() noexcept {
delete[] d; delete[] tuple_data;
count = 0; count = 0;
capacity = 20; capacity = 20;
d = new Data[capacity]; tuple_data = new Data[capacity];
} }
Data back() const noexcept { return (count > 0) ? d[count - 1] : Data{}; } Data back() const noexcept {
Data top() const noexcept { return (count > 0) ? d[0] : Data{}; } 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 { Data at(size_t i) const noexcept {
if (i >= count) return Data{}; if (i >= count) return Data{};
return d[i]; return tuple_data[i];
} }
void foreach (std::function<void(_Type1, _Type2, _Type3)> func) { void foreach (std::function<void(_Type1, _Type2, _Type3)> func) {
for (size_t i = 0; i < count; i++) 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<void(std::tuple<_Type1, _Type2, _Type3>)> func) { void foreach (std::function<void(std::tuple<_Type1, _Type2, _Type3>)> func) {
for (size_t i = 0; i < count; i++) 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]] size_t size() const noexcept { return count; }
[[nodiscard]] bool empty() const noexcept { return count == 0; } [[nodiscard]] bool empty() const noexcept { return count == 0; }
iterator begin() const noexcept { return iterator(d); } iterator begin() const noexcept { return iterator(tuple_data); }
iterator end() const noexcept { return iterator(d + count); } iterator end() const noexcept { return iterator(tuple_data + count); }
explicit operator bool() const noexcept { return count > 0; } explicit operator bool() const noexcept { return count > 0; }
bool operator!() const noexcept { return count == 0; } bool operator!() const noexcept { return count == 0; }
@@ -441,7 +438,7 @@ public:
return false; return false;
for (size_t i = 0; i < this->count; i++) 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; return true;
} }
@@ -451,34 +448,27 @@ public:
Data operator[](size_t i) const noexcept { Data operator[](size_t i) const noexcept {
if (i >= count) return Data{}; if (i >= count) return Data{};
return d[i]; return tuple_data[i];
} }
explicit operator int() const noexcept { return count; } explicit operator int() const noexcept { return count; }
PureTuple &operator=(const PureTuple &other) { PureTuple &operator=(const PureTuple &other) {
if (this != &other) { if (this != &other) {
delete[] d; delete[] tuple_data;
capacity = other.capacity; capacity = other.capacity;
count = other.count; 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; return *this;
} }
friend std::ostream &operator<<(std::ostream &os, PureTuple &operator<<(const std::tuple<_Type1, _Type2, _Type3> &t) noexcept {
const PureTuple &tuple) noexcept { insert(t);
os << "["; return *this;
for (size_t i = 0; i < tuple.count; i++) {
os << tuple.d[i];
if (i + 1 < tuple.count) os << ", ";
}
os << "]";
return os;
} }
friend PureTuple &operator>>(const std::tuple<_Type1, _Type2, _Type3> &t, friend PureTuple &operator>>(const std::tuple<_Type1, _Type2, _Type3> &t,
@@ -486,11 +476,6 @@ public:
tuple.insert(t); tuple.insert(t);
return tuple; return tuple;
} }
PureTuple &operator<<(const std::tuple<_Type1, _Type2, _Type3> &t) noexcept {
insert(t);
return *this;
}
}; };
namespace LoggingProperties { namespace LoggingProperties {

View File

@@ -130,7 +130,10 @@ int main(int argc, char **argv) {
std::cout << "pure tuple test: " << std::boolalpha std::cout << "pure tuple test: " << std::boolalpha
<< static_cast<bool>(values.at(0)) << std::endl; << static_cast<bool>(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; std::cout << Helper::getLibVersion() << std::endl;