Skip to content

Commit

Permalink
constexpr fast string comparator
Browse files Browse the repository at this point in the history
  • Loading branch information
malytomas committed Dec 28, 2023
1 parent b47d07b commit 39538b9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sources/include/cage-core/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ namespace cage
template<uint32 N>
struct StringComparatorFastBase
{
bool operator()(const StringBase<N> &a, const StringBase<N> &b) const noexcept
constexpr bool operator()(const StringBase<N> &a, const StringBase<N> &b) const noexcept
{
if (a.length() == b.length())
return detail::memcmp(a.begin(), b.begin(), a.length()) < 0;
Expand Down
27 changes: 27 additions & 0 deletions sources/test-core/flatSet.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
#include "main.h"

#include <cage-core/flatSet.h>
#include <cage-core/string.h>

namespace
{
constexpr uint32 testConstexpr()
{
FlatSet<String, StringComparatorFast> s = { "hello", "there" };
s.insert("Obi-Wan");
s.insert("Kenobi");
s.insert("says");
s.insert("hello");
s.insert("how");
s.insert("are");
s.insert("you");
s.insert("you");
s.insert("you");
s.insert("are");
return s.size();
}
}

void testFlatSet()
{
Expand Down Expand Up @@ -172,4 +192,11 @@ void testFlatSet()
CAGE_TEST(s.size() == 2);
CAGE_TEST(s == FlatSet<uint32>({ 13, 1024 }));
}

{
CAGE_TESTCASE("constexpr");
constexpr const uint32 a = testConstexpr();
uint32 b = testConstexpr();
CAGE_TEST(a == b);
}
}

0 comments on commit 39538b9

Please sign in to comment.