Skip to content

Commit

Permalink
Special case make_fixed_*() for 0 parameters
Browse files Browse the repository at this point in the history
This is to allow users to specify such containers without needing
to special-case on the client-side. As an example, this can happen
with conditionally populated entries from macros or codegen
  • Loading branch information
alexkaratarakis committed Jun 17, 2024
1 parent 24e67c4 commit d19efd1
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 37 deletions.
24 changes: 23 additions & 1 deletion include/fixed_containers/fixed_circular_deque.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "fixed_containers/source_location.hpp"

#include <algorithm>
#include <array>
#include <cstddef>
#include <initializer_list>
#include <iterator>
Expand Down Expand Up @@ -506,6 +507,16 @@ template <typename T,
{
return {std::begin(list), std::end(list), loc};
}
template <typename T,
customize::SequenceContainerChecking CheckingType,
typename FixedCircularDequeType = FixedCircularDeque<T, 0, CheckingType>>
[[nodiscard]] constexpr FixedCircularDequeType make_fixed_circular_deque(
const std::array<T, 0> /*list*/,
const std_transition::source_location& /*loc*/
= std_transition::source_location::current()) noexcept
{
return {};
}

template <typename T, std::size_t MAXIMUM_SIZE>
[[nodiscard]] constexpr auto make_fixed_circular_deque(
Expand All @@ -515,7 +526,18 @@ template <typename T, std::size_t MAXIMUM_SIZE>
{
using CheckingType = customize::SequenceContainerAbortChecking<T, MAXIMUM_SIZE>;
using FixedCircularDequeType = FixedCircularDeque<T, MAXIMUM_SIZE, CheckingType>;
return make_fixed_deque<T, CheckingType, MAXIMUM_SIZE, FixedCircularDequeType>(list, loc);
return make_fixed_circular_deque<T, CheckingType, MAXIMUM_SIZE, FixedCircularDequeType>(list,
loc);
}
template <typename T>
[[nodiscard]] constexpr auto make_fixed_circular_deque(
const std::array<T, 0> list,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
using CheckingType = customize::SequenceContainerAbortChecking<T, 0>;
using FixedCircularDequeType = FixedCircularDeque<T, 0, CheckingType>;
return make_fixed_circular_deque<T, CheckingType, FixedCircularDequeType>(list, loc);
}

} // namespace fixed_containers
Expand Down
20 changes: 20 additions & 0 deletions include/fixed_containers/fixed_deque.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,16 @@ template <typename T,
{
return {std::begin(list), std::end(list), loc};
}
template <typename T,
customize::SequenceContainerChecking CheckingType,
typename FixedDequeType = FixedDeque<T, 0, CheckingType>>
[[nodiscard]] constexpr FixedDequeType make_fixed_deque(
const std::array<T, 0> /*list*/,
const std_transition::source_location& /*loc*/
= std_transition::source_location::current()) noexcept
{
return {};
}

template <typename T, std::size_t MAXIMUM_SIZE>
[[nodiscard]] constexpr auto make_fixed_deque(
Expand All @@ -1037,6 +1047,16 @@ template <typename T, std::size_t MAXIMUM_SIZE>
using FixedDequeType = FixedDeque<T, MAXIMUM_SIZE, CheckingType>;
return make_fixed_deque<T, CheckingType, MAXIMUM_SIZE, FixedDequeType>(list, loc);
}
template <typename T>
[[nodiscard]] constexpr auto make_fixed_deque(
const std::array<T, 0> list,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
using CheckingType = customize::SequenceContainerAbortChecking<T, 0>;
using FixedDequeType = FixedDeque<T, 0, CheckingType>;
return make_fixed_deque<T, CheckingType, FixedDequeType>(list, loc);
}

} // namespace fixed_containers

Expand Down
21 changes: 21 additions & 0 deletions include/fixed_containers/fixed_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "fixed_containers/source_location.hpp"

#include <algorithm>
#include <array>
#include <cstddef>
#include <initializer_list>
#include <iterator>
Expand Down Expand Up @@ -652,6 +653,16 @@ template <typename T,
{
return {std::begin(list), std::end(list), loc};
}
template <typename T,
customize::SequenceContainerChecking CheckingType,
typename FixedListType = FixedList<T, 0, CheckingType>>
[[nodiscard]] constexpr FixedListType make_fixed_list(
const std::array<T, 0> /*list*/,
const std_transition::source_location& /*loc*/
= std_transition::source_location::current()) noexcept
{
return {};
}

template <typename T, std::size_t MAXIMUM_SIZE>
[[nodiscard]] constexpr auto make_fixed_list(
Expand All @@ -663,6 +674,16 @@ template <typename T, std::size_t MAXIMUM_SIZE>
using FixedListType = FixedList<T, MAXIMUM_SIZE, CheckingType>;
return make_fixed_list<T, CheckingType, MAXIMUM_SIZE, FixedListType>(list, loc);
}
template <typename T>
[[nodiscard]] constexpr auto make_fixed_list(
const std::array<T, 0> list,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
using CheckingType = customize::SequenceContainerAbortChecking<T, 0>;
using FixedListType = FixedList<T, 0, CheckingType>;
return make_fixed_list<T, CheckingType, FixedListType>(list, loc);
}

} // namespace fixed_containers

Expand Down
34 changes: 34 additions & 0 deletions include/fixed_containers/fixed_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "fixed_containers/source_location.hpp"

#include <algorithm>
#include <array>
#include <cstddef>
#include <functional>

Expand Down Expand Up @@ -753,6 +754,23 @@ template <typename K,
{
return {std::begin(list), std::end(list), comparator, loc};
}
template <
typename K,
typename V,
typename Compare = std::less<K>,
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS =
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness::EMBEDDED_COLOR,
template <typename, std::size_t> typename StorageTemplate = FixedIndexBasedPoolStorage,
customize::MapChecking<K> CheckingType,
typename FixedMapType = FixedMap<K, V, 0, Compare, COMPACTNESS, StorageTemplate, CheckingType>>
[[nodiscard]] constexpr FixedMapType make_fixed_map(
const std::array<std::pair<K, V>, 0>& /*list*/,
const Compare& comparator = Compare{},
const std_transition::source_location& /*loc*/ =
std_transition::source_location::current()) noexcept
{
return FixedMapType{comparator};
}

template <typename K,
typename V,
Expand All @@ -778,6 +796,22 @@ template <typename K,
MAXIMUM_SIZE,
FixedMapType>(list, comparator, loc);
}
template <typename K,
typename V,
typename Compare = std::less<K>,
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS =
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness::EMBEDDED_COLOR,
template <typename, std::size_t> typename StorageTemplate = FixedIndexBasedPoolStorage>
[[nodiscard]] constexpr auto make_fixed_map(const std::array<std::pair<K, V>, 0>& list,
const Compare& comparator = Compare{},
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
using CheckingType = customize::MapAbortChecking<K, V, 0>;
using FixedMapType = FixedMap<K, V, 0, Compare, COMPACTNESS, StorageTemplate, CheckingType>;
return make_fixed_map<K, V, Compare, COMPACTNESS, StorageTemplate, CheckingType, FixedMapType>(
list, comparator, loc);
}

} // namespace fixed_containers

Expand Down
32 changes: 32 additions & 0 deletions include/fixed_containers/fixed_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "fixed_containers/source_location.hpp"

#include <algorithm>
#include <array>
#include <cstddef>
#include <functional>
#include <memory>
Expand Down Expand Up @@ -491,6 +492,22 @@ template <typename K,
{
return {std::begin(list), std::end(list), comparator, loc};
}
template <
typename K,
typename Compare = std::less<K>,
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS =
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness::EMBEDDED_COLOR,
template <typename, std::size_t> typename StorageTemplate = FixedIndexBasedPoolStorage,
customize::SetChecking<K> CheckingType,
typename FixedSetType = FixedSet<K, 0, Compare, COMPACTNESS, StorageTemplate, CheckingType>>
[[nodiscard]] constexpr FixedSetType make_fixed_set(
const std::array<K, 0>& /*list*/,
const Compare& comparator = Compare{},
const std_transition::source_location& /*loc*/ =
std_transition::source_location::current()) noexcept
{
return FixedSetType{comparator};
}

template <typename K,
typename Compare = std::less<K>,
Expand All @@ -514,6 +531,21 @@ template <typename K,
MAXIMUM_SIZE,
FixedSetType>(list, comparator, loc);
}
template <typename K,
typename Compare = std::less<K>,
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness COMPACTNESS =
fixed_red_black_tree_detail::RedBlackTreeNodeColorCompactness::EMBEDDED_COLOR,
template <typename, std::size_t> typename StorageTemplate = FixedIndexBasedPoolStorage>
[[nodiscard]] constexpr auto make_fixed_set(const std::array<K, 0>& list,
const Compare& comparator = Compare{},
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
using CheckingType = customize::SetAbortChecking<K, 0>;
using FixedSetType = FixedSet<K, 0, Compare, COMPACTNESS, StorageTemplate, CheckingType>;
return make_fixed_set<K, Compare, COMPACTNESS, StorageTemplate, CheckingType, FixedSetType>(
list, comparator, loc);
}

} // namespace fixed_containers

Expand Down
30 changes: 30 additions & 0 deletions include/fixed_containers/fixed_unordered_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "fixed_containers/map_checking.hpp"
#include "fixed_containers/wyhash.hpp"

#include <array>

namespace fixed_containers
{

Expand Down Expand Up @@ -86,6 +88,21 @@ template <
{
return {std::begin(list), std::end(list), hash, key_equal, loc};
}
template <typename K,
typename V,
class Hash = wyhash::hash<K>,
class KeyEqual = std::equal_to<K>,
customize::MapChecking<K> CheckingType,
typename FixedMapType = FixedUnorderedMap<K, V, 0, Hash, KeyEqual, 0, CheckingType>>
[[nodiscard]] constexpr FixedMapType make_fixed_unordered_map(
const std::array<std::pair<K, V>, 0>& /*list*/,
const Hash& hash = Hash{},
const KeyEqual& key_equal = KeyEqual{},
const std_transition::source_location& /*loc*/ =
std_transition::source_location::current()) noexcept
{
return FixedMapType{hash, key_equal};
}

template <
typename K,
Expand Down Expand Up @@ -113,6 +130,19 @@ template <
BUCKET_COUNT,
FixedMapType>(list, hash, key_equal, loc);
}
template <typename K, typename V, class Hash = wyhash::hash<K>, class KeyEqual = std::equal_to<K>>
[[nodiscard]] constexpr auto make_fixed_unordered_map(
const std::array<std::pair<K, V>, 0> list,
const Hash& hash = Hash{},
const KeyEqual& key_equal = KeyEqual{},
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
using CheckingType = customize::MapAbortChecking<K, V, 0>;
using FixedMapType = FixedUnorderedMap<K, V, 0, Hash, KeyEqual, 0, CheckingType>;
return make_fixed_unordered_map<K, V, Hash, KeyEqual, CheckingType, FixedMapType>(
list, hash, key_equal, loc);
}

} // namespace fixed_containers

Expand Down
29 changes: 29 additions & 0 deletions include/fixed_containers/fixed_unordered_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "fixed_containers/set_checking.hpp"
#include "fixed_containers/wyhash.hpp"

#include <array>

namespace fixed_containers
{

Expand Down Expand Up @@ -83,6 +85,20 @@ template <
{
return {std::begin(list), std::end(list), hash, key_equal, loc};
}
template <typename K,
class Hash = wyhash::hash<K>,
class KeyEqual = std::equal_to<K>,
customize::SetChecking<K> CheckingType,
typename FixedSetType = FixedUnorderedSet<K, 0, Hash, KeyEqual, 0, CheckingType>>
[[nodiscard]] constexpr FixedSetType make_fixed_unordered_set(
const std::array<K, 0>& /*list*/,
const Hash& hash = Hash{},
const KeyEqual& key_equal = KeyEqual{},
const std_transition::source_location& /*loc*/ =
std_transition::source_location::current()) noexcept
{
return {hash, key_equal};
}

template <
typename K,
Expand All @@ -108,6 +124,19 @@ template <
BUCKET_COUNT,
FixedSetType>(list, hash, key_equal, loc);
}
template <typename K, class Hash = wyhash::hash<K>, class KeyEqual = std::equal_to<K>>
[[nodiscard]] constexpr auto make_fixed_unordered_set(
const std::array<K, 0>& list,
const Hash& hash = Hash{},
const KeyEqual& key_equal = KeyEqual{},
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
using CheckingType = customize::SetAbortChecking<K, 0>;
using FixedSetType = FixedUnorderedSet<K, 0, Hash, KeyEqual, 0, CheckingType>;
return make_fixed_unordered_set<K, Hash, KeyEqual, CheckingType, FixedSetType>(
list, hash, key_equal, loc);
}

} // namespace fixed_containers

Expand Down
20 changes: 20 additions & 0 deletions include/fixed_containers/fixed_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,16 @@ template <typename T,
{
return {std::begin(list), std::end(list), loc};
}
template <typename T,
customize::SequenceContainerChecking CheckingType,
typename FixedVectorType = FixedVector<T, 0, CheckingType>>
[[nodiscard]] constexpr FixedVectorType make_fixed_vector(
const std::array<T, 0> /*list*/,
const std_transition::source_location& /*loc*/
= std_transition::source_location::current()) noexcept
{
return {};
}

template <typename T, std::size_t MAXIMUM_SIZE>
[[nodiscard]] constexpr auto make_fixed_vector(
Expand All @@ -1030,6 +1040,16 @@ template <typename T, std::size_t MAXIMUM_SIZE>
using FixedVectorType = FixedVector<T, MAXIMUM_SIZE, CheckingType>;
return make_fixed_vector<T, CheckingType, MAXIMUM_SIZE, FixedVectorType>(list, loc);
}
template <typename T>
[[nodiscard]] constexpr auto make_fixed_vector(
const std::array<T, 0> list,
const std_transition::source_location& loc =
std_transition::source_location::current()) noexcept
{
using CheckingType = customize::SequenceContainerAbortChecking<T, 0>;
using FixedVectorType = FixedVector<T, 0, CheckingType>;
return make_fixed_vector<T, CheckingType, FixedVectorType>(list, loc);
}

} // namespace fixed_containers

Expand Down
12 changes: 9 additions & 3 deletions test/fixed_circular_deque_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,15 @@ TEST(FixedCircularDeque, CountConstructor_ExceedsCapacity)

TEST(FixedCircularDeque, MaxSizeDeduction)
{
constexpr auto v1 = make_fixed_circular_deque({10, 11, 12, 13, 14});
static_assert(v1.max_size() == 5);
static_assert(std::ranges::equal(v1, std::array{10, 11, 12, 13, 14}));
{
constexpr auto v1 = make_fixed_circular_deque({10, 11, 12, 13, 14});
static_assert(v1.max_size() == 5);
static_assert(std::ranges::equal(v1, std::array{10, 11, 12, 13, 14}));
}
{
constexpr auto v1 = make_fixed_circular_deque<int>({});
static_assert(v1.max_size() == 0);
}
}

TEST(FixedCircularDeque, IteratorConstructor)
Expand Down
Loading

0 comments on commit d19efd1

Please sign in to comment.