From e6a176c87087684fd633dbdca65d7951cba52792 Mon Sep 17 00:00:00 2001 From: q66 Date: Tue, 21 Apr 2015 17:57:20 +0100 Subject: [PATCH] add Array container --- octa/array.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 octa/array.h diff --git a/octa/array.h b/octa/array.h new file mode 100644 index 0000000..3a665b4 --- /dev/null +++ b/octa/array.h @@ -0,0 +1,66 @@ +/* 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 + +#include "octa/range.h" + +namespace octa { + template + struct Array { + struct type { + typedef T value; + typedef T &reference; + typedef const T &const_reference; + typedef T *pointer; + typedef const T *const_pointer; + typedef size_t size; + typedef ptrdiff_t difference; + typedef PointerRange< T> range; + typedef PointerRange const_range; + }; + + T &operator[](size_t i) { return p_buf[i]; } + const T &operator[](size_t i) const { return p_buf[i]; } + + T &at(size_t i) { return p_buf[i]; } + const T &at(size_t i) const { return p_buf[i]; } + + T &first() { return p_buf[0]; } + const T &first() const { return p_buf[0]; } + + T &last() { return p_buf[N - 1]; } + const T &last() const { return p_buf[N - 1]; } + + bool empty() const { return (N > 0); } + size_t length() const { return N; } + + T *get() { return p_buf; } + const T *get() const { return p_buf; } + + void swap(Array &v) { + swap(p_buf, v.p_buf); + } + + typename type::range each() { + return PointerRange(p_buf, p_buf + N); + } + typename type::const_range each() const { + return PointerRange(p_buf, p_buf + N); + } + + T p_buf[N]; + }; + + template + void swap(Array &a, Array &b) { + a.swap(b); + } +} + +#endif \ No newline at end of file