Skip to content

Commit a3cccab

Browse files
Merge pull request #1089 from Geode-solutions/feat/small-set
feat(SmallSet): new set class optimize for small capacity
2 parents 2e8fb58 + 0230d6e commit a3cccab

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

include/geode/basic/small_set.hpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2019 - 2025 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
24+
#pragma once
25+
26+
#include <absl/algorithm/container.h>
27+
#include <absl/container/inlined_vector.h>
28+
29+
#include <geode/basic/common.hpp>
30+
#include <geode/basic/range.hpp>
31+
32+
namespace geode
33+
{
34+
template < typename Type, index_t capacity = 10 >
35+
class SmallSet
36+
{
37+
public:
38+
auto size() const
39+
{
40+
return container_.size();
41+
}
42+
43+
auto empty() const
44+
{
45+
return container_.empty();
46+
}
47+
48+
auto begin() const
49+
{
50+
return container_.begin();
51+
}
52+
53+
auto end() const
54+
{
55+
return container_.end();
56+
}
57+
58+
auto insert( const Type& element )
59+
{
60+
if( absl::c_contains( container_, element ) )
61+
{
62+
return false;
63+
}
64+
container_.push_back( element );
65+
return true;
66+
}
67+
68+
private:
69+
absl::InlinedVector< Type, capacity > container_;
70+
};
71+
} // namespace geode

src/geode/basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ add_geode_library(
8282
"progress_logger_manager.hpp"
8383
"range.hpp"
8484
"singleton.hpp"
85+
"small_set.hpp"
8586
"string.hpp"
8687
"timer.hpp"
8788
"types.hpp"

tests/basic/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ add_geode_test(
8080
DEPENDENCIES
8181
${PROJECT_NAME}::basic
8282
)
83+
add_geode_test(
84+
SOURCE "test-small-set.cpp"
85+
DEPENDENCIES
86+
${PROJECT_NAME}::basic
87+
)
8388
add_geode_test(
8489
SOURCE "test-uuid.cpp"
8590
DEPENDENCIES

tests/basic/test-small-set.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2019 - 2025 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
24+
#include <geode/basic/logger.hpp>
25+
#include <geode/basic/small_set.hpp>
26+
27+
#include <geode/tests/common.hpp>
28+
29+
void test()
30+
{
31+
geode::OpenGeodeBasicLibrary::initialize();
32+
geode::Logger::set_level( geode::Logger::LEVEL::debug );
33+
geode::SmallSet< double > set;
34+
OPENGEODE_EXCEPTION( set.empty(), "[Test] Set should be empty" );
35+
OPENGEODE_EXCEPTION( set.insert( 0 ), "[Test] Insert should be done" );
36+
OPENGEODE_EXCEPTION( set.insert( 1 ), "[Test] Insert should be done" );
37+
OPENGEODE_EXCEPTION( !set.insert( 0 ), "[Test] Insert not allow" );
38+
OPENGEODE_EXCEPTION( !set.insert( 1 ), "[Test] Insert not allow" );
39+
OPENGEODE_EXCEPTION( set.size(), "[Test] Set size should be 2" );
40+
}
41+
42+
OPENGEODE_TEST( "small-set" )

0 commit comments

Comments
 (0)