mirror of
https://github.com/KolibriOS/kolibrios.git
synced 2024-12-22 22:56:53 +03:00
4f5f25a6c2
git-svn-id: svn://kolibrios.org@1882 a494cfbc-eb01-0410-851d-a64ba20cac60
136 lines
3.5 KiB
Plaintext
Executable File
136 lines
3.5 KiB
Plaintext
Executable File
#ifndef _VECTOR_INCLUDED
|
|
#define _VECTOR_INCLUDED
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
extern void *__cdecl operator new(size_t);
|
|
inline void *__cdecl operator new(size_t, void *_P)
|
|
{ return (_P); }
|
|
namespace std
|
|
{
|
|
template<class T> class vector
|
|
{
|
|
unsigned length;
|
|
unsigned allocated;
|
|
T* data;
|
|
public:
|
|
typedef unsigned size_type;
|
|
typedef T* iterator;
|
|
vector():length(0),allocated(0),data(NULL) {}
|
|
~vector() {for (unsigned i=length;i--;)data[i].~T();free(data);}
|
|
unsigned size() const {return length;}
|
|
void clear() {length=0;}
|
|
T& operator[](unsigned pos) {return data[pos];}
|
|
T* begin() {return data;}
|
|
T* end() {return data+length;}
|
|
void push_back(const T& x)
|
|
{
|
|
if (length==allocated)
|
|
{
|
|
allocated+=16;
|
|
data=(T*)realloc(data,allocated*sizeof(T));
|
|
}
|
|
new (data+length++) T(x);
|
|
}
|
|
bool empty() const {return length==0;}
|
|
void pop_back() {data[--length].~T();}
|
|
T& back() {return data[length-1];}
|
|
iterator erase(iterator it)
|
|
{
|
|
T* a=it;
|
|
while (++a != data+length)
|
|
{
|
|
a[-1] = *a;
|
|
}
|
|
length--;
|
|
return it;
|
|
}
|
|
/*iterator*/T* insert(iterator where, const T& what = T())
|
|
{
|
|
int z=where-data;
|
|
if (length==allocated)
|
|
{
|
|
allocated+=16;
|
|
data=(T*)realloc(data,allocated*sizeof(T));
|
|
}
|
|
T* a=data+length;
|
|
T* b=data+z;
|
|
length++;
|
|
while (a != b)
|
|
{
|
|
*a = a[-1];
|
|
--a;
|
|
}
|
|
*a = what;
|
|
return /*iterator*/a;
|
|
}
|
|
};
|
|
struct _generic_vector
|
|
{
|
|
unsigned length;
|
|
unsigned allocated;
|
|
void** data;
|
|
};
|
|
inline void _generic_pointer_push_back(_generic_vector* v, void* a)
|
|
{
|
|
if (v->length==v->allocated)
|
|
{
|
|
v->allocated+=16;
|
|
v->data=(void**)realloc(v->data,v->allocated*sizeof(void*));
|
|
}
|
|
v->data[v->length++]=a;
|
|
}
|
|
inline void _generic_pointer_erase(_generic_vector* v, void** a)
|
|
{
|
|
while (++a != v->data+v->length)
|
|
{
|
|
a[-1] = *a;
|
|
}
|
|
v->length--;
|
|
}
|
|
inline void** _generic_pointer_insert(_generic_vector* v, void** where, void* what)
|
|
{
|
|
int z=where-v->data;
|
|
if (v->length==v->allocated)
|
|
{
|
|
v->allocated+=16;
|
|
v->data=(void**)realloc(v->data,v->allocated*sizeof(void*));
|
|
}
|
|
void** a=v->data+v->length;
|
|
void** b=v->data+z;
|
|
v->length++;
|
|
while (a != b)
|
|
{
|
|
*a = a[-1];
|
|
--a;
|
|
}
|
|
*a = what;
|
|
return a;
|
|
}
|
|
template<class T> class vector<T*>
|
|
{
|
|
unsigned length;
|
|
unsigned allocated;
|
|
T** data;
|
|
public:
|
|
typedef unsigned size_type;
|
|
typedef T** iterator;
|
|
vector():length(0),allocated(0),data(NULL) {}
|
|
~vector() {free(data);}
|
|
unsigned size() const {return length;}
|
|
void clear() {length=0;}
|
|
T*& operator[](unsigned pos) {return data[pos];}
|
|
iterator begin() {return iterator(data);}
|
|
iterator end() {return iterator(data+length);}
|
|
void push_back(T* const& x)
|
|
{ _generic_pointer_push_back((_generic_vector*)this, (void*)x); }
|
|
bool empty() const {return length==0;}
|
|
void pop_back() {--length;}
|
|
T*& back() {return data[length-1];}
|
|
iterator erase(iterator it)
|
|
{ _generic_pointer_erase((_generic_vector*)this, (void**)it); return it; }
|
|
iterator insert(iterator where, T* const& what)
|
|
{ return (T**)_generic_pointer_insert((_generic_vector*)this, (void**)where, what); }
|
|
};
|
|
}
|
|
#endif
|