370 lines
12 KiB
C++
370 lines
12 KiB
C++
/**
|
|
* @file include/ActivMiddleware/Misc/StlHelper.h
|
|
*
|
|
* @brief Work arounds for STL disparities across platforms, plus various helpers.
|
|
*/
|
|
|
|
#if (!defined ACTIV_MISC_STL_HELPER_H)
|
|
#define ACTIV_MISC_STL_HELPER_H
|
|
|
|
#include "ActivMiddleware/ActivMiddleware.h"
|
|
|
|
#include <vector>
|
|
#include <list>
|
|
#include <map>
|
|
#include <algorithm>
|
|
|
|
#if (!defined ACTIV_BOOST_NO_CXX11_HDR_TUPLE)
|
|
#include <tuple>
|
|
#endif
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
/**
|
|
* @brief Unary function object to return the first element of a pair.
|
|
*
|
|
* @param P pair type (or any type with a first member and a first_type typedef).
|
|
*/
|
|
template <typename P>
|
|
class SelectFirst
|
|
{
|
|
public:
|
|
typedef P argument_type;
|
|
typedef typename P::first_type result_type;
|
|
|
|
/**
|
|
* @brief Function operator.
|
|
*
|
|
* @param p reference to an object of type P.
|
|
*
|
|
* @return Const reference to p.first
|
|
*/
|
|
const result_type &operator()(const argument_type &p) const
|
|
{
|
|
return p.first;
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Unary function object to return the second element of a pair.
|
|
*
|
|
* @param P pair type (or any type with a second member and a second_type typedef).
|
|
*/
|
|
template <typename P>
|
|
class SelectSecond
|
|
{
|
|
public:
|
|
typedef P argument_type;
|
|
typedef typename P::second_type result_type;
|
|
|
|
/**
|
|
* @brief Function operator.
|
|
*
|
|
* @param p reference to an object of type P.
|
|
*
|
|
* @return Const reference to p.second
|
|
*/
|
|
const result_type &operator()(const argument_type &p) const
|
|
{
|
|
return p.second;
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Unary function object to return the first element of a pair, given the second element.
|
|
*
|
|
* @param P pair type (or any type with a second member and a second_type typedef).
|
|
*/
|
|
template<typename P>
|
|
class SelectFirstFromSecond
|
|
{
|
|
public:
|
|
typedef P Pair;
|
|
typedef typename P::second_type argument_type;
|
|
typedef typename P::first_type result_type;
|
|
|
|
/**
|
|
* @brief Function operator.
|
|
*
|
|
* @param second reference to an object of type P::second_type.
|
|
*
|
|
* @return Const reference to the first member of the pair @a second is itself a member of.
|
|
*/
|
|
const result_type &operator()(const argument_type &second) const
|
|
{
|
|
const TypeToIntegralType<void *>::Type offset = ACTIV_FIELD_OFFSET(Pair, second) - ACTIV_FIELD_OFFSET(Pair, first);
|
|
|
|
return *reinterpret_cast<const result_type *>(reinterpret_cast<const byte_t *>(&second) - offset);
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Sort a container and check for duplicates.
|
|
*
|
|
* @tparam T container type.
|
|
*
|
|
* @param c container.
|
|
*
|
|
* @return true if there are no duplicates.
|
|
*/
|
|
template<typename T>
|
|
inline bool SortAndCheckForDuplicates(T &c)
|
|
{
|
|
std::sort(c.begin(), c.end());
|
|
return (c.end() == std::unique(c.begin(), c.end()));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Sort a container and check for duplicates with custom predicates.
|
|
*
|
|
* @tparam T container type.
|
|
* @tparam S ordering predicate.
|
|
* @tparam E equal predicate.
|
|
*
|
|
* @param c container.
|
|
* @param sortPredicate
|
|
* @param equalPredicate
|
|
*
|
|
* @return true if there are no duplicates.
|
|
*/
|
|
template<typename T, typename S, typename E>
|
|
inline bool SortAndCheckForDuplicates(T &c, const S &sortPredicate, const E &equalPredicate)
|
|
{
|
|
std::sort(c.begin(), c.end(), sortPredicate);
|
|
return (c.end() == std::unique(c.begin(), c.end(), equalPredicate));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Sort a container and remove any duplicates.
|
|
*
|
|
* @tparam T container type.
|
|
*
|
|
* @param c container.
|
|
*/
|
|
template<typename T>
|
|
inline void SortAndRemoveDuplicates(T &c)
|
|
{
|
|
std::sort(c.begin(), c.end());
|
|
(void) c.erase(std::unique(c.begin(), c.end()), c.end());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Sort a container and remove any duplicates.
|
|
*
|
|
* @tparam T container type.
|
|
* @tparam S ordering predicate.
|
|
* @tparam E equal predicate.
|
|
*
|
|
* @param c container.
|
|
* @param sortPredicate
|
|
* @param equalPredicate
|
|
*/
|
|
template<typename T, typename S, typename E>
|
|
inline void SortAndRemoveDuplicates(T &c, const S &sortPredicate, const E &equalPredicate)
|
|
{
|
|
std::sort(c.begin(), c.end(), sortPredicate);
|
|
(void) c.erase(std::unique(c.begin(), c.end(), equalPredicate), c.end());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Make an STL string lowercase.
|
|
*
|
|
* @param str the string to make lowercase.
|
|
*/
|
|
inline void StringToLower(String &str)
|
|
{
|
|
for (size_t i = 0, size = str.length(); i < size; ++i)
|
|
{
|
|
str[i] = ::tolower(str[i]);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Make an STL string uppercase.
|
|
*
|
|
* @param str the string to make uppercase.
|
|
*/
|
|
inline void StringToUpper(String &str)
|
|
{
|
|
for (size_t i = 0, size = str.length(); i < size; ++i)
|
|
{
|
|
str[i] = ::toupper(str[i]);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Find the iterator to an item in a set, or the point at which it can be inserted.
|
|
*
|
|
* @param set the set to search.
|
|
* @param value value to search for.
|
|
* @param iter reference to an iterator for the set.
|
|
*
|
|
* @retval true \a iter refers to the entry with value \a value.
|
|
* @retval false \a iter refers to the location at which an insert should be done.
|
|
*/
|
|
template<typename CONTAINER_TYPE, typename VALUE_TYPE>
|
|
inline bool SetFind(CONTAINER_TYPE &set, const VALUE_TYPE &value, typename CONTAINER_TYPE::iterator &iter)
|
|
{
|
|
// Returning bool and taking iterator reference is the fastest implementation (faster than
|
|
// returning a std::pair<iter, bool> or taking std::pair<iter, bool> by reference), at least with VC++.
|
|
iter = set.lower_bound(value);
|
|
|
|
// If lower_bound() doesn't return end(), it returns an iterator to an item that is
|
|
// !set.key_comp()(*iter, key), so to check for equivalence it is only necessary to
|
|
// check for !set.key_comp()(key, *iter).
|
|
return ((iter != set.end()) && !set.key_comp()(value, *iter));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Find the iterator to an item in a map, or the point at which it can be inserted.
|
|
*
|
|
* @param map the map to search.
|
|
* @param key key value to search for.
|
|
* @param iter reference to an iterator for the map.
|
|
*
|
|
* @retval true \a iter refers to the entry with key value \a key.
|
|
* @retval false \a iter refers to the location at which an insert should be done.
|
|
*/
|
|
template<typename CONTAINER_TYPE, typename KEY_TYPE>
|
|
inline bool MapFind(CONTAINER_TYPE &map, const KEY_TYPE &key, typename CONTAINER_TYPE::iterator &iter)
|
|
{
|
|
// Returning bool and taking iterator reference is the fastest implementation (faster than
|
|
// returning a std::pair<iter, bool> or taking std::pair<iter, bool> by reference), at least with VC++.
|
|
iter = map.lower_bound(key);
|
|
|
|
// If lower_bound() doesn't return end(), it returns an iterator to an item that is
|
|
// !map.key_comp()(iter->first, key), so to check for equivalence it is only necessary to
|
|
// check for !map.key_comp()(key, iter->first).
|
|
return ((iter != map.end()) && !map.key_comp()(key, iter->first));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Insert a (key, value) pair into a map.
|
|
*
|
|
* The std::map insert() method can incur the const of construction of the value_type parameter. If
|
|
* the mapped_type for the map is non-trivial, this can add unnecessary overhead if keys are always
|
|
* inserted into the map with the same, "default", mapped value.
|
|
*
|
|
* Using this helper function, it is possible to reuse an existing value_type value to save this construction overhead.
|
|
* Example usage:
|
|
*
|
|
* typedef std::map<String, NonTrivialType> MyMap;
|
|
*
|
|
* MyMap::value_type m_defaultMapValue; // member of a class, for instance
|
|
* MyMap m_map; // ditto
|
|
*
|
|
* In some function:
|
|
* std::pair<MyMap::iterator, bool> res = MapInsert(m_map, "Hello", m_defaultMapValue);
|
|
*
|
|
* @param map the map to insert into.
|
|
* @param key the key to insert.
|
|
* @param value reference to a value_type containing the value to insert into the map with key \a key.
|
|
*
|
|
* @return A pair, the first member is an iterator to the inserted or found element. The second member
|
|
* is true if an insert took place, or false if \a key was already in the map.
|
|
*/
|
|
template<typename CONTAINER_TYPE, typename KEY_TYPE>
|
|
inline std::pair<typename CONTAINER_TYPE::iterator, bool> MapInsert(CONTAINER_TYPE &map, const KEY_TYPE &key,
|
|
typename CONTAINER_TYPE::value_type &value)
|
|
{
|
|
const_cast<typename CONTAINER_TYPE::key_type &>(value.first) = key;
|
|
|
|
return map.insert(value);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Insert a (key, value) pair into a map.
|
|
*
|
|
* The std::map insert() method can incur the const of construction of the value_type parameter. If
|
|
* the mapped_type for the map is non-trivial, this can add unnecessary overhead if keys are always
|
|
* inserted into the map with the same, "default", mapped value.
|
|
*
|
|
* Using this helper function, it is possible to reuse an existing value_type value to save this construction overhead.
|
|
* Example usage:
|
|
*
|
|
* typedef std::map<String, NonTrivialType> MyMap;
|
|
*
|
|
* MyMap::value_type m_defaultMapValue; // member of a class, for instance
|
|
* MyMap m_map; // ditto
|
|
*
|
|
* In some function:
|
|
* String key("Hello");
|
|
* MyMap::iterator iter;
|
|
*
|
|
* if (!MapFind(m_map, key, iter))
|
|
* iter = MapInsert(m_map, iter, key, m_defaultMapValue);
|
|
*
|
|
* @param map the map to insert into.
|
|
* @param hint an iterator hinting at where to start the insert.
|
|
* @param key the key to insert.
|
|
* @param value reference to a value_type containing the value to insert into the map with key \a key.
|
|
*
|
|
* @return Iterator to the inserted item.
|
|
*/
|
|
template<typename CONTAINER_TYPE, typename KEY_TYPE>
|
|
inline typename CONTAINER_TYPE::iterator MapInsert(CONTAINER_TYPE &map, typename CONTAINER_TYPE::iterator hint,
|
|
const KEY_TYPE &key, typename CONTAINER_TYPE::value_type &value)
|
|
{
|
|
const_cast<typename CONTAINER_TYPE::key_type &>(value.first) = key;
|
|
|
|
return map.insert(hint, value);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
#if (!defined ACTIV_BOOST_NO_CXX11_HDR_TUPLE)
|
|
|
|
/**
|
|
* @brief Key extractor for a Boost::multi_index of std::tuple.
|
|
*
|
|
* @tparam INDEX the index in the tuple to extract.
|
|
* @tparam TUPLE the std::tuple type.
|
|
*/
|
|
template<size_t INDEX, typename TUPLE>
|
|
class TupleKeyExtractor
|
|
{
|
|
public:
|
|
typedef typename std::tuple_element<INDEX, TUPLE>::type result_type;
|
|
|
|
const typename Boost::call_traits<result_type>::param_type operator()(const TUPLE &tuple) const
|
|
{
|
|
return std::get<INDEX>(tuple);
|
|
}
|
|
};
|
|
|
|
#endif // (!defined ACTIV_BOOST_NO_CXX11_HDR_TUPLE)
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_MISC_STL_HELPER_H)
|