Added a template specialization to compare on value and not CC

This commit is contained in:
Paul Fd 2020-03-07 12:43:20 +01:00
parent 4398f3bab4
commit 3046c2393f
2 changed files with 23 additions and 5 deletions

View file

@ -44,7 +44,7 @@ public:
*/ */
const ValueType& getWithDefault(int index) const noexcept const ValueType& getWithDefault(int index) const noexcept
{ {
auto it = absl::c_lower_bound(container, index, CompareCC<ValueType>{}); auto it = absl::c_lower_bound(container, index, CCValuePairComparator<ValueType>{});
if (it == container.end() || it->cc != index) { if (it == container.end() || it->cc != index) {
return defaultValue; return defaultValue;
} else { } else {
@ -60,7 +60,7 @@ public:
*/ */
ValueType& operator[](const int& index) noexcept ValueType& operator[](const int& index) noexcept
{ {
auto it = absl::c_lower_bound(container, index, CompareCC<ValueType>{}); auto it = absl::c_lower_bound(container, index, CCValuePairComparator<ValueType>{});
if (it == container.end() || it->cc != index) { if (it == container.end() || it->cc != index) {
auto inserted = container.insert(it, { index, defaultValue }); auto inserted = container.insert(it, { index, defaultValue });
return inserted->value; return inserted->value;
@ -85,7 +85,7 @@ public:
*/ */
bool contains(int index) const noexcept bool contains(int index) const noexcept
{ {
return absl::c_binary_search(container, index, CompareCC<ValueType>{}); return absl::c_binary_search(container, index, CCValuePairComparator<ValueType>{});
} }
typename std::vector<CCValuePair<ValueType>>::const_iterator begin() const { return container.cbegin(); } typename std::vector<CCValuePair<ValueType>>::const_iterator begin() const { return container.cbegin(); }
typename std::vector<CCValuePair<ValueType>>::const_iterator end() const { return container.cend(); } typename std::vector<CCValuePair<ValueType>>::const_iterator end() const { return container.cend(); }

View file

@ -24,8 +24,8 @@ struct CCValuePair {
ValueType value; ValueType value;
}; };
template<class ValueType> template<class ValueType, bool CompareValue = false>
struct CompareCC { struct CCValuePairComparator {
bool operator()(const CCValuePair<ValueType>& valuePair, const int& cc) bool operator()(const CCValuePair<ValueType>& valuePair, const int& cc)
{ {
return (valuePair.cc < cc); return (valuePair.cc < cc);
@ -42,6 +42,24 @@ struct CompareCC {
} }
}; };
template<class ValueType>
struct CCValuePairComparator<ValueType, true> {
bool operator()(const CCValuePair<ValueType>& valuePair, const ValueType& value)
{
return (valuePair.value < value);
}
bool operator()(const ValueType& value, const CCValuePair<ValueType>& valuePair)
{
return (value < valuePair.value);
}
bool operator()(const CCValuePair<ValueType>& lhs, const CCValuePair<ValueType>& rhs)
{
return (lhs.value < rhs.value);
}
};
/** /**
* @brief Converts cents to a pitch ratio * @brief Converts cents to a pitch ratio
* *