forked from OctaForge/libostd
initializer list support + insert on vector
This commit is contained in:
parent
9d5acc2337
commit
e2f43ed25e
42
octa/utility.h
Normal file
42
octa/utility.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/* Utilities for OctaSTD.
|
||||||
|
*
|
||||||
|
* This file is part of OctaSTD. See COPYING.md for futher information.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef OCTA_UTILITY_H
|
||||||
|
#define OCTA_UTILITY_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
/* must be in std namespace otherwise the compiler won't know about it */
|
||||||
|
namespace std {
|
||||||
|
template<typename T>
|
||||||
|
class initializer_list {
|
||||||
|
const T *p_buf;
|
||||||
|
size_t p_len;
|
||||||
|
|
||||||
|
initializer_list(const T *v, size_t n): p_buf(v), p_len(n) {}
|
||||||
|
public:
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
initializer_list(): p_buf(NULL), p_len(0) {}
|
||||||
|
|
||||||
|
size_t length() const { return p_len; }
|
||||||
|
|
||||||
|
const T *get() const { return p_buf; }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace octa {
|
||||||
|
template<typename T> using initializer_list = std::initializer_list<T>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
|
@ -7,9 +7,11 @@
|
||||||
#define OCTA_VECTOR_H
|
#define OCTA_VECTOR_H
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "octa/new.h"
|
#include "octa/new.h"
|
||||||
#include "octa/traits.h"
|
#include "octa/traits.h"
|
||||||
|
#include "octa/utility.h"
|
||||||
|
|
||||||
namespace octa {
|
namespace octa {
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -21,11 +23,13 @@ namespace octa {
|
||||||
enum { MIN_SIZE = 8 };
|
enum { MIN_SIZE = 8 };
|
||||||
|
|
||||||
struct type {
|
struct type {
|
||||||
typedef T value;
|
typedef T value;
|
||||||
typedef T &reference;
|
typedef T &reference;
|
||||||
typedef const T &const_reference;
|
typedef const T &const_reference;
|
||||||
typedef T *pointer;
|
typedef T *pointer;
|
||||||
typedef const T *const_pointer;
|
typedef const T *const_pointer;
|
||||||
|
typedef size_t size;
|
||||||
|
typedef ptrdiff_t difference;
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit Vector(): p_buf(NULL), p_len(0), p_cap(0) {}
|
explicit Vector(): p_buf(NULL), p_len(0), p_cap(0) {}
|
||||||
|
@ -37,7 +41,7 @@ namespace octa {
|
||||||
while (cur != last) new (cur++) T(val);
|
while (cur != last) new (cur++) T(val);
|
||||||
}
|
}
|
||||||
|
|
||||||
Vector(const Vector &v): p_buf(NULL), p_len(0), p_cap(0) {
|
Vector(const Vector &v): Vector() {
|
||||||
*this = v;
|
*this = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,6 +50,10 @@ namespace octa {
|
||||||
v.p_len = v.p_cap = 0;
|
v.p_len = v.p_cap = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector(initializer_list<T> v): Vector() {
|
||||||
|
insert(0, v.length(), v.get());
|
||||||
|
}
|
||||||
|
|
||||||
~Vector() {
|
~Vector() {
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
|
@ -54,6 +62,7 @@ namespace octa {
|
||||||
if (p_cap > 0) {
|
if (p_cap > 0) {
|
||||||
if (octa::IsClass<T>::value) {
|
if (octa::IsClass<T>::value) {
|
||||||
T *cur = p_buf, *last = p_buf + p_len;
|
T *cur = p_buf, *last = p_buf + p_len;
|
||||||
|
while (cur != last) (*cur++).~T();
|
||||||
}
|
}
|
||||||
delete[] (uchar *)p_buf;
|
delete[] (uchar *)p_buf;
|
||||||
p_buf = NULL;
|
p_buf = NULL;
|
||||||
|
@ -197,6 +206,21 @@ namespace octa {
|
||||||
p_len = p_cap = 0;
|
p_len = p_cap = 0;
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
T &insert(size_t idx, const T &v) {
|
||||||
|
push();
|
||||||
|
for (size_t i = p_len - 1; i > idx; --i) p_buf[i] = p_buf[i - 1];
|
||||||
|
p_buf[idx] = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
T *insert(size_t idx, size_t n, const T *v) {
|
||||||
|
if (p_len + n > p_cap) reserve(p_len + n);
|
||||||
|
for (size_t i = 0; i < n; ++i) push();
|
||||||
|
for (size_t i = p_len - 1; i > idx + n; --i)
|
||||||
|
p_buf[i] = p_buf[i - n];
|
||||||
|
for (size_t i = 0; i < n; ++i) p_buf[idx + i] = v[i];
|
||||||
|
return &p_buf[idx];
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue