forked from OctaForge/libostd
use the pointer/reference typedefs + redefine them with allocator traits
parent
59c896e34f
commit
b079d3b51d
|
@ -44,12 +44,12 @@ struct Array {
|
||||||
|
|
||||||
bool in_range(Size idx) { return idx < N; }
|
bool in_range(Size idx) { return idx < N; }
|
||||||
bool in_range(int idx) { return idx >= 0 && Size(idx) < N; }
|
bool in_range(int idx) { return idx >= 0 && Size(idx) < N; }
|
||||||
bool in_range(const T *ptr) {
|
bool in_range(ConstPointer ptr) {
|
||||||
return ptr >= &p_buf[0] && ptr < &p_buf[N];
|
return ptr >= &p_buf[0] && ptr < &p_buf[N];
|
||||||
}
|
}
|
||||||
|
|
||||||
T *data() { return p_buf; }
|
Pointer data() { return p_buf; }
|
||||||
const T *data() const { return p_buf; }
|
ConstPointer data() const { return p_buf; }
|
||||||
|
|
||||||
Range each() {
|
Range each() {
|
||||||
return Range(p_buf, p_buf + N);
|
return Range(p_buf, p_buf + N);
|
||||||
|
|
|
@ -145,8 +145,8 @@ public:
|
||||||
using Value = T;
|
using Value = T;
|
||||||
using Reference = T &;
|
using Reference = T &;
|
||||||
using ConstReference = const T &;
|
using ConstReference = const T &;
|
||||||
using Pointer = T *;
|
using Pointer = octa::AllocatorPointer<A>;
|
||||||
using ConstPointer = const T *;
|
using ConstPointer = octa::AllocatorConstPointer<A>;
|
||||||
using Range = octa::StringRangeBase<T>;
|
using Range = octa::StringRangeBase<T>;
|
||||||
using ConstRange = octa::StringRangeBase<const T>;
|
using ConstRange = octa::StringRangeBase<const T>;
|
||||||
using Allocator = A;
|
using Allocator = A;
|
||||||
|
@ -168,10 +168,10 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: traits for utf-16/utf-32 string lengths, for now assume char */
|
/* TODO: traits for utf-16/utf-32 string lengths, for now assume char */
|
||||||
StringBase(const T *v, const A &a = A()):
|
StringBase(ConstPointer v, const A &a = A()):
|
||||||
p_buf(ConstRange(v, strlen(v) + 1), a) {}
|
p_buf(ConstRange(v, strlen(v) + 1), a) {}
|
||||||
|
|
||||||
StringBase(const T *v, Size n, const A &a = A()):
|
StringBase(ConstPointer v, Size n, const A &a = A()):
|
||||||
p_buf(ConstRange(v, n), a) {}
|
p_buf(ConstRange(v, n), a) {}
|
||||||
|
|
||||||
template<typename R> StringBase(R range, const A &a = A()):
|
template<typename R> StringBase(R range, const A &a = A()):
|
||||||
|
@ -189,7 +189,7 @@ public:
|
||||||
p_buf.operator=(octa::move(v));
|
p_buf.operator=(octa::move(v));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
StringBase &operator=(const T *v) {
|
StringBase &operator=(ConstPointer v) {
|
||||||
p_buf = ConstRange(v, strlen(v) + 1);
|
p_buf = ConstRange(v, strlen(v) + 1);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
@ -221,8 +221,8 @@ public:
|
||||||
T &back() { return p_buf[size() - 1]; }
|
T &back() { return p_buf[size() - 1]; }
|
||||||
const T &back() const { return p_buf[size() - 1]; }
|
const T &back() const { return p_buf[size() - 1]; }
|
||||||
|
|
||||||
T *data() { return p_buf.data(); }
|
Pointer data() { return p_buf.data(); }
|
||||||
const T *data() const { return p_buf.data(); }
|
ConstPointer data() const { return p_buf.data(); }
|
||||||
|
|
||||||
octa::Size size() const {
|
octa::Size size() const {
|
||||||
return p_buf.size() - 1;
|
return p_buf.size() - 1;
|
||||||
|
@ -253,7 +253,7 @@ public:
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
StringBase &append(const T *s) {
|
StringBase &append(ConstPointer s) {
|
||||||
p_buf.pop();
|
p_buf.pop();
|
||||||
p_buf.insert_range(p_buf.size(), ConstRange(s,
|
p_buf.insert_range(p_buf.size(), ConstRange(s,
|
||||||
strlen(s) + 1));
|
strlen(s) + 1));
|
||||||
|
@ -278,7 +278,7 @@ public:
|
||||||
StringBase &operator+=(const StringBase &s) {
|
StringBase &operator+=(const StringBase &s) {
|
||||||
return append(s);
|
return append(s);
|
||||||
}
|
}
|
||||||
StringBase &operator+=(const T *s) {
|
StringBase &operator+=(ConstPointer s) {
|
||||||
return append(s);
|
return append(s);
|
||||||
}
|
}
|
||||||
StringBase &operator+=(T c) {
|
StringBase &operator+=(T c) {
|
||||||
|
|
|
@ -123,8 +123,8 @@ public:
|
||||||
using Value = T;
|
using Value = T;
|
||||||
using Reference = T &;
|
using Reference = T &;
|
||||||
using ConstReference = const T &;
|
using ConstReference = const T &;
|
||||||
using Pointer = T *;
|
using Pointer = octa::AllocatorPointer<A>;
|
||||||
using ConstPointer = const T *;
|
using ConstPointer = octa::AllocatorConstPointer<A>;
|
||||||
using Range = octa::PointerRange<T>;
|
using Range = octa::PointerRange<T>;
|
||||||
using ConstRange = octa::PointerRange<const T>;
|
using ConstRange = octa::PointerRange<const T>;
|
||||||
using Allocator = A;
|
using Allocator = A;
|
||||||
|
@ -169,8 +169,8 @@ public:
|
||||||
if (octa::IsPod<T>()) {
|
if (octa::IsPod<T>()) {
|
||||||
memcpy(p_buf.p_ptr, v.p_buf.p_ptr, p_len * sizeof(T));
|
memcpy(p_buf.p_ptr, v.p_buf.p_ptr, p_len * sizeof(T));
|
||||||
} else {
|
} else {
|
||||||
T *cur = p_buf.p_ptr, *last = p_buf.p_ptr + p_len;
|
Pointer cur = p_buf.p_ptr, last = p_buf.p_ptr + p_len;
|
||||||
T *vbuf = v.p_buf.p_ptr;
|
Pointer vbuf = v.p_buf.p_ptr;
|
||||||
while (cur != last) {
|
while (cur != last) {
|
||||||
octa::allocator_construct(p_buf.get_alloc(), cur++,
|
octa::allocator_construct(p_buf.get_alloc(), cur++,
|
||||||
octa::move(*vbuf++));
|
octa::move(*vbuf++));
|
||||||
|
@ -186,7 +186,7 @@ public:
|
||||||
v.p_len = v.p_cap = 0;
|
v.p_len = v.p_cap = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector(const T *buf, Size n, const A &a = A()): Vector(a) {
|
Vector(ConstPointer buf, Size n, const A &a = A()): Vector(a) {
|
||||||
reserve(n);
|
reserve(n);
|
||||||
if (octa::IsPod<T>()) {
|
if (octa::IsPod<T>()) {
|
||||||
memcpy(p_buf.p_ptr, buf, n * sizeof(T));
|
memcpy(p_buf.p_ptr, buf, n * sizeof(T));
|
||||||
|
@ -213,7 +213,7 @@ public:
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
if (p_len > 0 && !octa::IsPod<T>()) {
|
if (p_len > 0 && !octa::IsPod<T>()) {
|
||||||
T *cur = p_buf.p_ptr, *last = p_buf.p_ptr + p_len;
|
Pointer cur = p_buf.p_ptr, last = p_buf.p_ptr + p_len;
|
||||||
while (cur != last)
|
while (cur != last)
|
||||||
octa::allocator_destroy(p_buf.get_alloc(), cur++);
|
octa::allocator_destroy(p_buf.get_alloc(), cur++);
|
||||||
}
|
}
|
||||||
|
@ -246,8 +246,8 @@ public:
|
||||||
if (octa::IsPod<T>()) {
|
if (octa::IsPod<T>()) {
|
||||||
memcpy(p_buf.p_ptr, il.begin(), ilen);
|
memcpy(p_buf.p_ptr, il.begin(), ilen);
|
||||||
} else {
|
} else {
|
||||||
T *tbuf = p_buf.p_ptr, *ibuf = il.begin(),
|
Pointer tbuf = p_buf.p_ptr, ibuf = il.begin(),
|
||||||
*last = il.end();
|
last = il.end();
|
||||||
while (ibuf != last) {
|
while (ibuf != last) {
|
||||||
octa::allocator_construct(p_buf.get_alloc(),
|
octa::allocator_construct(p_buf.get_alloc(),
|
||||||
tbuf++, *ibuf++);
|
tbuf++, *ibuf++);
|
||||||
|
@ -273,8 +273,8 @@ public:
|
||||||
p_buf.p_ptr[i] = T(v);
|
p_buf.p_ptr[i] = T(v);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
T *first = p_buf.p_ptr + l;
|
Pointer first = p_buf.p_ptr + l;
|
||||||
T *last = p_buf.p_ptr + p_len;
|
Pointer last = p_buf.p_ptr + p_len;
|
||||||
while (first != last)
|
while (first != last)
|
||||||
octa::allocator_construct(p_buf.get_alloc(), first++, v);
|
octa::allocator_construct(p_buf.get_alloc(), first++, v);
|
||||||
}
|
}
|
||||||
|
@ -288,13 +288,13 @@ public:
|
||||||
} else {
|
} else {
|
||||||
while (p_cap < n) p_cap *= 2;
|
while (p_cap < n) p_cap *= 2;
|
||||||
}
|
}
|
||||||
T *tmp = octa::allocator_allocate(p_buf.get_alloc(), p_cap);
|
Pointer tmp = octa::allocator_allocate(p_buf.get_alloc(), p_cap);
|
||||||
if (oc > 0) {
|
if (oc > 0) {
|
||||||
if (octa::IsPod<T>()) {
|
if (octa::IsPod<T>()) {
|
||||||
memcpy(tmp, p_buf.p_ptr, p_len * sizeof(T));
|
memcpy(tmp, p_buf.p_ptr, p_len * sizeof(T));
|
||||||
} else {
|
} else {
|
||||||
T *cur = p_buf.p_ptr, *tcur = tmp,
|
Pointer cur = p_buf.p_ptr, tcur = tmp,
|
||||||
*last = tmp + p_len;
|
last = tmp + p_len;
|
||||||
while (tcur != last) {
|
while (tcur != last) {
|
||||||
octa::allocator_construct(p_buf.get_alloc(), tcur++,
|
octa::allocator_construct(p_buf.get_alloc(), tcur++,
|
||||||
octa::move(*cur));
|
octa::move(*cur));
|
||||||
|
@ -349,8 +349,8 @@ public:
|
||||||
T &back() { return p_buf.p_ptr[p_len - 1]; }
|
T &back() { return p_buf.p_ptr[p_len - 1]; }
|
||||||
const T &back() const { return p_buf.p_ptr[p_len - 1]; }
|
const T &back() const { return p_buf.p_ptr[p_len - 1]; }
|
||||||
|
|
||||||
T *data() { return p_buf.p_ptr; }
|
Pointer data() { return p_buf.p_ptr; }
|
||||||
const T *data() const { return p_buf.p_ptr; }
|
ConstPointer data() const { return p_buf.p_ptr; }
|
||||||
|
|
||||||
Size size() const { return p_len; }
|
Size size() const { return p_len; }
|
||||||
Size capacity() const { return p_cap; }
|
Size capacity() const { return p_cap; }
|
||||||
|
@ -359,30 +359,30 @@ public:
|
||||||
|
|
||||||
bool in_range(Size idx) { return idx < p_len; }
|
bool in_range(Size idx) { return idx < p_len; }
|
||||||
bool in_range(int idx) { return idx >= 0 && Size(idx) < p_len; }
|
bool in_range(int idx) { return idx >= 0 && Size(idx) < p_len; }
|
||||||
bool in_range(const T *ptr) {
|
bool in_range(ConstPointer ptr) {
|
||||||
return ptr >= p_buf.p_ptr && ptr < &p_buf.p_ptr[p_len];
|
return ptr >= p_buf.p_ptr && ptr < &p_buf.p_ptr[p_len];
|
||||||
}
|
}
|
||||||
|
|
||||||
T *disown() {
|
Pointer disown() {
|
||||||
T *r = p_buf.p_ptr;
|
Pointer r = p_buf.p_ptr;
|
||||||
p_buf.p_ptr = nullptr;
|
p_buf.p_ptr = nullptr;
|
||||||
p_len = p_cap = 0;
|
p_len = p_cap = 0;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
T *insert(Size idx, T &&v) {
|
Pointer insert(Size idx, T &&v) {
|
||||||
insert_base(idx, 1);
|
insert_base(idx, 1);
|
||||||
p_buf.p_ptr[idx] = octa::move(v);
|
p_buf.p_ptr[idx] = octa::move(v);
|
||||||
return &p_buf.p_ptr[idx];
|
return &p_buf.p_ptr[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
T *insert(Size idx, const T &v) {
|
Pointer insert(Size idx, const T &v) {
|
||||||
insert_base(idx, 1);
|
insert_base(idx, 1);
|
||||||
p_buf.p_ptr[idx] = v;
|
p_buf.p_ptr[idx] = v;
|
||||||
return &p_buf.p_ptr[idx];
|
return &p_buf.p_ptr[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
T *insert(Size idx, Size n, const T &v) {
|
Pointer insert(Size idx, Size n, const T &v) {
|
||||||
insert_base(idx, n);
|
insert_base(idx, n);
|
||||||
for (Size i = 0; i < n; ++i) {
|
for (Size i = 0; i < n; ++i) {
|
||||||
p_buf.p_ptr[idx + i] = v;
|
p_buf.p_ptr[idx + i] = v;
|
||||||
|
@ -391,7 +391,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
T *insert_range(Size idx, U range) {
|
Pointer insert_range(Size idx, U range) {
|
||||||
Size l = range.size();
|
Size l = range.size();
|
||||||
insert_base(idx, l);
|
insert_base(idx, l);
|
||||||
for (Size i = 0; i < l; ++i) {
|
for (Size i = 0; i < l; ++i) {
|
||||||
|
@ -401,7 +401,7 @@ public:
|
||||||
return &p_buf.p_ptr[idx];
|
return &p_buf.p_ptr[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
T *insert(Size idx, InitializerList<T> il) {
|
Pointer insert(Size idx, InitializerList<T> il) {
|
||||||
return insert_range(idx, octa::each(il));
|
return insert_range(idx, octa::each(il));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue