Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use gsl::narrow in heap.cpp #713

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/crab_utils/heap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************/
#include <vector>

#include <gsl/narrow>

namespace crab {

// A heap implementation with support for decrease/increase key.
// @tparam Comp a predicate that compares two integers.
// @tparam Comparator a predicate that compares two integers.
template <std::predicate<int, int> Comparator>
elazarg marked this conversation as resolved.
Show resolved Hide resolved
class Heap {
Comparator lt;
Expand Down Expand Up @@ -80,16 +82,19 @@ class Heap {
int size() const {
return heap.size();
}

[[nodiscard]]
bool empty() const {
return heap.empty();
}

[[nodiscard]]
bool inHeap(const int n) const {
return static_cast<size_t>(n) < indices.size() && indices[n] >= 0;
return gsl::narrow_cast<size_t>(n) < indices.size() && indices[n] >= 0;
elazarg marked this conversation as resolved.
Show resolved Hide resolved
}

int operator[](const int index) const {
assert(static_cast<size_t>(index) < heap.size());
assert(gsl::narrow_cast<size_t>(index) < heap.size());
elazarg marked this conversation as resolved.
Show resolved Hide resolved
return heap[index];
}

Expand All @@ -100,12 +105,13 @@ class Heap {

void insert(const int n) {
assert(n >= 0);
if (static_cast<size_t>(n) >= indices.size()) {
const auto size = gsl::narrow_cast<int>(heap.size());
if (n >= size) {
elazarg marked this conversation as resolved.
Show resolved Hide resolved
indices.resize(n + 1, -1);
}
elazarg marked this conversation as resolved.
Show resolved Hide resolved
assert(!inHeap(n));

indices[n] = static_cast<int>(heap.size());
indices[n] = size;
heap.push_back(n);
percolateUp(indices[n]);
}
Expand Down
5 changes: 2 additions & 3 deletions src/crab_utils/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0
#include "stats.hpp"

#include <optional>
#ifdef _WIN32
#include <windows.h>
#undef max
Expand All @@ -13,8 +12,8 @@

namespace crab {

thread_local crab::lazy_allocator<std::map<std::string, unsigned>> CrabStats::counters;
thread_local crab::lazy_allocator<std::map<std::string, Stopwatch>> CrabStats::sw;
thread_local lazy_allocator<std::map<std::string, unsigned>> CrabStats::counters;
elazarg marked this conversation as resolved.
Show resolved Hide resolved
thread_local lazy_allocator<std::map<std::string, Stopwatch>> CrabStats::sw;

void CrabStats::clear_thread_local_state() {
counters.clear();
Expand Down
Loading