Skip to content

Commit

Permalink
Change max_size() and capacity() to members, instead of static functions
Browse files Browse the repository at this point in the history
For consistency with std:: containers
  • Loading branch information
alexkaratarakis committed Jan 3, 2024
1 parent ed5cb9b commit 12dcf71
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 36 deletions.
6 changes: 2 additions & 4 deletions include/fixed_containers/enum_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,6 @@ class EnumMapBase
return create_with_all_entries<EnumMapType, AS_ARRAY>();
}

static constexpr std::size_t max_size() noexcept { return ENUM_COUNT; }

public: // Public so this type is a structural type and can thus be used in template parameters
ValueArrayType IMPLEMENTATION_DETAIL_DO_NOT_USE_values_;
std::array<bool, ENUM_COUNT> IMPLEMENTATION_DETAIL_DO_NOT_USE_array_set_;
Expand Down Expand Up @@ -395,12 +393,12 @@ class EnumMapBase
return create_const_reverse_iterator(0);
}

[[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }

[[nodiscard]] constexpr std::size_t max_size() const noexcept { return ENUM_COUNT; }
[[nodiscard]] constexpr std::size_t size() const noexcept
{
return IMPLEMENTATION_DETAIL_DO_NOT_USE_size_;
}
[[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }

constexpr void clear() noexcept
{
Expand Down
6 changes: 2 additions & 4 deletions include/fixed_containers/enum_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,6 @@ class EnumSet
return output;
}

static constexpr std::size_t max_size() noexcept { return ENUM_COUNT; }

public: // Public so this type is a structural type and can thus be used in template parameters
// std::bitset is not sufficiently constexpr to use here, using a std::array instead.
std::array<bool, ENUM_COUNT> IMPLEMENTATION_DETAIL_DO_NOT_USE_array_set_;
Expand Down Expand Up @@ -246,12 +244,12 @@ class EnumSet
constexpr const_reverse_iterator rbegin() const noexcept { return crbegin(); }
constexpr const_reverse_iterator rend() const noexcept { return crend(); }

[[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }

[[nodiscard]] constexpr std::size_t max_size() const noexcept { return ENUM_COUNT; }
[[nodiscard]] constexpr std::size_t size() const noexcept
{
return IMPLEMENTATION_DETAIL_DO_NOT_USE_size_;
}
[[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; }

constexpr void clear() noexcept
{
Expand Down
4 changes: 1 addition & 3 deletions include/fixed_containers/fixed_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ class FixedMap
using size_type = typename Tree::size_type;
using difference_type = typename Tree::difference_type;

public:
static constexpr std::size_t max_size() noexcept { return MAXIMUM_SIZE; }

public: // Public so this type is a structural type and can thus be used in template parameters
Tree IMPLEMENTATION_DETAIL_DO_NOT_USE_tree_;

Expand Down Expand Up @@ -266,6 +263,7 @@ class FixedMap
return create_const_reverse_iterator(tree().index_of_min_at());
}

[[nodiscard]] constexpr std::size_t max_size() const noexcept { return MAXIMUM_SIZE; }
[[nodiscard]] constexpr std::size_t size() const noexcept { return tree().size(); }
[[nodiscard]] constexpr bool empty() const noexcept { return tree().empty(); }

Expand Down
4 changes: 1 addition & 3 deletions include/fixed_containers/fixed_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,6 @@ class FixedSet
using size_type = typename Tree::size_type;
using difference_type = typename Tree::difference_type;

public:
static constexpr std::size_t max_size() noexcept { return MAXIMUM_SIZE; }

public: // Public so this type is a structural type and can thus be used in template parameters
Tree IMPLEMENTATION_DETAIL_DO_NOT_USE_tree_;

Expand Down Expand Up @@ -180,6 +177,7 @@ class FixedSet
constexpr const_reverse_iterator rbegin() const noexcept { return crbegin(); }
constexpr const_reverse_iterator rend() const noexcept { return crend(); }

[[nodiscard]] constexpr std::size_t max_size() const noexcept { return MAXIMUM_SIZE; }
[[nodiscard]] constexpr std::size_t size() const noexcept { return tree().size(); }
[[nodiscard]] constexpr bool empty() const noexcept { return tree().empty(); }

Expand Down
25 changes: 12 additions & 13 deletions include/fixed_containers/fixed_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,19 +152,6 @@ class FixedVectorBase
std::array<OptionalT, MAXIMUM_SIZE> IMPLEMENTATION_DETAIL_DO_NOT_USE_array_;

public:
static constexpr std::size_t max_size() noexcept { return MAXIMUM_SIZE; }
static constexpr std::size_t capacity() noexcept { return max_size(); }
static constexpr void reserve(const std::size_t new_capacity,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
if (preconditions::test(new_capacity <= MAXIMUM_SIZE))
{
Checking::length_error(new_capacity, loc);
}
// Do nothing
}

constexpr FixedVectorBase() noexcept
: IMPLEMENTATION_DETAIL_DO_NOT_USE_size_{0}
// Don't initialize the array
Expand Down Expand Up @@ -538,6 +525,18 @@ class FixedVectorBase
/**
* Size
*/
[[nodiscard]] constexpr std::size_t max_size() const noexcept { return MAXIMUM_SIZE; }
[[nodiscard]] constexpr std::size_t capacity() const noexcept { return max_size(); }
constexpr void reserve(const std::size_t new_capacity,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
if (preconditions::test(new_capacity <= MAXIMUM_SIZE))
{
Checking::length_error(new_capacity, loc);
}
// Do nothing
}
[[nodiscard]] constexpr std::size_t size() const noexcept
{
return IMPLEMENTATION_DETAIL_DO_NOT_USE_size_;
Expand Down
20 changes: 11 additions & 9 deletions test/fixed_red_black_tree_view_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ TEST(FixedRedBlackTreeView, ViewOfPoolStorage)
auto view = FixedRedBlackTreeRawView(
ptr,
sizeof(FixedSetType::value_type),
FixedSetType::max_size(),
s1.max_size(),
COMPACTNESS,
fixed_red_black_tree_detail::RedBlackTreeStorageType::FIXED_INDEX_POOL);

Expand Down Expand Up @@ -75,7 +75,7 @@ TEST(FixedRedBlackTreeView, ViewWithStructValue)
auto view = FixedRedBlackTreeRawView(
ptr,
sizeof(FixedSetType::value_type),
FixedSetType::max_size(),
s1.max_size(),
COMPACTNESS,
fixed_red_black_tree_detail::RedBlackTreeStorageType::FIXED_INDEX_POOL);

Expand Down Expand Up @@ -104,7 +104,7 @@ TEST(FixedRedBlackTreeView, ViewOfContiguousStorage)
auto view = FixedRedBlackTreeRawView(
ptr,
sizeof(FixedSetType::value_type),
FixedSetType::max_size(),
s1.max_size(),
COMPACTNESS,
fixed_red_black_tree_detail::RedBlackTreeStorageType::FIXED_INDEX_CONTIGUOUS);

Expand Down Expand Up @@ -133,7 +133,7 @@ TEST(FixedRedBlackTreeView, PreservedOrdering)
auto view = FixedRedBlackTreeRawView(
ptr,
sizeof(FixedSetType::value_type),
FixedSetType::max_size(),
s1.max_size(),
COMPACTNESS,
fixed_red_black_tree_detail::RedBlackTreeStorageType::FIXED_INDEX_POOL);

Expand All @@ -151,16 +151,18 @@ TEST(FixedRedBlackTreeView, PreservedOrdering)

TEST(FixedRedBlackTreeView, SizeCalculation)
{
constexpr std::size_t MAXIMUM_ENTRIES = 10;
constexpr auto COMPACTNESS =
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness::EMBEDDED_COLOR;
using FixedSetType = FixedSet<int, 10, std::less<int>, COMPACTNESS, FixedIndexBasedPoolStorage>;
using FixedSetType =
FixedSet<int, MAXIMUM_ENTRIES, std::less<int>, COMPACTNESS, FixedIndexBasedPoolStorage>;

// Test empty set.
FixedSetType s1{};
auto v1 = FixedRedBlackTreeRawView(
&s1,
sizeof(FixedSetType::value_type),
FixedSetType::max_size(),
s1.max_size(),
COMPACTNESS,
fixed_red_black_tree_detail::RedBlackTreeStorageType::FIXED_INDEX_POOL);
EXPECT_EQ(v1.size(), 0);
Expand All @@ -170,7 +172,7 @@ TEST(FixedRedBlackTreeView, SizeCalculation)
auto v2 = FixedRedBlackTreeRawView(
&s2,
sizeof(FixedSetType::value_type),
FixedSetType::max_size(),
s2.max_size(),
COMPACTNESS,
fixed_red_black_tree_detail::RedBlackTreeStorageType::FIXED_INDEX_POOL);
EXPECT_EQ(v2.size(), s2.size());
Expand All @@ -180,7 +182,7 @@ TEST(FixedRedBlackTreeView, SizeCalculation)
auto v3 = FixedRedBlackTreeRawView(
&s3,
sizeof(FixedSetType::value_type),
FixedSetType::max_size(),
s3.max_size(),
COMPACTNESS,
fixed_red_black_tree_detail::RedBlackTreeStorageType::FIXED_INDEX_POOL);
EXPECT_EQ(v3.size(), s3.size());
Expand All @@ -191,7 +193,7 @@ TEST(FixedRedBlackTreeView, SizeCalculation)
auto v4 = FixedRedBlackTreeRawView(
buf,
sizeof(FixedSetType::value_type),
FixedSetType::max_size(),
MAXIMUM_ENTRIES,
COMPACTNESS,
fixed_red_black_tree_detail::RedBlackTreeStorageType::FIXED_INDEX_POOL);
EXPECT_EQ(v4.size(), 0);
Expand Down

0 comments on commit 12dcf71

Please sign in to comment.