Skip to content

Commit

Permalink
add API in ortools/util
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Sep 23, 2024
1 parent c971dc4 commit 2c94629
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
4 changes: 4 additions & 0 deletions ortools/util/bitset.h
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,10 @@ inline int Bitset64<int64_t>::Value(int64_t input) {
DCHECK_GE(input, 0);
return input;
}
template <>
inline int Bitset64<size_t>::Value(size_t input) {
return input;
}

// A simple utility class to set/unset integer in a range [0, size).
// This is optimized for sparsity.
Expand Down
26 changes: 14 additions & 12 deletions ortools/util/integer_pq.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ class IntegerPriorityQueue {

private:
// Puts the given element at heap index i.
void Set(int i, Element element) {
heap_[i] = element;
void Set(Element* heap, int i, Element element) {
heap[i] = element;
position_[element.Index()] = i;
}

Expand All @@ -139,44 +139,46 @@ class IntegerPriorityQueue {
// this position.
void SetAndDecreasePriority(int i, const Element element) {
const int size = size_;
Element* heap = heap_.data();
while (true) {
const int left = i * 2;
const int right = left + 1;
if (right > size) {
if (left > size) break;
const Element left_element = heap_[left];
const Element left_element = heap[left];
if (!less_(element, left_element)) break;
Set(i, left_element);
Set(heap, i, left_element);
i = left;
break;
}
const Element left_element = heap_[left];
const Element right_element = heap_[right];
const Element left_element = heap[left];
const Element right_element = heap[right];
if (less_(left_element, right_element)) {
if (!less_(element, right_element)) break;
Set(i, right_element);
Set(heap, i, right_element);
i = right;
} else {
if (!less_(element, left_element)) break;
Set(i, left_element);
Set(heap, i, left_element);
i = left;
}
}
Set(i, element);
Set(heap, i, element);
}

// Puts the given element at heap index i and update the heap knowing that the
// element has a priority >= than the priority of the element currently at
// this position.
void SetAndIncreasePriority(int i, const Element element) {
Element* heap = heap_.data();
while (i > 1) {
const int parent = i >> 1;
const Element parent_element = heap_[parent];
const Element parent_element = heap[parent];
if (!less_(parent_element, element)) break;
Set(i, parent_element);
Set(heap, i, parent_element);
i = parent;
}
Set(i, element);
Set(heap, i, element);
}

int size_;
Expand Down

0 comments on commit 2c94629

Please sign in to comment.