KiCad PCB EDA Suite
dlist.cpp
Go to the documentation of this file.
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 
25 
26 #include <fctsys.h>
27 #include <dlist.h>
28 #include <base_struct.h>
29 
30 
31 /* Implement the class DHEAD from dlist.h */
32 
33 
35 {
36  if( meOwner )
37  DeleteAll();
38 }
39 
40 
42 {
43  EDA_ITEM* next;
44  EDA_ITEM* item = first;
45 
46  while( item )
47  {
48  next = item->Next();
49  delete item; // virtual destructor, class specific
50  item = next;
51  }
52 
53  first = 0;
54  last = 0;
55  count = 0;
56 }
57 
58 
59 void DHEAD::append( EDA_ITEM* aNewElement )
60 {
61  wxASSERT( aNewElement != NULL );
62 
63  if( first ) // list is not empty, first is not touched
64  {
65  wxASSERT( last != NULL );
66 
67  aNewElement->SetNext( 0 );
68  aNewElement->SetBack( last );
69 
70  last->SetNext( aNewElement );
71  last = aNewElement;
72  }
73  else // list is empty, first and last are changed
74  {
75  aNewElement->SetNext( 0 );
76  aNewElement->SetBack( 0 );
77 
78  first = aNewElement;
79  last = aNewElement;
80  }
81 
82  aNewElement->SetList( this );
83 
84  ++count;
85 }
86 
87 
88 void DHEAD::append( DHEAD& aList )
89 {
90  if( aList.first )
91  {
92  // Change the item's list to me.
93  for( EDA_ITEM* item = aList.first; item; item = item->Next() )
94  item->SetList( this );
95 
96  if( first ) // this list is not empty, set last item's next to the first item in aList
97  {
98  wxCHECK_RET( last != NULL, wxT( "Last list element not set." ) );
99 
100  last->SetNext( aList.first );
101  aList.first->SetBack( last );
102  last = aList.last;
103  }
104  else // this list is empty, first and last are same as aList
105  {
106  first = aList.first;
107  last = aList.last;
108  }
109 
110  count += aList.count;
111 
112  aList.count = 0;
113  aList.first = NULL;
114  aList.last = NULL;
115  }
116 }
117 
118 
119 void DHEAD::insert( EDA_ITEM* aNewElement, EDA_ITEM* aAfterMe )
120 {
121  wxASSERT( aNewElement != NULL );
122 
123  if( !aAfterMe )
124  append( aNewElement );
125  else
126  {
127  wxASSERT( aAfterMe->GetList() == this );
128 
129  // the list cannot be empty if aAfterMe is supposedly on the list
130  wxASSERT( first && last );
131 
132  if( first == aAfterMe )
133  {
134  aAfterMe->SetBack( aNewElement );
135 
136  aNewElement->SetBack( 0 ); // first in list does not point back
137  aNewElement->SetNext( aAfterMe );
138 
139  first = aNewElement;
140  }
141  else
142  {
143  EDA_ITEM* oldBack = aAfterMe->Back();
144 
145  aAfterMe->SetBack( aNewElement );
146 
147  aNewElement->SetBack( oldBack );
148  aNewElement->SetNext( aAfterMe );
149 
150  oldBack->SetNext( aNewElement );
151  }
152 
153  aNewElement->SetList( this );
154 
155  ++count;
156  }
157 }
158 
159 
160 void DHEAD::remove( EDA_ITEM* aElement )
161 {
162  wxASSERT( aElement );
163  wxASSERT( aElement->GetList() == this );
164 
165  if( aElement->Next() )
166  {
167  aElement->Next()->SetBack( aElement->Back() );
168  }
169  else // element being removed is last
170  {
171  wxASSERT( last == aElement );
172  last = aElement->Back();
173  }
174 
175  if( aElement->Back() )
176  {
177  aElement->Back()->SetNext( aElement->Next() );
178  }
179  else // element being removed is first
180  {
181  wxASSERT( first == aElement );
182  first = aElement->Next();
183  }
184 
185  aElement->SetBack( 0 );
186  aElement->SetNext( 0 );
187  aElement->SetList( 0 );
188 
189  --count;
190 }
191 
192 #if defined(DEBUG)
193 
194 void DHEAD::VerifyListIntegrity()
195 {
196  EDA_ITEM* item;
197  unsigned i = 0;
198 
199  for( item = first; item && i<count; ++i, item = item->Next() )
200  {
201  if( i < count-1 )
202  {
203  wxASSERT( item->Next() );
204  }
205 
206  wxASSERT( item->GetList() == this );
207  }
208 
209  wxASSERT( item == NULL );
210  wxASSERT( i == count );
211 
212  i = 0;
213  for( item = last; item && i<count; ++i, item = item->Back() )
214  {
215  if( i < count-1 )
216  {
217  wxASSERT( item->Back() );
218  }
219  }
220 
221  wxASSERT( item == NULL );
222  wxASSERT( i == count );
223 
224  // printf("list %p has %d items.\n", this, count );
225 }
226 
227 #endif
228 
DHEAD * GetList() const
Definition: base_struct.h:209
CITER next(CITER it)
Definition: ptree.cpp:130
void SetBack(EDA_ITEM *aBack)
Definition: base_struct.h:212
Class DHEAD is only for use by template class DLIST, use that instead.
Definition: dlist.h:40
EDA_ITEM * Back() const
Definition: base_struct.h:207
void remove(EDA_ITEM *aElement)
Function remove removes aElement from the list, but does not delete it.
Definition: dlist.cpp:160
void SetList(DHEAD *aList)
Definition: base_struct.h:214
EDA_ITEM * Next() const
Definition: base_struct.h:206
void DeleteAll()
Function DeleteAll deletes all items on the list and leaves the list empty.
Definition: dlist.cpp:41
void insert(EDA_ITEM *aNewElement, EDA_ITEM *aElementAfterMe)
Function insert puts aNewElement just in front of aElementAfterMe in the list sequence.
Definition: dlist.cpp:119
unsigned count
how many elements are in the list, automatically maintained.
Definition: dlist.h:45
~DHEAD()
Definition: dlist.cpp:34
void SetNext(EDA_ITEM *aNext)
Definition: base_struct.h:211
EDA_ITEM * last
last elment in list, or NULL if empty
Definition: dlist.h:44
bool meOwner
I must delete the objects I hold in my destructor.
Definition: dlist.h:46
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
EDA_ITEM * first
first element in list, or NULL if list empty
Definition: dlist.h:43
void append(EDA_ITEM *aNewElement)
Function append adds aNewElement to the end of the list.
Definition: dlist.cpp:59
Basic classes for most KiCad items.