Skip to content

Commit

Permalink
use unordered_flat_map with robin_hood
Browse files Browse the repository at this point in the history
  • Loading branch information
hlefebvr committed Oct 25, 2023
1 parent e94b706 commit 2b8074e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cmake/FindROBINHOOD.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
find_path(
ROBINHOOD_INCLUDE_DIRS
NAMES robin_hood/robin_hood.h
HINTS /usr/include /usr/local/include/ ${ROBINHOOD_DIR} $ENV{ROBINHOOD_HOME}
NAMES robin_hood.h
HINTS /usr/include /usr/include/robin_hood/ /usr/local/include/ /usr/local/include/robin_hood/ ${ROBINHOOD_DIR} $ENV{ROBINHOOD_HOME}
)

include(FindPackageHandleStandardArgs)
Expand Down
3 changes: 2 additions & 1 deletion examples/knapsack-problem/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ int main(int t_argc, const char** t_argv) {
BranchAndBound()
.with_node_optimizer(HiGHS::ContinuousRelaxation())
.with_branching_rule(
Diver<BranchingRules::MostInfeasible<NodeVarInfo>>()
MostInfeasible()
//Diver<BranchingRules::MostInfeasible<NodeVarInfo>>()
)
.with_node_selection_rule(BestBound())
.with_log_level(Info, Blue)
Expand Down
21 changes: 15 additions & 6 deletions lib/include/idol/containers/Map.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@

#include <unordered_map>


#ifdef IDOL_USE_ROBINHOOD
#include <robin_hood/robin_hood.h>
#endif

// Implements hash for pairs (non-symmetric by default (std::hash<std::pair<T, U>>) and symmetric impls)
// See https://youngforest.github.io/2020/05/27/best-implement-to-use-pair-as-key-to-std-unordered-map-in-C/
namespace idol::impl {

#ifdef IDOL_USE_ROBINHOOD
template<class T, class EnableT = void> using hash = robin_hood::hash<T, EnableT>;
#else
template<class A> using hash = std::hash<A>;
#endif

template <typename T>
inline void hash_combine(std::size_t &seed, const T &val) {
seed ^= std::hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
seed ^= hash<T>()(val) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}

// auxiliary generic functions to create a hash value using a seed
Expand All @@ -38,8 +49,8 @@ namespace idol::impl {
template<class Key>
std::size_t operator()(const std::pair<Key, Key> &t_pair) const {
return std::less<Key>()(t_pair.first, t_pair.second) ?
std::hash<std::pair<Key, Key>>()(t_pair)
: std::hash<std::pair<Key, Key>>()(std::make_pair(t_pair.second, t_pair.first));
hash<std::pair<Key, Key>>()(t_pair)
: hash<std::pair<Key, Key>>()(std::make_pair(t_pair.second, t_pair.first));
}
};

Expand All @@ -63,8 +74,6 @@ struct std::hash<std::pair<Key1, Key2>> {

#ifdef IDOL_USE_ROBINHOOD

#include <robin_hood/robin_hood.h>

namespace idol {

template<
Expand All @@ -73,7 +82,7 @@ namespace idol {
class Hash = robin_hood::hash<Key>,
class KeyEqual = std::equal_to<Key>
>
using Map = robin_hood::unordered_map<Key, T, Hash, KeyEqual>;
using Map = robin_hood::unordered_flat_map<Key, T, Hash, KeyEqual>;

}

Expand Down

0 comments on commit 2b8074e

Please sign in to comment.