Skip to content

Commit

Permalink
Move to C++17 dropping C++11 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Tessil committed Sep 15, 2024
1 parent 2c48a1a commit 313abea
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ If the project has been installed through `make install`, you can also use `find

The library is available in [vcpkg](https://github.com/Microsoft/vcpkg/tree/master/ports/robin-map) and [conan](https://conan.io/center/tsl-robin-map). It's also present in [Debian](https://packages.debian.org/buster/robin-map-dev), [Ubuntu](https://packages.ubuntu.com/disco/robin-map-dev) and [Fedora](https://apps.fedoraproject.org/packages/robin-map-devel) package repositories.

The code should work with any C++11 standard-compliant compiler and has been tested with GCC 4.8.4, Clang 3.5.0 and Visual Studio 2015.
The code should work with any C++17 standard-compliant compiler.

To run the tests you will need the Boost Test library and CMake.

Expand Down
2 changes: 1 addition & 1 deletion doxygen.conf
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ CLANG_ASSISTED_PARSING = YES
# specified with INPUT and INCLUDE_PATH.
# This tag requires that the tag CLANG_ASSISTED_PARSING is set to YES.

CLANG_OPTIONS = -std=c++11
CLANG_OPTIONS = -std=c++17

#---------------------------------------------------------------------------
# Configuration options related to the alphabetical class index
Expand Down
64 changes: 4 additions & 60 deletions include/tsl/robin_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@ template <std::size_t GrowthFactor>
struct is_power_of_two_policy<tsl::rh::power_of_two_growth_policy<GrowthFactor>>
: std::true_type {};

// Only available in C++17, we need to be compatible with C++11
template <class T>
const T& clamp(const T& v, const T& lo, const T& hi) {
return std::min(hi, std::max(lo, v));
}

template <typename T, typename U>
static T numeric_cast(U value,
const char* error_message = "numeric_cast() failed.") {
Expand Down Expand Up @@ -254,22 +248,14 @@ class bucket_entry : public bucket_entry_hash<StoreHash> {

value_type& value() noexcept {
tsl_rh_assert(!empty());
#if defined(__cplusplus) && __cplusplus >= 201703L
return *std::launder(
reinterpret_cast<value_type*>(std::addressof(m_value)));
#else
return *reinterpret_cast<value_type*>(std::addressof(m_value));
#endif
}

const value_type& value() const noexcept {
tsl_rh_assert(!empty());
#if defined(__cplusplus) && __cplusplus >= 201703L
return *std::launder(
reinterpret_cast<const value_type*>(std::addressof(m_value)));
#else
return *reinterpret_cast<const value_type*>(std::addressof(m_value));
#endif
}

distance_type dist_from_ideal_bucket() const noexcept {
Expand Down Expand Up @@ -541,7 +527,6 @@ class robin_hash : private Hash, private KeyEqual, private GrowthPolicy {
};

public:
#if defined(__cplusplus) && __cplusplus >= 201402L
robin_hash(size_type bucket_count, const Hash& hash, const KeyEqual& equal,
const Allocator& alloc,
float min_load_factor = DEFAULT_MIN_LOAD_FACTOR,
Expand Down Expand Up @@ -569,47 +554,6 @@ class robin_hash : private Hash, private KeyEqual, private GrowthPolicy {
this->min_load_factor(min_load_factor);
this->max_load_factor(max_load_factor);
}
#else
/**
* C++11 doesn't support the creation of a std::vector with a custom allocator
* and 'count' default-inserted elements. The needed contructor `explicit
* vector(size_type count, const Allocator& alloc = Allocator());` is only
* available in C++14 and later. We thus must resize after using the
* `vector(const Allocator& alloc)` constructor.
*
* We can't use `vector(size_type count, const T& value, const Allocator&
* alloc)` as it requires the value T to be copyable.
*/
robin_hash(size_type bucket_count, const Hash& hash, const KeyEqual& equal,
const Allocator& alloc,
float min_load_factor = DEFAULT_MIN_LOAD_FACTOR,
float max_load_factor = DEFAULT_MAX_LOAD_FACTOR)
: Hash(hash),
KeyEqual(equal),
GrowthPolicy(bucket_count),
m_buckets_data(alloc),
m_buckets(static_empty_bucket_ptr()),
m_bucket_count(bucket_count),
m_nb_elements(0),
m_grow_on_next_insert(false),
m_try_shrink_on_next_insert(false) {
if (bucket_count > max_bucket_count()) {
TSL_RH_THROW_OR_TERMINATE(std::length_error,
"The map exceeds its maximum bucket count.");
}

if (m_bucket_count > 0) {
m_buckets_data.resize(m_bucket_count);
m_buckets = m_buckets_data.data();

tsl_rh_assert(!m_buckets_data.empty());
m_buckets_data.back().set_as_last_bucket();
}

this->min_load_factor(min_load_factor);
this->max_load_factor(max_load_factor);
}
#endif

robin_hash(const robin_hash& other)
: Hash(other),
Expand Down Expand Up @@ -1073,13 +1017,13 @@ class robin_hash : private Hash, private KeyEqual, private GrowthPolicy {
float max_load_factor() const { return m_max_load_factor; }

void min_load_factor(float ml) {
m_min_load_factor = clamp(ml, float(MINIMUM_MIN_LOAD_FACTOR),
float(MAXIMUM_MIN_LOAD_FACTOR));
m_min_load_factor = std::clamp(ml, float(MINIMUM_MIN_LOAD_FACTOR),
float(MAXIMUM_MIN_LOAD_FACTOR));
}

void max_load_factor(float ml) {
m_max_load_factor = clamp(ml, float(MINIMUM_MAX_LOAD_FACTOR),
float(MAXIMUM_MAX_LOAD_FACTOR));
m_max_load_factor = std::clamp(ml, float(MINIMUM_MAX_LOAD_FACTOR),
float(MAXIMUM_MAX_LOAD_FACTOR));
m_load_threshold = size_type(float(bucket_count()) * m_max_load_factor);
tsl_rh_assert(bucket_count() == 0 || m_load_threshold < bucket_count());
}
Expand Down
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ add_executable(tsl_robin_map_tests "main.cpp"
"robin_map_tests.cpp"
"robin_set_tests.cpp")

target_compile_features(tsl_robin_map_tests PRIVATE cxx_std_11)
target_compile_features(tsl_robin_map_tests PRIVATE cxx_std_17)

if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU")
target_compile_options(tsl_robin_map_tests PRIVATE -Werror -Wall -Wextra -Wold-style-cast -DTSL_DEBUG -UNDEBUG)
Expand All @@ -18,7 +18,7 @@ endif()

# Boost::unit_test_framework
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.54.0 REQUIRED COMPONENTS unit_test_framework)
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
target_link_libraries(tsl_robin_map_tests PRIVATE Boost::unit_test_framework)

# tsl::robin_map
Expand Down

0 comments on commit 313abea

Please sign in to comment.