wxArrayString::Sort() implemented
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1655 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -410,7 +410,7 @@ public:
|
|||||||
wxString& operator<<(double d);
|
wxString& operator<<(double d);
|
||||||
|
|
||||||
// string comparison
|
// string comparison
|
||||||
// case-sensitive comparison: return 0 if =, +1 if > or -1 if <
|
// case-sensitive comparison (returns a value < 0, = 0 or > 0)
|
||||||
int Cmp(const char *psz) const { return strcmp(c_str(), psz); }
|
int Cmp(const char *psz) const { return strcmp(c_str(), psz); }
|
||||||
// same as Cmp() but not case-sensitive
|
// same as Cmp() but not case-sensitive
|
||||||
int CmpNoCase(const char *psz) const { return Stricmp(c_str(), psz); }
|
int CmpNoCase(const char *psz) const { return Stricmp(c_str(), psz); }
|
||||||
@@ -740,6 +740,10 @@ public:
|
|||||||
class WXDLLEXPORT wxArrayString
|
class WXDLLEXPORT wxArrayString
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
// type of function used by wxArrayString::Sort()
|
||||||
|
typedef int (*CompareFunction)(const wxString& first,
|
||||||
|
const wxString& second);
|
||||||
|
|
||||||
// constructors and destructor
|
// constructors and destructor
|
||||||
// default ctor
|
// default ctor
|
||||||
wxArrayString();
|
wxArrayString();
|
||||||
@@ -792,13 +796,19 @@ public:
|
|||||||
// remove item by index
|
// remove item by index
|
||||||
void Remove(size_t nIndex);
|
void Remove(size_t nIndex);
|
||||||
|
|
||||||
// sort array elements
|
// sorting
|
||||||
void Sort(bool bCase = TRUE, bool bReverse = FALSE);
|
// sort array elements in alphabetical order (or reversed alphabetical
|
||||||
|
// order if reverseOrder parameter is TRUE)
|
||||||
|
void Sort(bool reverseOrder = FALSE);
|
||||||
|
// sort array elements using specified comparaison function
|
||||||
|
void Sort(CompareFunction compareFunction);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void Grow(); // makes array bigger if needed
|
void Grow(); // makes array bigger if needed
|
||||||
void Free(); // free the string stored
|
void Free(); // free the string stored
|
||||||
|
|
||||||
|
void DoSort(); // common part of all Sort() variants
|
||||||
|
|
||||||
size_t m_nSize, // current size of the array
|
size_t m_nSize, // current size of the array
|
||||||
m_nCount; // current number of elements
|
m_nCount; // current number of elements
|
||||||
|
|
||||||
|
@@ -10,7 +10,7 @@
|
|||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#ifdef __GNUG__
|
#ifdef __GNUG__
|
||||||
#pragma implementation "string.h"
|
#pragma implementation "string.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -28,7 +28,7 @@
|
|||||||
#include "wx/wxprec.h"
|
#include "wx/wxprec.h"
|
||||||
|
|
||||||
#ifdef __BORLANDC__
|
#ifdef __BORLANDC__
|
||||||
#pragma hdrstop
|
#pragma hdrstop
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef WX_PRECOMP
|
#ifndef WX_PRECOMP
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#ifdef __SALFORDC__
|
#ifdef __SALFORDC__
|
||||||
#include <clib.h>
|
#include <clib.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if wxUSE_WCSRTOMBS
|
#if wxUSE_WCSRTOMBS
|
||||||
@@ -1448,10 +1448,78 @@ void wxArrayString::Remove(const char *sz)
|
|||||||
Remove(iIndex);
|
Remove(iIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort array elements using passed comparaison function
|
// ----------------------------------------------------------------------------
|
||||||
|
// sorting
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
void wxArrayString::Sort(bool WXUNUSED(bCase), bool WXUNUSED(bReverse) )
|
// we can only sort one array at a time with the quick-sort based
|
||||||
|
// implementation
|
||||||
|
#if wxUSE_THREADS
|
||||||
|
#include <wx/thread.h>
|
||||||
|
|
||||||
|
// need a critical section to protect access to gs_compareFunction and
|
||||||
|
// gs_sortAscending variables
|
||||||
|
static wxCriticalSection gs_critsectStringSort;
|
||||||
|
|
||||||
|
// call this before the value of the global sort vars is changed/after
|
||||||
|
// you're finished with them
|
||||||
|
#define START_SORT() gs_critsectStringSort.Enter()
|
||||||
|
#define END_SORT() gs_critsectStringSort.Leave()
|
||||||
|
#else // !threads
|
||||||
|
#define START_SORT()
|
||||||
|
#define END_SORT()
|
||||||
|
#endif // wxUSE_THREADS
|
||||||
|
|
||||||
|
// function to use for string comparaison
|
||||||
|
static wxArrayString::CompareFunction gs_compareFunction = NULL;
|
||||||
|
|
||||||
|
// if we don't use the compare function, this flag tells us if we sort the
|
||||||
|
// array in ascending or descending order
|
||||||
|
static bool gs_sortAscending = TRUE;
|
||||||
|
|
||||||
|
// function which is called by quick sort
|
||||||
|
static int wxStringCompareFunction(const void *first, const void *second)
|
||||||
{
|
{
|
||||||
//@@@@ TO DO
|
wxString *strFirst = (wxString *)first;
|
||||||
//qsort(m_pItems, m_nCount, sizeof(char *), fCmp);
|
wxString *strSecond = (wxString *)second;
|
||||||
|
|
||||||
|
if ( gs_compareFunction )
|
||||||
|
return gs_compareFunction(*strFirst, *strSecond);
|
||||||
|
else {
|
||||||
|
int result = strcmp(strFirst->c_str(), strSecond->c_str());
|
||||||
|
|
||||||
|
return gs_sortAscending ? result : -result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort array elements using passed comparaison function
|
||||||
|
void wxArrayString::Sort(CompareFunction compareFunction)
|
||||||
|
{
|
||||||
|
START_SORT();
|
||||||
|
|
||||||
|
wxASSERT( !gs_compareFunction ); // must have been reset to NULL
|
||||||
|
gs_compareFunction = compareFunction;
|
||||||
|
|
||||||
|
DoSort();
|
||||||
|
|
||||||
|
END_SORT();
|
||||||
|
}
|
||||||
|
|
||||||
|
void wxArrayString::Sort(bool reverseOrder)
|
||||||
|
{
|
||||||
|
START_SORT();
|
||||||
|
|
||||||
|
wxASSERT( !gs_compareFunction ); // must have been reset to NULL
|
||||||
|
gs_sortAscending = !reverseOrder;
|
||||||
|
|
||||||
|
DoSort();
|
||||||
|
|
||||||
|
END_SORT();
|
||||||
|
}
|
||||||
|
|
||||||
|
void wxArrayString::DoSort()
|
||||||
|
{
|
||||||
|
// just sort the pointers using qsort() - of course it only works because
|
||||||
|
// wxString() *is* a pointer to its data
|
||||||
|
qsort(m_pItems, m_nCount, sizeof(char *), wxStringCompareFunction);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user