added sorted arrays: they automatically sort items on insertion (using user
provided compare function) and binary search in Index(). Using them in situations where Index() performance is important brings 30%-40% performance boost. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@280 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -17,7 +17,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "wx/defs.h"
|
#include "wx/defs.h"
|
||||||
#include "wx/utils.h"
|
#include "wx/debug.h"
|
||||||
|
|
||||||
/** @name Dynamic arrays and lists
|
/** @name Dynamic arrays and lists
|
||||||
@memo Arrays which grow on demand and do range checking (only in debug)
|
@memo Arrays which grow on demand and do range checking (only in debug)
|
||||||
@@ -41,7 +41,7 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
callback compare function for quick sort
|
callback compare function for quick sort
|
||||||
must return -1, 0 or +1 if pItem1 <, = or > pItem2
|
must return negative value, 0 or positive value if pItem1 <, = or > pItem2
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef __VISUALC__
|
#ifdef __VISUALC__
|
||||||
@@ -124,9 +124,13 @@ protected:
|
|||||||
@see NOT_FOUND
|
@see NOT_FOUND
|
||||||
*/
|
*/
|
||||||
int Index(long lItem, bool bFromEnd = FALSE) const;
|
int Index(long lItem, bool bFromEnd = FALSE) const;
|
||||||
|
/// search for an item using binary search in a sorted array
|
||||||
|
int Index(long lItem, CMPFUNC fnCompare);
|
||||||
/// add new element at the end
|
/// add new element at the end
|
||||||
void Add(long lItem);
|
void Add(long lItem);
|
||||||
/// add new element at given position
|
/// add item assuming the array is sorted with fnCompare function
|
||||||
|
void Add(long lItem, CMPFUNC fnCompare);
|
||||||
|
/// add new element at given position (it becomes Item[uiIndex])
|
||||||
void Insert(long lItem, uint uiIndex);
|
void Insert(long lItem, uint uiIndex);
|
||||||
/// remove first item matching this value
|
/// remove first item matching this value
|
||||||
void Remove(long lItem);
|
void Remove(long lItem);
|
||||||
@@ -135,7 +139,7 @@ protected:
|
|||||||
//@}
|
//@}
|
||||||
|
|
||||||
/// sort array elements using given compare function
|
/// sort array elements using given compare function
|
||||||
void Sort(CMPFUNC fCmp);
|
void Sort(CMPFUNC fnCompare);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void Grow(); // makes array bigger if needed
|
void Grow(); // makes array bigger if needed
|
||||||
@@ -193,6 +197,60 @@ public: \
|
|||||||
void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
|
void Sort(CMPFUNC##T fCmp) { wxBaseArray::Sort((CMPFUNC)fCmp); } \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// This is the same as the previous macro, but it defines a sorted array.
|
||||||
|
// Differences:
|
||||||
|
// 1) it must be given a COMPARE function in ctor which takes 2 items of type
|
||||||
|
// T* and should return -1, 0 or +1 if the first one is less/greater
|
||||||
|
// than/equal to the second one.
|
||||||
|
// 2) the Add() method inserts the item in such was that the array is always
|
||||||
|
// sorted (it uses the COMPARE function)
|
||||||
|
// 3) it has no Sort() method because it's always sorted
|
||||||
|
// 4) Index() method is much faster (the sorted arrays use binary search
|
||||||
|
// instead of linear one), but Add() is slower.
|
||||||
|
//
|
||||||
|
// Summary: use this class when the speed of Index() function is important, use
|
||||||
|
// the normal arrays otherwise.
|
||||||
|
//
|
||||||
|
// NB: it has only inline functions => takes no space at all
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
#define _WX_DEFINE_SORTED_ARRAY(T, name) \
|
||||||
|
typedef int (CMPFUNC_CONV *SCMPFUNC##T)(T pItem1, T pItem2); \
|
||||||
|
class name : public wxBaseArray \
|
||||||
|
{ \
|
||||||
|
public: \
|
||||||
|
name(SCMPFUNC##T fn) \
|
||||||
|
{ wxASSERT( sizeof(T) <= sizeof(long) ); m_fnCompare = fn; } \
|
||||||
|
\
|
||||||
|
name& operator=(const name& src) \
|
||||||
|
{ ((wxBaseArray *)this)->operator=((const wxBaseArray&)src); \
|
||||||
|
m_fnCompare = src.m_fnCompare; \
|
||||||
|
return *this; } \
|
||||||
|
\
|
||||||
|
T& operator[](uint uiIndex) const \
|
||||||
|
{ return (T&)(wxBaseArray::Item(uiIndex)); } \
|
||||||
|
T& Item(uint uiIndex) const \
|
||||||
|
{ return (T&)(wxBaseArray::Item(uiIndex)); } \
|
||||||
|
T& Last() const \
|
||||||
|
{ return (T&)(wxBaseArray::Item(Count() - 1)); } \
|
||||||
|
\
|
||||||
|
int Index(T Item) const \
|
||||||
|
{ return wxBaseArray::Index((long)Item, (CMPFUNC)m_fnCompare); }\
|
||||||
|
\
|
||||||
|
void Add(T Item) \
|
||||||
|
{ wxBaseArray::Add((long)Item, (CMPFUNC)m_fnCompare); } \
|
||||||
|
\
|
||||||
|
void Remove(uint uiIndex) { wxBaseArray::Remove(uiIndex); } \
|
||||||
|
void Remove(T Item) \
|
||||||
|
{ int iIndex = Index(Item); \
|
||||||
|
wxCHECK2_MSG( iIndex != NOT_FOUND, return, \
|
||||||
|
"removing inexisting element in wxArray::Remove" ); \
|
||||||
|
wxBaseArray::Remove((uint)iIndex); } \
|
||||||
|
\
|
||||||
|
private: \
|
||||||
|
SCMPFUNC##T m_fnCompare; \
|
||||||
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// see WX_DECLARE_LIST and WX_DEFINE_LIST
|
// see WX_DECLARE_LIST and WX_DEFINE_LIST
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
@@ -264,6 +322,14 @@ private: \
|
|||||||
*/
|
*/
|
||||||
#define WX_DEFINE_ARRAY(T, name) typedef T _A##name; \
|
#define WX_DEFINE_ARRAY(T, name) typedef T _A##name; \
|
||||||
_WX_DEFINE_ARRAY(_A##name, name)
|
_WX_DEFINE_ARRAY(_A##name, name)
|
||||||
|
|
||||||
|
/**
|
||||||
|
This macro does the same as WX_DEFINE_ARRAY except that the array will be
|
||||||
|
sorted with the specified compare function.
|
||||||
|
*/
|
||||||
|
#define WX_DEFINE_SORTED_ARRAY(T, name) typedef T _A##name; \
|
||||||
|
_WX_DEFINE_SORTED_ARRAY(_A##name, name)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
This macro generates a new list class which owns the objects it contains,
|
This macro generates a new list class which owns the objects it contains,
|
||||||
i.e. it will delete them when it is destroyed. An element is of type T*,
|
i.e. it will delete them when it is destroyed. An element is of type T*,
|
||||||
|
@@ -26,6 +26,7 @@
|
|||||||
#include "wx/dynarray.h"
|
#include "wx/dynarray.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h> // for memmove
|
||||||
|
|
||||||
#ifndef max
|
#ifndef max
|
||||||
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
#define max(a, b) (((a) > (b)) ? (a) : (b))
|
||||||
@@ -57,34 +58,32 @@ wxBaseArray::wxBaseArray()
|
|||||||
// copy ctor
|
// copy ctor
|
||||||
wxBaseArray::wxBaseArray(const wxBaseArray& src)
|
wxBaseArray::wxBaseArray(const wxBaseArray& src)
|
||||||
{
|
{
|
||||||
m_uiSize = src.m_uiSize;
|
m_uiSize = // not src.m_uiSize to save memory
|
||||||
m_uiCount = src.m_uiCount;
|
m_uiCount = src.m_uiCount;
|
||||||
|
|
||||||
if ( m_uiSize != 0 )
|
if ( m_uiSize != 0 ) {
|
||||||
m_pItems = new long[m_uiSize];
|
m_pItems = new long[m_uiSize];
|
||||||
else
|
|
||||||
m_pItems = NULL;
|
|
||||||
|
|
||||||
if ( m_uiCount != 0 )
|
|
||||||
memcpy(m_pItems, src.m_pItems, m_uiCount*sizeof(long));
|
memcpy(m_pItems, src.m_pItems, m_uiCount*sizeof(long));
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
m_pItems = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
// copy operator
|
// assignment operator
|
||||||
wxBaseArray& wxBaseArray::operator=(const wxBaseArray& src)
|
wxBaseArray& wxBaseArray::operator=(const wxBaseArray& src)
|
||||||
{
|
{
|
||||||
DELETEA(m_pItems);
|
DELETEA(m_pItems);
|
||||||
|
|
||||||
m_uiSize = src.m_uiSize;
|
m_uiSize = // not src.m_uiSize to save memory
|
||||||
m_uiCount = src.m_uiCount;
|
m_uiCount = src.m_uiCount;
|
||||||
|
|
||||||
if ( m_uiSize != 0 )
|
if ( m_uiSize != 0 ) {
|
||||||
m_pItems = new long[m_uiSize];
|
m_pItems = new long[m_uiSize];
|
||||||
|
memcpy(m_pItems, src.m_pItems, m_uiCount*sizeof(long));
|
||||||
|
}
|
||||||
else
|
else
|
||||||
m_pItems = NULL;
|
m_pItems = NULL;
|
||||||
|
|
||||||
if ( m_uiCount != 0 )
|
|
||||||
memcpy(m_pItems, src.m_pItems, m_uiCount*sizeof(long));
|
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,6 +168,28 @@ int wxBaseArray::Index(long lItem, bool bFromEnd) const
|
|||||||
return NOT_FOUND;
|
return NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// search for an item in a sorted array (binary search)
|
||||||
|
int wxBaseArray::Index(long lItem, CMPFUNC fnCompare)
|
||||||
|
{
|
||||||
|
uint i,
|
||||||
|
lo = 0,
|
||||||
|
hi = m_uiCount;
|
||||||
|
int res;
|
||||||
|
|
||||||
|
while ( lo < hi ) {
|
||||||
|
i = (lo + hi)/2;
|
||||||
|
|
||||||
|
res = (*fnCompare)((const void *)lItem, (const void *)m_pItems[i]);
|
||||||
|
if ( res < 0 )
|
||||||
|
hi = i;
|
||||||
|
else if ( res > 0 )
|
||||||
|
lo = i + 1;
|
||||||
|
else
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NOT_FOUND;
|
||||||
|
}
|
||||||
// add item at the end
|
// add item at the end
|
||||||
void wxBaseArray::Add(long lItem)
|
void wxBaseArray::Add(long lItem)
|
||||||
{
|
{
|
||||||
@@ -176,6 +197,33 @@ void wxBaseArray::Add(long lItem)
|
|||||||
m_pItems[m_uiCount++] = lItem;
|
m_pItems[m_uiCount++] = lItem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add item assuming the array is sorted with fnCompare function
|
||||||
|
void wxBaseArray::Add(long lItem, CMPFUNC fnCompare)
|
||||||
|
{
|
||||||
|
uint i,
|
||||||
|
lo = 0,
|
||||||
|
hi = m_uiCount;
|
||||||
|
int res;
|
||||||
|
|
||||||
|
while ( lo < hi ) {
|
||||||
|
i = (lo + hi)/2;
|
||||||
|
|
||||||
|
res = (*fnCompare)((const void *)lItem, (const void *)m_pItems[i]);
|
||||||
|
if ( res < 0 )
|
||||||
|
hi = i;
|
||||||
|
else if ( res > 0 )
|
||||||
|
lo = i + 1;
|
||||||
|
else {
|
||||||
|
lo = hi = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
wxASSERT( lo == hi ); // I hope I got binary search right :-)
|
||||||
|
|
||||||
|
Insert(lItem, lo);
|
||||||
|
}
|
||||||
|
|
||||||
// add item at the given position
|
// add item at the given position
|
||||||
void wxBaseArray::Insert(long lItem, uint uiIndex)
|
void wxBaseArray::Insert(long lItem, uint uiIndex)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user