Coin Logo Coin3D is Free Software,
published under the BSD 3-clause license.
https://bitbucket.org/Coin3D/
http://www.kongsberg.com/kogt/
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 
40 class COIN_DLL_API SbPList {
41  enum { DEFAULTSIZE = 4 };
42 
43 public:
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 
69 protected:
70 
71  void expand(const int size);
72  int getArraySize(void) const;
73 
74 private:
75  void expandlist(const int size) const;
76  void grow(const int size = -1);
77 
78  int itembuffersize;
79  int numitems;
80  void ** itembuffer;
81  void * builtinbuffer[DEFAULTSIZE];
82 };
83 
84 /* inlined methods ********************************************************/
85 
86 inline void
87 SbPList::append(void * item)
88 {
89  if (this->numitems == this->itembuffersize) this->grow();
90  this->itembuffer[this->numitems++] = item;
91 }
92 
93 inline void
94 SbPList::removeFast(const int index)
95 {
96 #ifdef COIN_EXTRA_DEBUG
97  assert(index >= 0 && index < this->numitems);
98 #endif // COIN_EXTRA_DEBUG
99  this->itembuffer[index] = this->itembuffer[--this->numitems];
100 }
101 
102 inline int
103 SbPList::getLength(void) const
104 {
105  return this->numitems;
106 }
107 
108 inline void
109 SbPList::truncate(const int length, const int dofit)
110 {
111 #ifdef COIN_EXTRA_DEBUG
112  assert(length <= this->numitems);
113 #endif // COIN_EXTRA_DEBUG
114  this->numitems = length;
115  if (dofit) this->fit();
116 }
117 
118 inline void **
119 SbPList::getArrayPtr(const int start) const
120 {
121 #ifdef COIN_EXTRA_DEBUG
122  assert(start >= 0 && start < this->numitems);
123 #endif // COIN_EXTRA_DEBUG
124  return &this->itembuffer[start];
125 }
126 
127 inline void *&
128 SbPList::operator[](const int index) const
129 {
130 #ifdef COIN_EXTRA_DEBUG
131  assert(index >= 0);
132 #endif // COIN_EXTRA_DEBUG
133  if (index >= this->getLength()) this->expandlist(index + 1);
134  return this->itembuffer[index];
135 }
136 
137 inline int
138 SbPList::operator!=(const SbPList & l) const
139 {
140  return !(*this == l);
141 }
142 
143 inline void *
144 SbPList::get(const int index) const
145 {
146  return this->itembuffer[index];
147 }
148 
149 inline void
150 SbPList::set(const int index, void * item)
151 {
152  this->itembuffer[index] = item;
153 }
154 
155 inline void
156 SbPList::expand(const int size)
157 {
158  this->grow(size);
159  this->numitems = size;
160 }
161 
162 inline int
164 {
165  return this->itembuffersize;
166 }
167 
168 
169 #endif // !COIN_LISTS_SBPLIST_H
void fit(void)
Definition: SbPList.cpp:199
void removeFast(const int index)
Definition: lists/SbPList.h:94
void ** getArrayPtr(const int start=0) const
Definition: lists/SbPList.h:119
void * get(const int index) const
Definition: lists/SbPList.h:144
The SbPList class is a container class for void pointers.
Definition: lists/SbPList.h:40
void append(void *item)
Definition: lists/SbPList.h:87
int operator!=(const SbPList &l) const
Definition: lists/SbPList.h:138
void *& operator[](const int index) const
Definition: lists/SbPList.h:128
void set(const int index, void *item)
Definition: lists/SbPList.h:150
void expand(const int size)
Definition: lists/SbPList.h:156
void truncate(const int length, const int fit=0)
Definition: lists/SbPList.h:109
int getLength(void) const
Definition: lists/SbPList.h:103
int getArraySize(void) const
Definition: lists/SbPList.h:163