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-03 22:10:10 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
template<typename T, size_t N>
|
2015-06-03 22:10:10 +00:00
|
|
|
struct Array {
|
|
|
|
typedef size_t Size;
|
|
|
|
typedef ptrdiff_t Difference;
|
2015-06-04 21:57:06 +00:00
|
|
|
typedef T Value;
|
|
|
|
typedef T &Reference;
|
|
|
|
typedef const T &ConstReference;
|
|
|
|
typedef T *Pointer;
|
|
|
|
typedef const T *ConstPointer;
|
|
|
|
typedef PointerRange< T> Range;
|
|
|
|
typedef PointerRange<const T> ConstRange;
|
2015-06-03 22:10:10 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
T &operator[](size_t i) { return p_buf[i]; }
|
|
|
|
const T &operator[](size_t i) const { return p_buf[i]; }
|
2015-06-03 22:10:10 +00:00
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
T &at(size_t i) { return p_buf[i]; }
|
|
|
|
const T &at(size_t i) const { 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-04 21:57:06 +00:00
|
|
|
size_t size() const { return N; }
|
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-04 21:57:06 +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 >= &p_buf[0] && ptr < &p_buf[N];
|
2015-06-03 22:10:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
T *data() { return p_buf; }
|
|
|
|
const T *data() const { return p_buf; }
|
2015-06-03 22:10:10 +00:00
|
|
|
|
|
|
|
Range each() {
|
2015-06-04 21:57:06 +00:00
|
|
|
return octa::PointerRange<T>(p_buf, p_buf + N);
|
2015-06-03 22:10:10 +00:00
|
|
|
}
|
|
|
|
ConstRange each() const {
|
2015-06-04 21:57:06 +00:00
|
|
|
return octa::PointerRange<const T>(p_buf, p_buf + N);
|
2015-06-03 22:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void swap(Array &v) {
|
|
|
|
octa::swap_ranges(each(), v.each());
|
|
|
|
}
|
|
|
|
|
2015-06-04 21:57:06 +00:00
|
|
|
T p_buf[(N > 0) ? N : 1];
|
2015-06-03 22:10:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace octa */
|
2015-04-21 16:57:20 +00:00
|
|
|
|
|
|
|
#endif
|