diff options
Diffstat (limited to 'include/util.h')
-rw-r--r-- | include/util.h | 48 |
1 files changed, 30 insertions, 18 deletions
diff --git a/include/util.h b/include/util.h index 2e87a7b..f0c2a39 100644 --- a/include/util.h +++ b/include/util.h @@ -5,6 +5,7 @@ #ifndef GESTURES_UTIL_H_ #define GESTURES_UTIL_H_ +#include <list> #include <map> #include <set> @@ -61,28 +62,16 @@ bool MapContainsKey(const Map& the_map, const Key& the_key) { } // Removes any ids from the map that are not finger ids in hs. -// This implementation supports returning removed elements for -// further processing. -template<typename Data> -void RemoveMissingIdsFromMap(std::map<short, Data>* the_map, - const HardwareState& hs, - std::map<short, Data>* removed) { - removed->clear(); - for (typename std::map<short, Data>::const_iterator it = - the_map->begin(); it != the_map->end(); ++it) - if (!hs.GetFingerState((*it).first)) - (*removed)[it->first] = it->second; - for (typename std::map<short, Data>::const_iterator it = - removed->begin(); it != removed->end(); ++it) - the_map->erase(it->first); -} - -// Removes any ids from the map that are not finger ids in hs. template<typename Data> void RemoveMissingIdsFromMap(std::map<short, Data>* the_map, const HardwareState& hs) { std::map<short, Data> removed; - RemoveMissingIdsFromMap(the_map, hs, &removed); + for (const auto& [key, value] : *the_map) { + if (!hs.GetFingerState(key)) + removed[key] = value; + } + for (const auto& [key, value] : removed) + the_map->erase(key); } // Removes any ids from the set that are not finger ids in hs. @@ -105,6 +94,29 @@ inline bool SetContainsValue(const Set& the_set, return the_set.find(elt) != the_set.end(); } +template<typename Elem> +class List : public std::list<Elem> { +public: + Elem& at(int offset) { + // Traverse to the appropriate offset + if (offset < 0) { + // negative offset is from end to begin + for (auto iter = this->rbegin(); iter != this->rend(); ++iter) { + if (++offset == 0) + return *iter; + } + } else { + // positive offset is from begin to end + for (auto iter = this->begin(); iter != this->end(); ++iter) { + if (offset-- == 0) + return *iter; + } + } + // Invalid offset + abort(); + } +}; + } // namespace gestures #endif // GESTURES_UTIL_H_ |