Skip to content

Commit

Permalink
further optimized bentley-ottmann
Browse files Browse the repository at this point in the history
  • Loading branch information
fhamonic committed Oct 10, 2024
1 parent b7d466c commit 5db9816
Show file tree
Hide file tree
Showing 20 changed files with 819 additions and 296 deletions.
3 changes: 3 additions & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def requirements(self):
self.requires("range-v3/0.12.0", transitive_headers=True)
self.requires("fmt/[>=10.0.0]", transitive_headers=True)
self.test_requires("gtest/[>=1.10.0 <cci]")
self.test_requires("boost/1.86.0")
self.test_requires("gmp/6.3.0")
self.test_requires("mppp/1.0.3")

def validate(self):
check_min_cppstd(self, 20)
Expand Down
274 changes: 141 additions & 133 deletions include/melon/algorithm/bentley_ottmann.hpp

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions include/melon/algorithm/competing_dijkstras.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class competing_dijkstras {
enum vertex_status : char { PRE_HEAP = 0, IN_HEAP = 1, POST_HEAP = 2 };
vertex_map_t<_Graph, vertex_status> _vertex_status_map;
heap _heap;
std::size_t _nb_blue_candidates;
std::size_t _num_blue_candidates;
[[no_unique_address]] entry_cmp _entry_cmp;

public:
Expand All @@ -89,7 +89,7 @@ class competing_dijkstras {
, _red_length_map(views::mapping_all(std::forward<_RLM>(l2)))
, _vertex_status_map(create_vertex_map<vertex_status>(_graph, PRE_HEAP))
, _heap(_entry_cmp, create_vertex_map<std::size_t>(_graph))
, _nb_blue_candidates(0) {}
, _num_blue_candidates(0) {}

template <typename... _Args>
[[nodiscard]] constexpr competing_dijkstras(_Traits, _Args &&... args)
Expand All @@ -113,7 +113,7 @@ class competing_dijkstras {
competing_dijkstras & reset() noexcept {
_vertex_status_map.fill(PRE_HEAP);
_heap.clear();
_nb_blue_candidates = 0;
_num_blue_candidates = 0;
return *this;
}

Expand All @@ -122,7 +122,7 @@ class competing_dijkstras {
const length_type dist_v = _Traits::semiring::zero) noexcept {
assert(_vertex_status_map[s] != IN_HEAP);
_heap.push(std::make_pair(s, entry_t{dist_v, true}));
++_nb_blue_candidates;
++_num_blue_candidates;
_vertex_status_map[s] = IN_HEAP;
// if constexpr(_Traits::store_paths) {
// _pred_arcs_map[s].reset();
Expand Down Expand Up @@ -151,14 +151,14 @@ class competing_dijkstras {
const entry_t old_dist = _heap.priority(w);
if(_entry_cmp(new_dist, old_dist)) {
if(!old_dist.second) {
++_nb_blue_candidates;
++_num_blue_candidates;
}
_heap.promote(w, new_dist);
}
} else if(w_status == PRE_HEAP) {
_heap.push(std::make_pair(w, new_dist));
_vertex_status_map[w] = IN_HEAP;
++_nb_blue_candidates;
++_num_blue_candidates;
}
}

Expand All @@ -170,7 +170,7 @@ class competing_dijkstras {
const entry_t old_dist = _heap.priority(w);
if(_entry_cmp(new_dist, old_dist)) {
if(old_dist.second) {
--_nb_blue_candidates;
--_num_blue_candidates;
}
_heap.promote(w, new_dist);
}
Expand All @@ -181,7 +181,7 @@ class competing_dijkstras {
}

[[nodiscard]] constexpr bool finished() const noexcept {
return _nb_blue_candidates == 0;
return _num_blue_candidates == 0;
}

[[nodiscard]] constexpr auto current() const noexcept {
Expand All @@ -199,7 +199,7 @@ class competing_dijkstras {
if(t_dist.second) {
prefetch_mapped_values(out_arcs_range, _blue_length_map);
_heap.pop();
--_nb_blue_candidates;
--_num_blue_candidates;
for(const arc & a : out_arcs_range) {
const vertex & w = arc_target(_graph, a);
relax_blue_vertex(
Expand All @@ -215,7 +215,7 @@ class competing_dijkstras {
t_dist.first, _red_length_map[a]));
}
}
} while(_nb_blue_candidates > 0 && !_heap.top().second.second);
} while(_num_blue_candidates > 0 && !_heap.top().second.second);
}

constexpr void init() noexcept {
Expand Down
6 changes: 3 additions & 3 deletions include/melon/algorithm/knapsack_bnb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ class knapsack_bnb {
_permuted_items.resize(0);
_value_cost_pairs.resize(0);
if constexpr(std::ranges::sized_range<_ItemRange>) {
auto nb_items = std::ranges::size(_items_range);
_permuted_items.reserve(nb_items);
_value_cost_pairs.reserve(nb_items);
auto num_items = std::ranges::size(_items_range);
_permuted_items.reserve(num_items);
_value_cost_pairs.reserve(num_items);
}
for(auto it = _items_range.begin(); it != _items_range.end(); ++it) {
const auto & i = *it;
Expand Down
4 changes: 2 additions & 2 deletions include/melon/algorithm/kruskal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class kruskal {
constexpr kruskal & operator=(kruskal &&) = default;

constexpr void reset() noexcept {
if constexpr(has_nb_edges<_UGraph>) {
_sorted_edges.reserve(nb_edges(_ugraph));
if constexpr(has_num_edges<_UGraph>) {
_sorted_edges.reserve(num_edges(_ugraph));
}
std::ranges::copy(edges(_ugraph), std::back_inserter(_sorted_edges));
std::ranges::sort(_sorted_edges, [this](auto && e1, auto && e2) {
Expand Down
26 changes: 13 additions & 13 deletions include/melon/algorithm/unbounded_knapsack_bnb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ class unbounded_knapsack_bnb {
best_sol_value)
goto backtrack;
begin: {
const std::size_t nb_take =
const std::size_t num_take =
static_cast<std::size_t>(budget_left / it->second);
current_sol_value += static_cast<Value>(nb_take) * it->first;
budget_left -= static_cast<Cost>(nb_take) * it->second;
current_sol.emplace_back(it, nb_take);
current_sol_value += static_cast<Value>(num_take) * it->first;
budget_left -= static_cast<Cost>(num_take) * it->second;
current_sol.emplace_back(it, num_take);
}
}
if(current_sol_value <= best_sol_value) continue;
Expand Down Expand Up @@ -109,11 +109,11 @@ class unbounded_knapsack_bnb {
best_sol_value)
goto backtrack;
begin: {
const std::size_t nb_take =
const std::size_t num_take =
static_cast<std::size_t>(budget_left / it->second);
current_sol_value += static_cast<Value>(nb_take) * it->first;
budget_left -= static_cast<Cost>(nb_take) * it->second;
current_sol.emplace_back(it, nb_take);
current_sol_value += static_cast<Value>(num_take) * it->first;
budget_left -= static_cast<Cost>(num_take) * it->second;
current_sol.emplace_back(it, num_take);
}
}
if(current_sol_value <= best_sol_value) continue;
Expand Down Expand Up @@ -146,8 +146,8 @@ class unbounded_knapsack_bnb {
if(i_cost == item_cost && i_value == item_value)
return (it < item_it);
if(i_cost > item_cost) continue;
int nb_times = static_cast<int>(item_cost / i_cost);
if(nb_times * i_value > item_value) return true;
int num_times = static_cast<int>(item_cost / i_cost);
if(num_times * i_value > item_value) return true;
}
return false;
}
Expand All @@ -157,9 +157,9 @@ class unbounded_knapsack_bnb {
_permuted_items.resize(0);
_value_cost_pairs.resize(0);
if constexpr(std::ranges::sized_range<_ItemRange>) {
auto nb_items = std::ranges::size(_items_range);
_permuted_items.reserve(nb_items);
_value_cost_pairs.reserve(nb_items);
auto num_items = std::ranges::size(_items_range);
_permuted_items.reserve(num_items);
_value_cost_pairs.reserve(num_items);
}
for(auto it = _items_range.begin(); it != _items_range.end(); ++it) {
const auto & i = *it;
Expand Down
10 changes: 5 additions & 5 deletions include/melon/container/d_ary_heap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ class d_ary_heap_base {
}
[[nodiscard]] constexpr size_type minimum_remaining_child(
const size_type first_child,
const size_type nb_children) const noexcept {
const size_type num_children) const noexcept {
if constexpr(D == 2)
return first_child;
else if constexpr(D == 4) {
switch(nb_children) {
switch(num_children) {
case 1:
return minimum_child<1>(first_child);
case 2:
Expand All @@ -110,19 +110,19 @@ class d_ary_heap_base {
return minimum_child<3>(first_child);
}
} else {
switch(nb_children) {
switch(num_children) {
case 1:
return minimum_child<1>(first_child);
case 2:
return minimum_child<2>(first_child);
default:
const size_type half = nb_children / 2;
const size_type half = num_children / 2;
const size_type first_half_minimum =
minimum_remaining_child(first_child, half);
const size_type second_half_minimum =
minimum_remaining_child(
first_child + half * sizeof(value_type),
nb_children - half);
num_children - half);
return _priority_cmp(_entry_priority_map[entry_ref(
second_half_minimum)],
_entry_priority_map[entry_ref(
Expand Down
12 changes: 6 additions & 6 deletions include/melon/container/static_filter_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class static_filter_map {
static_assert(std::is_unsigned_v<span_type>);
static constexpr size_type N = sizeof(span_type) << 3;
static constexpr size_type span_index_mask = N - 1;
static constexpr size_type nb_spans(std::size_t n) {
static constexpr size_type num_spans(std::size_t n) {
return (n + N - 1) / N;
}

Expand Down Expand Up @@ -241,7 +241,7 @@ class static_filter_map {
public:
static_filter_map() : _data(nullptr), _size(0) {};
static_filter_map(size_type size)
: _data(std::make_unique_for_overwrite<span_type[]>(nb_spans(size)))
: _data(std::make_unique_for_overwrite<span_type[]>(num_spans(size)))
, _size(size) {};

static_filter_map(size_type size, bool init_value)
Expand All @@ -251,14 +251,14 @@ class static_filter_map {

static_filter_map(const static_filter_map & other)
: static_filter_map(other._size) {
std::copy(other._data.get(), other._data.get() + nb_spans(other._size),
std::copy(other._data.get(), other._data.get() + num_spans(other._size),
_data.get());
};
static_filter_map(static_filter_map &&) = default;

static_filter_map & operator=(const static_filter_map & other) {
resize(other.size());
std::copy(other._data.get(), other._data.get() + nb_spans(other._size),
std::copy(other._data.get(), other._data.get() + num_spans(other._size),
_data.get());
return *this;
};
Expand All @@ -278,7 +278,7 @@ class static_filter_map {
size_type size() const noexcept { return _size; }
void resize(size_type n) {
if(n == _size) return;
_data = std::make_unique_for_overwrite<span_type[]>(nb_spans(n));
_data = std::make_unique_for_overwrite<span_type[]>(num_spans(n));
_size = n;
}

Expand All @@ -298,7 +298,7 @@ class static_filter_map {
}

void fill(bool b) noexcept {
std::fill(_data.get(), _data.get() + nb_spans(_size),
std::fill(_data.get(), _data.get() + num_spans(_size),
b ? ~span_type(0) : span_type(0));
}

Expand Down
Loading

0 comments on commit 5db9816

Please sign in to comment.