Coin  4.0.10
Coin3D core library
Loading...
Searching...
No Matches
lists/SbPList.h
1#ifndef COIN_LISTS_SBPLIST_H
2#define COIN_LISTS_SBPLIST_H
3
4/**************************************************************************\
5 * Copyright (c) Kongsberg Oil & Gas Technologies AS
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 *
15 * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * Neither the name of the copyright holder nor the names of its
20 * contributors may be used to endorse or promote products derived from
21 * this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34\**************************************************************************/
35
36#include <Inventor/SbBasic.h>
37#include <cassert>
38#include <cstddef> // NULL definition
39
41 enum { DEFAULTSIZE = 4 };
42
43public:
44 SbPList(const int sizehint = DEFAULTSIZE);
45 SbPList(const SbPList & l);
46 ~SbPList();
47
48 void copy(const SbPList & l);
49 SbPList & operator=(const SbPList & l);
50 void fit(void);
51
52 void append(void * item);
53 int find(const void * item) const;
54 void insert(void * item, const int insertbefore);
55 void removeItem(void * item);
56 void remove(const int index);
57 void removeFast(const int index);
58 int getLength(void) const;
59 void truncate(const int length, const int fit = 0);
60
61 void ** getArrayPtr(const int start = 0) const;
62 void *& operator[](const int index) const;
63
64 int operator==(const SbPList & l) const;
65 int operator!=(const SbPList & l) const;
66 void * get(const int index) const;
67 void set(const int index, void * item);
68
69protected:
70
71 void expand(const int size);
72 int getArraySize(void) const;
73
74private:
75 static void invalidIndex(const char * operation);
76 void expandindex(const int index) const;
77 void expandlist(const int size) const;
78 void grow(const int size = -1);
79
80 int itembuffersize;
81 int numitems;
82 void ** itembuffer;
83 void * builtinbuffer[DEFAULTSIZE];
84};
85
86/* inlined methods ********************************************************/
87
88inline void
90{
91 if (this->numitems == this->itembuffersize) this->grow();
92 this->itembuffer[this->numitems++] = item;
93}
94
95inline void
96SbPList::removeFast(const int index)
97{
98 if (index < 0 || index >= this->numitems) {
99 SbPList::invalidIndex("SbPList::removeFast(): index out of range");
100 }
101 this->itembuffer[index] = this->itembuffer[--this->numitems];
102}
103
104inline int
106{
107 return this->numitems;
108}
109
110inline void
111SbPList::truncate(const int length, const int dofit)
112{
113 if (length < 0 || length > this->numitems) {
114 SbPList::invalidIndex("SbPList::truncate(): length out of range");
115 }
116 this->numitems = length;
117 if (dofit) this->fit();
118}
119
120inline void **
121SbPList::getArrayPtr(const int start) const
122{
123 if (start < 0 || (this->numitems == 0 ? start != 0 : start >= this->numitems)) {
124 SbPList::invalidIndex("SbPList::getArrayPtr(): index out of range");
125 }
126 return &this->itembuffer[start];
127}
128
129inline void *&
130SbPList::operator[](const int index) const
131{
132 if (index < 0) {
133 SbPList::invalidIndex("SbPList::operator[](): index out of range");
134 }
135 if (index >= this->getLength()) this->expandindex(index);
136 return this->itembuffer[index];
137}
138
139inline int
141{
142 return !(*this == l);
143}
144
145inline void *
146SbPList::get(const int index) const
147{
148 if (index < 0 || index >= this->numitems) {
149 SbPList::invalidIndex("SbPList::get(): index out of range");
150 }
151 return this->itembuffer[index];
152}
153
154inline void
155SbPList::set(const int index, void * item)
156{
157 if (index < 0 || index >= this->numitems) {
158 SbPList::invalidIndex("SbPList::set(): index out of range");
159 }
160 this->itembuffer[index] = item;
161}
162
163inline void
164SbPList::expand(const int size)
165{
166 this->grow(size);
167 this->numitems = size;
168}
169
170inline int
172{
173 return this->itembuffersize;
174}
175
176
177#endif // !COIN_LISTS_SBPLIST_H
The SbList class is a template container class for lists.
Definition SbList.h:74
void truncate(const int length, const int dofit=0)
Definition SbList.h:195
int getLength(void) const
Definition SbList.h:191
int find(const Type item) const
Definition SbList.h:146
SbList< Type > & operator=(const SbList< Type > &l)
Definition SbList.h:112
void removeFast(const int index)
Definition SbList.h:182
int operator!=(const SbList< Type > &l) const
Definition SbList.h:245
int getArraySize(void) const
Definition SbList.h:266
const Type * getArrayPtr(const int start=0) const
Definition SbList.h:216
void remove(const int index)
Definition SbList.h:172
void removeItem(const Type item)
Definition SbList.h:164
int operator==(const SbList< Type > &l) const
Definition SbList.h:237
void copy(const SbList< Type > &l)
Definition SbList.h:96
void fit(void)
Definition SbList.h:117
void insert(const Type item, const int insertbefore)
Definition SbList.h:152
void append(const Type item)
Definition SbList.h:140
void expand(const int size)
Definition SbList.h:258
Type operator[](const int index) const
Definition SbList.h:223
The SbPList class is a container class for void pointers.
Definition lists/SbPList.h:40
void ** getArrayPtr(const int start=0) const
Definition lists/SbPList.h:121
void set(const int index, void *item)
Definition lists/SbPList.h:155
void expand(const int size)
Definition lists/SbPList.h:164
void * get(const int index) const
Definition lists/SbPList.h:146
int operator!=(const SbPList &l) const
Definition lists/SbPList.h:140
void append(void *item)
Definition lists/SbPList.h:89
int getLength(void) const
Definition lists/SbPList.h:105
void fit(void)
Definition SbPList.cpp:216
void removeFast(const int index)
Definition lists/SbPList.h:96
void *& operator[](const int index) const
Definition lists/SbPList.h:130
int getArraySize(void) const
Definition lists/SbPList.h:171
void truncate(const int length, const int fit=0)
Definition lists/SbPList.h:111