Replace the underlying structure of CCMap with a smallish vector

This commit is contained in:
Paul Fd 2020-03-05 01:38:04 +01:00
parent ab4fd7bbee
commit bce4fe96d0
2 changed files with 23 additions and 22 deletions

View file

@ -6,7 +6,8 @@
#pragma once #pragma once
#include "LeakDetector.h" #include "LeakDetector.h"
#include <map> #include <vector>
#include <absl/algorithm/container.h>
namespace sfz { namespace sfz {
/** /**
@ -42,7 +43,7 @@ public:
*/ */
const ValueType& getWithDefault(int index) const noexcept const ValueType& getWithDefault(int index) const noexcept
{ {
auto it = container.find(index); auto it = absl::c_find_if(container, [&](auto&& pair){ return pair.first == index; });
if (it == container.end()) { if (it == container.end()) {
return defaultValue; return defaultValue;
} else { } else {
@ -51,16 +52,20 @@ public:
} }
/** /**
* @brief Get the value at index key or emplace a new one if not present * @brief Get the value at index or emplace a new one if not present
* *
* @param key the index of the element * @param index the index of the element
* @return ValueType& * @return ValueType&
*/ */
ValueType& operator[](const int& key) noexcept ValueType& operator[](const int& index) noexcept
{ {
if (!contains(key)) auto it = absl::c_find_if(container, [&](auto&& pair){ return pair.first == index; });
container.emplace(key, defaultValue); if (it == container.end()) {
return container.operator[](key); container.emplace_back(index, defaultValue);
return container.back().second;
} else {
return it->second;
}
} }
/** /**
@ -70,13 +75,6 @@ public:
* @return false * @return false
*/ */
inline bool empty() const { return container.empty(); } inline bool empty() const { return container.empty(); }
/**
* @brief Returns the value at index with bounds checking (and possibly exceptions)
*
* @param index
* @return const ValueType&
*/
const ValueType& at(int index) const { return container.at(index); }
/** /**
* @brief Returns true if the container containers an element at index * @brief Returns true if the container containers an element at index
* *
@ -84,14 +82,17 @@ public:
* @return true * @return true
* @return false * @return false
*/ */
bool contains(int index) const noexcept { return container.find(index) != container.end(); } bool contains(int index) const noexcept
typename std::map<int, ValueType>::iterator begin() { return container.begin(); } {
typename std::map<int, ValueType>::const_iterator begin() const { return container.cbegin(); } return absl::c_find_if(container, [&](auto&& pair){ return pair.first == index; }) != container.end();
typename std::map<int, ValueType>::iterator end() { return container.end(); } }
typename std::map<int, ValueType>::const_iterator end() const { return container.cend(); } typename std::vector<std::pair<int, ValueType>>::iterator begin() { return container.begin(); }
typename std::vector<std::pair<int, ValueType>>::const_iterator begin() const { return container.cbegin(); }
typename std::vector<std::pair<int, ValueType>>::iterator end() { return container.end(); }
typename std::vector<std::pair<int, ValueType>>::const_iterator end() const { return container.cend(); }
private: private:
const ValueType defaultValue; const ValueType defaultValue;
std::map<int, ValueType> container; std::vector<std::pair<int, ValueType>> container;
LEAK_DETECTOR(CCMap); LEAK_DETECTOR(CCMap);
}; };
} }

View file

@ -854,7 +854,7 @@ bool sfz::Region::registerCC(int ccNumber, uint8_t ccValue) noexcept
if (!triggerOnCC) if (!triggerOnCC)
return false; return false;
if (ccTriggers.contains(ccNumber) && ccTriggers.at(ccNumber).containsWithEnd(ccValue)) if (ccTriggers.contains(ccNumber) && ccTriggers[ccNumber].containsWithEnd(ccValue))
return true; return true;
else else
return false; return false;