/
vtkCollection.cxx
362 lines (301 loc) · 6.95 KB
/
vtkCollection.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCollection.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkCollection.h"
#include "vtkCollectionIterator.h"
#include "vtkGarbageCollector.h"
#include "vtkObjectFactory.h"
#include <cassert>
#include <cstdlib>
#include <cmath>
vtkStandardNewMacro(vtkCollection);
// Construct with empty list.
vtkCollection::vtkCollection()
{
this->NumberOfItems = 0;
this->Top = NULL;
this->Bottom = NULL;
this->Current = NULL;
}
// Destructor for the vtkCollection class. This removes all
// objects from the collection.
vtkCollection::~vtkCollection()
{
this->RemoveAllItems();
}
// protected function to delete an element. Internal use only.
void vtkCollection::DeleteElement(vtkCollectionElement *e)
{
if (e->Item != NULL)
{
e->Item->UnRegister(this);
}
delete e;
}
// protected function to remove an element. Internal use only.
void vtkCollection::
RemoveElement(vtkCollectionElement *elem, vtkCollectionElement *prev)
{
assert(elem);
if (prev)
{
prev->Next = elem->Next;
}
else
{
this->Top = elem->Next;
}
if (!elem->Next)
{
this->Bottom = prev;
}
if ( this->Current == elem )
{
this->Current = elem->Next;
}
this->NumberOfItems--;
this->DeleteElement(elem);
}
// Add an object to the list. Does not prevent duplicate entries.
void vtkCollection::AddItem(vtkObject *a)
{
vtkCollectionElement *elem;
elem = new vtkCollectionElement;
if (!this->Top)
{
this->Top = elem;
}
else
{
this->Bottom->Next = elem;
}
this->Bottom = elem;
a->Register(this);
elem->Item = a;
elem->Next = NULL;
this->Modified();
this->NumberOfItems++;
}
// Insert an object into the list. There must be at least one
// entry pre-existing.
void vtkCollection::InsertItem(int i, vtkObject *a)
{
if( i >= this->NumberOfItems || !this->Top )
{
return;
}
vtkCollectionElement *elem;
elem = new vtkCollectionElement;
vtkCollectionElement *curr = this->Top;
if( i < 0 )
{
this->Top = elem;
elem->Next = curr;
}
else
{
vtkCollectionElement *next = curr->Next;
int j = 0;
while( j != i )
{
curr = next;
next = curr->Next;
j++;
}
curr->Next = elem;
if( curr == this->Bottom )
{
this->Bottom = elem;
}
else
{
elem->Next = next;
}
}
a->Register(this);
elem->Item = a;
this->Modified();
this->NumberOfItems++;
}
// Remove an object from the list. Removes the first object found, not
// all occurrences. If no object found, list is unaffected. See warning
// in description of RemoveItem(int).
void vtkCollection::RemoveItem(vtkObject *a)
{
if (!this->Top)
{
return;
}
vtkCollectionElement *prev = NULL;
vtkCollectionElement *elem = this->Top;
for (int i = 0; i < this->NumberOfItems; i++)
{
if (elem->Item == a)
{
this->RemoveElement(elem, prev);
this->Modified();
return;
}
else
{
prev = elem;
elem = elem->Next;
}
}
}
// Remove all objects from the list.
void vtkCollection::RemoveAllItems()
{
// Don't modify if collection is empty
if(this->NumberOfItems == 0)
{
return;
}
while (this->NumberOfItems)
{
this->RemoveElement(this->Top, NULL);
}
this->Modified();
}
// Search for an object and return location in list. If location == 0,
// object was not found.
int vtkCollection::IsItemPresent(vtkObject *a)
{
int i;
vtkCollectionElement *elem;
if (!this->Top)
{
return 0;
}
elem = this->Top;
for (i = 0; i < this->NumberOfItems; i++)
{
if (elem->Item == a)
{
return i + 1;
}
else
{
elem = elem->Next;
}
}
return 0;
}
void vtkCollection::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Number Of Items: " << this->NumberOfItems << "\n";
}
// Get the i'th item in the collection. NULL is returned if i is out
// of range
vtkObject *vtkCollection::GetItemAsObject(int i)
{
vtkCollectionElement *elem=this->Top;
if (i < 0)
{
return NULL;
}
if (i == this->NumberOfItems - 1)
{
// optimize for the special case where we're looking for the last elem
elem = this->Bottom;
}
else
{
while (elem != NULL && i > 0)
{
elem = elem->Next;
i--;
}
}
if ( elem != NULL )
{
return elem->Item;
}
else
{
return NULL;
}
}
// Replace the i'th item in the collection with a
void vtkCollection::ReplaceItem(int i, vtkObject *a)
{
vtkCollectionElement *elem;
if( i < 0 || i >= this->NumberOfItems )
{
return;
}
elem = this->Top;
if (i == this->NumberOfItems - 1)
{
elem = this->Bottom;
}
else
{
for (int j = 0; j < i; j++, elem = elem->Next )
{}
}
// Take care of reference counting
if (elem->Item != NULL)
{
elem->Item->UnRegister(this);
}
a->Register(this);
// j == i
elem->Item = a;
this->Modified();
}
// Remove the i'th item in the list.
// Be careful if using this function during traversal of the list using
// GetNextItemAsObject (or GetNextItem in derived class). The list WILL
// be shortened if a valid index is given! If this->Current is equal to the
// element being removed, have it point to then next element in the list.
void vtkCollection::RemoveItem(int i)
{
vtkCollectionElement *elem,*prev;
if( i < 0 || i >= this->NumberOfItems )
{
return;
}
elem = this->Top;
prev = NULL;
for (int j = 0; j < i; j++)
{
prev = elem;
elem = elem->Next;
}
this->RemoveElement(elem, prev);
this->Modified();
}
vtkCollectionIterator* vtkCollection::NewIterator()
{
vtkCollectionIterator* it = vtkCollectionIterator::New();
it->SetCollection(this);
return it;
}
//----------------------------------------------------------------------------
void vtkCollection::Register(vtkObjectBase* o)
{
this->RegisterInternal(o, 1);
}
//----------------------------------------------------------------------------
void vtkCollection::UnRegister(vtkObjectBase* o)
{
this->UnRegisterInternal(o, 1);
}
//----------------------------------------------------------------------------
void vtkCollection::ReportReferences(vtkGarbageCollector* collector)
{
this->Superclass::ReportReferences(collector);
for(vtkCollectionElement* elem = this->Top; elem; elem = elem->Next)
{
vtkGarbageCollectorReport(collector, elem->Item, "Element");
}
}