HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_DAIndexedString.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_DAIndexedString.h ( GT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GT_DAIndexedString__
12 #define __GT_DAIndexedString__
13 
14 #include "GT_API.h"
15 #include "GT_Handles.h"
16 #include "GT_DataArray.h"
17 #include "GT_Memory.h"
18 #include <UT/UT_Assert.h>
19 #include <UT/UT_IndexedHashMapT.h>
20 
22 {
23 public:
24  GT_DAIndexedString(GT_Size array_size, int tuple_size=1);
25  virtual ~GT_DAIndexedString();
26 
27  virtual const char *className() const { return "GT_DAIndexedString"; }
28 
29  /// @{
30  /// Methods defined on GT_DataArray
31  /// An indexed string array is hard to begin with
32  virtual GT_DataArrayHandle harden() const
33  { return GT_DataArrayHandle(SYSconst_cast(this)); }
34  virtual GT_Storage getStorage() const { return GT_STORE_STRING; }
35  virtual GT_Size getTupleSize() const { return myTupleSize; }
36  virtual GT_Size entries() const { return mySize; }
37  virtual GT_Type getTypeInfo() const { return GT_TYPE_NONE; }
38  virtual int64 getMemoryUsage() const
39  {
40 #if 0
41  return myStrings.getMemoryUsage() +
42  sizeof(int32)*mySize*myTupleSize;
43 #else
44  return sizeof(int32)*mySize*myTupleSize;
45 #endif
46  }
47 
48  virtual uint8 getU8(GT_Offset offset, int index=0) const
49  {
50  UT_ASSERT_P(offset >= 0 && offset < mySize);
51  UT_ASSERT_P(index >= 0 && index < myTupleSize);
52  offset = offset*myTupleSize + index;
53  return myData[offset];
54  }
55  virtual int32 getI32(GT_Offset offset, int index=0) const
56  {
57  UT_ASSERT_P(offset >= 0 && offset < mySize);
58  UT_ASSERT_P(index >= 0 && index < myTupleSize);
59  offset = offset*myTupleSize + index;
60  return myData[offset];
61  }
62  virtual int64 getI64(GT_Offset offset, int index=0) const
63  {
64  UT_ASSERT_P(offset >= 0 && offset < mySize);
65  UT_ASSERT_P(index >= 0 && index < myTupleSize);
66  offset = offset*myTupleSize + index;
67  return myData[offset];
68  }
69  virtual fpreal16 getF16(GT_Offset offset, int index=0) const
70  {
71  UT_ASSERT_P(offset >= 0 && offset < mySize);
72  UT_ASSERT_P(index >= 0 && index < myTupleSize);
73  offset = offset*myTupleSize + index;
74  return myData[offset];
75  }
76  virtual fpreal32 getF32(GT_Offset offset, int index=0) const
77  {
78  UT_ASSERT_P(offset >= 0 && offset < mySize);
79  UT_ASSERT_P(index >= 0 && index < myTupleSize);
80  offset = offset*myTupleSize + index;
81  return myData[offset];
82  }
83  virtual fpreal64 getF64(GT_Offset offset, int index=0) const
84  {
85  UT_ASSERT_P(offset >= 0 && offset < mySize);
86  UT_ASSERT_P(index >= 0 && index < myTupleSize);
87  offset = offset*myTupleSize + index;
88  return myData[offset];
89  }
90  virtual GT_String getS(GT_Offset offset, int index) const
91  {
92  UT_ASSERT_P(offset >= 0 && offset < mySize);
93  UT_ASSERT_P(index >= 0 && index < myTupleSize);
94  // Find out which string
95  int sidx;
96  const UT_IndexedHashMapStringItem *item;
97  const char *str = "";
98  sidx = getI32(offset, index);
99  if (sidx >= 0 && (item = myStrings.get(sidx)))
100  str = item->getKey().getString();
101  return str;
102  }
103  virtual GT_Size getStringIndexCount() const
104  {
105  return myStrings.getItemIdUpperBound()+1;
106  }
107  virtual GT_Offset getStringIndex(GT_Offset offset, int idx) const
108  {
109  return getI32(offset, idx);
110  }
112  UT_IntArray &indices) const;
113  virtual void getStrings(UT_StringArray &strings) const;
114  /// @}
115 
117  {
118  if (!size)
119  {
120  GT_Memory::Free(GT_MEM_DATA, myData,
121  mySize*myTupleSize*sizeof(int32));
122  myStrings.clear();
123  }
124  else
125  {
126  for (GT_Size i = size; i < mySize; ++i)
127  {
128  for (GT_Size j = 0; j < myTupleSize; ++j)
129  freeString(i, j);
130  }
131  myData = (int32 *)GT_Memory::Realloc(GT_MEM_DATA,
132  myData,
133  mySize*myTupleSize*sizeof(int32),
134  size*myTupleSize*sizeof(int32));
135  for (GT_Size i = mySize; i < size; ++i)
136  {
137  for (GT_Size j = 0; j < myTupleSize; ++j)
138  myData[i*myTupleSize+j] = -1;
139  }
140  mySize = size;
141  }
142  }
143  /// Set an index to a value
144  void setString(GT_Size offset, int index, const char *str)
145  {
146  UT_ASSERT_P(offset >= 0 && offset < mySize);
147  UT_ASSERT_P(index >= 0 && index < myTupleSize);
148  if (!str || *str == 0)
149  freeString(offset, index);
150  else
151  {
152  int old_id;
154  offset = offset*myTupleSize + index;
155  old_id = myData[offset];
156  myStrings.add(str, NULL, &new_id);
157  if (old_id >= 0)
158  myStrings.remove(old_id);
159  myData[offset] = new_id;
160  }
161  }
162 private:
163  void freeString(GT_Size offset, int index)
164  {
165  UT_ASSERT_P(offset >= 0 && offset < mySize);
166  UT_ASSERT_P(index >= 0 && index < myTupleSize);
167  offset = offset*myTupleSize + index;
168  if (myData[offset] >= 0)
169  {
170  myStrings.remove(myData[offset]);
171  myData[offset] = -1;
172  }
173  }
174  UT_IndexedStringMap myStrings; // Shared string table
175  int32 *myData;
176  GT_Size mySize;
177  GT_Size myTupleSize;
178 };
179 
180 #endif
GT_Storage
Definition: GT_Types.h:17
GLsizei GLenum const void * indices
Definition: glcorearb.h:405
virtual GT_Size getStringIndexCount() const
const char * GT_String
Definition: GT_Types.h:111
virtual GT_Storage getStorage() const
SYS_FORCE_INLINE T * SYSconst_cast(const T *foo)
Definition: SYS_Types.h:127
#define GT_API
Definition: GT_API.h:11
GT_Type
Definition: GT_Types.h:33
void setString(GT_Size offset, int index, const char *str)
Set an index to a value.
virtual fpreal64 getF64(GT_Offset offset, int index=0) const
png_uint_32 i
Definition: png.h:2877
GLsizeiptr size
Definition: glcorearb.h:663
long long int64
Definition: SYS_Types.h:107
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
virtual GT_Size getTupleSize() const
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:125
double fpreal64
Definition: SYS_Types.h:192
virtual int64 getI64(GT_Offset offset, int index=0) const
GLintptr offset
Definition: glcorearb.h:664
virtual GT_DataArrayHandle harden() const
virtual int32 getI32(GT_Offset offset, int index=0) const
virtual GT_Offset getStringIndex(GT_Offset offset, int idx) const
virtual void getStrings(UT_StringArray &strings) const
int int32
Definition: SYS_Types.h:35
GLsizei const GLchar *const * strings
Definition: glcorearb.h:1932
virtual GT_Size entries() const
virtual uint8 getU8(GT_Offset offset, int index=0) const
virtual GT_String getS(GT_Offset offset, int index) const
unsigned char uint8
Definition: SYS_Types.h:32
virtual GT_Type getTypeInfo() const
GLuint index
Definition: glcorearb.h:785
virtual fpreal16 getF16(GT_Offset offset, int index=0) const
virtual void getIndexedStrings(UT_StringArray &strings, UT_IntArray &indices) const =0
virtual fpreal32 getF32(GT_Offset offset, int index=0) const
virtual const char * className() const
int64 GT_Offset
Definition: GT_Types.h:113
int64 GT_Size
Definition: GT_Types.h:112
virtual int32 getI32(GT_Offset offset, int idx=0) const =0
void resize(GT_Size size)
float fpreal32
Definition: SYS_Types.h:191
virtual int64 getMemoryUsage() const
int UT_IndexedHashMapItemId
Each item in the shared map is assigned a unique id.