Coin Logo http://www.sim.no
http://www.coin3d.org

SbList.h
1#ifndef COIN_SBLIST_H
2#define COIN_SBLIST_H
3
4/**************************************************************************\
5 *
6 * This file is part of the Coin 3D visualization library.
7 * Copyright (C) 1998-2007 by Systems in Motion. All rights reserved.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * ("GPL") version 2 as published by the Free Software Foundation.
12 * See the file LICENSE.GPL at the root directory of this source
13 * distribution for additional information about the GNU GPL.
14 *
15 * For using Coin with software that can not be combined with the GNU
16 * GPL, and for taking advantage of the additional benefits of our
17 * support services, please contact Systems in Motion about acquiring
18 * a Coin Professional Edition License.
19 *
20 * See http://www.coin3d.org/ for more information.
21 *
22 * Systems in Motion, Postboks 1283, Pirsenteret, 7462 Trondheim, NORWAY.
23 * http://www.sim.no/ sales@sim.no coin-support@coin3d.org
24 *
25\**************************************************************************/
26
27#include <assert.h>
28#include <stddef.h> // NULL definition
29#include <Inventor/SbBasic.h> // TRUE/FALSE
30
31// We usually implement inline functions below the class definition,
32// since we think that makes the file more readable. However, this is
33// not done for this class, since Visual C++ is not too happy about
34// having functions declared as inline for a template class.
35// pederb, 2001-10-12
36
37// FIXME: this is just a quick hack to avoid heaps of irritating
38// warning messages from the compiler for client code compiled under
39// MSVC++. Should try to find the real reason for the warnings and fix
40// the cause of the problem instead. 20020730 mortene.
41#ifdef _MSC_VER // Microsoft Visual C++
42#pragma warning(disable:4251)
43#pragma warning(disable:4275)
44#endif // _MSC_VER
45
46template <class Type>
47class SbList {
48 // Older compilers aren't too happy about const declarations in the
49 // class definitions, so use the enum trick described by Scott
50 // Meyers in "Effective C++".
51 enum { DEFAULTSIZE = 4 };
52
53public:
54
55 SbList(const int sizehint = DEFAULTSIZE)
56 : itembuffersize(DEFAULTSIZE), numitems(0), itembuffer(builtinbuffer) {
57 if (sizehint > DEFAULTSIZE) this->grow(sizehint);
58 }
59
61 : itembuffersize(DEFAULTSIZE), numitems(0), itembuffer(builtinbuffer) {
62 this->copy(l);
63 }
64
66 if (this->itembuffer != builtinbuffer) delete[] this->itembuffer;
67 }
68
69 void copy(const SbList<Type> & l) {
70 if (this == &l) return;
71 const int n = l.numitems;
72 this->expand(n);
73 for (int i = 0; i < n; i++) this->itembuffer[i] = l.itembuffer[i];
74 }
75
76 SbList <Type> & operator=(const SbList<Type> & l) {
77 this->copy(l);
78 return *this;
79 }
80
81 void fit(void) {
82 const int items = this->numitems;
83
84 if (items < this->itembuffersize) {
85 Type * newitembuffer = this->builtinbuffer;
86 if (items > DEFAULTSIZE) newitembuffer = new Type[items];
87
88 if (newitembuffer != this->itembuffer) {
89 for (int i = 0; i < items; i++) newitembuffer[i] = this->itembuffer[i];
90 }
91
92 if (this->itembuffer != this->builtinbuffer) delete[] this->itembuffer;
93 this->itembuffer = newitembuffer;
94 this->itembuffersize = items > DEFAULTSIZE ? items : DEFAULTSIZE;
95 }
96 }
97
98 void append(const Type item) {
99 if (this->numitems == this->itembuffersize) this->grow();
100 this->itembuffer[this->numitems++] = item;
101 }
102
103 int find(const Type item) const {
104 for (int i = 0; i < this->numitems; i++)
105 if (this->itembuffer[i] == item) return i;
106 return -1;
107 }
108
109 void insert(const Type item, const int insertbefore) {
110#ifdef COIN_EXTRA_DEBUG
112#endif // COIN_EXTRA_DEBUG
113 if (this->numitems == this->itembuffersize) this->grow();
114
115 for (int i = this->numitems; i > insertbefore; i--)
116 this->itembuffer[i] = this->itembuffer[i-1];
117 this->itembuffer[insertbefore] = item;
118 this->numitems++;
119 }
120
121 void removeItem(const Type item) {
122 int idx = this->find(item);
123#ifdef COIN_EXTRA_DEBUG
124 assert(idx != -1);
125#endif // COIN_EXTRA_DEBUG
126 this->remove(idx);
127 }
128
129 void remove(const int index) {
130#ifdef COIN_EXTRA_DEBUG
131 assert(index >= 0 && index < this->numitems);
132#endif // COIN_EXTRA_DEBUG
133 this->numitems--;
134 for (int i = index; i < this->numitems; i++)
135 this->itembuffer[i] = this->itembuffer[i + 1];
136 }
137
138 void removeFast(const int index) {
139#ifdef COIN_EXTRA_DEBUG
140 assert(index >= 0 && index < this->numitems);
141#endif // COIN_EXTRA_DEBUG
142 this->itembuffer[index] = this->itembuffer[--this->numitems];
143 }
144
145 int getLength(void) const {
146 return this->numitems;
147 }
148
149 void truncate(const int length, const int dofit = 0) {
150#ifdef COIN_EXTRA_DEBUG
151 assert(length <= this->numitems);
152#endif // COIN_EXTRA_DEBUG
153 this->numitems = length;
154 if (dofit) this->fit();
155 }
156
157 void push(const Type item) {
158 this->append(item);
159 }
160
161 Type pop(void) {
162#ifdef COIN_EXTRA_DEBUG
163 assert(this->numitems > 0);
164#endif // COIN_EXTRA_DEBUG
165 return this->itembuffer[--this->numitems];
166 }
167
168 const Type * getArrayPtr(const int start = 0) const {
169 return &this->itembuffer[start];
170 }
171
172 Type operator[](const int index) const {
173#ifdef COIN_EXTRA_DEBUG
174 assert(index >= 0 && index < this->numitems);
175#endif // COIN_EXTRA_DEBUG
176 return this->itembuffer[index];
177 }
178
179 Type & operator[](const int index) {
180#ifdef COIN_EXTRA_DEBUG
181 assert(index >= 0 && index < this->numitems);
182#endif // COIN_EXTRA_DEBUG
183 return this->itembuffer[index];
184 }
185
186 int operator==(const SbList<Type> & l) const {
187 if (this == &l) return TRUE;
188 if (this->numitems != l.numitems) return FALSE;
189 for (int i = 0; i < this->numitems; i++)
190 if (this->itembuffer[i] != l.itembuffer[i]) return FALSE;
191 return TRUE;
192 }
193
194 int operator!=(const SbList<Type> & l) const {
195 return !(*this == l);
196 }
197
198protected:
199
200 void expand(const int size) {
201 this->grow(size);
202 this->numitems = size;
203 }
204
205 int getArraySize(void) const {
206 return this->itembuffersize;
207 }
208
209private:
210 void grow(const int size = -1) {
211 // Default behavior is to double array size.
212 if (size == -1) this->itembuffersize <<= 1;
213 else if (size <= this->itembuffersize) return;
214 else { this->itembuffersize = size; }
215
216 Type * newbuffer = new Type[this->itembuffersize];
217 const int n = this->numitems;
218 for (int i = 0; i < n; i++) newbuffer[i] = this->itembuffer[i];
219 if (this->itembuffer != this->builtinbuffer) delete[] this->itembuffer;
220 this->itembuffer = newbuffer;
221 }
222
223 int itembuffersize;
224 int numitems;
225 Type * itembuffer;
226 Type builtinbuffer[DEFAULTSIZE];
227};
228
229#endif // !COIN_SBLIST_H
The SbList class is a template container class for lists.
Definition SbList.h:47
void truncate(const int length, const int dofit=0)
Definition SbList.h:149
int getLength(void) const
Definition SbList.h:145
SbList(const SbList< Type > &l)
Definition SbList.h:60
Type pop(void)
Definition SbList.h:161
int find(const Type item) const
Definition SbList.h:103
SbList< Type > & operator=(const SbList< Type > &l)
Definition SbList.h:76
void removeFast(const int index)
Definition SbList.h:138
Type & operator[](const int index)
Definition SbList.h:179
int operator!=(const SbList< Type > &l) const
Definition SbList.h:194
int getArraySize(void) const
Definition SbList.h:205
const Type * getArrayPtr(const int start=0) const
Definition SbList.h:168
void remove(const int index)
Definition SbList.h:129
SbList(const int sizehint=DEFAULTSIZE)
Definition SbList.h:55
void removeItem(const Type item)
Definition SbList.h:121
int operator==(const SbList< Type > &l) const
Definition SbList.h:186
void copy(const SbList< Type > &l)
Definition SbList.h:69
void fit(void)
Definition SbList.h:81
void insert(const Type item, const int insertbefore)
Definition SbList.h:109
~SbList()
Definition SbList.h:65
void append(const Type item)
Definition SbList.h:98
void expand(const int size)
Definition SbList.h:200
Type operator[](const int index) const
Definition SbList.h:172
void push(const Type item)
Definition SbList.h:157

Copyright © 1998-2007 by Systems in Motion AS. All rights reserved.

Generated on Wed Jul 19 2023 for Coin by Doxygen. 1.9.8