GeneralUtility/Src/GeneralUtility.h

52 lines
1.8 KiB
C
Raw Normal View History

2022-02-27 13:48:21 +01:00
#ifndef GENERALUTILITY_GENERALUTILITY_H
#define GENERALUTILITY_GENERALUTILITY_H
#include <algorithm>
#include <utility>
2022-02-27 13:48:21 +01:00
class GeneralUtility {
public:
2022-02-27 14:33:54 +01:00
//! Will return the index of `item` in `set`.
//! \tparam T_Type The type of `item`
//! \tparam T_Container The type of container
//! \param item The item to find the index for
//! \param set The container to be looking in
//! \return The index of `item` in `set`. -1 if not found.
template <typename T_Type, class T_Container>
static int Ord(const T_Type& item, const T_Container& set);
//! Will divide a number of arbitrary base in `dividend` by an integer divisor.
//! This is a specific helper function for the base conversion functions.
//! \param dividend The number to be divided in string form
//! \param divisor The integer divisor
//! \param set The set/base of `dividend`
//! \return A pair of the result. (result, rest)
static std::pair<std::string, int> StringDivision(const std::string& dividend, const unsigned int divisor, const std::string& set = "0123456789");
//! Will convert a number of arbitrary base to base 10
//! \param num A string representing the number
//! \param set The set/base of the number
//! \return A 64-bit integer representing the number
static std::uint64_t BaseX_2_10(const std::string& num, const std::string& set);
2022-02-27 13:48:21 +01:00
private:
2022-02-27 14:32:09 +01:00
// No instantiation! >:(
2022-02-27 13:48:21 +01:00
GeneralUtility();
};
template<typename T_Type, class T_Container>
2022-02-27 14:32:09 +01:00
int GeneralUtility::Ord(const T_Type& item, const T_Container& set) {
const auto result =
2022-02-27 14:32:09 +01:00
std::find_if(set.begin(), set.end(), [item](const T_Type& c) -> bool {
return c == item;
});
// No item found
if (result == set.end())
return -1;
else
return result - set.begin();
}
2022-02-27 13:48:21 +01:00
#endif //GENERALUTILITY_GENERALUTILITY_H