libostd/octa/array.h

70 lines
1.8 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.
*/
#ifndef OCTA_ARRAY_H
#define OCTA_ARRAY_H
#include <stddef.h>
2015-05-21 01:34:42 +02:00
#include "octa/algorithm.h"
2015-04-21 18:57:20 +02:00
#include "octa/range.h"
2015-05-28 03:38:52 +02:00
#include "octa/string.h"
2015-04-21 18:57:20 +02:00
namespace octa {
2015-06-04 00:10:10 +02:00
2015-06-04 23:57:06 +02:00
template<typename T, size_t N>
2015-06-04 00:10:10 +02:00
struct Array {
typedef size_t Size;
typedef ptrdiff_t Difference;
2015-06-04 23:57:06 +02: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-04 00:10:10 +02:00
2015-06-04 23:57:06 +02:00
T &operator[](size_t i) { return p_buf[i]; }
const T &operator[](size_t i) const { return p_buf[i]; }
2015-06-04 00:10:10 +02:00
2015-06-04 23:57:06 +02:00
T &at(size_t i) { return p_buf[i]; }
const T &at(size_t i) const { return p_buf[i]; }
2015-06-04 00:10:10 +02:00
2015-06-04 23:57:06 +02:00
T &front() { return p_buf[0]; }
const T &front() const { return p_buf[0]; }
2015-06-04 00:10:10 +02:00
2015-06-04 23:57:06 +02: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-04 00:10:10 +02:00
2015-06-04 23:57:06 +02:00
size_t size() const { return N; }
2015-06-04 00:10:10 +02:00
2015-06-04 23:57:06 +02:00
bool empty() const { return N == 0; }
2015-06-04 00:10:10 +02:00
2015-06-04 23:57:06 +02: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-04 00:10:10 +02:00
}
2015-06-04 23:57:06 +02:00
T *data() { return p_buf; }
const T *data() const { return p_buf; }
2015-06-04 00:10:10 +02:00
Range each() {
2015-06-04 23:57:06 +02:00
return octa::PointerRange<T>(p_buf, p_buf + N);
2015-06-04 00:10:10 +02:00
}
ConstRange each() const {
2015-06-04 23:57:06 +02:00
return octa::PointerRange<const T>(p_buf, p_buf + N);
2015-06-04 00:10:10 +02:00
}
void swap(Array &v) {
octa::swap_ranges(each(), v.each());
}
2015-06-04 23:57:06 +02:00
T p_buf[(N > 0) ? N : 1];
2015-06-04 00:10:10 +02:00
};
} /* namespace octa */
2015-04-21 18:57:20 +02:00
#endif