HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_DANumeric.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: GT_DANumeric.h ( GT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GT_DANumeric__
12 #define __GT_DANumeric__
13 
14 #include "GT_API.h"
15 #include "GT_DataArray.h"
16 #include "GT_Memory.h"
17 #include <UT/UT_Assert.h>
18 #include <UT/UT_ArrayHelp.h>
19 #include <string.h>
20 
21 /// @brief An array of numeric values (int32, int64, fpreal16, fpreal32, fpreal64)
22 template <typename T>
23 class GT_DANumeric : public GT_DataArray
24 {
25 public:
26  typedef T data_type;
27 
28  /// Create a numeric array
29  GT_DANumeric(GT_Size array_size, int tuple_size, GT_Type type=GT_TYPE_NONE)
30  : myData(NULL)
31  , myTupleSize(tuple_size)
32  , myType(type)
33  , myCapacity(0)
34  , mySize(0)
35  {
36  resize(array_size);
37  }
38  /// Create a numeric array, copying values from the source data. The data
39  /// array does @b not hold a reference to the source data, it's copied.
40  GT_DANumeric(const T *data, GT_Size array_size, int tuple_size,
42  : myData(NULL)
43  , myTupleSize(tuple_size)
44  , myType(type)
45  , myCapacity(0)
46  , mySize(0)
47  {
48  resize(array_size);
49  copyFrom(data);
50  }
51  virtual ~GT_DANumeric()
52  {
53  resize(0);
54  }
55 
56  virtual const char *className() const { return "GT_DANumeric"; }
57 
58  /// A numeric array is hard to begin with
59  virtual GT_DataArrayHandle harden() const
60  {
61  GT_DataArray *me;
62  me = const_cast<
63  GT_DANumeric<T> *>(this);
64  return GT_DataArrayHandle(me);
65  }
66 
67  void truncate()
68  {
69  if (mySize != myCapacity)
70  resize(mySize);
71  }
72 
73  void resize(GT_Size size, GT_Size capacity = -1)
74  {
75  if (capacity < 0)
76  capacity = size;
77 
78  if (!capacity)
79  {
80  GT_Size bytes = myCapacity * myTupleSize * sizeof(T);
81  GT_Memory::Free(GT_MEM_DATA, myData, bytes);
82  myData = NULL;
83  }
84  else
85  {
86  GT_Size obytes = myCapacity * myTupleSize * sizeof(T);
87  GT_Size nbytes = capacity * myTupleSize * sizeof(T);
88  myData = (T *)GT_Memory::Realloc(GT_MEM_DATA,
89  myData, obytes, nbytes);
90  }
91  mySize = size;
92  myCapacity = capacity;
93  }
94 
95  /// Append a scalar value to the array
96  void append(T value)
97  {
98  if (mySize == myCapacity)
99  grow();
100  mySize++;
101  set(value, mySize-1);
102  }
103  void append(const T *value)
104  {
105  if (mySize == myCapacity)
106  grow();
107  mySize++;
108  setTuple(value, mySize-1);
109  }
111  {
112  UT_ASSERT_P(src.myTupleSize == myTupleSize);
113  UT_ASSERT_P(src.myType == myType);
114  if (src.mySize)
115  {
116  // Ensure we have enough room
117  if (mySize + src.mySize > myCapacity)
118  grow(mySize + src.mySize);
119  memcpy(myData + mySize*myTupleSize,
120  src.myData,
121  src.mySize*src.myTupleSize*sizeof(T));
122  mySize += src.mySize;
123  }
124  }
125 
126  /// Raw access to the data array
127  T *data() const { return myData; }
128  /// Raw pointer access to a tuple
130  {
131  UT_ASSERT_P(offset >= 0 && offset < mySize);
132  offset *= myTupleSize;
133  return myData + offset;
134  }
135  /// Raw pointer access to a tuple
136  const T *getData(GT_Offset offset) const
137  {
138  UT_ASSERT_P(offset >= 0 && offset < mySize);
139  offset *= myTupleSize;
140  return myData + offset;
141  }
142  /// Set a component of the tuple
143  void set(T value, GT_Offset offset, int index=0)
144  {
145  offset = offset * myTupleSize + index;
146  UT_ASSERT_P(offset>=0 && offset<mySize*myTupleSize);
147  myData[offset] = value;
148  }
149  /// Set an entire tuple
151  {
152  UT_ASSERT_P(offset>=0 && offset<mySize);
153  offset = offset * myTupleSize;
154  memcpy(myData + offset, value, myTupleSize*sizeof(T));
155  }
156  /// Set a block of entire tuples
158  {
159  if (values->mySize)
160  {
161  UT_ASSERT_P(offset>=0 && offset<mySize);
162  offset = offset * myTupleSize;
163  memcpy(myData + offset, values->myData, values->mySize*myTupleSize*sizeof(T));
164  }
165  }
166  /// Set a block of entire tuples
168  {
169  if (n)
170  {
171  UT_ASSERT_P(offset>=0 && offset<mySize);
172  offset = offset * myTupleSize;
173  memcpy(myData + offset, values, n*myTupleSize*sizeof(T));
174  }
175  }
176  /// Copy an entire data from a flat array
177  void copyFrom(const T *src)
178  {
179  if (src)
180  {
181  memcpy(myData, src, myTupleSize*mySize*sizeof(T));
182  }
183  else UT_ASSERT_P(mySize == 0);
184  }
185 
186  /// @{
187  /// Methods defined on GT_DataArray
188  virtual GT_Storage getStorage() const { return GTstorage<T>(); }
189  virtual GT_Size getTupleSize() const { return myTupleSize; }
190  virtual GT_Size entries() const { return mySize; }
191  virtual GT_Type getTypeInfo() const { return myType; }
192  virtual int64 getMemoryUsage() const
193  { return sizeof(T)*mySize*myTupleSize; }
194 
195  virtual uint8 getU8(GT_Offset offset, int index=0) const
196  {
197  offset = offset * myTupleSize + index;
198  UT_ASSERT_P(offset>=0 && offset<mySize*myTupleSize);
199  return (uint8)myData[offset];
200  }
201  virtual int32 getI32(GT_Offset offset, int index=0) const
202  {
203  offset = offset * myTupleSize + index;
204  UT_ASSERT_P(offset>=0 && offset<mySize*myTupleSize);
205  return (int32)myData[offset];
206  }
207  virtual int64 getI64(GT_Offset offset, int index=0) const
208  {
209  offset = offset * myTupleSize + index;
210  UT_ASSERT_P(offset>=0 && offset<mySize*myTupleSize);
211  return (int64)myData[offset];
212  }
213  virtual fpreal16 getF16(GT_Offset offset, int index=0) const
214  {
215  offset = offset * myTupleSize + index;
216  UT_ASSERT_P(offset>=0 && offset<mySize*myTupleSize);
217  return (fpreal16)myData[offset];
218  }
219  virtual fpreal32 getF32(GT_Offset offset, int index=0) const
220  {
221  offset = offset * myTupleSize + index;
222  UT_ASSERT_P(offset>=0 && offset<mySize*myTupleSize);
223  return (fpreal32)myData[offset];
224  }
225  virtual fpreal64 getF64(GT_Offset offset, int index=0) const
226  {
227  offset = offset * myTupleSize + index;
228  UT_ASSERT_P(offset>=0 && offset<mySize*myTupleSize);
229  return (fpreal64)myData[offset];
230  }
231  virtual GT_String getS(GT_Offset, int) const { return NULL; }
232  virtual GT_Size getStringIndexCount() const { return -1; }
233  virtual GT_Offset getStringIndex(GT_Offset, int) const { return -1; }
235  UT_IntArray &) const {}
236 
237  virtual const uint8 *getU8Array(GT_DataArrayHandle &buffer) const
238  {
239  if (SYSisSame<T, uint8>())
240  return reinterpret_cast<const uint8 *>(data());
241  return GT_DataArray::getU8Array(buffer);
242  }
243  virtual const int8 *getI8Array(GT_DataArrayHandle &buffer) const
244  {
245  if (SYSisSame<T, int8>())
246  return reinterpret_cast<const int8 *>(data());
247  return GT_DataArray::getI8Array(buffer);
248  }
250  {
251  if (SYSisSame<T, int16>())
252  return reinterpret_cast<const int16 *>(data());
253  return GT_DataArray::getI16Array(buffer);
254  }
256  {
257  if (SYSisSame<T, int32>())
258  return reinterpret_cast<const int32 *>(data());
259  return GT_DataArray::getI32Array(buffer);
260  }
262  {
263  if (SYSisSame<T, int64>())
264  return reinterpret_cast<const int64 *>(data());
265  return GT_DataArray::getI64Array(buffer);
266  }
268  {
269  if (SYSisSame<T, fpreal16>())
270  return reinterpret_cast<const fpreal16 *>(data());
271  return GT_DataArray::getF16Array(buffer);
272  }
274  {
275  if (SYSisSame<T, fpreal32>())
276  return reinterpret_cast<const fpreal32 *>(data());
277  return GT_DataArray::getF32Array(buffer);
278  }
280  {
281  if (SYSisSame<T, fpreal64>())
282  return reinterpret_cast<const fpreal64 *>(data());
283  return GT_DataArray::getF64Array(buffer);
284  }
285 
286  virtual void doImport(GT_Offset idx, uint8 *data, GT_Size size) const
287  { importTuple<uint8>(idx, data, size); }
288  virtual void doImport(GT_Offset idx, int8 *data, GT_Size size) const
289  { importTuple<int8>(idx, data, size); }
290  virtual void doImport(GT_Offset idx, int16 *data, GT_Size size) const
291  { importTuple<int16>(idx, data, size); }
292  virtual void doImport(GT_Offset idx, int32 *data, GT_Size size) const
293  { importTuple<int32>(idx, data, size); }
294  virtual void doImport(GT_Offset idx, int64 *data, GT_Size size) const
295  { importTuple<int64>(idx, data, size); }
296  virtual void doImport(GT_Offset idx, fpreal16 *data, GT_Size size) const
297  { importTuple<fpreal16>(idx, data, size); }
298  virtual void doImport(GT_Offset idx, fpreal32 *data, GT_Size size) const
299  { importTuple<fpreal32>(idx, data, size); }
300  virtual void doImport(GT_Offset idx, fpreal64 *data, GT_Size size) const
301  { importTuple<fpreal64>(idx, data, size); }
302 
304  int tsize, int stride) const
305  { t_NumericFill(data, start, length, tsize, stride); }
307  int tsize, int stride) const
308  { t_NumericFill(data, start, length, tsize, stride); }
310  int tsize, int stride) const
311  { t_NumericFill(data, start, length, tsize, stride); }
313  int tsize, int stride) const
314  { t_NumericFill(data, start, length, tsize, stride); }
316  int tsize, int stride) const
317  { t_NumericFill(data, start, length, tsize, stride); }
319  int tsize, int stride) const
320  { t_NumericFill(data, start, length, tsize, stride); }
322  int tsize, int stride) const
323  { t_NumericFill(data, start, length, tsize, stride); }
325  int tsize, int stride) const
326  { t_NumericFill(data, start, length, tsize, stride); }
327  /// @}
328 
329 private:
330  void grow(exint sz=-1)
331  {
332  exint obytes = myCapacity * myTupleSize * sizeof(T);
333  myCapacity = sz < 0 ? UTbumpAlloc(myCapacity+1) : sz;
334  exint nbytes = myCapacity * myTupleSize * sizeof(T);
335  myData = (T *)GT_Memory::Realloc(GT_MEM_DATA,
336  myData, obytes, nbytes);
337  }
338  template <typename T_POD> inline void
339  importTuple(GT_Offset idx, T_POD *data, GT_Size tsize) const
340  {
341  if (tsize < 1)
342  tsize = myTupleSize;
343  else
344  tsize = SYSmin(tsize, myTupleSize);
345  const T *src = myData + idx*myTupleSize;
346  for (int i = 0; i < tsize; ++i)
347  data[i] = (T_POD)src[i];
348  }
349 
350  inline void
351  t_NumericFill(T *dest, GT_Offset start, GT_Size length,
352  int tsize, int stride) const
353  {
354  if (tsize < 1)
355  tsize = myTupleSize;
356  stride = SYSmax(stride, tsize);
357  int n = SYSmin(tsize, myTupleSize);
358  if (n == myTupleSize && stride == myTupleSize)
359  {
360  memcpy(dest, myData+start*myTupleSize,
361  length*n*sizeof(T));
362  }
363  else
364  {
365  const T *src = myData+start*myTupleSize;
366  for (GT_Offset i = 0; i < length; ++i,
367  src += myTupleSize, dest += stride)
368  {
369  for (int j = 0; j < n; ++j)
370  dest[j] = src[j];
371  }
372  }
373  }
374 
375  template <typename T_POD> inline void
376  t_NumericFill(T_POD *dest, GT_Offset start, GT_Size length,
377  int tsize, int stride) const
378  {
379  if (tsize < 1)
380  tsize = myTupleSize;
381  stride = SYSmax(stride, tsize);
382  int n = SYSmin(tsize, myTupleSize);
383  const T *src = myData+start*myTupleSize;
384  for (GT_Offset i = 0; i < length; ++i,
385  src += myTupleSize, dest += stride)
386  {
387  for (int j = 0; j < n; ++j)
388  dest[j] = (T_POD)src[j];
389  }
390  }
391 
392  T *myData;
393  GT_Size mySize;
394  GT_Size myCapacity;
395  int myTupleSize;
396  GT_Type myType;
397 };
408 
409 /// Return either an int64 or int32 array based on the contents of the
410 /// array. That is, only allocate a 64 bit integer array if it's needed.
411 /// For tuple sizes > 1, this function assumes that the array is an array
412 /// of structs, rather than a struct of arrays.
413 GT_API extern GT_DataArray *GTallocateIntArray(const int64 *array,
414  GT_Size size, int tuple_size=1);
415 GT_API extern GT_DataArray *GTallocateIntArray(const int32 *array,
416  GT_Size size, int tuple_size=1);
417 
418 #endif
virtual void doImport(GT_Offset idx, fpreal16 *data, GT_Size size) const
Definition: GT_DANumeric.h:296
virtual const int64 * getI64Array(GT_DataArrayHandle &buffer) const
Definition: GT_DANumeric.h:261
#define SYSmax(a, b)
Definition: SYS_Math.h:1367
GT_Storage
Definition: GT_Types.h:17
virtual int32 getI32(GT_Offset offset, int index=0) const
Definition: GT_DANumeric.h:201
GT_API GT_DataArray * GTallocateIntArray(const int64 *array, GT_Size size, int tuple_size=1)
virtual GT_Type getTypeInfo() const
Definition: GT_DANumeric.h:191
void append(const T *value)
Definition: GT_DANumeric.h:103
virtual void doFillArray(fpreal32 *data, GT_Offset start, GT_Size length, int tsize, int stride) const
Definition: GT_DANumeric.h:321
virtual void doImport(GT_Offset idx, int8 *data, GT_Size size) const
Definition: GT_DANumeric.h:288
virtual void doImport(GT_Offset idx, int16 *data, GT_Size size) const
Definition: GT_DANumeric.h:290
virtual const uint8 * getU8Array(GT_DataArrayHandle &buffer) const
Definition: GT_DANumeric.h:237
GLuint start
Definition: glcorearb.h:474
const char * GT_String
Definition: GT_Types.h:111
virtual void doFillArray(fpreal16 *data, GT_Offset start, GT_Size length, int tsize, int stride) const
Definition: GT_DANumeric.h:318
virtual int64 getMemoryUsage() const
Definition: GT_DANumeric.h:192
virtual const fpreal32 * getF32Array(GT_DataArrayHandle &buffer) const
virtual void doImport(GT_Offset idx, fpreal64 *data, GT_Size size) const
Definition: GT_DANumeric.h:300
#define GT_API
Definition: GT_API.h:11
GT_Type
Definition: GT_Types.h:33
virtual fpreal64 getF64(GT_Offset offset, int index=0) const
Definition: GT_DANumeric.h:225
virtual void doImport(GT_Offset idx, int64 *data, GT_Size size) const
Definition: GT_DANumeric.h:294
virtual void doImport(GT_Offset idx, int32 *data, GT_Size size) const
Definition: GT_DANumeric.h:292
virtual ~GT_DANumeric()
Definition: GT_DANumeric.h:51
T * data() const
Raw access to the data array.
Definition: GT_DANumeric.h:127
T * getData(GT_Offset offset)
Raw pointer access to a tuple.
Definition: GT_DANumeric.h:129
virtual const fpreal64 * getF64Array(GT_DataArrayHandle &buffer) const
GLuint buffer
Definition: glcorearb.h:659
png_uint_32 i
Definition: png.h:2877
GLsizeiptr size
Definition: glcorearb.h:663
virtual const int16 * getI16Array(GT_DataArrayHandle &buffer) const
virtual const char * className() const
Definition: GT_DANumeric.h:56
virtual GT_Size entries() const
Definition: GT_DANumeric.h:190
virtual GT_Size getTupleSize() const
Definition: GT_DANumeric.h:189
signed char int8
Definition: SYS_Types.h:31
GT_DANumeric< uint8 > GT_Unsigned8Array
Definition: GT_DANumeric.h:398
virtual const fpreal16 * getF16Array(GT_DataArrayHandle &buffer) const
Definition: GT_DANumeric.h:267
long long int64
Definition: SYS_Types.h:107
virtual const fpreal16 * getF16Array(GT_DataArrayHandle &buffer) const
GLdouble n
Definition: glcorearb.h:2007
Abstract data class for an array of float, int or string data.
Definition: GT_DataArray.h:38
UT_IntrusivePtr< GT_DataArray > GT_DataArrayHandle
Definition: GT_DataArray.h:30
int64 exint
Definition: SYS_Types.h:116
virtual const fpreal64 * getF64Array(GT_DataArrayHandle &buffer) const
Definition: GT_DANumeric.h:279
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:125
virtual void doFillArray(uint8 *data, GT_Offset start, GT_Size length, int tsize, int stride) const
Definition: GT_DANumeric.h:303
double fpreal64
Definition: SYS_Types.h:192
const T * getData(GT_Offset offset) const
Raw pointer access to a tuple.
Definition: GT_DANumeric.h:136
virtual void doFillArray(int64 *data, GT_Offset start, GT_Size length, int tsize, int stride) const
Definition: GT_DANumeric.h:315
GLint GLenum GLboolean GLsizei stride
Definition: glcorearb.h:871
virtual GT_Size getStringIndexCount() const
Definition: GT_DANumeric.h:232
virtual int64 getI64(GT_Offset offset, int index=0) const
Definition: GT_DANumeric.h:207
GLintptr offset
Definition: glcorearb.h:664
virtual fpreal16 getF16(GT_Offset offset, int index=0) const
Definition: GT_DANumeric.h:213
virtual const int8 * getI8Array(GT_DataArrayHandle &buffer) const
Definition: GT_DANumeric.h:243
virtual void doFillArray(int32 *data, GT_Offset start, GT_Size length, int tsize, int stride) const
Definition: GT_DANumeric.h:312
virtual void doImport(GT_Offset idx, uint8 *data, GT_Size size) const
Definition: GT_DANumeric.h:286
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1601
virtual const int8 * getI8Array(GT_DataArrayHandle &buffer) const
GLboolean * data
Definition: glcorearb.h:130
void resize(GT_Size size, GT_Size capacity=-1)
Definition: GT_DANumeric.h:73
virtual GT_Offset getStringIndex(GT_Offset, int) const
Definition: GT_DANumeric.h:233
int int32
Definition: SYS_Types.h:35
GT_DANumeric(GT_Size array_size, int tuple_size, GT_Type type=GT_TYPE_NONE)
Create a numeric array.
Definition: GT_DANumeric.h:29
virtual const int16 * getI16Array(GT_DataArrayHandle &buffer) const
Definition: GT_DANumeric.h:249
virtual GT_String getS(GT_Offset, int) const
Definition: GT_DANumeric.h:231
void truncate()
Definition: GT_DANumeric.h:67
GLsizei const GLfloat * value
Definition: glcorearb.h:823
void setTuple(const T *value, GT_Offset offset)
Set an entire tuple.
Definition: GT_DANumeric.h:150
virtual GT_DataArrayHandle harden() const
A numeric array is hard to begin with.
Definition: GT_DANumeric.h:59
void set(T value, GT_Offset offset, int index=0)
Set a component of the tuple.
Definition: GT_DANumeric.h:143
unsigned char uint8
Definition: SYS_Types.h:32
GT_DANumeric(const T *data, GT_Size array_size, int tuple_size, GT_Type type=GT_TYPE_NONE)
Definition: GT_DANumeric.h:40
virtual GT_Storage getStorage() const
Definition: GT_DANumeric.h:188
void concat(const GT_DANumeric< T > &src)
Definition: GT_DANumeric.h:110
GLuint index
Definition: glcorearb.h:785
virtual const int64 * getI64Array(GT_DataArrayHandle &buffer) const
void copyFrom(const T *src)
Copy an entire data from a flat array.
Definition: GT_DANumeric.h:177
virtual void getIndexedStrings(UT_StringArray &, UT_IntArray &) const
Definition: GT_DANumeric.h:234
virtual void doFillArray(int16 *data, GT_Offset start, GT_Size length, int tsize, int stride) const
Definition: GT_DANumeric.h:309
GLint GLint GLsizei GLint GLenum GLenum type
Definition: glcorearb.h:107
virtual void doFillArray(fpreal64 *data, GT_Offset start, GT_Size length, int tsize, int stride) const
Definition: GT_DANumeric.h:324
void append(T value)
Append a scalar value to the array.
Definition: GT_DANumeric.h:96
An array of numeric values (int32, int64, fpreal16, fpreal32, fpreal64)
Definition: GT_DANumeric.h:23
virtual const int32 * getI32Array(GT_DataArrayHandle &buffer) const
Definition: GT_DANumeric.h:255
virtual const uint8 * getU8Array(GT_DataArrayHandle &buffer) const
void setTupleBlock(const T *values, GT_Size n, GT_Offset offset)
Set a block of entire tuples.
Definition: GT_DANumeric.h:167
virtual fpreal32 getF32(GT_Offset offset, int index=0) const
Definition: GT_DANumeric.h:219
void setTupleBlock(const GT_DANumeric< T > *values, GT_Offset offset)
Set a block of entire tuples.
Definition: GT_DANumeric.h:157
int64 GT_Offset
Definition: GT_Types.h:113
int64 GT_Size
Definition: GT_Types.h:112
short int16
Definition: SYS_Types.h:33
virtual void doFillArray(int8 *data, GT_Offset start, GT_Size length, int tsize, int stride) const
Definition: GT_DANumeric.h:306
#define SYSmin(a, b)
Definition: SYS_Math.h:1368
float fpreal32
Definition: SYS_Types.h:191
virtual void doImport(GT_Offset idx, fpreal32 *data, GT_Size size) const
Definition: GT_DANumeric.h:298
virtual uint8 getU8(GT_Offset offset, int index=0) const
Definition: GT_DANumeric.h:195
virtual const fpreal32 * getF32Array(GT_DataArrayHandle &buffer) const
Definition: GT_DANumeric.h:273
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:794
virtual const int32 * getI32Array(GT_DataArrayHandle &buffer) const
GLenum src
Definition: glcorearb.h:1792