1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
|
///////////////////////////////////////////////////////////////////////////////
// Name: src/common/dynarray.cpp
// Purpose: implementation of wxBaseArray class
// Author: Vadim Zeitlin
// Modified by:
// Created: 12.09.97
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
// ============================================================================
// headers
// ============================================================================
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/dynarray.h"
#include "wx/intl.h"
#endif //WX_PRECOMP
#include <stdlib.h>
#include <string.h> // for memmove
#if !wxUSE_STD_CONTAINERS
// we cast the value to long from which we cast it to void * in IndexForInsert:
// this can't work if the pointers are not big enough
wxCOMPILE_TIME_ASSERT( sizeof(wxUIntPtr) <= sizeof(void *),
wxArraySizeOfPtrLessSizeOfLong ); // < 32 symbols
// ============================================================================
// constants
// ============================================================================
// size increment = max(50% of current size, ARRAY_MAXSIZE_INCREMENT)
#define ARRAY_MAXSIZE_INCREMENT 4096
// ============================================================================
// implementation
// ============================================================================
// ----------------------------------------------------------------------------
// wxBaseArray - dynamic array of 'T's
// ----------------------------------------------------------------------------
#define _WX_DEFINE_BASEARRAY(T, name) \
/* searches the array for an item (forward or backwards) */ \
int name::Index(T lItem, bool bFromEnd) const \
{ \
if ( bFromEnd ) { \
if ( size() > 0 ) { \
size_t n = size(); \
do { \
if ( (*this)[--n] == lItem ) \
return n; \
} \
while ( n != 0 ); \
} \
} \
else { \
for( size_t n = 0; n < size(); n++ ) { \
if( (*this)[n] == lItem ) \
return n; \
} \
} \
\
return wxNOT_FOUND; \
} \
\
/* add item assuming the array is sorted with fnCompare function */ \
size_t name::Add(T lItem, CMPFUNC fnCompare) \
{ \
size_t idx = IndexForInsert(lItem, fnCompare); \
Insert(lItem, idx); \
return idx; \
} \
\
/* ctor */ \
name::name() \
{ \
m_nSize = \
m_nCount = 0; \
m_pItems = NULL; \
} \
\
/* copy ctor */ \
name::name(const name& src) \
{ \
m_nSize = /* not src.m_nSize to save memory */ \
m_nCount = src.m_nCount; \
\
if ( m_nSize != 0 ) { \
m_pItems = new T[m_nSize]; \
/* only copy if allocation succeeded */ \
if ( m_pItems ) { \
memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
} \
else { \
m_nSize = 0; \
} \
} \
else \
m_pItems = NULL; \
} \
\
/* assignment operator */ \
name& name::operator=(const name& src) \
{ \
wxDELETEA(m_pItems); \
\
m_nSize = /* not src.m_nSize to save memory */ \
m_nCount = src.m_nCount; \
\
if ( m_nSize != 0 ){ \
m_pItems = new T[m_nSize]; \
/* only copy if allocation succeeded */ \
if ( m_pItems ) { \
memcpy(m_pItems, src.m_pItems, m_nCount*sizeof(T)); \
} \
else { \
m_nSize = 0; \
} \
} \
else \
m_pItems = NULL; \
\
return *this; \
} \
\
/* allocate new buffer of the given size and move our data to it */ \
bool name::Realloc(size_t nSize) \
{ \
T *pNew = new T[nSize]; \
/* only grow if allocation succeeded */ \
if ( !pNew ) \
return false; \
\
m_nSize = nSize; \
/* copy data to new location */ \
memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
delete [] m_pItems; \
m_pItems = pNew; \
\
return true; \
} \
\
/* grow the array */ \
void name::Grow(size_t nIncrement) \
{ \
/* only do it if no more place */ \
if( (m_nCount == m_nSize) || ((m_nSize - m_nCount) < nIncrement) ) { \
if( m_nSize == 0 ) { \
/* was empty, determine initial size */ \
size_t sz = WX_ARRAY_DEFAULT_INITIAL_SIZE; \
if (sz < nIncrement) sz = nIncrement; \
/* allocate some memory */ \
m_pItems = new T[sz]; \
/* only grow if allocation succeeded */ \
if ( m_pItems ) { \
m_nSize = sz; \
} \
} \
else \
{ \
/* add at least 50% but not too much */ \
size_t ndefIncrement = m_nSize < WX_ARRAY_DEFAULT_INITIAL_SIZE \
? WX_ARRAY_DEFAULT_INITIAL_SIZE : m_nSize >> 1; \
if ( ndefIncrement > ARRAY_MAXSIZE_INCREMENT ) \
ndefIncrement = ARRAY_MAXSIZE_INCREMENT; \
if ( nIncrement < ndefIncrement ) \
nIncrement = ndefIncrement; \
Realloc(m_nSize + nIncrement); \
} \
} \
} \
\
/* make sure that the array has at least count elements */ \
void name::SetCount(size_t count, T defval) \
{ \
if ( m_nSize < count ) \
{ \
/* need to realloc memory: don't overallocate it here as if */ \
/* SetCount() is called, it probably means that the caller */ \
/* knows in advance how many elements there will be in the */ \
/* array and so it won't be necessary to realloc it later */ \
if ( !Realloc(count) ) \
{ \
/* out of memory -- what can we do? */ \
return; \
} \
} \
\
/* add new elements if we extend the array */ \
while ( m_nCount < count ) \
{ \
m_pItems[m_nCount++] = defval; \
} \
} \
\
/* dtor */ \
name::~name() \
{ \
wxDELETEA(m_pItems); \
} \
\
/* clears the list */ \
void name::Clear() \
{ \
m_nSize = \
m_nCount = 0; \
\
wxDELETEA(m_pItems); \
} \
\
/* minimizes the memory usage by freeing unused memory */ \
void name::Shrink() \
{ \
/* only do it if we have some memory to free */ \
if( m_nCount < m_nSize ) { \
/* allocates exactly as much memory as we need */ \
T *pNew = new T[m_nCount]; \
/* only shrink if allocation succeeded */ \
if ( pNew ) { \
/* copy data to new location */ \
memcpy(pNew, m_pItems, m_nCount*sizeof(T)); \
delete [] m_pItems; \
m_pItems = pNew; \
\
/* update the size of the new block */ \
m_nSize = m_nCount; \
} \
/* else: don't do anything, better keep old memory block! */ \
} \
} \
\
/* add item at the end */ \
void name::Add(T lItem, size_t nInsert) \
{ \
if (nInsert == 0) \
return; \
Grow(nInsert); \
for (size_t i = 0; i < nInsert; i++) \
m_pItems[m_nCount++] = lItem; \
} \
\
/* add item at the given position */ \
void name::Insert(T lItem, size_t nIndex, size_t nInsert) \
{ \
wxCHECK_RET( nIndex <= m_nCount, wxT("bad index in wxArray::Insert") ); \
wxCHECK_RET( m_nCount <= m_nCount + nInsert, \
wxT("array size overflow in wxArray::Insert") ); \
\
if (nInsert == 0) \
return; \
Grow(nInsert); \
\
memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
(m_nCount - nIndex)*sizeof(T)); \
for (size_t i = 0; i < nInsert; i++) \
m_pItems[nIndex + i] = lItem; \
m_nCount += nInsert; \
} \
\
/* search for a place to insert item into sorted array (binary search) */ \
size_t name::IndexForInsert(T lItem, CMPFUNC fnCompare) const \
{ \
size_t i, \
lo = 0, \
hi = m_nCount; \
int res; \
\
while ( lo < hi ) { \
i = (lo + hi)/2; \
\
res = (*fnCompare)((const void *)(wxUIntPtr)lItem, \
(const void *)(wxUIntPtr)(m_pItems[i])); \
if ( res < 0 ) \
hi = i; \
else if ( res > 0 ) \
lo = i + 1; \
else { \
lo = i; \
break; \
} \
} \
\
return lo; \
} \
\
/* search for an item in a sorted array (binary search) */ \
int name::Index(T lItem, CMPFUNC fnCompare) const \
{ \
size_t n = IndexForInsert(lItem, fnCompare); \
\
return (n >= m_nCount || \
(*fnCompare)((const void *)(wxUIntPtr)lItem, \
((const void *)(wxUIntPtr)m_pItems[n]))) \
? wxNOT_FOUND \
: (int)n; \
} \
\
/* removes item from array (by index) */ \
void name::RemoveAt(size_t nIndex, size_t nRemove) \
{ \
wxCHECK_RET( nIndex < m_nCount, wxT("bad index in wxArray::RemoveAt") ); \
wxCHECK_RET( nIndex + nRemove <= m_nCount, \
wxT("removing too many elements in wxArray::RemoveAt") ); \
\
memmove(&m_pItems[nIndex], &m_pItems[nIndex + nRemove], \
(m_nCount - nIndex - nRemove)*sizeof(T)); \
m_nCount -= nRemove; \
} \
\
/* removes item from array (by value) */ \
void name::Remove(T lItem) \
{ \
int iIndex = Index(lItem); \
\
wxCHECK_RET( iIndex != wxNOT_FOUND, \
wxT("removing inexistent item in wxArray::Remove") ); \
\
RemoveAt((size_t)iIndex); \
} \
\
/* sort array elements using passed comparaison function */ \
void name::Sort(CMPFUNC fCmp) \
{ \
qsort(m_pItems, m_nCount, sizeof(T), fCmp); \
} \
\
void name::assign(const_iterator first, const_iterator last) \
{ \
clear(); \
reserve(last - first); \
for(; first != last; ++first) \
push_back(*first); \
} \
\
void name::assign(size_type n, const_reference v) \
{ \
clear(); \
reserve(n); \
for( size_type i = 0; i < n; ++i ) \
push_back(v); \
} \
\
void name::insert(iterator it, const_iterator first, const_iterator last) \
{ \
size_t nInsert = last - first, nIndex = it - begin(); \
if (nInsert == 0) \
return; \
Grow(nInsert); \
\
/* old iterator could have been invalidated by Grow(). */ \
it = begin() + nIndex; \
\
memmove(&m_pItems[nIndex + nInsert], &m_pItems[nIndex], \
(m_nCount - nIndex)*sizeof(T)); \
for (size_t i = 0; i < nInsert; ++i, ++it, ++first) \
*it = *first; \
m_nCount += nInsert; \
}
#ifdef __INTELC__
#pragma warning(push)
#pragma warning(disable: 1684)
#pragma warning(disable: 1572)
#endif
_WX_DEFINE_BASEARRAY(const void *, wxBaseArrayPtrVoid)
_WX_DEFINE_BASEARRAY(char, wxBaseArrayChar)
_WX_DEFINE_BASEARRAY(short, wxBaseArrayShort)
_WX_DEFINE_BASEARRAY(int, wxBaseArrayInt)
_WX_DEFINE_BASEARRAY(long, wxBaseArrayLong)
_WX_DEFINE_BASEARRAY(size_t, wxBaseArraySizeT)
_WX_DEFINE_BASEARRAY(double, wxBaseArrayDouble)
#ifdef __INTELC__
#pragma warning(pop)
#endif
#else // wxUSE_STD_CONTAINERS
#include "wx/arrstr.h"
#include "wx/beforestd.h"
#include <functional>
#include "wx/afterstd.h"
// some compilers (Sun CC being the only known example) distinguish between
// extern "C" functions and the functions with C++ linkage and ptr_fun and
// wxStringCompareLess can't take wxStrcmp/wxStricmp directly as arguments in
// this case, we need the wrappers below to make this work
struct wxStringCmp
{
typedef wxString first_argument_type;
typedef wxString second_argument_type;
typedef int result_type;
int operator()(const wxString& s1, const wxString& s2) const
{
return s1.compare(s2);
}
};
struct wxStringCmpNoCase
{
typedef wxString first_argument_type;
typedef wxString second_argument_type;
typedef int result_type;
int operator()(const wxString& s1, const wxString& s2) const
{
return s1.CmpNoCase(s2);
}
};
int wxArrayString::Index(const wxString& str, bool bCase, bool WXUNUSED(bFromEnd)) const
{
wxArrayString::const_iterator it;
if (bCase)
{
it = std::find_if(begin(), end(),
std::not1(
std::bind2nd(
wxStringCmp(), str)));
}
else // !bCase
{
it = std::find_if(begin(), end(),
std::not1(
std::bind2nd(
wxStringCmpNoCase(), str)));
}
return it == end() ? wxNOT_FOUND : it - begin();
}
template<class F>
class wxStringCompareLess
{
public:
wxStringCompareLess(F f) : m_f(f) { }
bool operator()(const wxString& s1, const wxString& s2)
{ return m_f(s1, s2) < 0; }
private:
F m_f;
};
template<class F>
wxStringCompareLess<F> wxStringCompare(F f)
{
return wxStringCompareLess<F>(f);
}
void wxArrayString::Sort(CompareFunction function)
{
std::sort(begin(), end(), wxStringCompare(function));
}
void wxArrayString::Sort(bool reverseOrder)
{
if (reverseOrder)
{
std::sort(begin(), end(), std::greater<wxString>());
}
else
{
std::sort(begin(), end());
}
}
int wxSortedArrayString::Index(const wxString& str,
bool WXUNUSED_UNLESS_DEBUG(bCase),
bool WXUNUSED_UNLESS_DEBUG(bFromEnd)) const
{
wxASSERT_MSG( bCase && !bFromEnd,
"search parameters ignored for sorted array" );
wxSortedArrayString::const_iterator
it = std::lower_bound(begin(), end(), str, wxStringCompare(wxStringCmp()));
if ( it == end() || str.Cmp(*it) != 0 )
return wxNOT_FOUND;
return it - begin();
}
#endif // !wxUSE_STD_CONTAINERS/wxUSE_STD_CONTAINERS
|