HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 
21 #include <SYS/SYS_Types.h>
22 
23 #include <string.h>
24 
25 
26 #define GA_TEMP_BUFFER_SIZE 32
27 
28 /// Template class to hold an array of temporary objects.
29 ///
30 /// These arrays have a temporary buffer. When the buffer fills, the array
31 /// will grow by allocating off the heap. The buffers are implemented for
32 /// simple POD objects.
33 template <typename T>
34 class GA_TempBuffer
35 {
36 public:
38  : myData(myBuffer)
39  , mySize(0)
40  , myCapacity(GA_TEMP_BUFFER_SIZE)
41  {}
43  {
44  clear();
45  }
46 
47  /// Clear all entries
48  void clear()
49  {
50  if (myData != myBuffer)
51  {
52  free(myData);
53  }
54  myData = myBuffer;
55  mySize = 0;
56  myCapacity = GA_TEMP_BUFFER_SIZE;
57  }
58 
59  /// How many elements in the buffer
60  GA_Size entries() const { return mySize; }
61 
62  /// Query the reserved capacity
63  GA_Size getCapacity() const { return myCapacity; }
64 
65  /// Reserve storage
66  void reserve(GA_Size size, bool setsize=false)
67  {
68  if (size > myCapacity)
69  growBuffer(size);
70  if (setsize)
71  mySize = size;
72  UT_ASSERT(myCapacity >= size);
73  }
74 
75  /// Append an element to the array
76  void append(const T &value)
77  {
78  if (mySize == myCapacity)
79  growBuffer();
80  myData[mySize] = value;
81  mySize++;
82  }
83  /// Set an item in the buffer to the given value
84  void setValue(GA_Size i, const T &value)
85  {
86  UT_ASSERT_P(i >= 0 && i < mySize);
87  myData[i] = value;
88  }
89  /// @{
90  /// Return the value from the array
91  const T &getValue(GA_Size i) const
92  {
93  UT_ASSERT_P(i >= 0 && i < mySize);
94  return myData[i];
95  }
96  const T &operator()(GA_Size i) const { return getValue(i); }
97  const T &last() const
98  {
99  UT_ASSERT_P(mySize > 0);
100  return myData[mySize-1];
101  }
102  /// @}
103 
104  /// Return the last entry and remove it from the list
105  const T &removeLast()
106  {
107  UT_ASSERT_P(mySize > 0);
108  mySize--;
109  return myData[mySize];
110  }
111  void truncate(GA_Size sz)
112  {
113  mySize = SYSmin(mySize, sz);
114  }
116  {
117  exint s, d;
118  exint nrm = 0;
119  for (s = d = 0; s < mySize; ++s)
120  {
121  if (myData[s] != value)
122  {
123  if (s != d)
124  myData[d] = myData[s];
125  d++;
126  }
127  else
128  nrm++;
129  }
130  mySize = d;
131  return nrm;
132  }
134  {
135  UT_ASSERT_P(index >= 0 && index < mySize);
136  for (exint i = index+1; i < mySize; ++i)
137  myData[i-1] = myData;
138  mySize--;
139  }
140 
141  const T *data() const { return myData; }
143  {
144  reserve(size);
145  return myData;
146  }
147 
148  void swap(GA_Size i0, GA_Size i1)
149  {
150  UT_ASSERT(i0 >= 0 && i0 < mySize);
151  UT_ASSERT(i1 >= 0 && i1 < mySize);
152  UTswap(myData[i0], myData[i1]);
153  }
154 
155 private:
156  void growBuffer(GA_Size nsize=-1)
157  {
158  // Always grow at least to the next UTbumpAlloc
159  nsize = SYSmax(nsize, UTbumpAlloc(myCapacity+1));
160  if (myData == myBuffer)
161  {
162  UT_ASSERT_P(myCapacity == GA_TEMP_BUFFER_SIZE);
163  myData = (T *)malloc(sizeof(T)*nsize);
164  memcpy(myData, myBuffer, sizeof(T)*myCapacity);
165  }
166  else
167  {
168  myData = (T *)realloc(myData, sizeof(T)*nsize);
169  }
170  myCapacity = nsize;
171  }
172  T *myData;
173  T myBuffer[GA_TEMP_BUFFER_SIZE];
174  GA_Size mySize, myCapacity;
175 };
176 
177 #endif
#define SYSmax(a, b)
Definition: SYS_Math.h:1365
void setValue(GA_Size i, const T &value)
Set an item in the buffer to the given value.
Definition: GA_TempBuffer.h:84
void truncate(GA_Size sz)
void UTswap(T &a, T &b)
Definition: UT_Swap.h:35
GA_Size entries() const
How many elements in the buffer.
Definition: GA_TempBuffer.h:60
const T & last() const
Definition: GA_TempBuffer.h:97
png_uint_32 i
Definition: png.h:2877
exint GA_Size
Defines the bit width for index and offset types in GA.
Definition: GA_Types.h:211
GLsizeiptr size
Definition: glcorearb.h:663
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:101
void clear()
Clear all entries.
Definition: GA_TempBuffer.h:48
const T & getValue(GA_Size i) const
Definition: GA_TempBuffer.h:91
exint removeMatchingItems(const T &value)
#define UT_ASSERT(ZZ)
Definition: UT_Assert.h:102
T * data(GA_Size size)
int64 exint
Definition: SYS_Types.h:115
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:63
GLsizei const GLfloat * value
Definition: glcorearb.h:823
void removeIndex(exint index)
GLuint index
Definition: glcorearb.h:785
const T * data() const
void reserve(GA_Size size, bool setsize=false)
Reserve storage.
Definition: GA_TempBuffer.h:66
void swap(GA_Size i0, GA_Size i1)
const T & operator()(GA_Size i) const
Definition: GA_TempBuffer.h:96
#define SYSmin(a, b)
Definition: SYS_Math.h:1366
void append(const T &value)
Append an element to the array.
Definition: GA_TempBuffer.h:76
#define GA_TEMP_BUFFER_SIZE
Definition: GA_TempBuffer.h:26