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) by Kongsberg Oil & Gas Technologies.
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 Kongsberg Oil & Gas Technologies
18 * about acquiring a Coin Professional Edition License.
19 *
20 * See http://www.coin3d.org/ for more information.
21 *
22 * Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, 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 Microsoft Visual C++ is not too
34// happy about having functions declared as inline for a template
35// class.
36
37// FIXME: the #pragmas below is just a quick hack to avoid heaps of
38// irritating warning messages from the compiler for client code
39// compiled under MSVC++. Should try to find the real reason for the
40// warnings and fix the cause of the problem instead. 20020730 mortene.
41//
42// UPDATE 20030617 mortene: there is a Microsoft Knowledge Base
43// article at <URL:http://support.microsoft.com> which is related to
44// this problem. It's article number KB168958.
45//
46// In short, the general solution is that classes that exposes usage
47// of SbList<type> needs to declare the specific template instance
48// with "extern" and __declspec(dllimport/export).
49//
50// That is a lot of work to change, tho'. Another possibility which
51// might be better is to simply avoid using (exposing) SbList from any
52// of the other public classes. Judging from a quick look, this seems
53// feasible, and just a couple of hours or so of work.
54//
55#ifdef _MSC_VER // Microsoft Visual C++
56#pragma warning(disable:4251)
57#pragma warning(disable:4275)
58#endif // _MSC_VER
59
60template <class Type>
61class SbList {
62 // Older compilers aren't too happy about const declarations in the
63 // class definitions, so use the enum trick described by Scott
64 // Meyers in "Effective C++".
65 enum { DEFAULTSIZE = 4 };
66
67public:
68
69 SbList(const int sizehint = DEFAULTSIZE)
70 : itembuffersize(DEFAULTSIZE), numitems(0), itembuffer(builtinbuffer) {
71 if (sizehint > DEFAULTSIZE) this->grow(sizehint);
72 }
73
75 : itembuffersize(DEFAULTSIZE), numitems(0), itembuffer(builtinbuffer) {
76 this->copy(l);
77 }
78
80 if (this->itembuffer != builtinbuffer) delete[] this->itembuffer;
81 }
82
83 void copy(const SbList<Type> & l) {
84 if (this == &l) return;
85 const int n = l.numitems;
86 this->expand(n);
87 for (int i = 0; i < n; i++) this->itembuffer[i] = l.itembuffer[i];
88 }
89
91 this->copy(l);
92 return *this;
93 }
94
95 void fit(void) {
96 const int items = this->numitems;
97
98 if (items < this->itembuffersize) {
99 Type * newitembuffer = this->builtinbuffer;
100 if (items > DEFAULTSIZE) newitembuffer = new Type[items];
101
102 if (newitembuffer != this->itembuffer) {
103 for (int i = 0; i < items; i++) newitembuffer[i] = this->itembuffer[i];
104 }
105
106 if (this->itembuffer != this->builtinbuffer) delete[] this->itembuffer;
107 this->itembuffer = newitembuffer;
108 this->itembuffersize = items > DEFAULTSIZE ? items : DEFAULTSIZE;
109 }
110 }
111
112 void append(const Type item) {
113 if (this->numitems == this->itembuffersize) this->grow();
114 this->itembuffer[this->numitems++] = item;
115 }
116
117 int find(const Type item) const {
118 for (int i = 0; i < this->numitems; i++)
119 if (this->itembuffer[i] == item) return i;
120 return -1;
121 }
122
123 void insert(const Type item, const int insertbefore) {
124#ifdef COIN_EXTRA_DEBUG
125 assert(insertbefore >= 0 && insertbefore <= this->numitems);
126#endif // COIN_EXTRA_DEBUG
127 if (this->numitems == this->itembuffersize) this->grow();
128
129 for (int i = this->numitems; i > insertbefore; i--)
130 this->itembuffer[i] = this->itembuffer[i-1];
131 this->itembuffer[insertbefore] = item;
132 this->numitems++;
133 }
134
135 void removeItem(const Type item) {
136 int idx = this->find(item);
137#ifdef COIN_EXTRA_DEBUG
138 assert(idx != -1);
139#endif // COIN_EXTRA_DEBUG
140 this->remove(idx);
141 }
142
143 void remove(const int index) {
144#ifdef COIN_EXTRA_DEBUG
145 assert(index >= 0 && index < this->numitems);
146#endif // COIN_EXTRA_DEBUG
147 this->numitems--;
148 for (int i = index; i < this->numitems; i++)
149 this->itembuffer[i] = this->itembuffer[i + 1];
150 }
151
152 void removeFast(const int index) {
153#ifdef COIN_EXTRA_DEBUG
154 assert(index >= 0 && index < this->numitems);
155#endif // COIN_EXTRA_DEBUG
156 this->itembuffer[index] = this->itembuffer[--this->numitems];
157 }
158
159 int getLength(void) const {
160 return this->numitems;
161 }
162
163 void truncate(const int length, const int dofit = 0) {
164#ifdef COIN_EXTRA_DEBUG
165 assert(length <= this->numitems);
166#endif // COIN_EXTRA_DEBUG
167 this->numitems = length;
168 if (dofit) this->fit();
169 }
170
171 void push(const Type item) {
172 this->append(item);
173 }
174
175 Type pop(void) {
176#ifdef COIN_EXTRA_DEBUG
177 assert(this->numitems > 0);
178#endif // COIN_EXTRA_DEBUG
179 return this->itembuffer[--this->numitems];
180 }
181
182 const Type * getArrayPtr(const int start = 0) const {
183 return &this->itembuffer[start];
184 }
185
186 Type operator[](const int index) const {
187#ifdef COIN_EXTRA_DEBUG
188 assert(index >= 0 && index < this->numitems);
189#endif // COIN_EXTRA_DEBUG
190 return this->itembuffer[index];
191 }
192
193 Type & operator[](const int index) {
194#ifdef COIN_EXTRA_DEBUG
195 assert(index >= 0 && index < this->numitems);
196#endif // COIN_EXTRA_DEBUG
197 return this->itembuffer[index];
198 }
199
200 int operator==(const SbList<Type> & l) const {
201 if (this == &l) return TRUE;
202 if (this->numitems != l.numitems) return FALSE;
203 for (int i = 0; i < this->numitems; i++)
204 if (this->itembuffer[i] != l.itembuffer[i]) return FALSE;
205 return TRUE;
206 }
207
208 int operator!=(const SbList<Type> & l) const {
209 return !(*this == l);
210 }
211
212 void ensureCapacity(const int size) {
213 if ((size > itembuffersize) &&
214 (size > DEFAULTSIZE)) {
215 this->grow(size);
216 }
217 }
218
219protected:
220
221 void expand(const int size) {
222 this->grow(size);
223 this->numitems = size;
224 }
225
226 int getArraySize(void) const {
227 return this->itembuffersize;
228 }
229
230private:
231 void grow(const int size = -1) {
232 // Default behavior is to double array size.
233 if (size == -1) this->itembuffersize <<= 1;
234 else if (size <= this->itembuffersize) return;
235 else { this->itembuffersize = size; }
236
237 Type * newbuffer = new Type[this->itembuffersize];
238 const int n = this->numitems;
239 for (int i = 0; i < n; i++) newbuffer[i] = this->itembuffer[i];
240 if (this->itembuffer != this->builtinbuffer) delete[] this->itembuffer;
241 this->itembuffer = newbuffer;
242 }
243
244 int itembuffersize;
245 int numitems;
246 Type * itembuffer;
247 Type builtinbuffer[DEFAULTSIZE];
248};
249
250#endif // !COIN_SBLIST_H
The SbList class is a template container class for lists.
Definition SbList.h:61
void truncate(const int length, const int dofit=0)
Definition SbList.h:163
int getLength(void) const
Definition SbList.h:159
SbList(const SbList< Type > &l)
Definition SbList.h:74
Type pop(void)
Definition SbList.h:175
int find(const Type item) const
Definition SbList.h:117
int operator!=(const SbList< Type > &l) const
Definition SbList.h:208
int operator==(const SbList< Type > &l) const
Definition SbList.h:200
SbList< Type > & operator=(const SbList< Type > &l)
Definition SbList.h:90
void removeFast(const int index)
Definition SbList.h:152
Type & operator[](const int index)
Definition SbList.h:193
int getArraySize(void) const
Definition SbList.h:226
const Type * getArrayPtr(const int start=0) const
Definition SbList.h:182
void remove(const int index)
Definition SbList.h:143
SbList(const int sizehint=DEFAULTSIZE)
Definition SbList.h:69
void removeItem(const Type item)
Definition SbList.h:135
void copy(const SbList< Type > &l)
Definition SbList.h:83
void fit(void)
Definition SbList.h:95
void insert(const Type item, const int insertbefore)
Definition SbList.h:123
~SbList()
Definition SbList.h:79
void append(const Type item)
Definition SbList.h:112
void expand(const int size)
Definition SbList.h:221
void ensureCapacity(const int size)
Definition SbList.h:212
Type operator[](const int index) const
Definition SbList.h:186
void push(const Type item)
Definition SbList.h:171

Copyright © 1998-2010 by Kongsberg Oil & Gas Technologies. All rights reserved.

Generated on Wed Jul 17 2024 for Coin by Doxygen 1.12.0.