MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
SbList.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef SB_LIST_H
14#define SB_LIST_H
15
16#include "SoShaderSystem.h"
17
21
22// This class is loosely derived from class SbPList.
23
24template <class Type> class SbList
25{
26public:
27 // Constructor
29 {
30 ptrs = nullptr;
31 nPtrs = ptrsSize = 0;
32
33 setSize(0);
34 }
35
36 // Constructor allocates enough room for the given number of pointers
38 {
39 ptrs = NULL;
40 nPtrs = ptrsSize = 0;
41
42 setSize(initSize); // Makes enough room for initSize entries
43 setSize(0); // Sets nPtrs back to 0
44 }
45
46 // Constructor for copying
48 {
49 nPtrs = pl.nPtrs;
50 ptrsSize = pl.ptrsSize;
51 ptrs = new Type[ptrsSize];
52
53 for (int i = 0; i < nPtrs; i++)
54 ptrs[i] = pl.ptrs[i];
55 }
56
57 // Destructor
59 {
60 delete[] ptrs;
61 }
62
63 // Adds given pointer to end of list
64 void append(const Type& value)
65 {
66 if (nPtrs + 1 > ptrsSize)
67 expand(nPtrs + 1);
68 ptrs[nPtrs++] = value;
69 }
70
71 // Returns index of given pointer in list, or -1 if not found
72 int find(const Type& value) const
73 {
74 for (int i = 0; i < nPtrs; i++)
75 if (ptrs[i] == value)
76 return (i);
77
78 return -1; // Not found
79 }
80
81 // Inserts given pointer in list before pointer with given index
82 void insert(const Type& value, int addBefore)
83 {
84 // If addBefore is off the end of the list, grow the list (and
85 // initialize any new elements to NULL)
86 if (addBefore > nPtrs)
87 grow(addBefore);
88
89 // Make room for one more
90 setSize(nPtrs + 1);
91
92 // Move pointers forward to make room
93 for (int i = nPtrs - 1; i > addBefore; --i)
94 ptrs[i] = ptrs[i - 1];
95
96 // Insert the new one
97 ptrs[addBefore] = value;
98 }
99
100 // Removes pointer with given index
101 void remove(int which)
102 {
103 // Move all pointers after the ith one backward
104 for (int i = which; i < nPtrs - 1; i++)
105 ptrs[i] = ptrs[i + 1];
106
107 // Shrink the list
108 setSize(nPtrs - 1);
109 }
110
111 // Returns number of pointers in list
112 int getLength() const
113 {
114 return nPtrs;
115 }
116
117 // Removes all pointers after one with given index, inclusive
118 // ??? should really be an error check in here
119 void truncate(int start)
120 {
121 nPtrs = start;
122 }
123
124 // Copy a PList
125 void copy(const SbList& pl)
126 {
127 setSize(pl.nPtrs);
128
129 for (int i = 0; i < nPtrs; i++)
130 ptrs[i] = pl.ptrs[i];
131 }
132
134 {
135 copy(pl);
136 return *this;
137 }
138
139 // Returns pointer with given index
140 Type& operator[](int i) const
141 {
142 if (i >= nPtrs)
143 grow(i);
144 return ptrs[i];
145 }
146
147 // equality tests
148 int operator==(const SbList& pl) const
149 {
150 return pl.nPtrs == nPtrs ? compare(pl) : FALSE;
151 }
152 int operator!=(const SbList& pl) const
153 {
154 return pl.nPtrs == nPtrs ? !compare(pl) : TRUE;
155 }
156
157 // Internal versions of [] that do not check for bounds:
159
160 void*
161 get(int i) const
162 {
163 return ptrs[i];
164 }
165 void set(int i, void* j)
166 {
167 ptrs[i] = j;
168 }
169
170private:
171 // NOTE: this must be called only if the number of elements in the two
172 // lists is the same, otherwise badness could result
173 int compare(const SbList& pl) const
174 {
175 for (int i = 0; i < nPtrs; i++)
176 if ((*this)[i] != pl[i])
177 return FALSE;
178
179 return TRUE;
180 }
181
182 Type* ptrs; // The collection of pointers
183 int nPtrs; // Number of pointers used
184 int ptrsSize; // Number of pointers allocated
185
186 // There are three(!) methods to expand the list. grow() is used
187 // when space is dynamically created, and needs to be initialized
188 // to NULL:
189 void grow(int max) const
190 {
191 int newSize = max + 1;
192 int oldSize = nPtrs;
193
194#ifdef DEBUG
195 if (newSize <= oldSize)
196 SoDebugError::post("(internal) SbList::grow", "newSize <= oldSize!");
197#endif // DEBUG
198
199 // Get around the const thing:
200 SbList* me = static_cast<SbList*>(this);
201
202 me->setSize(newSize);
203
204 for (int i = oldSize; i < newSize; i++)
205 me->ptrs[i] = NULL;
206 }
207
208 // setSize is used by grow and in other places internally where we
209 // know that nothing needs to be initialized to NULL.
210 void setSize(int size)
211 {
212 if (size > ptrsSize)
213 expand(size);
214 nPtrs = size;
215 }
216
217 // expand is the lowest level routine. It just reallocates the
218 // array and copies over the old values.
219 void expand(int size)
220 {
221 if (ptrsSize == 0)
222 ptrsSize = 4;
223
224 while (size > ptrsSize) {
225#ifdef DEBUG
226 // check for overflow
227 int oldPtrsSize = ptrsSize;
228 ptrsSize *= 2;
229 if (ptrsSize < oldPtrsSize)
230 SoDebugError::post("SbList::expand", "Attempt to expand list beyond capacity;\n A core dump is likely");
231#else
232 ptrsSize *= 2;
233#endif
234 }
235
236 Type* newPtrs = new Type[ptrsSize];
237
238 if (ptrs != nullptr) {
239 for (int i = 0; i < nPtrs; i++)
240 newPtrs[i] = ptrs[i];
241 delete[] ptrs;
242 }
243
244 ptrs = newPtrs;
245 }
246};
247
248#endif // _SB_LIST_
This contains the definition of the SbList generic pointer list class; an SbList is a list of (void *...
Definition SbList.h:25
SbList & operator=(const SbList &pl)
Definition SbList.h:133
void append(const Type &value)
Definition SbList.h:64
Type & operator[](int i) const
Definition SbList.h:140
int operator==(const SbList &pl) const
Definition SbList.h:148
void copy(const SbList &pl)
Definition SbList.h:125
SbList()
Definition SbList.h:28
SbList(const SbList &pl)
Definition SbList.h:47
void remove(int which)
Definition SbList.h:101
int find(const Type &value) const
Definition SbList.h:72
void truncate(int start)
Definition SbList.h:119
void * get(int i) const
Definition SbList.h:161
SbList(int initSize)
Definition SbList.h:37
int operator!=(const SbList &pl) const
Definition SbList.h:152
void insert(const Type &value, int addBefore)
Definition SbList.h:82
int getLength() const
Definition SbList.h:112
void set(int i, void *j)
Definition SbList.h:165
~SbList()
Definition SbList.h:58
Target mlrange_cast(Source arg)
Generic version of checked ML casts.