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  GT_Size getDictIndexCount() const override { return -1; }
117  GT_Offset getDictIndex(GT_Offset, int) const override { return -1; }
119  UT_IntArray &) const override {}
120  /// @}
121 
123  {
124  if (!size)
125  {
126  GT_Memory::Free(GT_MEM_DATA, myData,
127  mySize*myTupleSize*sizeof(int32));
128  myStrings.clear();
129  }
130  else
131  {
132  for (GT_Size i = size; i < mySize; ++i)
133  {
134  for (GT_Size j = 0; j < myTupleSize; ++j)
135  freeString(i, j);
136  }
137  myData = (int32 *)GT_Memory::Realloc(GT_MEM_DATA,
138  myData,
139  mySize*myTupleSize*sizeof(int32),
140  size*myTupleSize*sizeof(int32));
141  for (GT_Size i = mySize; i < size; ++i)
142  {
143  for (GT_Size j = 0; j < myTupleSize; ++j)
144  myData[i*myTupleSize+j] = -1;
145  }
146  mySize = size;
147  }
148  }
149  /// Set an index to a value
150  void setString(GT_Size offset, int index, const GT_String &str)
151  {
152  UT_ASSERT_P(offset >= 0 && offset < mySize);
153  UT_ASSERT_P(index >= 0 && index < myTupleSize);
154  if (!str || *str == 0)
155  freeString(offset, index);
156  else
157  {
158  int old_id;
160  offset = offset*myTupleSize + index;
161  old_id = myData[offset];
162  myStrings.add(str, NULL, &new_id);
163  if (old_id >= 0)
164  myStrings.remove(old_id);
165  myData[offset] = new_id;
166  }
167  }
168  /// Directly set the string index for a particular entry.
170  {
171  UT_ASSERT_P(offset >= 0 && offset < mySize);
172  UT_ASSERT_P(index >= 0 && index < myTupleSize);
173  offset = offset*myTupleSize + index;
174 
175  UT_IndexedHashMapItemId new_id = str_id;
176  int old_id = myData[offset];
177  myStrings.addReference(new_id);
178  if (old_id >= 0)
179  myStrings.remove(old_id);
180  myData[offset] = new_id;
181  }
182 private:
183  void freeString(GT_Size offset, int index)
184  {
185  UT_ASSERT_P(offset >= 0 && offset < mySize);
186  UT_ASSERT_P(index >= 0 && index < myTupleSize);
187  offset = offset*myTupleSize + index;
188  if (myData[offset] >= 0)
189  {
190  myStrings.remove(myData[offset]);
191  myData[offset] = -1;
192  }
193  }
194  UT_IndexedStringMap myStrings; // Shared string table
195  int32 *myData;
196  GT_Size mySize;
197  GT_Size myTupleSize;
198 };
199 
200 #endif
GT_Storage
Definition: GT_Types.h:19
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.
GT_Offset getDictIndex(GT_Offset, int) const override
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
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
void getIndexedDicts(UT_Array< UT_OptionsHolder > &, UT_IntArray &) 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:13
GT_Type
Definition: GT_Types.h:36
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
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
GT_Type getTypeInfo() const override
GLintptr offset
Definition: glcorearb.h:665
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:155
static const UT_StringHolder theEmptyString
GT_Storage getStorage() const override
fpreal32 getF32(GT_Offset offset, int index=0) const override
GT_Size entries() const override
int64 GT_Offset
Definition: GT_Types.h:129
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
GLint j
Definition: glad.h:2733
int64 GT_Size
Definition: GT_Types.h:128
GLsizeiptr size
Definition: glcorearb.h:664
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
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)
GT_Size getDictIndexCount() const override
int UT_IndexedHashMapItemId
Each item in the shared map is assigned a unique id.