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 void expandindex(const int index) const;
76 void expandlist(const int size) const;
77 void grow(const int size = -1);
78
79 int itembuffersize;
80 int numitems;
81 void ** itembuffer;
82 void * builtinbuffer[DEFAULTSIZE];
83};
84
85/* inlined methods ********************************************************/
86
87inline void
89{
90 if (this->numitems == this->itembuffersize) this->grow();
91 this->itembuffer[this->numitems++] = item;
92}
93
94inline void
95SbPList::removeFast(const int index)
96{
97#ifdef COIN_EXTRA_DEBUG
98 assert(index >= 0 && index < this->numitems);
99#endif // COIN_EXTRA_DEBUG
100 this->itembuffer[index] = this->itembuffer[--this->numitems];
101}
102
103inline int
105{
106 return this->numitems;
107}
108
109inline void
110SbPList::truncate(const int length, const int dofit)
111{
112#ifdef COIN_EXTRA_DEBUG
113 assert(length <= this->numitems);
114#endif // COIN_EXTRA_DEBUG
115 this->numitems = length;
116 if (dofit) this->fit();
117}
118
119inline void **
120SbPList::getArrayPtr(const int start) const
121{
122#ifdef COIN_EXTRA_DEBUG
123 assert(start >= 0 && start < this->numitems);
124#endif // COIN_EXTRA_DEBUG
125 return &this->itembuffer[start];
126}
127
128inline void *&
129SbPList::operator[](const int index) const
130{
131#ifdef COIN_EXTRA_DEBUG
132 assert(index >= 0);
133#endif // COIN_EXTRA_DEBUG
134 if (index >= this->getLength()) this->expandindex(index);
135 return this->itembuffer[index];
136}
137
138inline int
140{
141 return !(*this == l);
142}
143
144inline void *
145SbPList::get(const int index) const
146{
147 return this->itembuffer[index];
148}
149
150inline void
151SbPList::set(const int index, void * item)
152{
153 this->itembuffer[index] = item;
154}
155
156inline void
157SbPList::expand(const int size)
158{
159 this->grow(size);
160 this->numitems = size;
161}
162
163inline int
165{
166 return this->itembuffersize;
167}
168
169
170#endif // !COIN_LISTS_SBPLIST_H
The SbList class is a template container class for lists.
Definition SbList.h:72
void truncate(const int length, const int dofit=0)
Definition SbList.h:180
int getLength(void) const
Definition SbList.h:176
int find(const Type item) const
Definition SbList.h:134
SbList< Type > & operator=(const SbList< Type > &l)
Definition SbList.h:101
void removeFast(const int index)
Definition SbList.h:169
int operator!=(const SbList< Type > &l) const
Definition SbList.h:225
int getArraySize(void) const
Definition SbList.h:243
const Type * getArrayPtr(const int start=0) const
Definition SbList.h:199
void remove(const int index)
Definition SbList.h:160
void removeItem(const Type item)
Definition SbList.h:152
int operator==(const SbList< Type > &l) const
Definition SbList.h:217
void copy(const SbList< Type > &l)
Definition SbList.h:94
void fit(void)
Definition SbList.h:106
void insert(const Type item, const int insertbefore)
Definition SbList.h:140
void append(const Type item)
Definition SbList.h:129
void expand(const int size)
Definition SbList.h:238
Type operator[](const int index) const
Definition SbList.h:203
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:120
void set(const int index, void *item)
Definition lists/SbPList.h:151
void expand(const int size)
Definition lists/SbPList.h:157
void * get(const int index) const
Definition lists/SbPList.h:145
int operator!=(const SbPList &l) const
Definition lists/SbPList.h:139
void append(void *item)
Definition lists/SbPList.h:88
int getLength(void) const
Definition lists/SbPList.h:104
void fit(void)
Definition SbPList.cpp:205
void removeFast(const int index)
Definition lists/SbPList.h:95
void *& operator[](const int index) const
Definition lists/SbPList.h:129
int getArraySize(void) const
Definition lists/SbPList.h:164
void truncate(const int length, const int fit=0)
Definition lists/SbPList.h:110