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 
19 #include <UT/UT_Assert.h>
20 #include <UT/UT_NonCopyable.h>
21 #include <UT/UT_IndexedHashSet.h>
23 
25 {
26 public:
27  GT_DAIndexedString(GT_Size array_size, int tuple_size=1);
28  ~GT_DAIndexedString() override;
29 
31 
32  const char *className() const override
33  { return "GT_DAIndexedString"; }
34 
35  /// @{
36  /// Methods defined on GT_DataArray
37  /// An indexed string array is hard to begin with
38  GT_DataArrayHandle harden() const override
39  { return GT_DataArrayHandle(SYSconst_cast(this)); }
40  GT_Storage getStorage() const override { return GT_STORE_STRING; }
41  GT_Size getTupleSize() const override { return myTupleSize; }
42  GT_Size entries() const override { return mySize; }
43  GT_Type getTypeInfo() const override { return GT_TYPE_NONE; }
44  int64 getMemoryUsage() const override
45  {
46 #if 0
47  return myStrings.getMemoryUsage() +
48  sizeof(int32)*mySize*myTupleSize;
49 #else
50  return sizeof(int32)*mySize*myTupleSize;
51 #endif
52  }
53 
54  uint8 getU8(GT_Offset offset, int index=0) const override
55  {
56  UT_ASSERT_P(offset >= 0 && offset < mySize);
57  UT_ASSERT_P(index >= 0 && index < myTupleSize);
58  offset = offset*myTupleSize + index;
59  return myData[offset];
60  }
61  int32 getI32(GT_Offset offset, int index=0) const override
62  {
63  UT_ASSERT_P(offset >= 0 && offset < mySize);
64  UT_ASSERT_P(index >= 0 && index < myTupleSize);
65  offset = offset*myTupleSize + index;
66  return myData[offset];
67  }
68  int64 getI64(GT_Offset offset, int index=0) const override
69  {
70  UT_ASSERT_P(offset >= 0 && offset < mySize);
71  UT_ASSERT_P(index >= 0 && index < myTupleSize);
72  offset = offset*myTupleSize + index;
73  return myData[offset];
74  }
75  fpreal16 getF16(GT_Offset offset, int index=0) const override
76  {
77  UT_ASSERT_P(offset >= 0 && offset < mySize);
78  UT_ASSERT_P(index >= 0 && index < myTupleSize);
79  offset = offset*myTupleSize + index;
80  return myData[offset];
81  }
82  fpreal32 getF32(GT_Offset offset, int index=0) const override
83  {
84  UT_ASSERT_P(offset >= 0 && offset < mySize);
85  UT_ASSERT_P(index >= 0 && index < myTupleSize);
86  offset = offset*myTupleSize + index;
87  return myData[offset];
88  }
89  fpreal64 getF64(GT_Offset offset, int index=0) const override
90  {
91  UT_ASSERT_P(offset >= 0 && offset < mySize);
92  UT_ASSERT_P(index >= 0 && index < myTupleSize);
93  offset = offset*myTupleSize + index;
94  return myData[offset];
95  }
96  GT_String getS(GT_Offset offset, int index) const override
97  {
98  UT_ASSERT_P(offset >= 0 && offset < mySize);
99  UT_ASSERT_P(index >= 0 && index < myTupleSize);
100  offset = offset * myTupleSize + index;
101  UT_IndexedHashSetItemId id = myData[offset];
102  return (id >= 0) ? *myStrings.get(id) : UT_StringHolder::theEmptyString;
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)
155  freeString(offset, index);
156  else
157  {
158  offset = offset * myTupleSize + index;
159  UT_IndexedHashSetItemId old_id = myData[offset];
160  myData[offset] = myStrings.add(str);
161  myStrings.remove(old_id);
162  }
163  }
164  /// Directly set the string index for a particular entry.
166  {
167  UT_ASSERT_P(offset >= 0 && offset < mySize);
168  UT_ASSERT_P(index >= 0 && index < myTupleSize);
169  offset = offset*myTupleSize + index;
170 
171  UT_IndexedHashSetItemId new_id = str_id;
172  UT_IndexedHashSetItemId old_id = myData[offset];
173  myStrings.addReference(new_id);
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 
190  UT_IndexedHashSet<GT_String> myStrings; // Shared string table
191  int32 *myData;
192  GT_Size mySize;
193  GT_Size myTupleSize;
194 };
195 
196 #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
exint UT_IndexedHashSetItemId
Each item in the shared map is assigned a unique id.
A thread-safe hash map which stores indexed shared items.
GT_DataArrayHandle harden() 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:41
UT_IntrusivePtr< GT_DataArray > GT_DataArrayHandle
Definition: GT_DataArray.h:33
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:164
static const UT_StringHolder theEmptyString
GT_Storage getStorage() const override
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
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