forked from OctaForge/libostd
iter extensions for std::array
This commit is contained in:
parent
56a3327dce
commit
0bf36b679a
|
@ -68,7 +68,7 @@ int main() {
|
||||||
writeln("several piped algorithms");
|
writeln("several piped algorithms");
|
||||||
|
|
||||||
srand(time(0));
|
srand(time(0));
|
||||||
Array<int, 100> arr;
|
std::array<int, 100> arr;
|
||||||
generate(iter(arr), []() { return rand() % 128; });
|
generate(iter(arr), []() { return rand() % 128; });
|
||||||
|
|
||||||
auto r = iter(arr)
|
auto r = iter(arr)
|
||||||
|
|
136
ostd/array.hh
136
ostd/array.hh
|
@ -1,4 +1,4 @@
|
||||||
/* Static array implementation for OctaSTD.
|
/* OctaSTD extensions for std::array.
|
||||||
*
|
*
|
||||||
* This file is part of OctaSTD. See COPYING.md for futher information.
|
* This file is part of OctaSTD. See COPYING.md for futher information.
|
||||||
*/
|
*/
|
||||||
|
@ -6,139 +6,25 @@
|
||||||
#ifndef OSTD_ARRAY_HH
|
#ifndef OSTD_ARRAY_HH
|
||||||
#define OSTD_ARRAY_HH
|
#define OSTD_ARRAY_HH
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <array>
|
||||||
|
|
||||||
#include "ostd/algorithm.hh"
|
|
||||||
#include "ostd/range.hh"
|
#include "ostd/range.hh"
|
||||||
#include "ostd/string.hh"
|
|
||||||
#include "ostd/utility.hh"
|
|
||||||
#include "ostd/internal/tuple.hh"
|
|
||||||
|
|
||||||
namespace ostd {
|
namespace ostd {
|
||||||
|
|
||||||
template<typename T, Size N>
|
template<typename T, size_t N>
|
||||||
struct Array {
|
inline PointerRange<T> iter(std::array<T, N> &v) {
|
||||||
using Size = ostd::Size;
|
return PointerRange<T>{v.data(), N};
|
||||||
using Difference = Ptrdiff;
|
|
||||||
using Value = T;
|
|
||||||
using Reference = T &;
|
|
||||||
using ConstReference = T const &;
|
|
||||||
using Pointer = T *;
|
|
||||||
using ConstPointer = T const *;
|
|
||||||
using Range = PointerRange<T>;
|
|
||||||
using ConstRange = PointerRange<T const>;
|
|
||||||
|
|
||||||
T &operator[](Size i) noexcept { return p_buf[i]; }
|
|
||||||
T const &operator[](Size i) const noexcept { return p_buf[i]; }
|
|
||||||
|
|
||||||
T *at(Size i) noexcept {
|
|
||||||
if (!in_range(i)) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return &p_buf[i];
|
|
||||||
}
|
|
||||||
T const *at(Size i) const noexcept {
|
|
||||||
if (!in_range(i)) {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
return &p_buf[i];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
T &front() noexcept { return p_buf[0]; }
|
template<typename T, size_t N>
|
||||||
T const &front() const noexcept { return p_buf[0]; }
|
inline PointerRange<T const> iter(std::array<T, N> const &v) {
|
||||||
|
return PointerRange<T const>{v.data(), N};
|
||||||
T &back() noexcept { return p_buf[(N > 0) ? (N - 1) : 0]; }
|
|
||||||
T const &back() const noexcept { return p_buf[(N > 0) ? (N - 1) : 0]; }
|
|
||||||
|
|
||||||
Size size() const noexcept { return N; }
|
|
||||||
Size max_size() const noexcept { return Size(~0) / sizeof(T); }
|
|
||||||
|
|
||||||
bool empty() const noexcept { return N == 0; }
|
|
||||||
|
|
||||||
bool in_range(Size idx) noexcept { return idx < N; }
|
|
||||||
bool in_range(int idx) noexcept { return idx >= 0 && Size(idx) < N; }
|
|
||||||
bool in_range(ConstPointer ptr) noexcept {
|
|
||||||
return ptr >= &p_buf[0] && ptr < &p_buf[N];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Pointer data() noexcept { return p_buf; }
|
template<typename T, size_t N>
|
||||||
ConstPointer data() const noexcept { return p_buf; }
|
inline PointerRange<T const> citer(std::array<T, N> const &v) {
|
||||||
|
return PointerRange<T const>{v.data(), N};
|
||||||
Range iter() noexcept {
|
|
||||||
return Range(p_buf, p_buf + N);
|
|
||||||
}
|
|
||||||
ConstRange iter() const noexcept {
|
|
||||||
return ConstRange(p_buf, p_buf + N);
|
|
||||||
}
|
|
||||||
ConstRange citer() const noexcept {
|
|
||||||
return ConstRange(p_buf, p_buf + N);
|
|
||||||
}
|
|
||||||
|
|
||||||
void swap(Array &v) noexcept(
|
|
||||||
noexcept(ostd::swap(declval<T &>(), declval<T &>()))
|
|
||||||
) {
|
|
||||||
ostd::swap_ranges(iter(), v.iter());
|
|
||||||
}
|
|
||||||
|
|
||||||
T p_buf[(N > 0) ? N : 1];
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename T, Size N>
|
|
||||||
constexpr Size TupleSize<Array<T, N>> = N;
|
|
||||||
|
|
||||||
template<Size I, typename T, Size N>
|
|
||||||
struct TupleElementBase<I, Array<T, N>> {
|
|
||||||
using Type = T;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<Size I, typename T, Size N>
|
|
||||||
inline TupleElement<I, Array<T, N>> &get(Array<T, N> &a) noexcept {
|
|
||||||
return a[I];
|
|
||||||
}
|
|
||||||
|
|
||||||
template<Size I, typename T, Size N>
|
|
||||||
inline TupleElement<I, Array<T, N>> const &get(Array<T, N> const &a) noexcept {
|
|
||||||
return a[I];
|
|
||||||
}
|
|
||||||
|
|
||||||
template<Size I, typename T, Size N>
|
|
||||||
inline TupleElement<I, Array<T, N>> &&get(Array<T, N> &&a) noexcept {
|
|
||||||
return std::move(a.p_buf[I]);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<Size I, typename T, Size N>
|
|
||||||
inline TupleElement<I, Array<T, N>> const &&get(Array<T, N> const &&a) noexcept {
|
|
||||||
return std::move(a.p_buf[I]);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, Size N>
|
|
||||||
inline bool operator==(Array<T, N> const &x, Array<T, N> const &y) {
|
|
||||||
return equal(x.iter(), y.iter());
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, Size N>
|
|
||||||
inline bool operator!=(Array<T, N> const &x, Array<T, N> const &y) {
|
|
||||||
return !(x == y);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, Size N>
|
|
||||||
inline bool operator<(Array<T, N> const &x, Array<T, N> const &y) {
|
|
||||||
return lexicographical_compare(x.iter(), y.iter());
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, Size N>
|
|
||||||
inline bool operator>(Array<T, N> const &x, Array<T, N> const &y) {
|
|
||||||
return (y < x);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, Size N>
|
|
||||||
inline bool operator<=(Array<T, N> const &x, Array<T, N> const &y) {
|
|
||||||
return !(y < x);
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename T, Size N>
|
|
||||||
inline bool operator>=(Array<T, N> const &x, Array<T, N> const &y) {
|
|
||||||
return !(x < y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} /* namespace ostd */
|
} /* namespace ostd */
|
||||||
|
|
Loading…
Reference in a new issue