Skip to content

Commit

Permalink
Merge pull request #19 from coopsdev/value-compare-template
Browse files Browse the repository at this point in the history
add value-based comparator template
  • Loading branch information
cooperlarson authored Aug 7, 2024
2 parents d218770 + 5d00a57 commit 6b96a8e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Pkg(ConanFile):
name = "bpd"
version = "0.2.8"
version = "0.3.0"
license = "MIT"
author = "Cooper Larson | cooper.larson1@gmail.com"
url = ""
Expand Down
47 changes: 44 additions & 3 deletions include/BoundedPriorityDeque.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ class BoundedMaxPriorityDeque : public BoundedPriorityDequeBase<K, V> {
};

/**
* @class BoundedPriorityDeque
* @brief Lightweight custom-comparator priority dequeue
* @class BoundedPriorityDequeKeyed
* @brief Lightweight custom key comparator priority dequeue
*
* This class provides a lightweight custom-comparator implement of the base class.
* This derived class allows for non-standard or non-arithmetic key types via a custom-comparator template argument.
Expand All @@ -472,15 +472,56 @@ class BoundedMaxPriorityDeque : public BoundedPriorityDequeBase<K, V> {
* @tparam V Type of the value.
*/
template<typename K, typename V, typename Comparator = std::less<K>>
class BoundedPriorityDeque : public BoundedPriorityDequeBase<K, V> {
class BoundedPriorityDequeKeyed : public BoundedPriorityDequeBase<K, V> {
protected:
Comparator comparator;

[[nodiscard]] bool compare(K a, K b) const override { return comparator(a, b); }

public:
explicit BoundedPriorityDequeKeyed(unsigned int capacity = 0, Comparator comp = Comparator()) :
BoundedPriorityDequeBase<K, V>(capacity), comparator(comp) {}
};

/**
* @class BoundedPriorityDeque
* @brief Lightweight custom value-based comparator priority dequeue
*
* This derived class enables comparison on a value objects internal state with a comparator.
*
* The addition of a comparisonValue() allows for a key to be extracted from the value objects internal structure.
*
* ex. for double: [[nodiscard]] double comparisonValue(const Edge& edge) const { return edge.distance; }
*
* The custom-comparator should be in the form of a function<bool(K a, K b)> comparator which returns
* true if 'a' has a higher-priority than 'b'.
*
* @tparam K Template-comparator compatible key Type.
* @tparam V Type of the value.
*/
template<typename V, typename Comparator, typename K = decltype(std::declval<Comparator>().comparisonValue(std::declval<V>()))>
class BoundedPriorityDeque : public BoundedPriorityDequeBase<K, V> {
protected:
Comparator comparator;

[[nodiscard]] bool compare(K a, K b) const override {
return comparator(a, b);
}

K extractKey(const V& value) const {
return comparator.comparisonValue(value);
}

public:
explicit BoundedPriorityDeque(unsigned int capacity = 0, Comparator comp = Comparator()) :
BoundedPriorityDequeBase<K, V>(capacity), comparator(comp) {}

void emplace(const V& value) {
K key = extractKey(value);
BoundedPriorityDequeBase<K, V>::emplace(key, value);
}

void push(const V& value) { emplace(value); }
};

#endif // BOUNDED_PRIORITY_DEQUE_H
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project('bpd', 'cpp',
version : '0.2.8',
version : '0.3.0',
default_options : ['warning_level=3', 'cpp_std=c++23', 'optimization=3', 'buildtype=release'])

source_root = meson.source_root()
Expand Down
2 changes: 1 addition & 1 deletion test/deque_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ TEST(MaxDequeTest, BasicOperations) {
}

TEST(BoundedDequeTest, CustomComparator) {
BoundedPriorityDeque<int, std::string, std::greater<>> deque(10, std::greater<>());
BoundedPriorityDequeKeyed<int, std::string, std::greater<>> deque(10, std::greater<>());
deque.push(BoundingPair<int, std::string>(10, "ten"));
deque.push(BoundingPair<int, std::string>(20, "twenty"));
deque.push(BoundingPair<int, std::string>(5, "five"));
Expand Down

0 comments on commit 6b96a8e

Please sign in to comment.