Timeline step back

This commit is contained in:
Dan Paulat 2023-05-26 16:14:33 -05:00
parent 6f1fb84397
commit 0ddd9d91ea
2 changed files with 90 additions and 12 deletions

View file

@ -8,14 +8,13 @@ namespace scwx
namespace util
{
template<class Container, class ReturnType = Container::const_pointer>
ReturnType GetBoundedElementPointer(Container& container,
const typename Container::key_type& key)
template<class Container>
Container::const_iterator
GetBoundedElementIterator(Container& container,
const typename Container::key_type& key)
{
ReturnType elementPtr {nullptr};
// Find the first element greater than the key requested
auto it = container.upper_bound(key);
typename Container::const_iterator it = container.upper_bound(key);
// An element with a key greater was found
if (it != container.cend())
@ -25,19 +24,34 @@ ReturnType GetBoundedElementPointer(Container& container,
{
// Get the element immediately preceding, this the element we are
// looking for
elementPtr = &(*(--it));
--it;
}
else
{
// The current element is a good substitute
elementPtr = &(*it);
}
}
else if (container.size() > 0)
{
// An element with a key greater was not found. If it exists, it must be
// the last element.
elementPtr = &(*container.rbegin());
// the last element. Decrement the end iterator.
--it;
}
return it;
}
template<class Container, class ReturnType = Container::const_pointer>
ReturnType GetBoundedElementPointer(Container& container,
const typename Container::key_type& key)
{
ReturnType elementPtr {nullptr};
auto it = GetBoundedElementIterator(container, key);
if (it != container.cend())
{
elementPtr = &(*(it));
}
return elementPtr;
@ -48,9 +62,10 @@ ReturnType GetBoundedElement(std::map<Key, T>& map, const Key& key)
{
ReturnType element;
typename std::map<Key, T>::pointer elementPtr =
typename std::map<Key, T>::const_pointer elementPtr =
GetBoundedElementPointer<std::map<Key, T>,
typename std::map<Key, T>::pointer>(map, key);
typename std::map<Key, T>::const_pointer>(map,
key);
if (elementPtr != nullptr)
{
element = elementPtr->second;