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>
|
|
|
|
|
2015-05-20 23:34:42 +00:00
|
|
|
#include "octa/algorithm.h"
|
2015-04-21 16:57:20 +00:00
|
|
|
#include "octa/range.h"
|
2015-05-28 01:38:52 +00:00
|
|
|
#include "octa/string.h"
|
2015-04-21 16:57:20 +00:00
|
|
|
|
|
|
|
namespace octa {
|
2015-06-01 23:57:34 +00:00
|
|
|
template<typename _T, size_t _N>
|
2015-04-21 16:57:20 +00:00
|
|
|
struct Array {
|
2015-06-01 23:57:34 +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-06-01 23:57:34 +00:00
|
|
|
_T &operator[](size_t __i) { return __buf[__i]; }
|
|
|
|
const _T &operator[](size_t __i) const { return __buf[__i]; }
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
_T &at(size_t __i) { return __buf[__i]; }
|
|
|
|
const _T &at(size_t __i) const { return __buf[__i]; }
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
_T &front() { return __buf[0]; }
|
|
|
|
const _T &front() const { return __buf[0]; }
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
_T &back() { return __buf[(_N > 0) ? (_N - 1) : 0]; }
|
|
|
|
const _T &back() const { return __buf[(_N > 0) ? (_N - 1) : 0]; }
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
size_t size() const { return _N; }
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
bool empty() const { return _N == 0; }
|
2015-05-17 21:51:42 +00:00
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
bool in_range(size_t __idx) { return __idx < _N; }
|
|
|
|
bool in_range(int __idx) { return __idx >= 0 && size_t(__idx) < _N; }
|
|
|
|
bool in_range(const _T *__ptr) {
|
|
|
|
return __ptr >= &__buf[0] && __ptr < &__buf[_N];
|
2015-05-17 21:51:42 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
_T *data() { return __buf; }
|
|
|
|
const _T *data() const { return __buf; }
|
2015-04-21 16:57:20 +00:00
|
|
|
|
2015-05-20 21:05:41 +00:00
|
|
|
RangeType each() {
|
2015-06-01 23:57:34 +00:00
|
|
|
return octa::PointerRange<_T>(__buf, __buf + _N);
|
2015-04-21 16:57:20 +00:00
|
|
|
}
|
2015-05-20 21:05:41 +00:00
|
|
|
ConstRangeType each() const {
|
2015-06-01 23:57:34 +00:00
|
|
|
return octa::PointerRange<const _T>(__buf, __buf + _N);
|
2015-04-21 16:57:20 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
void swap(Array &__v) {
|
|
|
|
octa::swap_ranges(each(), __v.each());
|
2015-05-20 23:34:42 +00:00
|
|
|
}
|
|
|
|
|
2015-06-01 23:57:34 +00:00
|
|
|
_T __buf[(_N > 0) ? _N : 1];
|
2015-04-21 16:57:20 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|