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