2001-07-07 19:16:38 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/vector.h
|
|
|
|
// Purpose: STL vector clone
|
|
|
|
// Author: Lindsay Mathieson
|
2007-07-07 06:09:42 -04:00
|
|
|
// Modified by: Vaclav Slavik - make it a template
|
2001-07-07 19:16:38 -04:00
|
|
|
// Created: 30.07.2001
|
2007-07-07 06:09:42 -04:00
|
|
|
// Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org>,
|
|
|
|
// 2007 Vaclav Slavik <vslavik@fastmail.fm>
|
2004-05-23 16:53:33 -04:00
|
|
|
// Licence: wxWindows licence
|
2001-07-07 19:16:38 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_VECTOR_H_
|
|
|
|
#define _WX_VECTOR_H_
|
|
|
|
|
|
|
|
#include "wx/defs.h"
|
|
|
|
|
2007-07-25 06:19:08 -04:00
|
|
|
#if wxUSE_STL
|
2007-07-07 06:09:42 -04:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#define wxVector std::vector
|
|
|
|
|
|
|
|
#else // !wxUSE_STL
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class wxVector
|
2001-07-07 19:16:38 -04:00
|
|
|
{
|
2002-12-09 13:08:12 -05:00
|
|
|
public:
|
2002-12-08 13:38:46 -05:00
|
|
|
typedef size_t size_type;
|
2007-07-07 06:09:42 -04:00
|
|
|
typedef T value_type;
|
2007-07-25 06:19:08 -04:00
|
|
|
typedef value_type* iterator;
|
|
|
|
typedef value_type& reference;
|
2007-07-07 06:09:42 -04:00
|
|
|
|
|
|
|
wxVector() : m_allocsize(16), m_size(0), m_capacity(0), m_objects(0) {}
|
|
|
|
|
|
|
|
wxVector(const wxVector& c)
|
|
|
|
{
|
|
|
|
wxCHECK2(Copy(c), return);
|
|
|
|
}
|
|
|
|
|
|
|
|
~wxVector()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
for (size_type i = 0; i < size(); i++)
|
|
|
|
delete m_objects[i];
|
|
|
|
free(m_objects);
|
|
|
|
m_objects = 0;
|
|
|
|
m_size = m_capacity = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reserve(size_type n)
|
|
|
|
{
|
|
|
|
if ( !Alloc(n) )
|
|
|
|
{
|
|
|
|
wxFAIL_MSG( _T("out of memory in wxVector::reserve()") );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type size() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type capacity() const
|
|
|
|
{
|
|
|
|
return m_capacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const
|
|
|
|
{
|
|
|
|
return size() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxVector& operator=(const wxVector& vb)
|
|
|
|
{
|
|
|
|
wxCHECK(Copy(vb), *this);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void push_back(const value_type& o)
|
|
|
|
{
|
|
|
|
wxCHECK2(Alloc(size() + 1), return);
|
|
|
|
Append(new value_type(o));
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop_back()
|
|
|
|
{
|
|
|
|
RemoveAt(size() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const value_type& at(size_type idx) const
|
|
|
|
{
|
|
|
|
wxASSERT(idx < m_size);
|
|
|
|
return *m_objects[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
value_type& at(size_type idx)
|
|
|
|
{
|
|
|
|
wxASSERT(idx < m_size);
|
|
|
|
return *m_objects[idx];
|
|
|
|
}
|
2001-07-07 19:16:38 -04:00
|
|
|
|
2007-07-07 06:09:42 -04:00
|
|
|
const value_type& operator[](size_type idx) const { return at(idx); }
|
|
|
|
value_type& operator[](size_type idx) { return at(idx); }
|
|
|
|
const value_type& front() const { return at(0); }
|
|
|
|
value_type& front() { return at(0); }
|
|
|
|
const value_type& back() const { return at(size() - 1); }
|
|
|
|
value_type& back() { return at(size() - 1); }
|
|
|
|
|
2007-07-25 06:19:08 -04:00
|
|
|
iterator begin() { return m_objects[0]; }
|
|
|
|
iterator end() { return m_objects[size()]; }
|
|
|
|
|
|
|
|
iterator erase(iterator first, iterator last)
|
2007-07-07 06:09:42 -04:00
|
|
|
{
|
2007-07-25 06:19:08 -04:00
|
|
|
size_type idx = first - begin();
|
|
|
|
RemoveAt(idx, last - first);
|
|
|
|
return begin() + idx;
|
|
|
|
}
|
|
|
|
iterator erase(iterator it)
|
|
|
|
{
|
|
|
|
size_type idx = it - begin();
|
2007-07-07 06:09:42 -04:00
|
|
|
RemoveAt(idx);
|
2007-07-25 06:19:08 -04:00
|
|
|
return begin() + idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator insert(iterator it, const value_type& v = value_type())
|
|
|
|
{
|
|
|
|
wxCHECK2(Alloc(size() + 1), return 0);
|
|
|
|
size_type idx = it - begin();
|
|
|
|
InsertAt(new value_type(o), idx);
|
|
|
|
return begin() + idx;
|
2007-07-07 06:09:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2002-12-08 13:38:46 -05:00
|
|
|
bool Alloc(size_type sz)
|
2001-07-07 19:16:38 -04:00
|
|
|
{
|
|
|
|
// work in multiples of m_allocsize;
|
|
|
|
sz = (sz / m_allocsize + 1) * m_allocsize;
|
|
|
|
if (sz <= m_capacity)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// try to realloc
|
2007-07-07 06:09:42 -04:00
|
|
|
void *mem = realloc(m_objects, sizeof(value_type*) * sz);
|
2001-07-07 19:16:38 -04:00
|
|
|
if (! mem)
|
|
|
|
return false; // failed
|
|
|
|
// success
|
2007-07-07 06:09:42 -04:00
|
|
|
m_objects = (value_type **) mem;
|
2001-07-07 19:16:38 -04:00
|
|
|
m_capacity = sz;
|
|
|
|
return true;
|
2005-04-01 19:34:04 -05:00
|
|
|
}
|
2001-07-07 19:16:38 -04:00
|
|
|
|
2007-07-07 06:09:42 -04:00
|
|
|
void Append(value_type *obj)
|
2001-07-07 19:16:38 -04:00
|
|
|
{
|
|
|
|
wxASSERT(m_size < m_capacity);
|
|
|
|
m_objects[m_size] = obj;
|
|
|
|
m_size++;
|
2005-04-01 19:34:04 -05:00
|
|
|
}
|
2001-07-07 19:16:38 -04:00
|
|
|
|
2007-07-25 06:19:08 -04:00
|
|
|
void InsertAt(size_type idx, value_type *obj)
|
|
|
|
{
|
|
|
|
wxASSERT(idx <= m_size);
|
|
|
|
wxASSERT(m_size < m_capacity);
|
|
|
|
if (idx < m_size)
|
|
|
|
memmove(
|
|
|
|
m_objects + idx + 1,
|
|
|
|
m_objects + idx,
|
|
|
|
( m_size - idx ) * sizeof(value_type*) );
|
|
|
|
|
|
|
|
m_size++;
|
|
|
|
}
|
|
|
|
|
2002-12-08 13:38:46 -05:00
|
|
|
void RemoveAt(size_type idx)
|
2001-07-07 19:16:38 -04:00
|
|
|
{
|
2005-08-05 11:23:38 -04:00
|
|
|
wxASSERT(idx < m_size);
|
2007-07-07 06:09:42 -04:00
|
|
|
delete m_objects[idx];
|
2001-07-07 19:16:38 -04:00
|
|
|
if (idx < m_size - 1)
|
|
|
|
memcpy(
|
2002-12-08 13:38:46 -05:00
|
|
|
m_objects + idx,
|
|
|
|
m_objects + idx + 1,
|
2007-07-25 06:19:08 -04:00
|
|
|
( m_size - idx - 1 ) * sizeof(value_type*) );
|
2001-07-07 19:16:38 -04:00
|
|
|
m_size--;
|
2005-04-01 19:34:04 -05:00
|
|
|
}
|
2007-07-25 06:19:08 -04:00
|
|
|
|
|
|
|
void RemoveAt(size_type idx, size_type count)
|
|
|
|
{
|
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
wxASSERT(idx < m_size);
|
|
|
|
size_type i;
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
delete m_objects[idx+1];
|
|
|
|
if (idx < m_size - count)
|
|
|
|
memcpy(
|
|
|
|
m_objects + idx,
|
|
|
|
m_objects + idx + count,
|
|
|
|
( m_size - idx - count ) * sizeof(value_type*) );
|
|
|
|
m_size -= count;
|
|
|
|
}
|
2007-07-07 06:09:42 -04:00
|
|
|
bool Copy(const wxVector& vb)
|
2001-07-07 19:16:38 -04:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
if (! Alloc(vb.size()))
|
|
|
|
return false;
|
|
|
|
|
2002-12-08 13:38:46 -05:00
|
|
|
for (size_type i = 0; i < vb.size(); i++)
|
2001-07-07 19:16:38 -04:00
|
|
|
{
|
2007-07-07 06:09:42 -04:00
|
|
|
value_type *o = new value_type(vb.at(i));
|
2001-07-07 19:16:38 -04:00
|
|
|
if (! o)
|
|
|
|
return false;
|
|
|
|
Append(o);
|
2005-04-01 19:34:04 -05:00
|
|
|
}
|
2001-07-07 19:16:38 -04:00
|
|
|
|
|
|
|
return true;
|
2005-04-01 19:34:04 -05:00
|
|
|
}
|
2001-07-07 19:16:38 -04:00
|
|
|
|
2007-07-07 06:09:42 -04:00
|
|
|
private:
|
|
|
|
size_type m_allocsize;
|
|
|
|
size_type m_size,
|
|
|
|
m_capacity;
|
|
|
|
value_type **m_objects;
|
2001-07-07 19:16:38 -04:00
|
|
|
};
|
|
|
|
|
2007-07-07 06:09:42 -04:00
|
|
|
#endif // wxUSE_STL/!wxUSE_STL
|
2002-12-08 13:38:46 -05:00
|
|
|
|
2007-07-07 06:09:42 -04:00
|
|
|
#if WXWIN_COMPATIBILITY_2_8
|
|
|
|
#define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
|
|
|
|
#define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
|
|
|
|
#define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
|
|
|
|
#endif // WXWIN_COMPATIBILITY_2_8
|
2001-07-07 19:16:38 -04:00
|
|
|
|
2007-07-07 06:09:42 -04:00
|
|
|
#endif // _WX_VECTOR_H_
|