dlist.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/dlist.h
  3. // Purpose: wxDList<T> which is a template version of wxList
  4. // Author: Robert Roebling
  5. // Created: 18.09.2008
  6. // Copyright: (c) 2008 wxWidgets team
  7. // Licence: wxWindows licence
  8. ///////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_DLIST_H_
  10. #define _WX_DLIST_H_
  11. #include "wx/defs.h"
  12. #include "wx/utils.h"
  13. #if wxUSE_STD_CONTAINERS
  14. #include "wx/beforestd.h"
  15. #include <algorithm>
  16. #include <iterator>
  17. #include <list>
  18. #include "wx/afterstd.h"
  19. template<typename T>
  20. class wxDList: public std::list<T*>
  21. {
  22. private:
  23. bool m_destroy;
  24. typedef std::list<T*> BaseListType;
  25. typedef wxDList<T> ListType;
  26. public:
  27. typedef typename BaseListType::iterator iterator;
  28. class compatibility_iterator
  29. {
  30. private:
  31. /* Workaround for broken VC6 nested class name resolution */
  32. typedef typename BaseListType::iterator iterator;
  33. friend class wxDList<T>;
  34. iterator m_iter;
  35. ListType *m_list;
  36. public:
  37. compatibility_iterator()
  38. : m_iter(), m_list( NULL ) {}
  39. compatibility_iterator( ListType* li, iterator i )
  40. : m_iter( i ), m_list( li ) {}
  41. compatibility_iterator( const ListType* li, iterator i )
  42. : m_iter( i ), m_list( const_cast<ListType*>(li) ) {}
  43. compatibility_iterator* operator->() { return this; }
  44. const compatibility_iterator* operator->() const { return this; }
  45. bool operator==(const compatibility_iterator& i) const
  46. {
  47. wxASSERT_MSG( m_list && i.m_list,
  48. "comparing invalid iterators is illegal" );
  49. return (m_list == i.m_list) && (m_iter == i.m_iter);
  50. }
  51. bool operator!=(const compatibility_iterator& i) const
  52. { return !( operator==( i ) ); }
  53. operator bool() const
  54. { return m_list ? m_iter != m_list->end() : false; }
  55. bool operator !() const
  56. { return !( operator bool() ); }
  57. T* GetData() const { return *m_iter; }
  58. void SetData( T* e ) { *m_iter = e; }
  59. compatibility_iterator GetNext() const
  60. {
  61. iterator i = m_iter;
  62. return compatibility_iterator( m_list, ++i );
  63. }
  64. compatibility_iterator GetPrevious() const
  65. {
  66. if ( m_iter == m_list->begin() )
  67. return compatibility_iterator();
  68. iterator i = m_iter;
  69. return compatibility_iterator( m_list, --i );
  70. }
  71. int IndexOf() const
  72. {
  73. return *this ? std::distance( m_list->begin(), m_iter )
  74. : wxNOT_FOUND;
  75. }
  76. };
  77. public:
  78. wxDList() : m_destroy( false ) {}
  79. ~wxDList() { Clear(); }
  80. compatibility_iterator Find( const T* e ) const
  81. {
  82. return compatibility_iterator( this,
  83. std::find( const_cast<ListType*>(this)->begin(),
  84. const_cast<ListType*>(this)->end(), e ) );
  85. }
  86. bool IsEmpty() const
  87. { return this->empty(); }
  88. size_t GetCount() const
  89. { return this->size(); }
  90. compatibility_iterator Item( size_t idx ) const
  91. {
  92. iterator i = const_cast<ListType*>(this)->begin();
  93. std::advance( i, idx );
  94. return compatibility_iterator( this, i );
  95. }
  96. T* operator[](size_t idx) const
  97. {
  98. return Item(idx).GetData();
  99. }
  100. compatibility_iterator GetFirst() const
  101. {
  102. return compatibility_iterator( this, const_cast<ListType*>(this)->begin() );
  103. }
  104. compatibility_iterator GetLast() const
  105. {
  106. iterator i = const_cast<ListType*>(this)->end();
  107. return compatibility_iterator( this, !(this->empty()) ? --i : i );
  108. }
  109. compatibility_iterator Member( T* e ) const
  110. { return Find( e ); }
  111. compatibility_iterator Nth( int n ) const
  112. { return Item( n ); }
  113. int IndexOf( T* e ) const
  114. { return Find( e ).IndexOf(); }
  115. compatibility_iterator Append( T* e )
  116. {
  117. this->push_back( e );
  118. return GetLast();
  119. }
  120. compatibility_iterator Insert( T* e )
  121. {
  122. this->push_front( e );
  123. return compatibility_iterator( this, this->begin() );
  124. }
  125. compatibility_iterator Insert( compatibility_iterator & i, T* e )
  126. {
  127. return compatibility_iterator( this, this->insert( i.m_iter, e ) );
  128. }
  129. compatibility_iterator Insert( size_t idx, T* e )
  130. {
  131. return compatibility_iterator( this,
  132. this->insert( Item( idx ).m_iter, e ) );
  133. }
  134. void DeleteContents( bool destroy )
  135. { m_destroy = destroy; }
  136. bool GetDeleteContents() const
  137. { return m_destroy; }
  138. void Erase( const compatibility_iterator& i )
  139. {
  140. if ( m_destroy )
  141. delete i->GetData();
  142. this->erase( i.m_iter );
  143. }
  144. bool DeleteNode( const compatibility_iterator& i )
  145. {
  146. if( i )
  147. {
  148. Erase( i );
  149. return true;
  150. }
  151. return false;
  152. }
  153. bool DeleteObject( T* e )
  154. {
  155. return DeleteNode( Find( e ) );
  156. }
  157. void Clear()
  158. {
  159. if ( m_destroy )
  160. {
  161. iterator it, en;
  162. for ( it = this->begin(), en = this->end(); it != en; ++it )
  163. delete *it;
  164. }
  165. this->clear();
  166. }
  167. };
  168. #else // !wxUSE_STD_CONTAINERS
  169. template <typename T>
  170. class wxDList
  171. {
  172. public:
  173. class Node
  174. {
  175. public:
  176. Node(wxDList<T> *list = NULL,
  177. Node *previous = NULL,
  178. Node *next = NULL,
  179. T *data = NULL)
  180. {
  181. m_list = list;
  182. m_previous = previous;
  183. m_next = next;
  184. m_data = data;
  185. if (previous)
  186. previous->m_next = this;
  187. if (next)
  188. next->m_previous = this;
  189. }
  190. ~Node()
  191. {
  192. // handle the case when we're being deleted from the list by
  193. // the user (i.e. not by the list itself from DeleteNode) -
  194. // we must do it for compatibility with old code
  195. if (m_list != NULL)
  196. m_list->DetachNode(this);
  197. }
  198. void DeleteData()
  199. {
  200. delete m_data;
  201. }
  202. Node *GetNext() const { return m_next; }
  203. Node *GetPrevious() const { return m_previous; }
  204. T *GetData() const { return m_data; }
  205. T **GetDataPtr() const { return &(wx_const_cast(nodetype*,this)->m_data); }
  206. void SetData( T *data ) { m_data = data; }
  207. int IndexOf() const
  208. {
  209. wxCHECK_MSG( m_list, wxNOT_FOUND,
  210. "node doesn't belong to a list in IndexOf" );
  211. int i;
  212. Node *prev = m_previous;
  213. for( i = 0; prev; i++ )
  214. prev = prev->m_previous;
  215. return i;
  216. }
  217. private:
  218. T *m_data; // user data
  219. Node *m_next, // next and previous nodes in the list
  220. *m_previous;
  221. wxDList<T> *m_list; // list we belong to
  222. friend class wxDList<T>;
  223. };
  224. typedef Node nodetype;
  225. class compatibility_iterator
  226. {
  227. public:
  228. compatibility_iterator(nodetype *ptr = NULL) : m_ptr(ptr) { }
  229. nodetype *operator->() const { return m_ptr; }
  230. operator nodetype *() const { return m_ptr; }
  231. private:
  232. nodetype *m_ptr;
  233. };
  234. private:
  235. void Init()
  236. {
  237. m_nodeFirst =
  238. m_nodeLast = NULL;
  239. m_count = 0;
  240. m_destroy = false;
  241. }
  242. void DoDeleteNode( nodetype *node )
  243. {
  244. if ( m_destroy )
  245. node->DeleteData();
  246. // so that the node knows that it's being deleted by the list
  247. node->m_list = NULL;
  248. delete node;
  249. }
  250. size_t m_count; // number of elements in the list
  251. bool m_destroy; // destroy user data when deleting list items?
  252. nodetype *m_nodeFirst, // pointers to the head and tail of the list
  253. *m_nodeLast;
  254. public:
  255. wxDList()
  256. {
  257. Init();
  258. }
  259. wxDList( const wxDList<T>& list )
  260. {
  261. Init();
  262. Assign(list);
  263. }
  264. wxDList( size_t count, T *elements[] )
  265. {
  266. Init();
  267. size_t n;
  268. for (n = 0; n < count; n++)
  269. Append( elements[n] );
  270. }
  271. wxDList& operator=( const wxDList<T>& list )
  272. {
  273. if (&list != this)
  274. Assign(list);
  275. return *this;
  276. }
  277. ~wxDList()
  278. {
  279. nodetype *each = m_nodeFirst;
  280. while ( each != NULL )
  281. {
  282. nodetype *next = each->GetNext();
  283. DoDeleteNode(each);
  284. each = next;
  285. }
  286. }
  287. void Assign(const wxDList<T> &list)
  288. {
  289. wxASSERT_MSG( !list.m_destroy,
  290. "copying list which owns it's elements is a bad idea" );
  291. Clear();
  292. m_destroy = list.m_destroy;
  293. m_nodeFirst = NULL;
  294. m_nodeLast = NULL;
  295. nodetype* node;
  296. for (node = list.GetFirst(); node; node = node->GetNext() )
  297. Append(node->GetData());
  298. wxASSERT_MSG( m_count == list.m_count, "logic error in Assign()" );
  299. }
  300. nodetype *Append( T *object )
  301. {
  302. nodetype *node = new nodetype( this, m_nodeLast, NULL, object );
  303. if ( !m_nodeFirst )
  304. {
  305. m_nodeFirst = node;
  306. m_nodeLast = m_nodeFirst;
  307. }
  308. else
  309. {
  310. m_nodeLast->m_next = node;
  311. m_nodeLast = node;
  312. }
  313. m_count++;
  314. return node;
  315. }
  316. nodetype *Insert( T* object )
  317. {
  318. return Insert( NULL, object );
  319. }
  320. nodetype *Insert( size_t pos, T* object )
  321. {
  322. if (pos == m_count)
  323. return Append( object );
  324. else
  325. return Insert( Item(pos), object );
  326. }
  327. nodetype *Insert( nodetype *position, T* object )
  328. {
  329. wxCHECK_MSG( !position || position->m_list == this, NULL,
  330. "can't insert before a node from another list" );
  331. // previous and next node for the node being inserted
  332. nodetype *prev, *next;
  333. if ( position )
  334. {
  335. prev = position->GetPrevious();
  336. next = position;
  337. }
  338. else
  339. {
  340. // inserting in the beginning of the list
  341. prev = NULL;
  342. next = m_nodeFirst;
  343. }
  344. nodetype *node = new nodetype( this, prev, next, object );
  345. if ( !m_nodeFirst )
  346. m_nodeLast = node;
  347. if ( prev == NULL )
  348. m_nodeFirst = node;
  349. m_count++;
  350. return node;
  351. }
  352. nodetype *GetFirst() const { return m_nodeFirst; }
  353. nodetype *GetLast() const { return m_nodeLast; }
  354. size_t GetCount() const { return m_count; }
  355. bool IsEmpty() const { return m_count == 0; }
  356. void DeleteContents(bool destroy) { m_destroy = destroy; }
  357. bool GetDeleteContents() const { return m_destroy; }
  358. nodetype *Item(size_t index) const
  359. {
  360. for ( nodetype *current = GetFirst(); current; current = current->GetNext() )
  361. {
  362. if ( index-- == 0 )
  363. return current;
  364. }
  365. wxFAIL_MSG( "invalid index in Item()" );
  366. return NULL;
  367. }
  368. T *operator[](size_t index) const
  369. {
  370. nodetype *node = Item(index);
  371. return node ? node->GetData() : NULL;
  372. }
  373. nodetype *DetachNode( nodetype *node )
  374. {
  375. wxCHECK_MSG( node, NULL, "detaching NULL wxNodeBase" );
  376. wxCHECK_MSG( node->m_list == this, NULL,
  377. "detaching node which is not from this list" );
  378. // update the list
  379. nodetype **prevNext = node->GetPrevious() ? &node->GetPrevious()->m_next
  380. : &m_nodeFirst;
  381. nodetype **nextPrev = node->GetNext() ? &node->GetNext()->m_previous
  382. : &m_nodeLast;
  383. *prevNext = node->GetNext();
  384. *nextPrev = node->GetPrevious();
  385. m_count--;
  386. // mark the node as not belonging to this list any more
  387. node->m_list = NULL;
  388. return node;
  389. }
  390. void Erase( nodetype *node )
  391. {
  392. DeleteNode(node);
  393. }
  394. bool DeleteNode( nodetype *node )
  395. {
  396. if ( !DetachNode(node) )
  397. return false;
  398. DoDeleteNode(node);
  399. return true;
  400. }
  401. bool DeleteObject( T *object )
  402. {
  403. for ( nodetype *current = GetFirst(); current; current = current->GetNext() )
  404. {
  405. if ( current->GetData() == object )
  406. {
  407. DeleteNode(current);
  408. return true;
  409. }
  410. }
  411. // not found
  412. return false;
  413. }
  414. nodetype *Find(const T *object) const
  415. {
  416. for ( nodetype *current = GetFirst(); current; current = current->GetNext() )
  417. {
  418. if ( current->GetData() == object )
  419. return current;
  420. }
  421. // not found
  422. return NULL;
  423. }
  424. int IndexOf(const T *object) const
  425. {
  426. int n = 0;
  427. for ( nodetype *current = GetFirst(); current; current = current->GetNext() )
  428. {
  429. if ( current->GetData() == object )
  430. return n;
  431. n++;
  432. }
  433. return wxNOT_FOUND;
  434. }
  435. void Clear()
  436. {
  437. nodetype *current = m_nodeFirst;
  438. while ( current )
  439. {
  440. nodetype *next = current->GetNext();
  441. DoDeleteNode(current);
  442. current = next;
  443. }
  444. m_nodeFirst =
  445. m_nodeLast = NULL;
  446. m_count = 0;
  447. }
  448. void Reverse()
  449. {
  450. nodetype * node = m_nodeFirst;
  451. nodetype* tmp;
  452. while (node)
  453. {
  454. // swap prev and next pointers
  455. tmp = node->m_next;
  456. node->m_next = node->m_previous;
  457. node->m_previous = tmp;
  458. // this is the node that was next before swapping
  459. node = tmp;
  460. }
  461. // swap first and last node
  462. tmp = m_nodeFirst; m_nodeFirst = m_nodeLast; m_nodeLast = tmp;
  463. }
  464. void DeleteNodes(nodetype* first, nodetype* last)
  465. {
  466. nodetype * node = first;
  467. while (node != last)
  468. {
  469. nodetype* next = node->GetNext();
  470. DeleteNode(node);
  471. node = next;
  472. }
  473. }
  474. void ForEach(wxListIterateFunction F)
  475. {
  476. for ( nodetype *current = GetFirst(); current; current = current->GetNext() )
  477. (*F)(current->GetData());
  478. }
  479. T *FirstThat(wxListIterateFunction F)
  480. {
  481. for ( nodetype *current = GetFirst(); current; current = current->GetNext() )
  482. {
  483. if ( (*F)(current->GetData()) )
  484. return current->GetData();
  485. }
  486. return NULL;
  487. }
  488. T *LastThat(wxListIterateFunction F)
  489. {
  490. for ( nodetype *current = GetLast(); current; current = current->GetPrevious() )
  491. {
  492. if ( (*F)(current->GetData()) )
  493. return current->GetData();
  494. }
  495. return NULL;
  496. }
  497. /* STL interface */
  498. public:
  499. typedef size_t size_type;
  500. typedef int difference_type;
  501. typedef T* value_type;
  502. typedef value_type& reference;
  503. typedef const value_type& const_reference;
  504. class iterator
  505. {
  506. public:
  507. typedef nodetype Node;
  508. typedef iterator itor;
  509. typedef T* value_type;
  510. typedef value_type* ptr_type;
  511. typedef value_type& reference;
  512. Node* m_node;
  513. Node* m_init;
  514. public:
  515. typedef reference reference_type;
  516. typedef ptr_type pointer_type;
  517. iterator(Node* node, Node* init) : m_node(node), m_init(init) {}
  518. iterator() : m_node(NULL), m_init(NULL) { }
  519. reference_type operator*() const
  520. { return *m_node->GetDataPtr(); }
  521. // ptrop
  522. itor& operator++() { m_node = m_node->GetNext(); return *this; }
  523. const itor operator++(int)
  524. { itor tmp = *this; m_node = m_node->GetNext(); return tmp; }
  525. itor& operator--()
  526. {
  527. m_node = m_node ? m_node->GetPrevious() : m_init;
  528. return *this;
  529. }
  530. const itor operator--(int)
  531. {
  532. itor tmp = *this;
  533. m_node = m_node ? m_node->GetPrevious() : m_init;
  534. return tmp;
  535. }
  536. bool operator!=(const itor& it) const
  537. { return it.m_node != m_node; }
  538. bool operator==(const itor& it) const
  539. { return it.m_node == m_node; }
  540. };
  541. class const_iterator
  542. {
  543. public:
  544. typedef nodetype Node;
  545. typedef T* value_type;
  546. typedef const value_type& const_reference;
  547. typedef const_iterator itor;
  548. typedef value_type* ptr_type;
  549. Node* m_node;
  550. Node* m_init;
  551. public:
  552. typedef const_reference reference_type;
  553. typedef const ptr_type pointer_type;
  554. const_iterator(Node* node, Node* init)
  555. : m_node(node), m_init(init) { }
  556. const_iterator() : m_node(NULL), m_init(NULL) { }
  557. const_iterator(const iterator& it)
  558. : m_node(it.m_node), m_init(it.m_init) { }
  559. reference_type operator*() const
  560. { return *m_node->GetDataPtr(); }
  561. // ptrop
  562. itor& operator++() { m_node = m_node->GetNext(); return *this; }
  563. const itor operator++(int)
  564. { itor tmp = *this; m_node = m_node->GetNext(); return tmp; }
  565. itor& operator--()
  566. {
  567. m_node = m_node ? m_node->GetPrevious() : m_init;
  568. return *this;
  569. }
  570. const itor operator--(int)
  571. {
  572. itor tmp = *this;
  573. m_node = m_node ? m_node->GetPrevious() : m_init;
  574. return tmp;
  575. }
  576. bool operator!=(const itor& it) const
  577. { return it.m_node != m_node; }
  578. bool operator==(const itor& it) const
  579. { return it.m_node == m_node; }
  580. };
  581. class reverse_iterator
  582. {
  583. public:
  584. typedef nodetype Node;
  585. typedef T* value_type;
  586. typedef reverse_iterator itor;
  587. typedef value_type* ptr_type;
  588. typedef value_type& reference;
  589. Node* m_node;
  590. Node* m_init;
  591. public:
  592. typedef reference reference_type;
  593. typedef ptr_type pointer_type;
  594. reverse_iterator(Node* node, Node* init)
  595. : m_node(node), m_init(init) { }
  596. reverse_iterator() : m_node(NULL), m_init(NULL) { }
  597. reference_type operator*() const
  598. { return *m_node->GetDataPtr(); }
  599. // ptrop
  600. itor& operator++()
  601. { m_node = m_node->GetPrevious(); return *this; }
  602. const itor operator++(int)
  603. { itor tmp = *this; m_node = m_node->GetPrevious(); return tmp; }
  604. itor& operator--()
  605. { m_node = m_node ? m_node->GetNext() : m_init; return *this; }
  606. const itor operator--(int)
  607. {
  608. itor tmp = *this;
  609. m_node = m_node ? m_node->GetNext() : m_init;
  610. return tmp;
  611. }
  612. bool operator!=(const itor& it) const
  613. { return it.m_node != m_node; }
  614. bool operator==(const itor& it) const
  615. { return it.m_node == m_node; }
  616. };
  617. class const_reverse_iterator
  618. {
  619. public:
  620. typedef nodetype Node;
  621. typedef T* value_type;
  622. typedef const_reverse_iterator itor;
  623. typedef value_type* ptr_type;
  624. typedef const value_type& const_reference;
  625. Node* m_node;
  626. Node* m_init;
  627. public:
  628. typedef const_reference reference_type;
  629. typedef const ptr_type pointer_type;
  630. const_reverse_iterator(Node* node, Node* init)
  631. : m_node(node), m_init(init) { }
  632. const_reverse_iterator() : m_node(NULL), m_init(NULL) { }
  633. const_reverse_iterator(const reverse_iterator& it)
  634. : m_node(it.m_node), m_init(it.m_init) { }
  635. reference_type operator*() const
  636. { return *m_node->GetDataPtr(); }
  637. // ptrop
  638. itor& operator++()
  639. { m_node = m_node->GetPrevious(); return *this; }
  640. const itor operator++(int)
  641. { itor tmp = *this; m_node = m_node->GetPrevious(); return tmp; }
  642. itor& operator--()
  643. { m_node = m_node ? m_node->GetNext() : m_init; return *this;}
  644. const itor operator--(int)
  645. {
  646. itor tmp = *this;
  647. m_node = m_node ? m_node->GetNext() : m_init;
  648. return tmp;
  649. }
  650. bool operator!=(const itor& it) const
  651. { return it.m_node != m_node; }
  652. bool operator==(const itor& it) const
  653. { return it.m_node == m_node; }
  654. };
  655. wxEXPLICIT wxDList(size_type n, const_reference v = value_type())
  656. { assign(n, v); }
  657. wxDList(const const_iterator& first, const const_iterator& last)
  658. { assign(first, last); }
  659. iterator begin() { return iterator(GetFirst(), GetLast()); }
  660. const_iterator begin() const
  661. { return const_iterator(GetFirst(), GetLast()); }
  662. iterator end() { return iterator(NULL, GetLast()); }
  663. const_iterator end() const { return const_iterator(NULL, GetLast()); }
  664. reverse_iterator rbegin()
  665. { return reverse_iterator(GetLast(), GetFirst()); }
  666. const_reverse_iterator rbegin() const
  667. { return const_reverse_iterator(GetLast(), GetFirst()); }
  668. reverse_iterator rend() { return reverse_iterator(NULL, GetFirst()); }
  669. const_reverse_iterator rend() const
  670. { return const_reverse_iterator(NULL, GetFirst()); }
  671. void resize(size_type n, value_type v = value_type())
  672. {
  673. while (n < size())
  674. pop_back();
  675. while (n > size())
  676. push_back(v);
  677. }
  678. size_type size() const { return GetCount(); }
  679. size_type max_size() const { return INT_MAX; }
  680. bool empty() const { return IsEmpty(); }
  681. reference front() { return *begin(); }
  682. const_reference front() const { return *begin(); }
  683. reference back() { iterator tmp = end(); return *--tmp; }
  684. const_reference back() const { const_iterator tmp = end(); return *--tmp; }
  685. void push_front(const_reference v = value_type())
  686. { Insert(GetFirst(), v); }
  687. void pop_front() { DeleteNode(GetFirst()); }
  688. void push_back(const_reference v = value_type())
  689. { Append( v ); }
  690. void pop_back() { DeleteNode(GetLast()); }
  691. void assign(const_iterator first, const const_iterator& last)
  692. {
  693. clear();
  694. for(; first != last; ++first)
  695. Append(*first);
  696. }
  697. void assign(size_type n, const_reference v = value_type())
  698. {
  699. clear();
  700. for(size_type i = 0; i < n; ++i)
  701. Append(v);
  702. }
  703. iterator insert(const iterator& it, const_reference v)
  704. {
  705. if (it == end())
  706. Append( v );
  707. else
  708. Insert(it.m_node,v);
  709. iterator itprev(it);
  710. return itprev--;
  711. }
  712. void insert(const iterator& it, size_type n, const_reference v)
  713. {
  714. for(size_type i = 0; i < n; ++i)
  715. Insert(it.m_node, v);
  716. }
  717. void insert(const iterator& it, const_iterator first, const const_iterator& last)
  718. {
  719. for(; first != last; ++first)
  720. Insert(it.m_node, *first);
  721. }
  722. iterator erase(const iterator& it)
  723. {
  724. iterator next = iterator(it.m_node->GetNext(), GetLast());
  725. DeleteNode(it.m_node); return next;
  726. }
  727. iterator erase(const iterator& first, const iterator& last)
  728. {
  729. iterator next = last; ++next;
  730. DeleteNodes(first.m_node, last.m_node);
  731. return next;
  732. }
  733. void clear() { Clear(); }
  734. void splice(const iterator& it, wxDList<T>& l, const iterator& first, const iterator& last)
  735. { insert(it, first, last); l.erase(first, last); }
  736. void splice(const iterator& it, wxDList<T>& l)
  737. { splice(it, l, l.begin(), l.end() ); }
  738. void splice(const iterator& it, wxDList<T>& l, const iterator& first)
  739. {
  740. iterator tmp = first; ++tmp;
  741. if(it == first || it == tmp) return;
  742. insert(it, *first);
  743. l.erase(first);
  744. }
  745. void remove(const_reference v)
  746. { DeleteObject(v); }
  747. void reverse()
  748. { Reverse(); }
  749. /* void swap(list<T>& l)
  750. {
  751. { size_t t = m_count; m_count = l.m_count; l.m_count = t; }
  752. { bool t = m_destroy; m_destroy = l.m_destroy; l.m_destroy = t; }
  753. { wxNodeBase* t = m_nodeFirst; m_nodeFirst = l.m_nodeFirst; l.m_nodeFirst = t; }
  754. { wxNodeBase* t = m_nodeLast; m_nodeLast = l.m_nodeLast; l.m_nodeLast = t; }
  755. { wxKeyType t = m_keyType; m_keyType = l.m_keyType; l.m_keyType = t; }
  756. } */
  757. };
  758. #endif // wxUSE_STD_CONTAINERS/!wxUSE_STD_CONTAINERS
  759. #endif // _WX_DLIST_H_