Skip to content

Commit

Permalink
Cleanup. (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
acodcha committed Oct 5, 2023
1 parent 2465b60 commit 84a7575
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 53 deletions.
4 changes: 2 additions & 2 deletions include/cpp-utilities/constexpr_sqrt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace internal {
// std::sqrt. This function is an internal implementation detail and is not
// intended to be used except by the utility::constexpr_sqrt function.
inline constexpr double constexpr_sqrt_solver(
const double number, const double factor) noexcept {
const double number, const double factor) {
// Recursively factor the input number until it falls within the [0.25, 4]
// interval, which greatly reduces the number of Newton-Raphson iterations
// needed to numerically compute the square root. When solving s = sqrt(x), if
Expand Down Expand Up @@ -141,7 +141,7 @@ inline constexpr double constexpr_sqrt_solver(

// Returns the square root of a double-precision floating-point number. This
// function is a constant expression, unlike std::sqrt.
inline constexpr double constexpr_sqrt(const double number) noexcept {
inline constexpr double constexpr_sqrt(const double number) {
// The square root of zero is zero. Notably, the square root solver does not
// handle this value, so it must be treated separately.
if (number == 0.0) {
Expand Down
30 changes: 17 additions & 13 deletions include/cpp-utilities/updatable_priority_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace utility {
//
// By default, elements in the queue are ordered in increasing order of priority
// given by std::less<Priority>, such that the element with the lowest priority
// is at the head of the queue. This can be changed by providing a different
// is at the front of the queue. This can be changed by providing a different
// PriorityComparator.
//
// If multiple elements in the queue have the same priority, by default they are
Expand All @@ -48,7 +48,7 @@ template <class Value, class Priority,
class updatable_priority_queue {
public:
// Constructs an empty queue.
updatable_priority_queue() noexcept = default;
updatable_priority_queue() = default;

// Returns whether the queue is empty. The time complexity is O(1).
bool empty() const noexcept {
Expand All @@ -60,26 +60,26 @@ class updatable_priority_queue {
return value_to_priority_.size();
}

// Returns the value of the head element in the queue. If multiple elements
// are tied for the head priority, returns the first element value given by
// Returns the value of the front element in the queue. If multiple elements
// are tied for the front priority, returns the first element value given by
// ValueComparator. Results in undefined behavior if the queue is empty, so
// make sure the queue is not empty before calling this function. The time
// complexity is O(1).
const Value& head_value() const noexcept {
const Value& front_value() const noexcept {
return priority_to_values_.begin()->second;
}

// Returns the priority of the head element in the queue. Results in undefined
// behavior if the queue is empty, so make sure the queue is not empty before
// calling this function. The time complexity is O(1).
const Priority& head_priority() const noexcept {
// Returns the priority of the front element in the queue. Results in
// undefined behavior if the queue is empty, so make sure the queue is not
// empty before calling this function. The time complexity is O(1).
const Priority& front_priority() const noexcept {
return priority_to_values_.begin()->first;
}

// Attempts to remove the head element in the queue. Returns true if the head
// element is successfully removed, or false if the queue is empty. The time
// Attempts to erase the front element in the queue. Returns true if the front
// element is successfully erased, or false if the queue is empty. The time
// complexity is O(log(N)), where N is the number of elements in the queue.
bool remove_head() noexcept {
bool erase_front() {
if (empty()) {
return false;
}
Expand All @@ -105,7 +105,7 @@ class updatable_priority_queue {
// the element with the given value is successfully updated to the new
// priority, or false if the given value is not in the queue. The time
// complexity is O(log(N)), where N is the number of elements in the queue.
bool update(const Value& value, const Priority& priority) noexcept {
bool update(const Value& value, const Priority& priority) {
const typename std::map<Value, Priority, ValueComparator>::iterator
found_value_and_priority = value_to_priority_.find(value);

Expand Down Expand Up @@ -137,7 +137,11 @@ class updatable_priority_queue {
}

private:
// Map of values to their corresponding priorities.
std::map<Value, Priority, ValueComparator> value_to_priority_;

// Map of priorities to values. There can be multiple values for the same
// priority, so this is a multimap.
std::multimap<Priority, Value, PriorityComparator> priority_to_values_;
};

Expand Down
76 changes: 38 additions & 38 deletions test/updatable_priority_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,58 +35,58 @@ TEST(updatable_priority_queue, empty) {
EXPECT_TRUE(queue.empty());
EXPECT_TRUE(queue.insert("Alice", 10.0));
EXPECT_FALSE(queue.empty());
EXPECT_TRUE(queue.remove_head());
EXPECT_TRUE(queue.erase_front());
EXPECT_TRUE(queue.empty());
}

TEST(updatable_priority_queue, head_priority) {
TEST(updatable_priority_queue, erase_front) {
updatable_priority_queue<std::string, double> queue;
EXPECT_FALSE(queue.erase_front());
EXPECT_TRUE(queue.insert("Alice", 10.0));
EXPECT_EQ(queue.head_priority(), 10.0);
EXPECT_TRUE(queue.insert("Bob", 20.0));
EXPECT_EQ(queue.head_priority(), 10.0);
EXPECT_TRUE(queue.insert("Claire", 5.0));
EXPECT_EQ(queue.head_priority(), 5.0);
EXPECT_TRUE(queue.remove_head());
EXPECT_EQ(queue.head_priority(), 10.0);
EXPECT_TRUE(queue.remove_head());
EXPECT_EQ(queue.head_priority(), 20.0);
EXPECT_TRUE(queue.insert("Claire", 30.0));
EXPECT_TRUE(queue.erase_front());
EXPECT_TRUE(queue.erase_front());
EXPECT_TRUE(queue.erase_front());
EXPECT_FALSE(queue.erase_front());
}

TEST(updatable_priority_queue, head_value) {
TEST(updatable_priority_queue, front_priority) {
updatable_priority_queue<std::string, double> queue;
EXPECT_TRUE(queue.insert("Alice", 10.0));
EXPECT_EQ(queue.head_value(), "Alice");
EXPECT_EQ(queue.front_priority(), 10.0);
EXPECT_TRUE(queue.insert("Bob", 20.0));
EXPECT_EQ(queue.head_value(), "Alice");
EXPECT_EQ(queue.front_priority(), 10.0);
EXPECT_TRUE(queue.insert("Claire", 5.0));
EXPECT_EQ(queue.head_value(), "Claire");
EXPECT_TRUE(queue.remove_head());
EXPECT_EQ(queue.head_value(), "Alice");
EXPECT_TRUE(queue.remove_head());
EXPECT_EQ(queue.head_value(), "Bob");
EXPECT_EQ(queue.front_priority(), 5.0);
EXPECT_TRUE(queue.erase_front());
EXPECT_EQ(queue.front_priority(), 10.0);
EXPECT_TRUE(queue.erase_front());
EXPECT_EQ(queue.front_priority(), 20.0);
}

TEST(updatable_priority_queue, insert) {
TEST(updatable_priority_queue, front_value) {
updatable_priority_queue<std::string, double> queue;
EXPECT_TRUE(queue.insert("Alice", 10.0));
EXPECT_EQ(queue.front_value(), "Alice");
EXPECT_TRUE(queue.insert("Bob", 20.0));
EXPECT_TRUE(queue.insert("Claire", 30.0));
EXPECT_FALSE(queue.insert("Alice", 40.0));
EXPECT_TRUE(queue.insert("David", 10.0));
EXPECT_TRUE(queue.insert("Erin", 10.0));
EXPECT_EQ(queue.front_value(), "Alice");
EXPECT_TRUE(queue.insert("Claire", 5.0));
EXPECT_EQ(queue.front_value(), "Claire");
EXPECT_TRUE(queue.erase_front());
EXPECT_EQ(queue.front_value(), "Alice");
EXPECT_TRUE(queue.erase_front());
EXPECT_EQ(queue.front_value(), "Bob");
}

TEST(updatable_priority_queue, remove_head) {
TEST(updatable_priority_queue, insert) {
updatable_priority_queue<std::string, double> queue;
EXPECT_FALSE(queue.remove_head());
EXPECT_TRUE(queue.insert("Alice", 10.0));
EXPECT_TRUE(queue.insert("Bob", 20.0));
EXPECT_TRUE(queue.insert("Claire", 30.0));
EXPECT_TRUE(queue.remove_head());
EXPECT_TRUE(queue.remove_head());
EXPECT_TRUE(queue.remove_head());
EXPECT_FALSE(queue.remove_head());
EXPECT_FALSE(queue.insert("Alice", 40.0));
EXPECT_TRUE(queue.insert("David", 10.0));
EXPECT_TRUE(queue.insert("Erin", 10.0));
}

TEST(updatable_priority_queue, size) {
Expand All @@ -96,9 +96,9 @@ TEST(updatable_priority_queue, size) {
EXPECT_EQ(queue.size(), 1);
EXPECT_TRUE(queue.insert("Bob", 20.0));
EXPECT_EQ(queue.size(), 2);
EXPECT_TRUE(queue.remove_head());
EXPECT_TRUE(queue.erase_front());
EXPECT_EQ(queue.size(), 1);
EXPECT_TRUE(queue.remove_head());
EXPECT_TRUE(queue.erase_front());
EXPECT_EQ(queue.size(), 0);
}

Expand All @@ -107,16 +107,16 @@ TEST(updatable_priority_queue, update) {
EXPECT_TRUE(queue.insert("Alice", 10.0));
EXPECT_TRUE(queue.insert("Bob", 20.0));
EXPECT_TRUE(queue.insert("Claire", 5.0));
EXPECT_EQ(queue.head_value(), "Claire");
EXPECT_EQ(queue.front_value(), "Claire");
EXPECT_TRUE(queue.update("Claire", 30.0));
EXPECT_EQ(queue.head_value(), "Alice");
EXPECT_EQ(queue.front_value(), "Alice");
EXPECT_TRUE(queue.update("Bob", 5.0));
EXPECT_EQ(queue.head_value(), "Bob");
EXPECT_EQ(queue.front_value(), "Bob");
EXPECT_FALSE(queue.update("Erin", 40.0));
EXPECT_TRUE(queue.remove_head());
EXPECT_EQ(queue.head_value(), "Alice");
EXPECT_TRUE(queue.remove_head());
EXPECT_EQ(queue.head_value(), "Claire");
EXPECT_TRUE(queue.erase_front());
EXPECT_EQ(queue.front_value(), "Alice");
EXPECT_TRUE(queue.erase_front());
EXPECT_EQ(queue.front_value(), "Claire");
}

} // namespace
Expand Down

0 comments on commit 84a7575

Please sign in to comment.