#pragma once #include #include namespace scwx { namespace util { template::const_pointer> ReturnType GetBoundedElementPointer(std::map& map, const Key& key) { ReturnType elementPtr {nullptr}; // Find the first element greater than the key requested auto it = map.upper_bound(key); // An element with a key greater was found if (it != map.cend()) { // Are there elements prior to this element? if (it != map.cbegin()) { // Get the element immediately preceding, this the element we are // looking for elementPtr = &(*(--it)); } else { // The current element is a good substitute elementPtr = &(*it); } } else if (map.size() > 0) { // An element with a key greater was not found. If it exists, it must be // the last element. elementPtr = &(*map.rbegin()); } return elementPtr; } template> ReturnType GetBoundedElement(std::map& map, const Key& key) { ReturnType element; typename std::map::pointer elementPtr = GetBoundedElementPointer::pointer>(map, key); if (elementPtr != nullptr) { element = elementPtr->second; } return element; } template inline T GetBoundedElementValue(std::map& map, const Key& key) { return GetBoundedElement(map, key); } } // namespace util } // namespace scwx