HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GA_TempBuffer.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: GA_TempBuffer.h ( GA Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GA_TempBuffer__
12 #define __GA_TempBuffer__
13 
14 #include "GA_API.h"
15 #include "GA_Types.h"
16 
17 #include <UT/UT_Algorithm.h>
18 #include <UT/UT_ArrayHelp.h>
19 #include <UT/UT_Assert.h>
20 #include <UT/UT_Swap.h>
21 
22 #include <SYS/SYS_Types.h>
23 
24 #include <string.h>
25 
26 
27 #define GA_TEMP_BUFFER_SIZE 32
28 
29 /// Template class to hold an array of temporary objects.
30 ///
31 /// These arrays have a temporary buffer. When the buffer fills, the array
32 /// will grow by allocating off the heap. The buffers are implemented for
33 /// simple POD objects.
34 template <typename T>
36 {
37 public:
39  : myData(myBuffer)
40  , mySize(0)
41  , myCapacity(GA_TEMP_BUFFER_SIZE)
42  {}
44  {
45  clear();
46  }
47 
48  /// Clear all entries
49  void clear()
50  {
51  if (myData != myBuffer)
52  {
53  free(myData);
54  }
55  myData = myBuffer;
56  mySize = 0;
57  myCapacity = GA_TEMP_BUFFER_SIZE;
58  }
59 
60  /// How many elements in the buffer
61  GA_Size entries() const { return mySize; }
62 
63  /// Query the reserved capacity
64  GA_Size getCapacity() const { return myCapacity; }
65 
66  /// Reserve storage
67  void reserve(GA_Size size, bool setsize=false)
68  {
69  if (size > myCapacity)
70  growBuffer(size);
71  if (setsize)
72  mySize = size;
73  UT_ASSERT(myCapacity >= size);
74  }
75 
76  /// Append an element to the array
77  void append(const T &value)
78  {
79  if (mySize == myCapacity)
80  growBuffer();
81  myData[mySize] = value;
82  mySize++;
83  }
84  /// Set an item in the buffer to the given value
85  void setValue(GA_Size i, const T &value)
86  {
87  UT_ASSERT_P(i >= 0 && i < mySize);
88  myData[i] = value;
89  }
90  /// @{
91  /// Return the value from the array
92  const T &getValue(GA_Size i) const
93  {
94  UT_ASSERT_P(i >= 0 && i < mySize);
95  return myData[i];
96  }
97  const T &operator()(GA_Size i) const { return getValue(i); }
98  const T &last() const
99  {
100  UT_ASSERT_P(mySize > 0);
101  return myData[mySize-1];
102  }
103  /// @}
104 
105  /// Return the last entry and remove it from the list
106  const T &removeLast()
107  {
108  UT_ASSERT_P(mySize > 0);
109  mySize--;
110  return myData[mySize];
111  }
112  void truncate(GA_Size sz)
113  {
114  mySize = SYSmin(mySize, sz);
115  }
117  {
118  exint s, d;
119  exint nrm = 0;
120  for (s = d = 0; s < mySize; ++s)
121  {
122  if (myData[s] != value)
123  {
124  if (s != d)
125  myData[d] = myData[s];
126  d++;
127  }
128  else
129  nrm++;
130  }
131  mySize = d;
132  return nrm;
133  }
135  {
136  UT_ASSERT_P(index >= 0 && index < mySize);
137  for (exint i = index+1; i < mySize; ++i)
138  myData[i-1] = myData;
139  mySize--;
140  }
141 
142  const T *data() const { return myData; }
144  {
145  reserve(size);
146  return myData;
147  }
148 
149  void swap(GA_Size i0, GA_Size i1)
150  {
151  UT_ASSERT(i0 >= 0 && i0 < mySize);
152  UT_ASSERT(i1 >= 0 && i1 < mySize);
153  UTswap(myData[i0], myData[i1]);
154  }
155 
156 private:
157  void growBuffer(GA_Size nsize=-1)
158  {
159  // Always grow at least to the next UTbumpAlloc
160  nsize = SYSmax(nsize, UTbumpAlloc(myCapacity+1));
161  if (myData == myBuffer)
162  {
163  UT_ASSERT_P(myCapacity == GA_TEMP_BUFFER_SIZE);
164  myData = (T *)malloc(sizeof(T)*nsize);
165  memcpy(myData, myBuffer, sizeof(T)*myCapacity);
166  }
167  else
168  {
169  myData = (T *)realloc(myData, sizeof(T)*nsize);
170  }
171  myCapacity = nsize;
172  }
173  T *myData;
174  T myBuffer[GA_TEMP_BUFFER_SIZE];
175  GA_Size mySize, myCapacity;
176 };
177 
178 #endif
#define SYSmax(a, b)
Definition: SYS_Math.h:1538
void setValue(GA_Size i, const T &value)
Set an item in the buffer to the given value.
Definition: GA_TempBuffer.h:85
void truncate(GA_Size sz)
void UTswap(T &a, T &b)
Definition: UT_Swap.h:35
GLsizei const GLfloat * value
Definition: glcorearb.h:824
int64 exint
Definition: SYS_Types.h:125
GLdouble s
Definition: glad.h:3009
GA_Size entries() const
How many elements in the buffer.
Definition: GA_TempBuffer.h:61
const T & last() const
Definition: GA_TempBuffer.h:98
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:235
void clear()
Clear all entries.
Definition: GA_TempBuffer.h:49
const T & getValue(GA_Size i) const
Definition: GA_TempBuffer.h:92
exint removeMatchingItems(const T &value)
T * data(GA_Size size)
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:155
GLint i1
Definition: glad.h:2724
const T & removeLast()
Return the last entry and remove it from the list.
GA_Size getCapacity() const
Query the reserved capacity.
Definition: GA_TempBuffer.h:64
GLsizeiptr size
Definition: glcorearb.h:664
void removeIndex(exint index)
GLuint index
Definition: glcorearb.h:786
const T * data() const
void reserve(GA_Size size, bool setsize=false)
Reserve storage.
Definition: GA_TempBuffer.h:67
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:156
Definition: core.h:1131
void swap(GA_Size i0, GA_Size i1)
const T & operator()(GA_Size i) const
Definition: GA_TempBuffer.h:97
#define SYSmin(a, b)
Definition: SYS_Math.h:1539
void append(const T &value)
Append an element to the array.
Definition: GA_TempBuffer.h:77
#define GA_TEMP_BUFFER_SIZE
Definition: GA_TempBuffer.h:27