libostd/octa/array.h

64 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>
#include "octa/range.h"
namespace octa {
template<typename T, size_t N>
struct Array {
2015-04-27 20:38:34 +02:00
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T &reference;
typedef const T &const_reference;
typedef T *pointer;
typedef const T *const_pointer;
typedef PointerRange< T> range;
typedef PointerRange<const T> const_range;
2015-04-21 18:57:20 +02:00
2015-04-28 19:48:58 +02: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 18:57:20 +02:00
2015-04-28 19:48:58 +02: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 18:57:20 +02:00
2015-04-28 19:48:58 +02:00
T &first() noexcept { return p_buf[0]; }
const T &first() const noexcept { return p_buf[0]; }
2015-04-21 18:57:20 +02:00
2015-04-28 19:48:58 +02:00
T &last() noexcept { return p_buf[N - 1]; }
const T &last() const noexcept { return p_buf[N - 1]; }
2015-04-21 18:57:20 +02:00
2015-04-28 19:48:58 +02:00
bool empty() const noexcept { return (N > 0); }
size_t length() const noexcept { return N; }
2015-04-21 18:57:20 +02:00
2015-04-28 19:48:58 +02:00
T *get() noexcept { return p_buf; }
const T *get() const noexcept { return p_buf; }
2015-04-21 18:57:20 +02:00
2015-04-28 19:48:58 +02:00
void swap(Array &v) noexcept {
2015-04-21 18:57:20 +02:00
swap(p_buf, v.p_buf);
}
2015-04-28 19:48:58 +02:00
range each() noexcept {
2015-04-21 18:57:20 +02:00
return PointerRange<T>(p_buf, p_buf + N);
}
2015-04-28 19:48:58 +02:00
const_range each() const noexcept {
2015-04-21 18:57:20 +02:00
return PointerRange<const T>(p_buf, p_buf + N);
}
T p_buf[N];
};
template<typename T, size_t N>
2015-04-28 19:48:58 +02:00
void swap(Array<T, N> &a, Array<T, N> &b) noexcept {
2015-04-21 18:57:20 +02:00
a.swap(b);
}
}
#endif