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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OCTA_ARRAY_H
|
|
|
|
#define OCTA_ARRAY_H
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include "octa/range.h"
|
|
|
|
|
|
|
|
namespace octa {
|
|
|
|
template<typename T, size_t N>
|
|
|
|
struct Array {
|
2015-05-03 22:44:48 +00:00
|
|
|
typedef size_t SizeType;
|
|
|
|
typedef ptrdiff_t DiffType;
|
|
|
|
typedef T ValType;
|
|
|
|
typedef T &RefType;
|
|
|
|
typedef const T &ConstRefType;
|
|
|
|
typedef T *PtrType;
|
|
|
|
typedef const T *ConstPtrType;
|
|
|
|
typedef PointerRange< T> RangeType;
|
|
|
|
typedef PointerRange<const T> ConstRangeType;
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
T &operator[](size_t i) noexcept { return p_buf[i]; }
|
|
|
|
const T &operator[](size_t i) const noexcept { return p_buf[i]; }
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
T &at(size_t i) noexcept { return p_buf[i]; }
|
|
|
|
const T &at(size_t i) const noexcept { return p_buf[i]; }
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
T &first() noexcept { return p_buf[0]; }
|
|
|
|
const T &first() const noexcept { return p_buf[0]; }
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
T &last() noexcept { return p_buf[N - 1]; }
|
|
|
|
const T &last() const noexcept { return p_buf[N - 1]; }
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
size_t length() const noexcept { return N; }
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-05-18 00:56:25 +00:00
|
|
|
bool empty() const noexcept { return N == 0; }
|
2015-05-17 21:51:42 +00:00
|
|
|
|
|
|
|
bool in_range(size_t idx) noexcept { return idx < N; }
|
|
|
|
bool in_range(int idx) noexcept { return idx >= 0 && idx < N; }
|
|
|
|
bool in_range(const T *ptr) noexcept {
|
2015-05-18 00:57:44 +00:00
|
|
|
return ptr >= &p_buf[0] && ptr < &p_buf[N];
|
2015-05-17 21:51:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 17:48:58 +00:00
|
|
|
T *get() noexcept { return p_buf; }
|
|
|
|
const T *get() const noexcept { return p_buf; }
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-05-18 00:59:04 +00:00
|
|
|
void swap(Array &v) noexcept(swap(declval<T &>(), declval<T &>())) {
|
2015-04-21 16:57:20 +00:00
|
|
|
swap(p_buf, v.p_buf);
|
|
|
|
}
|
|
|
|
|
2015-05-03 22:44:48 +00:00
|
|
|
RangeType each() noexcept {
|
2015-04-21 16:57:20 +00:00
|
|
|
return PointerRange<T>(p_buf, p_buf + N);
|
|
|
|
}
|
2015-05-03 22:44:48 +00:00
|
|
|
ConstRangeType each() const noexcept {
|
2015-04-21 16:57:20 +00:00
|
|
|
return PointerRange<const T>(p_buf, p_buf + N);
|
|
|
|
}
|
|
|
|
|
|
|
|
T p_buf[N];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, size_t N>
|
2015-05-17 21:59:32 +00:00
|
|
|
void swap(Array<T, N> &a, Array<T, N> &b) noexcept(noexcept(a.swap(b))) {
|
2015-04-21 16:57:20 +00:00
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|