You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
5.9 KiB

17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
17 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2008 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 1992-2008 KiCad Developers, see change_log.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <fctsys.h>
  25. #include <dlist.h>
  26. #include <base_struct.h>
  27. // verifies conditions for setting a parent list for an element, i.e.
  28. // element either does not belong to any list or it already belongs to the same list
  29. #define CHECK_OWNERSHIP(a) wxASSERT( !a->GetList() || a->GetList() == this );
  30. /* Implement the class DHEAD from dlist.h */
  31. DHEAD::~DHEAD()
  32. {
  33. if( meOwner )
  34. DeleteAll();
  35. }
  36. void DHEAD::DeleteAll()
  37. {
  38. wxCHECK( meOwner, /*void*/ ); // only owning lists may delete the contents
  39. EDA_ITEM* next;
  40. EDA_ITEM* item = first;
  41. while( item )
  42. {
  43. next = item->Next();
  44. delete item; // virtual destructor, class specific
  45. item = next;
  46. }
  47. first = 0;
  48. last = 0;
  49. count = 0;
  50. }
  51. void DHEAD::append( EDA_ITEM* aNewElement )
  52. {
  53. wxCHECK( aNewElement, /*void*/ );
  54. if( first ) // list is not empty, first is not touched
  55. {
  56. wxASSERT( count > 0 );
  57. wxCHECK( last, /*void*/ ); // 'first' is non-null, so 'last' should be non-null too
  58. aNewElement->SetNext( 0 );
  59. aNewElement->SetBack( last );
  60. wxASSERT( !last->Next() ); // the last element should point to nullptr
  61. last->SetNext( aNewElement );
  62. last = aNewElement;
  63. }
  64. else // list is empty, first and last are changed
  65. {
  66. wxASSERT( count == 0 );
  67. wxASSERT( !last ); // 'first' is null, then 'last' should be too
  68. aNewElement->SetNext( 0 );
  69. aNewElement->SetBack( 0 );
  70. first = aNewElement;
  71. last = aNewElement;
  72. }
  73. CHECK_OWNERSHIP( aNewElement );
  74. aNewElement->SetList( this );
  75. ++count;
  76. }
  77. void DHEAD::append( DHEAD& aList )
  78. {
  79. if( aList.first )
  80. {
  81. // Change the item's list to me.
  82. for( EDA_ITEM* item = aList.first; item; item = item->Next() )
  83. {
  84. wxASSERT( item->GetList() == &aList );
  85. item->SetList( this );
  86. }
  87. if( first ) // this list is not empty, set last item's next to the first item in aList
  88. {
  89. wxCHECK_RET( last != NULL, wxT( "Last list element not set." ) );
  90. last->SetNext( aList.first );
  91. aList.first->SetBack( last );
  92. last = aList.last;
  93. }
  94. else // this list is empty, first and last are same as aList
  95. {
  96. first = aList.first;
  97. last = aList.last;
  98. }
  99. count += aList.count;
  100. aList.count = 0;
  101. aList.first = NULL;
  102. aList.last = NULL;
  103. }
  104. }
  105. void DHEAD::insert( EDA_ITEM* aNewElement, EDA_ITEM* aAfterMe )
  106. {
  107. wxCHECK( aNewElement, /*void*/ );
  108. if( !aAfterMe )
  109. append( aNewElement );
  110. else
  111. {
  112. wxCHECK( aAfterMe->GetList() == this, /*void*/ );
  113. // the list cannot be empty if aAfterMe is supposedly on the list
  114. wxASSERT( first && last && count > 0 );
  115. if( first == aAfterMe )
  116. {
  117. aAfterMe->SetBack( aNewElement );
  118. aNewElement->SetBack( 0 ); // first in list does not point back
  119. aNewElement->SetNext( aAfterMe );
  120. first = aNewElement;
  121. }
  122. else
  123. {
  124. EDA_ITEM* oldBack = aAfterMe->Back();
  125. aAfterMe->SetBack( aNewElement );
  126. aNewElement->SetBack( oldBack );
  127. aNewElement->SetNext( aAfterMe );
  128. oldBack->SetNext( aNewElement );
  129. }
  130. CHECK_OWNERSHIP( aNewElement );
  131. aNewElement->SetList( this );
  132. ++count;
  133. }
  134. }
  135. void DHEAD::remove( EDA_ITEM* aElement )
  136. {
  137. wxCHECK( aElement && aElement->GetList() == this, /*void*/ );
  138. if( aElement->Next() )
  139. {
  140. aElement->Next()->SetBack( aElement->Back() );
  141. }
  142. else // element being removed is last
  143. {
  144. wxASSERT( last == aElement );
  145. last = aElement->Back();
  146. }
  147. if( aElement->Back() )
  148. {
  149. aElement->Back()->SetNext( aElement->Next() );
  150. }
  151. else // element being removed is first
  152. {
  153. wxASSERT( first == aElement );
  154. first = aElement->Next();
  155. }
  156. aElement->SetBack( 0 );
  157. aElement->SetNext( 0 );
  158. aElement->SetList( 0 );
  159. --count;
  160. wxASSERT( ( first && last ) || count == 0 );
  161. }
  162. #if defined(DEBUG)
  163. void DHEAD::VerifyListIntegrity()
  164. {
  165. EDA_ITEM* item;
  166. unsigned i = 0;
  167. for( item = first; item && i<count; ++i, item = item->Next() )
  168. {
  169. if( i < count-1 )
  170. {
  171. wxASSERT( item->Next() );
  172. }
  173. wxASSERT( item->GetList() == this );
  174. }
  175. wxASSERT( item == NULL );
  176. wxASSERT( i == count );
  177. i = 0;
  178. for( item = last; item && i<count; ++i, item = item->Back() )
  179. {
  180. if( i < count-1 )
  181. {
  182. wxASSERT( item->Back() );
  183. }
  184. }
  185. wxASSERT( item == NULL );
  186. wxASSERT( i == count );
  187. // printf("list %p has %d items.\n", this, count );
  188. }
  189. #endif