From 7873d0e7242f7e52e277f55d34ab3e2630f72594 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Sun, 11 May 2025 16:31:12 +0000 Subject: [PATCH] revert(kernel/std): :fire: std::set is too hard to implement for now --- Kernel/include_std/set | 41 ++++++++-- Kernel/tests/stl/set.cpp | 161 +++++++++++++++++++++++++++++++++++++++ Kernel/tests/stl/stl.cpp | 4 + 3 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 Kernel/tests/stl/set.cpp diff --git a/Kernel/include_std/set b/Kernel/include_std/set index 1bc02ce7..8f9849f5 100644 --- a/Kernel/include_std/set +++ b/Kernel/include_std/set @@ -20,8 +20,11 @@ #include #include #include +#include #include +#warning "std::set not implemented; Do not use" + namespace std { template , class Allocator = std::allocator> @@ -256,13 +259,41 @@ namespace std }; template - bool operator==(const std::set &lhs, const std::set &rhs); - - // template - // std::strong_ordering operator<=>(const std::set &lhs, const std::set &rhs); + bool operator==(const std::set &lhs, const std::set &rhs) + { + return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()); + } template - void swap(std::set &lhs, std::set &rhs) noexcept(noexcept(lhs.swap(rhs))); + std::strong_ordering operator<=>(const std::set &lhs, const std::set &rhs) + { + // return std::lexicographical_compare_three_way(lhs.begin(), lhs.end(), rhs.begin(), rhs.end(), __synth_three_way); + + auto it1 = lhs.begin(); + auto it2 = rhs.begin(); + auto end1 = lhs.end(); + auto end2 = rhs.end(); + + while (it1 != end1 && it2 != end2) + { + if (*it1 < *it2) + return std::strong_ordering::less; + if (*it2 < *it1) + return std::strong_ordering::greater; + ++it1; + ++it2; + } + + if (it1 == end1 && it2 == end2) + return std::strong_ordering::equal; + return (it1 == end1) ? std::strong_ordering::less : std::strong_ordering::greater; + } + + template + void swap(std::set &lhs, std::set &rhs) noexcept(noexcept(lhs.swap(rhs))) + { + lhs.swap(rhs); + } template std::set::size_type erase_if(std::set &c, Pred pred) diff --git a/Kernel/tests/stl/set.cpp b/Kernel/tests/stl/set.cpp new file mode 100644 index 00000000..093f1d36 --- /dev/null +++ b/Kernel/tests/stl/set.cpp @@ -0,0 +1,161 @@ +/* + This file is part of Fennix Kernel. + + Fennix Kernel is free software: you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + Fennix Kernel is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Fennix Kernel. If not, see . +*/ + +#ifdef DEBUG + +#include +// #include +#include +#include +// #include + +void test_stl_set() +{ + debug("std::set ..."); + +// auto expect = false; + +// std::set a{1, 2, 3}; +// std::set b; +// b = a; +// expect = b == a; +// assert(expect); + +// auto alloc = a.get_allocator(); +// int *ptr = alloc.allocate(1); +// alloc.deallocate(ptr, 1); + +// expect = *a.begin() == 1; +// assert(expect); + +// expect = *a.cbegin() == 1; +// assert(expect); + +// expect = *a.rbegin() == 3; +// assert(expect); + +// expect = *a.crbegin() == 3; +// assert(expect); + +// expect = std::next(a.begin(), 3) == a.end(); +// assert(expect); + +// expect = std::next(a.cbegin(), 3) == a.cend(); +// assert(expect); + +// expect = std::next(a.rbegin(), 3) == a.rend(); +// assert(expect); + +// expect = std::next(a.crbegin(), 3) == a.crend(); +// assert(expect); + +// assert(!a.empty()); +// assert(a.size() == 3); +// assert(a.max_size() > 3); + +// std::set c; +// c.insert(10); +// c.insert({20, 30}); +// assert(c.size() == 3); + +// c.emplace(40); +// c.emplace_hint(c.begin(), 5); +// assert(c.count(5) == 1); +// assert(c.count(40) == 1); + +// auto it = c.find(20); +// c.erase(it); +// assert(c.count(20) == 0); + +// c.clear(); +// assert(c.empty()); + +// std::set d{1, 2, 3}; +// std::set e{4, 5}; +// d.swap(e); +// assert(d.count(4) == 1 && e.count(1) == 1); + +// #if __cpp_lib_node_extract >= 201606L +// std::set f{1, 2, 3}; +// auto node = f.extract(2); +// assert(node.value() == 2); +// assert(f.count(2) == 0); +// std::set g{10, 11}; +// g.insert(std::move(node)); +// assert(g.count(2) == 1); + +// std::set h{1, 2}; +// std::set i{2, 3}; +// h.merge(i); +// assert(h.count(3) == 1); +// assert(i.count(3) == 0); +// #endif + +// std::set s{10, 20, 30}; +// assert(s.count(20) == 1); +// assert(s.find(20) != s.end()); +// assert(s.contains(10)); +// auto range = s.equal_range(20); +// assert(range.first == s.find(20) && range.second == std::next(s.find(20))); +// assert(*s.lower_bound(15) == 20); +// assert(*s.upper_bound(20) == 30); + +// auto kc = s.key_comp(); +// assert(kc(10, 20)); +// auto vc = s.value_comp(); +// assert(vc(10, 20)); + +// std::set x{1, 2}; +// std::set y{1, 2}; +// std::set z{3, 4}; +// assert(x == y); +// assert(x != z); + +// expect = x < z; +// assert(expect); + +// expect = z > x; +// assert(expect); + +// expect = x <= y; +// assert(expect); + +// expect = z >= y; +// assert(expect); + +// #if __cpp_impl_three_way_comparison >= 201907L +// expect = (x <=> y) == std::strong_ordering::equal; +// assert(expect); +// #endif + +// std::set q{5, 6}; +// std::set r{7, 8}; +// std::swap(q, r); +// assert(q.count(7) == 1 && r.count(5) == 1); + +// #if __cpp_lib_erase_if >= 202002L +// std::set m{1, 2, 3, 4}; +// auto erased = std::erase_if(m, [](int v) +// { return v % 2 == 0; }); +// assert(erased == 2); +// assert(m == std::set({1, 3})); +// #endif + + debug("std::set OK"); +} + +#endif // DEBUG diff --git a/Kernel/tests/stl/stl.cpp b/Kernel/tests/stl/stl.cpp index fbe8b4c7..6a5ffdb1 100644 --- a/Kernel/tests/stl/stl.cpp +++ b/Kernel/tests/stl/stl.cpp @@ -28,6 +28,8 @@ void test_stl_unordered_map() {} void test_stl_future(); void test_stl_array(); void test_stl_shared_ptr(); +void test_stl_set(); +void test_stl_compare(); void Test_stl() { @@ -42,6 +44,8 @@ void Test_stl() test_stl_future(); test_stl_array(); test_stl_shared_ptr(); + test_stl_set(); + test_stl_compare(); } #endif // DEBUG