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.

228 lines
5.1 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 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. /* Implement the class DHEAD from dlist.h */
  28. DHEAD::~DHEAD()
  29. {
  30. if( meOwner )
  31. DeleteAll();
  32. }
  33. void DHEAD::DeleteAll()
  34. {
  35. EDA_ITEM* next;
  36. EDA_ITEM* item = first;
  37. while( item )
  38. {
  39. next = item->Next();
  40. delete item; // virtual destructor, class specific
  41. item = next;
  42. }
  43. first = 0;
  44. last = 0;
  45. count = 0;
  46. }
  47. void DHEAD::append( EDA_ITEM* aNewElement )
  48. {
  49. wxASSERT( aNewElement != NULL );
  50. if( first ) // list is not empty, first is not touched
  51. {
  52. wxASSERT( last != NULL );
  53. aNewElement->SetNext( 0 );
  54. aNewElement->SetBack( last );
  55. last->SetNext( aNewElement );
  56. last = aNewElement;
  57. }
  58. else // list is empty, first and last are changed
  59. {
  60. aNewElement->SetNext( 0 );
  61. aNewElement->SetBack( 0 );
  62. first = aNewElement;
  63. last = aNewElement;
  64. }
  65. aNewElement->SetList( this );
  66. ++count;
  67. }
  68. void DHEAD::append( DHEAD& aList )
  69. {
  70. if( aList.first )
  71. {
  72. // Change the item's list to me.
  73. for( EDA_ITEM* item = aList.first; item; item = item->Next() )
  74. item->SetList( this );
  75. if( first ) // this list is not empty, set last item's next to the first item in aList
  76. {
  77. wxCHECK_RET( last != NULL, wxT( "Last list element not set." ) );
  78. last->SetNext( aList.first );
  79. aList.first->SetBack( last );
  80. last = aList.last;
  81. }
  82. else // this list is empty, first and last are same as aList
  83. {
  84. first = aList.first;
  85. last = aList.last;
  86. }
  87. count += aList.count;
  88. aList.count = 0;
  89. aList.first = NULL;
  90. aList.last = NULL;
  91. }
  92. }
  93. void DHEAD::insert( EDA_ITEM* aNewElement, EDA_ITEM* aAfterMe )
  94. {
  95. wxASSERT( aNewElement != NULL );
  96. if( !aAfterMe )
  97. append( aNewElement );
  98. else
  99. {
  100. wxASSERT( aAfterMe->GetList() == this );
  101. // the list cannot be empty if aAfterMe is supposedly on the list
  102. wxASSERT( first && last );
  103. if( first == aAfterMe )
  104. {
  105. aAfterMe->SetBack( aNewElement );
  106. aNewElement->SetBack( 0 ); // first in list does not point back
  107. aNewElement->SetNext( aAfterMe );
  108. first = aNewElement;
  109. }
  110. else
  111. {
  112. EDA_ITEM* oldBack = aAfterMe->Back();
  113. aAfterMe->SetBack( aNewElement );
  114. aNewElement->SetBack( oldBack );
  115. aNewElement->SetNext( aAfterMe );
  116. oldBack->SetNext( aNewElement );
  117. }
  118. aNewElement->SetList( this );
  119. ++count;
  120. }
  121. }
  122. void DHEAD::remove( EDA_ITEM* aElement )
  123. {
  124. wxASSERT( aElement );
  125. wxASSERT( aElement->GetList() == this );
  126. if( aElement->Next() )
  127. {
  128. aElement->Next()->SetBack( aElement->Back() );
  129. }
  130. else // element being removed is last
  131. {
  132. wxASSERT( last == aElement );
  133. last = aElement->Back();
  134. }
  135. if( aElement->Back() )
  136. {
  137. aElement->Back()->SetNext( aElement->Next() );
  138. }
  139. else // element being removed is first
  140. {
  141. wxASSERT( first == aElement );
  142. first = aElement->Next();
  143. }
  144. aElement->SetBack( 0 );
  145. aElement->SetNext( 0 );
  146. aElement->SetList( 0 );
  147. --count;
  148. }
  149. #if defined(DEBUG)
  150. void DHEAD::VerifyListIntegrity()
  151. {
  152. EDA_ITEM* item;
  153. unsigned i = 0;
  154. for( item = first; item && i<count; ++i, item = item->Next() )
  155. {
  156. if( i < count-1 )
  157. {
  158. wxASSERT( item->Next() );
  159. }
  160. wxASSERT( item->GetList() == this );
  161. }
  162. wxASSERT( item == NULL );
  163. wxASSERT( i == count );
  164. i = 0;
  165. for( item = last; item && i<count; ++i, item = item->Back() )
  166. {
  167. if( i < count-1 )
  168. {
  169. wxASSERT( item->Back() );
  170. }
  171. }
  172. wxASSERT( item == NULL );
  173. wxASSERT( i == count );
  174. // printf("list %p has %d items.\n", this, count );
  175. }
  176. #endif