metaforce/Runtime/rstl.hpp

65 lines
1.6 KiB
C++
Raw Normal View History

2015-08-22 01:58:41 +00:00
#ifndef __RSTL_HPP__
#define __RSTL_HPP__
#include <vector>
#include <stdlib.h>
2016-04-29 10:08:46 +00:00
#include "optional.hpp"
2015-08-22 01:58:41 +00:00
namespace rstl
{
2016-04-29 10:08:46 +00:00
template <typename T>
using optional_object = std::experimental::optional<T>;
2015-08-22 01:58:41 +00:00
/**
* @brief Vector reserved on construction
*/
template <class T, size_t N>
class reserved_vector : public std::vector<T>
{
public:
2017-01-07 01:58:05 +00:00
reserved_vector() { this->reserve(N); }
reserved_vector(size_t n, const T& val) : std::vector<T>(n, val) {}
2015-08-22 01:58:41 +00:00
};
2017-01-07 01:58:05 +00:00
template <class T, size_t N>
class prereserved_vector
{
size_t x0_size = 1;
T x4_data[N];
public:
void set_size(size_t n)
{
if (n <= N)
x0_size = n;
}
void set_data(const T* data) { memmove(x4_data, data, sizeof(T) * x0_size); }
size_t size() const { return x0_size; }
2017-07-14 05:14:19 +00:00
T& back() const { return x4_data[(x0_size == 0) ? 0 : x0_size - 1]; }
2017-01-07 01:58:05 +00:00
T& front() const { return x4_data[0]; }
2017-04-02 03:03:37 +00:00
T& operator[](size_t idx) { return x4_data[idx]; }
2017-04-08 20:40:36 +00:00
const T& operator[](size_t idx) const { return x4_data[idx]; }
2017-01-07 01:58:05 +00:00
};
template<class ForwardIt, class T>
ForwardIt binary_find(ForwardIt first, ForwardIt last, const T& value)
{
first = std::lower_bound(first, last, value);
return (!(first == last) && !(value < *first)) ? first : last;
}
template<class ForwardIt, class T, class GetKey>
ForwardIt binary_find(ForwardIt first, ForwardIt last, const T& value, GetKey getkey)
{
auto comp = [&](const auto& left, const T& right) { return getkey(left) < right; };
first = std::lower_bound(first, last, value, comp);
return (!(first == last) && !(value < getkey(*first))) ? first : last;
}
2015-08-22 01:58:41 +00:00
}
#endif // __RSTL_HPP__