libostd/octa/vector.h

435 lines
12 KiB
C
Raw Normal View History

2015-04-06 01:05:21 +02:00
/* Self-expanding dynamic array implementation for OctaSTD.
*
2015-04-12 22:41:02 +02:00
* This file is part of OctaSTD. See COPYING.md for futher information.
2015-04-06 01:05:21 +02:00
*/
2015-04-11 22:00:10 +02:00
#ifndef OCTA_VECTOR_H
#define OCTA_VECTOR_H
2015-04-06 01:05:21 +02:00
2015-04-14 23:16:06 +02:00
#include <string.h>
#include <stddef.h>
2015-04-14 23:16:06 +02:00
2015-04-25 17:13:21 +02:00
#include "octa/type_traits.h"
#include "octa/utility.h"
#include "octa/range.h"
#include "octa/algorithm.h"
#include "octa/initializer_list.h"
#include "octa/memory.h"
2015-04-06 01:05:21 +02:00
2015-04-11 22:00:10 +02:00
namespace octa {
2015-06-04 01:06:43 +02:00
namespace detail {
template<typename A, bool = octa::IsEmpty<A>::value>
2015-06-04 01:06:43 +02:00
struct VectorPair;
template<typename A>
struct VectorPair<A, false> { /* non-empty allocator */
octa::AllocatorPointer<A> p_ptr;
A p_a;
2015-06-04 23:57:06 +02:00
template<typename U>
VectorPair(octa::AllocatorPointer<A> ptr, U &&a): p_ptr(ptr),
2015-06-04 23:57:06 +02:00
p_a(octa::forward<U>(a)) {}
2015-06-04 23:57:06 +02:00
A &get_alloc() { return p_a; }
const A &get_alloc() const { return p_a; }
2015-06-04 01:06:43 +02:00
void swap(VectorPair &v) {
octa::swap(p_ptr, v.p_ptr);
octa::swap(p_a , v.p_a );
}
};
template<typename A>
struct VectorPair<A, true>: A { /* empty allocator */
octa::AllocatorPointer<A> p_ptr;
2015-06-04 23:57:06 +02:00
template<typename U>
VectorPair(octa::AllocatorPointer<A> ptr, U &&a):
2015-06-04 23:57:06 +02:00
A(octa::forward<U>(a)), p_ptr(ptr) {}
2015-06-04 23:57:06 +02:00
A &get_alloc() { return *this; }
const A &get_alloc() const { return *this; }
2015-06-04 01:06:43 +02:00
void swap(VectorPair &v) {
octa::swap(p_ptr, v.p_ptr);
}
};
2015-06-04 01:06:43 +02:00
} /* namespace detail */
2015-06-04 23:57:06 +02:00
template<typename T, typename A = octa::Allocator<T>>
2015-06-04 01:06:43 +02:00
class Vector {
using VecPair = octa::detail::VectorPair<A>;
2015-06-04 01:06:43 +02:00
2015-06-08 01:55:08 +02:00
VecPair p_buf;
octa::Size p_len, p_cap;
2015-06-04 01:06:43 +02:00
void insert_base(octa::Size idx, octa::Size n) {
2015-06-04 01:06:43 +02:00
if (p_len + n > p_cap) reserve(p_len + n);
p_len += n;
for (octa::Size i = p_len - 1; i > idx + n - 1; --i) {
2015-06-04 01:06:43 +02:00
p_buf.p_ptr[i] = octa::move(p_buf.p_ptr[i - n]);
}
}
2015-06-04 23:57:06 +02:00
template<typename R>
void ctor_from_range(R &range, octa::EnableIf<
octa::IsFiniteRandomAccessRange<R>::value, bool
2015-06-04 01:06:43 +02:00
> = true) {
2015-06-04 23:57:06 +02:00
octa::RangeSize<R> l = range.size();
2015-06-04 01:06:43 +02:00
reserve(l);
p_len = l;
if (octa::IsPod<T>() && octa::IsSame<T, octa::RangeValue<R>>()) {
memcpy(p_buf.p_ptr, &range.front(), range.size());
return;
}
for (octa::Size i = 0; !range.empty(); range.pop_front()) {
2015-06-04 01:06:43 +02:00
octa::allocator_construct(p_buf.get_alloc(),
&p_buf.p_ptr[i], range.front());
++i;
}
}
2015-06-04 23:57:06 +02:00
template<typename R>
void ctor_from_range(R &range, EnableIf<
!octa::IsFiniteRandomAccessRange<R>::value, bool
2015-06-04 01:06:43 +02:00
> = true) {
octa::Size i = 0;
2015-06-04 01:06:43 +02:00
for (; !range.empty(); range.pop_front()) {
reserve(i + 1);
octa::allocator_construct(p_buf.get_alloc(),
&p_buf.p_ptr[i], range.front());
++i;
p_len = i;
}
}
void copy_contents(const Vector &v) {
2015-06-04 23:57:06 +02:00
if (octa::IsPod<T>()) {
memcpy(p_buf.p_ptr, v.p_buf.p_ptr, p_len * sizeof(T));
2015-06-04 01:06:43 +02:00
} else {
2015-06-11 01:21:11 +02:00
Pointer cur = p_buf.p_ptr, last = p_buf.p_ptr + p_len;
Pointer vbuf = v.p_buf.p_ptr;
2015-06-04 01:06:43 +02:00
while (cur != last) {
octa::allocator_construct(p_buf.get_alloc(),
cur++, *vbuf++);
}
}
2015-06-04 01:06:43 +02:00
}
public:
using Size = octa::Size;
using Difference = octa::Ptrdiff;
2015-06-08 01:55:08 +02:00
using Value = T;
using Reference = T &;
using ConstReference = const T &;
using Pointer = octa::AllocatorPointer<A>;
using ConstPointer = octa::AllocatorConstPointer<A>;
using Range = octa::PointerRange<Value, Reference, Pointer, Size,
Difference
>;
using ConstRange = octa::PointerRange<const Value, ConstReference,
ConstPointer, Size, Difference
>;
2015-06-08 01:55:08 +02:00
using Allocator = A;
2015-06-04 23:57:06 +02:00
Vector(const A &a = A()): p_buf(nullptr, a), p_len(0), p_cap(0) {}
explicit Vector(Size n, const T &val = T(),
2015-06-04 23:57:06 +02:00
const A &al = A()): Vector(al) {
2015-06-04 01:06:43 +02:00
p_buf.p_ptr = octa::allocator_allocate(p_buf.get_alloc(), n);
p_len = p_cap = n;
2015-06-11 01:21:11 +02:00
Pointer cur = p_buf.p_ptr, last = p_buf.p_ptr + n;
2015-06-04 01:06:43 +02:00
while (cur != last)
octa::allocator_construct(p_buf.get_alloc(), cur++, val);
}
Vector(const Vector &v): p_buf(nullptr,
octa::allocator_container_copy(v.p_buf.get_alloc())), p_len(0),
p_cap(0) {
reserve(v.p_cap);
p_len = v.p_len;
copy_contents(v);
}
2015-06-04 23:57:06 +02:00
Vector(const Vector &v, const A &a): p_buf(nullptr, a),
2015-06-04 01:06:43 +02:00
p_len(0), p_cap(0) {
reserve(v.p_cap);
p_len = v.p_len;
copy_contents(v);
}
Vector(Vector &&v): p_buf(v.p_buf.p_ptr,
octa::move(v.p_buf.get_alloc())), p_len(v.p_len), p_cap(v.p_cap) {
v.p_buf.p_ptr = nullptr;
v.p_len = v.p_cap = 0;
}
2015-06-04 23:57:06 +02:00
Vector(Vector &&v, const A &a) {
2015-06-04 01:06:43 +02:00
if (a != v.a) {
p_buf.get_alloc() = a;
reserve(v.p_cap);
p_len = v.p_len;
2015-06-04 23:57:06 +02:00
if (octa::IsPod<T>()) {
memcpy(p_buf.p_ptr, v.p_buf.p_ptr, p_len * sizeof(T));
} else {
Pointer cur = p_buf.p_ptr, last = p_buf.p_ptr + p_len;
Pointer vbuf = v.p_buf.p_ptr;
2015-06-04 01:06:43 +02:00
while (cur != last) {
octa::allocator_construct(p_buf.get_alloc(), cur++,
octa::move(*vbuf++));
}
}
2015-06-04 01:06:43 +02:00
return;
}
2015-06-08 01:55:08 +02:00
new (&p_buf) VecPair(v.p_buf.p_ptr,
2015-06-04 01:06:43 +02:00
octa::move(v.p_buf.get_alloc()));
p_len = v.p_len;
p_cap = v.p_cap;
v.p_buf.p_ptr = nullptr;
v.p_len = v.p_cap = 0;
}
Vector(ConstPointer buf, Size n, const A &a = A()): Vector(a) {
reserve(n);
if (octa::IsPod<T>()) {
memcpy(p_buf.p_ptr, buf, n * sizeof(T));
} else {
for (Size i = 0; i < n; ++i)
octa::allocator_construct(p_buf.get_alloc(),
&p_buf.p_ptr[i], buf[i]);
}
p_len = n;
2015-06-04 01:06:43 +02:00
}
Vector(InitializerList<T> v, const A &a = A()):
Vector(v.begin(), v.size(), a) {}
2015-06-04 23:57:06 +02:00
template<typename R> Vector(R range, const A &a = A()):
2015-06-04 01:06:43 +02:00
Vector(a) {
ctor_from_range(range);
}
~Vector() {
clear();
octa::allocator_deallocate(p_buf.get_alloc(), p_buf.p_ptr, p_cap);
}
void clear() {
2015-06-04 23:57:06 +02:00
if (p_len > 0 && !octa::IsPod<T>()) {
Pointer cur = p_buf.p_ptr, last = p_buf.p_ptr + p_len;
2015-06-04 01:06:43 +02:00
while (cur != last)
octa::allocator_destroy(p_buf.get_alloc(), cur++);
}
p_len = 0;
}
Vector &operator=(const Vector &v) {
if (this == &v) return *this;
clear();
reserve(v.p_cap);
p_len = v.p_len;
copy_contents(v);
return *this;
}
Vector &operator=(Vector &&v) {
clear();
octa::allocator_deallocate(p_buf.get_alloc(), p_buf.p_ptr, p_cap);
p_len = v.p_len;
p_cap = v.p_cap;
2015-06-08 01:55:08 +02:00
p_buf.~VecPair();
new (&p_buf) VecPair(v.disown(), octa::move(v.p_buf.get_alloc()));
2015-06-04 01:06:43 +02:00
return *this;
}
2015-06-04 23:57:06 +02:00
Vector &operator=(InitializerList<T> il) {
2015-06-04 01:06:43 +02:00
clear();
Size ilen = il.end() - il.begin();
2015-06-04 01:06:43 +02:00
reserve(ilen);
2015-06-04 23:57:06 +02:00
if (octa::IsPod<T>()) {
2015-06-04 01:06:43 +02:00
memcpy(p_buf.p_ptr, il.begin(), ilen);
} else {
Pointer tbuf = p_buf.p_ptr, ibuf = il.begin(),
last = il.end();
2015-06-04 01:06:43 +02:00
while (ibuf != last) {
octa::allocator_construct(p_buf.get_alloc(),
tbuf++, *ibuf++);
}
}
2015-06-04 01:06:43 +02:00
p_len = ilen;
return *this;
}
2015-06-04 23:57:06 +02:00
template<typename R>
Vector &operator=(R range) {
2015-06-04 01:06:43 +02:00
clear();
ctor_from_range(range);
return *this;
2015-06-04 01:06:43 +02:00
}
void resize(Size n, const T &v = T()) {
Size l = p_len;
2015-06-04 01:06:43 +02:00
reserve(n);
p_len = n;
2015-06-04 23:57:06 +02:00
if (octa::IsPod<T>()) {
for (Size i = l; i < p_len; ++i) {
2015-06-04 23:57:06 +02:00
p_buf.p_ptr[i] = T(v);
2015-04-14 23:16:06 +02:00
}
2015-06-04 01:06:43 +02:00
} else {
Pointer first = p_buf.p_ptr + l;
Pointer last = p_buf.p_ptr + p_len;
2015-06-04 01:06:43 +02:00
while (first != last)
octa::allocator_construct(p_buf.get_alloc(), first++, v);
}
}
void reserve(Size n) {
2015-06-04 01:06:43 +02:00
if (n <= p_cap) return;
Size oc = p_cap;
2015-06-04 01:06:43 +02:00
if (!oc) {
p_cap = octa::max(n, Size(8));
2015-06-04 01:06:43 +02:00
} else {
while (p_cap < n) p_cap *= 2;
}
Pointer tmp = octa::allocator_allocate(p_buf.get_alloc(), p_cap);
2015-06-04 01:06:43 +02:00
if (oc > 0) {
2015-06-04 23:57:06 +02:00
if (octa::IsPod<T>()) {
memcpy(tmp, p_buf.p_ptr, p_len * sizeof(T));
} else {
Pointer cur = p_buf.p_ptr, tcur = tmp,
last = tmp + p_len;
2015-06-04 01:06:43 +02:00
while (tcur != last) {
octa::allocator_construct(p_buf.get_alloc(), tcur++,
octa::move(*cur));
octa::allocator_destroy(p_buf.get_alloc(), cur);
++cur;
}
}
2015-06-04 01:06:43 +02:00
octa::allocator_deallocate(p_buf.get_alloc(), p_buf.p_ptr, oc);
}
p_buf.p_ptr = tmp;
}
T &operator[](Size i) { return p_buf.p_ptr[i]; }
const T &operator[](Size i) const { return p_buf.p_ptr[i]; }
2015-06-04 01:06:43 +02:00
T &at(Size i) { return p_buf.p_ptr[i]; }
const T &at(Size i) const { return p_buf.p_ptr[i]; }
2015-06-04 01:06:43 +02:00
2015-06-04 23:57:06 +02:00
T &push(const T &v) {
2015-06-04 01:06:43 +02:00
if (p_len == p_cap) reserve(p_len + 1);
octa::allocator_construct(p_buf.get_alloc(),
&p_buf.p_ptr[p_len], v);
return p_buf.p_ptr[p_len++];
}
2015-06-04 23:57:06 +02:00
T &push() {
2015-06-04 01:06:43 +02:00
if (p_len == p_cap) reserve(p_len + 1);
octa::allocator_construct(p_buf.get_alloc(), &p_buf.p_ptr[p_len]);
return p_buf.p_ptr[p_len++];
}
2015-06-04 23:57:06 +02:00
template<typename ...U>
T &emplace_back(U &&...args) {
2015-06-04 01:06:43 +02:00
if (p_len == p_cap) reserve(p_len + 1);
octa::allocator_construct(p_buf.get_alloc(), &p_buf.p_ptr[p_len],
2015-06-04 23:57:06 +02:00
octa::forward<U>(args)...);
2015-06-04 01:06:43 +02:00
return p_buf.p_ptr[p_len++];
}
void pop() {
2015-06-04 23:57:06 +02:00
if (!octa::IsPod<T>()) {
2015-06-04 01:06:43 +02:00
octa::allocator_destroy(p_buf.get_alloc(),
&p_buf.p_ptr[--p_len]);
} else {
--p_len;
}
}
2015-06-04 23:57:06 +02:00
T &front() { return p_buf.p_ptr[0]; }
const T &front() const { return p_buf.p_ptr[0]; }
2015-06-04 01:06:43 +02:00
2015-06-04 23:57:06 +02:00
T &back() { return p_buf.p_ptr[p_len - 1]; }
const T &back() const { return p_buf.p_ptr[p_len - 1]; }
2015-06-04 01:06:43 +02:00
Pointer data() { return p_buf.p_ptr; }
ConstPointer data() const { return p_buf.p_ptr; }
2015-06-04 01:06:43 +02:00
Size size() const { return p_len; }
Size capacity() const { return p_cap; }
2015-06-04 01:06:43 +02:00
bool empty() const { return (p_len == 0); }
bool in_range(Size idx) { return idx < p_len; }
bool in_range(int idx) { return idx >= 0 && Size(idx) < p_len; }
bool in_range(ConstPointer ptr) {
2015-06-04 01:06:43 +02:00
return ptr >= p_buf.p_ptr && ptr < &p_buf.p_ptr[p_len];
}
Pointer disown() {
Pointer r = p_buf.p_ptr;
2015-06-04 01:06:43 +02:00
p_buf.p_ptr = nullptr;
p_len = p_cap = 0;
return r;
}
Range insert(Size idx, T &&v) {
2015-06-04 01:06:43 +02:00
insert_base(idx, 1);
p_buf.p_ptr[idx] = octa::move(v);
return Range(&p_buf.p_ptr[idx], &p_buf.p_ptr[p_len]);
2015-06-04 01:06:43 +02:00
}
Range insert(Size idx, const T &v) {
2015-06-04 01:06:43 +02:00
insert_base(idx, 1);
p_buf.p_ptr[idx] = v;
return Range(&p_buf.p_ptr[idx], &p_buf.p_ptr[p_len]);
2015-06-04 01:06:43 +02:00
}
Range insert(Size idx, Size n, const T &v) {
2015-06-04 01:06:43 +02:00
insert_base(idx, n);
for (Size i = 0; i < n; ++i) {
2015-06-04 01:06:43 +02:00
p_buf.p_ptr[idx + i] = v;
}
return Range(&p_buf.p_ptr[idx], &p_buf.p_ptr[p_len]);
2015-06-04 01:06:43 +02:00
}
2015-06-04 23:57:06 +02:00
template<typename U>
Range insert_range(Size idx, U range) {
Size l = range.size();
2015-06-04 01:06:43 +02:00
insert_base(idx, l);
for (Size i = 0; i < l; ++i) {
2015-06-04 01:06:43 +02:00
p_buf.p_ptr[idx + i] = range.front();
range.pop_front();
}
return Range(&p_buf.p_ptr[idx], &p_buf.p_ptr[p_len]);
2015-06-04 01:06:43 +02:00
}
Range insert(Size idx, InitializerList<T> il) {
2015-06-04 01:06:43 +02:00
return insert_range(idx, octa::each(il));
}
Range each() {
return Range(p_buf.p_ptr, p_buf.p_ptr + p_len);
}
ConstRange each() const {
return ConstRange(p_buf.p_ptr, p_buf.p_ptr + p_len);
}
2015-06-09 22:18:43 +02:00
ConstRange ceach() const {
return ConstRange(p_buf.p_ptr, p_buf.p_ptr + p_len);
}
2015-06-04 01:06:43 +02:00
void swap(Vector &v) {
octa::swap(p_len, v.p_len);
octa::swap(p_cap, v.p_cap);
p_buf.swap(v.p_buf);
}
2015-06-12 21:13:27 +02:00
A get_allocator() const {
return p_buf.get_alloc();
}
2015-06-04 01:06:43 +02:00
};
} /* namespace octa */
2015-04-06 01:05:21 +02:00
#endif