MeVisLab Toolbox Reference
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>
25class SbList
26{
27 public:
28
29 // Constructor
31 ptrs = nullptr;
32 nPtrs = ptrsSize = 0;
33
34 setSize(0);
35 }
36
37 // Constructor allocates enough room for the given number of pointers
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
47 SbList(const SbList &pl) {
48 nPtrs = pl.nPtrs;
49 ptrsSize = pl.ptrsSize;
50 ptrs = new Type[ptrsSize];
51
52 for(int i = 0; i < nPtrs; i++)
53 ptrs[i] = pl.ptrs[i];
54 }
55
56 // Destructor
58 delete [] ptrs;
59 }
60
61 // Adds given pointer to end of list
62 void append(const Type &value) {
63 if(nPtrs + 1 > ptrsSize) expand(nPtrs + 1);
64 ptrs[nPtrs++] = value;
65 }
66
67 // Returns index of given pointer in list, or -1 if not found
68 int find(const Type &value) const {
69 for(int i = 0; i < nPtrs; i++)
70 if(ptrs[i] == value)
71 return(i);
72
73 return -1; // Not found
74 }
75
76 // Inserts given pointer in list before pointer with given index
77 void insert(const Type &value, int addBefore) {
78 // If addBefore is off the end of the list, grow the list (and
79 // initialize any new elements to NULL)
80 if(addBefore > nPtrs) grow(addBefore);
81
82 // Make room for one more
83 setSize(nPtrs + 1);
84
85 // Move pointers forward to make room
86 for(int i = nPtrs - 1; i > addBefore; --i)
87 ptrs[i] = ptrs[i - 1];
88
89 // Insert the new one
90 ptrs[addBefore] = value;
91 }
92
93 // Removes pointer with given index
94 void remove(int which) {
95 // Move all pointers after the ith one backward
96 for(int i = which; i < nPtrs - 1; i++)
97 ptrs[i] = ptrs[i + 1];
98
99 // Shrink the list
100 setSize(nPtrs - 1);
101 }
102
103 // Returns number of pointers in list
104 int getLength() const {
105 return nPtrs;
106 }
107
108 // Removes all pointers after one with given index, inclusive
109 // ??? should really be an error check in here
110 void truncate(int start) {
111 nPtrs = start;
112 }
113
114 // Copy a PList
115 void copy(const SbList &pl) {
116 setSize(pl.nPtrs);
117
118 for(int i = 0; i < nPtrs; i++)
119 ptrs[i] = pl.ptrs[i];
120 }
121
123 copy(pl); return *this;
124 }
125
126 // Returns pointer with given index
127 Type &operator [](int i) const {
128 if(i >= nPtrs) grow(i);
129 return ptrs[i];
130 }
131
132 // equality tests
133 int operator ==(const SbList &pl) const {
134 return pl.nPtrs == nPtrs ? compare(pl) : FALSE;
135 }
136 int operator !=(const SbList &pl) const {
137 return pl.nPtrs == nPtrs ? ! compare(pl) : TRUE;
138 }
139
140 // Internal versions of [] that do not check for bounds:
141 SoINTERNAL public:
142
143 void * get(int i) const {
144 return ptrs[i];
145 }
146 void set(int i, void *j) {
147 ptrs[i] = j;
148 }
149
150 private:
151
152 // NOTE: this must be called only if the number of elements in the two
153 // lists is the same, otherwise badness could result
154 int compare(const SbList &pl) const {
155 for(int i = 0; i < nPtrs; i++)
156 if((*this)[i] != pl[i])
157 return FALSE;
158
159 return TRUE;
160 }
161
162 Type *ptrs; // The collection of pointers
163 int nPtrs; // Number of pointers used
164 int ptrsSize; // Number of pointers allocated
165
166 // There are three(!) methods to expand the list. grow() is used
167 // when space is dynamically created, and needs to be initialized
168 // to NULL:
169 void grow(int max) const {
170 int newSize = max+1;
171 int oldSize = nPtrs;
172
173#ifdef DEBUG
174 if(newSize <= oldSize)
175 SoDebugError::post("(internal) SbList::grow", "newSize <= oldSize!");
176#endif // DEBUG
177
178 // Get around the const thing:
179 SbList *me = static_cast<SbList *>(this);
180
181 me->setSize(newSize);
182
183 for(int i = oldSize; i < newSize; i++)
184 me->ptrs[i] = NULL;
185 }
186
187 // setSize is used by grow and in other places internally where we
188 // know that nothing needs to be initialized to NULL.
189 void setSize(int size) {
190 if(size > ptrsSize) expand(size);
191 nPtrs = size;
192 }
193
194 // expand is the lowest level routine. It just reallocates the
195 // array and copies over the old values.
196 void expand(int size) {
197 if(ptrsSize == 0)
198 ptrsSize = 4;
199
200 while(size > ptrsSize) {
201#ifdef DEBUG
202 // check for overflow
203 int oldPtrsSize = ptrsSize;
204 ptrsSize *= 2;
205 if(ptrsSize < oldPtrsSize)
206 SoDebugError::post("SbList::expand", "Attempt to expand list beyond capacity;\n A core dump is likely");
207#else
208 ptrsSize *= 2;
209#endif
210 }
211
212 Type *newPtrs = new Type[ptrsSize];
213
214 if(ptrs != nullptr) {
215 for(int i = 0; i < nPtrs; i++)
216 newPtrs[i] = ptrs[i];
217 delete [] ptrs;
218 }
219
220 ptrs = newPtrs;
221 }
222};
223
224#endif // _SB_LIST_
This contains the definition of the SbList generic pointer list class; an SbList is a list of (void *...
Definition SbList.h:26
SbList & operator=(const SbList &pl)
Definition SbList.h:122
void append(const Type &value)
Definition SbList.h:62
Type & operator[](int i) const
Definition SbList.h:127
int operator==(const SbList &pl) const
Definition SbList.h:133
void copy(const SbList &pl)
Definition SbList.h:115
SbList()
Definition SbList.h:30
SbList(const SbList &pl)
Definition SbList.h:47
void remove(int which)
Definition SbList.h:94
int find(const Type &value) const
Definition SbList.h:68
void truncate(int start)
Definition SbList.h:110
void * get(int i) const
Definition SbList.h:143
SbList(int initSize)
Definition SbList.h:38
int operator!=(const SbList &pl) const
Definition SbList.h:136
void insert(const Type &value, int addBefore)
Definition SbList.h:77
int getLength() const
Definition SbList.h:104
void set(int i, void *j)
Definition SbList.h:146
~SbList()
Definition SbList.h:57
Target mlrange_cast(Source arg)
Generic version of checked ML casts.