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  ~GT_DAIndexedString() override;
26 
27  const char *className() const override
28  { return "GT_DAIndexedString"; }
29 
30  /// @{
31  /// Methods defined on GT_DataArray
32  /// An indexed string array is hard to begin with
33  GT_DataArrayHandle harden() const override
34  { return GT_DataArrayHandle(SYSconst_cast(this)); }
35  GT_Storage getStorage() const override { return GT_STORE_STRING; }
36  GT_Size getTupleSize() const override { return myTupleSize; }
37  GT_Size entries() const override { return mySize; }
38  GT_Type getTypeInfo() const override { return GT_TYPE_NONE; }
39  int64 getMemoryUsage() const override
40  {
41 #if 0
42  return myStrings.getMemoryUsage() +
43  sizeof(int32)*mySize*myTupleSize;
44 #else
45  return sizeof(int32)*mySize*myTupleSize;
46 #endif
47  }
48 
49  uint8 getU8(GT_Offset offset, int index=0) const override
50  {
51  UT_ASSERT_P(offset >= 0 && offset < mySize);
52  UT_ASSERT_P(index >= 0 && index < myTupleSize);
53  offset = offset*myTupleSize + index;
54  return myData[offset];
55  }
56  int32 getI32(GT_Offset offset, int index=0) const override
57  {
58  UT_ASSERT_P(offset >= 0 && offset < mySize);
59  UT_ASSERT_P(index >= 0 && index < myTupleSize);
60  offset = offset*myTupleSize + index;
61  return myData[offset];
62  }
63  int64 getI64(GT_Offset offset, int index=0) const override
64  {
65  UT_ASSERT_P(offset >= 0 && offset < mySize);
66  UT_ASSERT_P(index >= 0 && index < myTupleSize);
67  offset = offset*myTupleSize + index;
68  return myData[offset];
69  }
70  fpreal16 getF16(GT_Offset offset, int index=0) const override
71  {
72  UT_ASSERT_P(offset >= 0 && offset < mySize);
73  UT_ASSERT_P(index >= 0 && index < myTupleSize);
74  offset = offset*myTupleSize + index;
75  return myData[offset];
76  }
77  fpreal32 getF32(GT_Offset offset, int index=0) const override
78  {
79  UT_ASSERT_P(offset >= 0 && offset < mySize);
80  UT_ASSERT_P(index >= 0 && index < myTupleSize);
81  offset = offset*myTupleSize + index;
82  return myData[offset];
83  }
84  fpreal64 getF64(GT_Offset offset, int index=0) const override
85  {
86  UT_ASSERT_P(offset >= 0 && offset < mySize);
87  UT_ASSERT_P(index >= 0 && index < myTupleSize);
88  offset = offset*myTupleSize + index;
89  return myData[offset];
90  }
91  GT_String getS(GT_Offset offset, int index) const override
92  {
93  UT_ASSERT_P(offset >= 0 && offset < mySize);
94  UT_ASSERT_P(index >= 0 && index < myTupleSize);
95  // Find out which string
96  int sidx;
97  const UT_IndexedHashMapStringItem *item;
99  sidx = getI32(offset, index);
100  if (sidx >= 0 && (item = myStrings.get(sidx)))
101  str = item->getKey();
102  return str;
103  }
104  GT_Size getStringIndexCount() const override
105  {
106  return myStrings.getItemIdUpperBound()+1;
107  }
108  GT_Offset getStringIndex(GT_Offset offset, int idx) const override
109  {
110  return getI32(offset, idx);
111  }
113  UT_IntArray &indices) const override;
114  void getStrings(UT_StringArray &strings) const override;
115  /// @}
116 
118  {
119  if (!size)
120  {
121  GT_Memory::Free(GT_MEM_DATA, myData,
122  mySize*myTupleSize*sizeof(int32));
123  myStrings.clear();
124  }
125  else
126  {
127  for (GT_Size i = size; i < mySize; ++i)
128  {
129  for (GT_Size j = 0; j < myTupleSize; ++j)
130  freeString(i, j);
131  }
132  myData = (int32 *)GT_Memory::Realloc(GT_MEM_DATA,
133  myData,
134  mySize*myTupleSize*sizeof(int32),
135  size*myTupleSize*sizeof(int32));
136  for (GT_Size i = mySize; i < size; ++i)
137  {
138  for (GT_Size j = 0; j < myTupleSize; ++j)
139  myData[i*myTupleSize+j] = -1;
140  }
141  mySize = size;
142  }
143  }
144  /// Set an index to a value
145  void setString(GT_Size offset, int index, const GT_String &str)
146  {
147  UT_ASSERT_P(offset >= 0 && offset < mySize);
148  UT_ASSERT_P(index >= 0 && index < myTupleSize);
149  if (!str || *str == 0)
150  freeString(offset, index);
151  else
152  {
153  int old_id;
155  offset = offset*myTupleSize + index;
156  old_id = myData[offset];
157  myStrings.add(str, NULL, &new_id);
158  if (old_id >= 0)
159  myStrings.remove(old_id);
160  myData[offset] = new_id;
161  }
162  }
163  /// Directly set the string index for a particular entry.
165  {
166  UT_ASSERT_P(offset >= 0 && offset < mySize);
167  UT_ASSERT_P(index >= 0 && index < myTupleSize);
168  offset = offset*myTupleSize + index;
169 
170  UT_IndexedHashMapItemId new_id = str_id;
171  int old_id = myData[offset];
172  myStrings.addReference(new_id);
173  if (old_id >= 0)
174  myStrings.remove(old_id);
175  myData[offset] = new_id;
176  }
177 private:
178  void freeString(GT_Size offset, int index)
179  {
180  UT_ASSERT_P(offset >= 0 && offset < mySize);
181  UT_ASSERT_P(index >= 0 && index < myTupleSize);
182  offset = offset*myTupleSize + index;
183  if (myData[offset] >= 0)
184  {
185  myStrings.remove(myData[offset]);
186  myData[offset] = -1;
187  }
188  }
189  UT_IndexedStringMap myStrings; // Shared string table
190  int32 *myData;
191  GT_Size mySize;
192  GT_Size myTupleSize;
193 };
194 
195 #endif
GT_Storage
Definition: GT_Types.h:18
void setString(GT_Size offset, int index, const GT_String &str)
Set an index to a value.
void setStringIndex(GT_Size offset, int index, GT_Offset str_id)
Directly set the string index for a particular entry.
int int32
Definition: SYS_Types.h:39
fpreal64 getF64(GT_Offset offset, int index=0) const override
int64 getMemoryUsage() const override
int64 getI64(GT_Offset offset, int index=0) const override
SYS_FORCE_INLINE T * SYSconst_cast(const T *foo)
Definition: SYS_Types.h:136
int32 getI32(GT_Offset offset, int index=0) const override
#define GT_API
Definition: GT_API.h:11
GT_Type
Definition: GT_Types.h:34
GT_DataArrayHandle harden() const override
const char * className() const override
GT_String getS(GT_Offset offset, int index) const override
float fpreal32
Definition: SYS_Types.h:200
GLsizeiptr size
Definition: glcorearb.h:664
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
GT_Type getTypeInfo() const override
Abstract data class for an array of float, int or string data.
Definition: GT_DataArray.h:40
UT_IntrusivePtr< GT_DataArray > GT_DataArrayHandle
Definition: GT_DataArray.h:32
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:152
static const UT_StringHolder theEmptyString
GT_Storage getStorage() const override
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
fpreal32 getF32(GT_Offset offset, int index=0) const override
GT_Size entries() const override
int64 GT_Offset
Definition: GT_Types.h:124
long long int64
Definition: SYS_Types.h:116
GT_Offset getStringIndex(GT_Offset offset, int idx) const override
virtual void getStrings(UT_StringArray &strings) const
GLsizei const GLchar *const * strings
Definition: glcorearb.h:1933
int64 GT_Size
Definition: GT_Types.h:123
GLuint index
Definition: glcorearb.h:786
uint8 getU8(GT_Offset offset, int index=0) const override
virtual void getIndexedStrings(UT_StringArray &strings, UT_IntArray &indices) const =0
GLintptr offset
Definition: glcorearb.h:665
fpreal16 getF16(GT_Offset offset, int index=0) const override
GT_Size getTupleSize() const override
virtual int32 getI32(GT_Offset offset, int idx=0) const =0
GT_Size getStringIndexCount() const override
void resize(GT_Size size)
int UT_IndexedHashMapItemId
Each item in the shared map is assigned a unique id.