libostd/ostd/array.hh

147 lines
3.7 KiB
C++
Raw Normal View History

2015-04-21 18:57:20 +02:00
/* Static array implementation for OctaSTD.
*
* This file is part of OctaSTD. See COPYING.md for futher information.
*/
2015-07-13 21:08:55 +02:00
#ifndef OSTD_ARRAY_HH
#define OSTD_ARRAY_HH
2015-04-21 18:57:20 +02:00
#include <stddef.h>
2015-07-13 21:08:55 +02:00
#include "ostd/algorithm.hh"
#include "ostd/range.hh"
#include "ostd/string.hh"
2016-09-11 21:17:49 +02:00
#include "ostd/utility.hh"
2015-07-13 21:08:55 +02:00
#include "ostd/internal/tuple.hh"
2015-04-21 18:57:20 +02:00
2015-07-13 21:07:14 +02:00
namespace ostd {
2015-06-04 00:10:10 +02:00
template<typename T, Size N>
2015-06-04 00:10:10 +02:00
struct Array {
2015-07-13 21:07:14 +02:00
using Size = ostd::Size;
using Difference = Ptrdiff;
2015-06-08 01:55:08 +02:00
using Value = T;
using Reference = T &;
2016-06-23 20:18:35 +02:00
using ConstReference = T const &;
2015-06-08 01:55:08 +02:00
using Pointer = T *;
2016-06-23 20:18:35 +02:00
using ConstPointer = T const *;
using Range = PointerRange<T>;
2016-06-23 20:18:35 +02:00
using ConstRange = PointerRange<T const>;
2015-06-04 00:10:10 +02:00
2016-09-11 21:17:49 +02:00
T &operator[](Size i) noexcept { return p_buf[i]; }
T const &operator[](Size i) const noexcept { return p_buf[i]; }
2015-06-04 00:10:10 +02:00
2016-09-11 21:17:49 +02:00
T *at(Size i) noexcept {
if (!in_range(i)) {
return nullptr;
}
return &p_buf[i];
}
2016-09-11 21:17:49 +02:00
T const *at(Size i) const noexcept {
if (!in_range(i)) {
return nullptr;
}
return &p_buf[i];
}
2015-06-04 00:10:10 +02:00
2016-09-11 21:17:49 +02:00
T &front() noexcept { return p_buf[0]; }
T const &front() const noexcept { return p_buf[0]; }
2015-06-04 00:10:10 +02:00
2016-09-11 21:17:49 +02:00
T &back() noexcept { return p_buf[(N > 0) ? (N - 1) : 0]; }
T const &back() const noexcept { return p_buf[(N > 0) ? (N - 1) : 0]; }
2015-06-04 00:10:10 +02:00
2016-09-11 21:17:49 +02:00
Size size() const noexcept { return N; }
Size max_size() const noexcept { return Size(~0) / sizeof(T); }
2015-06-04 00:10:10 +02:00
2016-09-11 21:17:49 +02:00
bool empty() const noexcept { return N == 0; }
2015-06-04 00:10:10 +02:00
2016-09-11 21:17:49 +02:00
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 {
2015-06-04 23:57:06 +02:00
return ptr >= &p_buf[0] && ptr < &p_buf[N];
2015-06-04 00:10:10 +02:00
}
2016-09-11 21:17:49 +02:00
Pointer data() noexcept { return p_buf; }
ConstPointer data() const noexcept { return p_buf; }
2015-06-04 00:10:10 +02:00
2016-09-11 21:17:49 +02:00
Range iter() noexcept {
return Range(p_buf, p_buf + N);
2015-06-04 00:10:10 +02:00
}
2016-09-11 21:17:49 +02:00
ConstRange iter() const noexcept {
return ConstRange(p_buf, p_buf + N);
2015-06-04 00:10:10 +02:00
}
2016-09-11 21:17:49 +02:00
ConstRange citer() const noexcept {
2015-06-09 22:18:43 +02:00
return ConstRange(p_buf, p_buf + N);
}
2016-09-11 21:21:07 +02:00
void swap(Array &v) noexcept(
noexcept(ostd::swap(declval<T &>(), declval<T &>()))
) {
2015-07-13 21:07:14 +02:00
ostd::swap_ranges(iter(), v.iter());
2015-06-04 00:10:10 +02:00
}
2015-06-04 23:57:06 +02:00
T p_buf[(N > 0) ? N : 1];
2015-06-04 00:10:10 +02:00
};
2015-07-11 16:06:42 +02:00
template<typename T, Size N>
2016-01-19 20:14:02 +01:00
constexpr Size TupleSize<Array<T, N>> = N;
2015-07-11 16:06:42 +02:00
template<Size I, typename T, Size N>
struct TupleElementBase<I, Array<T, N>> {
using Type = T;
};
2015-07-11 16:06:42 +02:00
template<Size I, typename T, Size N>
2016-09-11 21:17:49 +02:00
inline TupleElement<I, Array<T, N>> &get(Array<T, N> &a) noexcept {
2015-07-11 16:06:42 +02:00
return a[I];
}
template<Size I, typename T, Size N>
2016-09-11 21:17:49 +02:00
inline TupleElement<I, Array<T, N>> const &get(Array<T, N> const &a) noexcept {
2015-07-11 16:06:42 +02:00
return a[I];
}
template<Size I, typename T, Size N>
2016-09-11 21:17:49 +02:00
inline TupleElement<I, Array<T, N>> &&get(Array<T, N> &&a) noexcept {
2017-01-25 01:44:22 +01:00
return std::move(a.p_buf[I]);
}
template<Size I, typename T, Size N>
2016-09-11 21:17:49 +02:00
inline TupleElement<I, Array<T, N>> const &&get(Array<T, N> const &&a) noexcept {
2017-01-25 01:44:22 +01:00
return std::move(a.p_buf[I]);
2015-07-11 16:06:42 +02:00
}
template<typename T, Size N>
2016-06-23 20:18:35 +02:00
inline bool operator==(Array<T, N> const &x, Array<T, N> const &y) {
return equal(x.iter(), y.iter());
}
template<typename T, Size N>
2016-06-23 20:18:35 +02:00
inline bool operator!=(Array<T, N> const &x, Array<T, N> const &y) {
return !(x == y);
}
template<typename T, Size N>
2016-06-23 20:18:35 +02:00
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>
2016-06-23 20:18:35 +02:00
inline bool operator>(Array<T, N> const &x, Array<T, N> const &y) {
return (y < x);
}
template<typename T, Size N>
2016-06-23 20:18:35 +02:00
inline bool operator<=(Array<T, N> const &x, Array<T, N> const &y) {
return !(y < x);
}
template<typename T, Size N>
2016-06-23 20:18:35 +02:00
inline bool operator>=(Array<T, N> const &x, Array<T, N> const &y) {
return !(x < y);
}
2015-07-13 21:07:14 +02:00
} /* namespace ostd */
2015-04-21 18:57:20 +02:00
2016-02-07 22:17:15 +01:00
#endif