revert(kernel/std): 🔥 std::set is too hard to implement for now

This commit is contained in:
EnderIce2 2025-05-11 16:31:12 +00:00
parent 9626ec4662
commit 7873d0e724
Signed by: enderice2
GPG Key ID: FEB6B8A8507BA62E
3 changed files with 201 additions and 5 deletions

View File

@ -20,8 +20,11 @@
#include <initializer_list>
#include <functional>
#include <algorithm>
#include <compare>
#include <memory>
#warning "std::set not implemented; Do not use"
namespace std
{
template <class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key>>
@ -256,13 +259,41 @@ namespace std
};
template <class Key, class Compare, class Alloc>
bool operator==(const std::set<Key, Compare, Alloc> &lhs, const std::set<Key, Compare, Alloc> &rhs);
// template <class Key, class Compare, class Alloc>
// std::strong_ordering operator<=>(const std::set<Key, Compare, Alloc> &lhs, const std::set<Key, Compare, Alloc> &rhs);
bool operator==(const std::set<Key, Compare, Alloc> &lhs, const std::set<Key, Compare, Alloc> &rhs)
{
return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
template <class Key, class Compare, class Alloc>
void swap(std::set<Key, Compare, Alloc> &lhs, std::set<Key, Compare, Alloc> &rhs) noexcept(noexcept(lhs.swap(rhs)));
std::strong_ordering operator<=>(const std::set<Key, Compare, Alloc> &lhs, const std::set<Key, Compare, Alloc> &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 <class Key, class Compare, class Alloc>
void swap(std::set<Key, Compare, Alloc> &lhs, std::set<Key, Compare, Alloc> &rhs) noexcept(noexcept(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
template <class Key, class Compare, class Alloc, class Pred>
std::set<Key, Compare, Alloc>::size_type erase_if(std::set<Key, Compare, Alloc> &c, Pred pred)

161
Kernel/tests/stl/set.cpp Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
*/
#ifdef DEBUG
#include <assert.h>
// #include <set>
#include <cassert>
#include <algorithm>
// #include <numeric>
void test_stl_set()
{
debug("std::set ...");
// auto expect = false;
// std::set<int> a{1, 2, 3};
// std::set<int> 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<int> 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<int> d{1, 2, 3};
// std::set<int> e{4, 5};
// d.swap(e);
// assert(d.count(4) == 1 && e.count(1) == 1);
// #if __cpp_lib_node_extract >= 201606L
// std::set<int> f{1, 2, 3};
// auto node = f.extract(2);
// assert(node.value() == 2);
// assert(f.count(2) == 0);
// std::set<int> g{10, 11};
// g.insert(std::move(node));
// assert(g.count(2) == 1);
// std::set<int> h{1, 2};
// std::set<int> i{2, 3};
// h.merge(i);
// assert(h.count(3) == 1);
// assert(i.count(3) == 0);
// #endif
// std::set<int> 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<int> x{1, 2};
// std::set<int> y{1, 2};
// std::set<int> 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<int> q{5, 6};
// std::set<int> r{7, 8};
// std::swap(q, r);
// assert(q.count(7) == 1 && r.count(5) == 1);
// #if __cpp_lib_erase_if >= 202002L
// std::set<int> m{1, 2, 3, 4};
// auto erased = std::erase_if(m, [](int v)
// { return v % 2 == 0; });
// assert(erased == 2);
// assert(m == std::set<int>({1, 3}));
// #endif
debug("std::set OK");
}
#endif // DEBUG

View File

@ -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