HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GT_DAIndexedDict.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_DAIndexedDict.h ( GT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __GT_DAIndexedDict__
12 #define __GT_DAIndexedDict__
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_IndexedHashMapT.h>
19 #include <UT/UT_Options.h>
20 
22 {
23 public:
24  GT_DAIndexedDict(GT_Size array_size, int tuple_size=1);
25  ~GT_DAIndexedDict() override;
26 
27  const char* className() const override
28  {
29  return "GT_DAIndexedDict";
30  }
31  GT_DataArrayHandle harden() const override
32  {
33  return GT_DataArrayHandle(SYSconst_cast(this));
34  }
35  GT_Storage getStorage() const override
36  {
37  return GT_STORE_DICT;
38  }
39  GT_Size getTupleSize() const override { return myTupleSize; }
40  GT_Size entries() const override { return mySize; }
41  GT_Type getTypeInfo() const override { return GT_TYPE_NONE; }
42  int64 getMemoryUsage() const override
43  {
44 #if 0
45  return myDictionaries.getMemoryUsage() +
46  sizeof(int32)*mySize*myTupleSize;
47 #else
48  return sizeof(int32)*mySize*myTupleSize;
49 #endif
50  }
51 
52  uint8 getU8(GT_Offset offset, int index=0) const override
53  {
54  UT_ASSERT_P(offset >= 0 && offset < mySize);
55  UT_ASSERT_P(index >= 0 && index < myTupleSize);
56  offset = offset*myTupleSize + index;
57  return myData[offset];
58  }
59  int32 getI32(GT_Offset offset, int index=0) const override
60  {
61  UT_ASSERT_P(offset >= 0 && offset < mySize);
62  UT_ASSERT_P(index >= 0 && index < myTupleSize);
63  offset = offset*myTupleSize + index;
64  return myData[offset];
65  }
66  int64 getI64(GT_Offset offset, int index=0) const override
67  {
68  UT_ASSERT_P(offset >= 0 && offset < mySize);
69  UT_ASSERT_P(index >= 0 && index < myTupleSize);
70  offset = offset*myTupleSize + index;
71  return myData[offset];
72  }
73  fpreal16 getF16(GT_Offset offset, int index=0) const override
74  {
75  UT_ASSERT_P(offset >= 0 && offset < mySize);
76  UT_ASSERT_P(index >= 0 && index < myTupleSize);
77  offset = offset*myTupleSize + index;
78  return myData[offset];
79  }
80  fpreal32 getF32(GT_Offset offset, int index=0) const override
81  {
82  UT_ASSERT_P(offset >= 0 && offset < mySize);
83  UT_ASSERT_P(index >= 0 && index < myTupleSize);
84  offset = offset*myTupleSize + index;
85  return myData[offset];
86  }
87  fpreal64 getF64(GT_Offset offset, int index=0) const override
88  {
89  UT_ASSERT_P(offset >= 0 && offset < mySize);
90  UT_ASSERT_P(index >= 0 && index < myTupleSize);
91  offset = offset*myTupleSize + index;
92  return myData[offset];
93  }
94 
95  GT_String getS(GT_Offset, int) const override { return UT_StringHolder::theEmptyString; }
96  GT_Size getStringIndexCount() const override { return -1; }
97  GT_Offset getStringIndex(GT_Offset, int) const override { return -1; }
98  void getIndexedStrings(UT_StringArray &, UT_IntArray &) const override {}
99 
100  GT_Size getDictIndexCount() const override
101  {
102  return myDictionaries.getItemIdUpperBound() + 1;
103  }
104  GT_Offset getDictIndex(GT_Offset offset, int idx) const override
105  {
106  return getI32(offset, idx);
107  }
108  void getIndexedDicts(
110  UT_IntArray &indices) const override;
111 
112  GT_Dict getDict(GT_Offset offset, int index = 0) const override
113  {
114  UT_ASSERT_P(offset >= 0 && offset < mySize);
115  UT_ASSERT_P(index >= 0 && index < myTupleSize);
116 
117  // Find out which dictionary
118  int dict_idx;
121 
122  dict_idx = getI32(offset, index);
123  if (dict_idx >= 0 && (item = myDictionaries.get(dict_idx)))
124  {
125  MapKey key = item->getKey();
126  opt = key.getOpt();
127  }
128  return opt;
129  }
130 
132  int index,
133  const GT_Dict &dict)
134  {
135  UT_ASSERT_P(offset >= 0 && offset < mySize);
136  UT_ASSERT_P(index >= 0 && index < myTupleSize);
137  if (!dict || dict.isEmpty())
138  freeDictionary(offset, index);
139  else
140  {
141  int old_id;
143 
144  offset = offset * myTupleSize + index;
145  old_id = myData[offset];
146  myDictionaries.add(dict, NULL, &new_id);
147  if (old_id >= 0)
148  myDictionaries.remove(old_id);
149  myData[offset] = new_id;
150  }
151  }
152 
153 private:
154  void resize(GT_Size size)
155  {
156  if (!size)
157  {
158  GT_Memory::Free(
159  GT_MEM_DATA, myData,
160  mySize * myTupleSize * sizeof(int32));
161  myDictionaries.clear();
162  }
163  else
164  {
165  for (GT_Size i = size; i < mySize; ++i)
166  {
167  for (GT_Size j = 0; j < myTupleSize; ++j)
168  freeDictionary(i, j);
169  }
170 
171  myData = (int32 *)GT_Memory::Realloc(
172  GT_MEM_DATA, myData,
173  mySize * myTupleSize * sizeof(int32),
174  size * myTupleSize * sizeof(int32));
175 
176  for (GT_Size i = mySize; i < size; ++i)
177  {
178  for (GT_Size j = 0; j < myTupleSize; ++j)
179  myData[i * myTupleSize + j] = -1;
180  }
181  mySize = size;
182  }
183  }
184 
185  void freeDictionary(GT_Size offset, int index)
186  {
187  UT_ASSERT_P(offset >= 0 && offset < mySize);
188  UT_ASSERT_P(index >= 0 && index < myTupleSize);
189  offset = offset*myTupleSize + index;
190  if (myData[offset] >= 0)
191  {
192  myDictionaries.remove(myData[offset]);
193  myData[offset] = -1;
194  }
195  }
196 
197  class MapKey
198  {
199  public:
200  MapKey(const UT_OptionsHolder &opt) : myOpt(opt) {}
201  MapKey(const MapKey &src) : myOpt(src.myOpt) {}
202  uint hash() const
203  {
204  return myOpt ? myOpt.hash() : 123456789;
205  }
206  bool isEqual(const MapKey &src) const
207  {
208  if (!myOpt || !src.myOpt)
209  return myOpt == src.myOpt;
210 
211  return myOpt.isEqual(src.myOpt, 0.0);
212  }
213  bool operator==(const MapKey &key) const
214  {
215  return isEqual(key);
216  }
218  const UT_OptionsHolder &getOpt() const { return myOpt; }
219 
220  bool operator<(const MapKey &src) const
221  {
222  return myOpt.options() < src.myOpt.options();
223  }
224  int64 getMemoryUsage(bool inclusive) const
225  {
226  int64 mem = inclusive ? sizeof(*this) : 0;
227  mem += myOpt.getMemoryUsage(true);
228  return mem;
229  }
230 
231  private:
232  UT_OptionsHolder myOpt;
233  };
234 
237 
238  /// A dictionary map
239  using UT_IndexedDictMap = UT_IndexedHashMapT<MapKey, MapItem, MapAlloc>;
240 
241  UT_IndexedDictMap myDictionaries;
242  int32 *myData;
243  GT_Size mySize;
244  GT_Size myTupleSize;
245 };
246 
247 #endif
void getIndexedStrings(UT_StringArray &, UT_IntArray &) const override
GT_Storage
Definition: GT_Types.h:19
GT_DataArrayHandle harden() const override
Create a "hardened" version of the array.
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
int int32
Definition: SYS_Types.h:39
GT_Offset getDictIndex(GT_Offset offset, int idx) const override
fpreal64 getF64(GT_Offset offset, int index=0) const override
bool isEmpty() const
Definition: UT_Options.h:907
SYS_FORCE_INLINE T * SYSconst_cast(const T *foo)
Definition: SYS_Types.h:136
#define GT_API
Definition: GT_API.h:13
GT_Type
Definition: GT_Types.h:36
float fpreal32
Definition: SYS_Types.h:200
const char * className() const override
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
GT_String getS(GT_Offset, int) 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
GT_Type getTypeInfo() const override
Return "type" information for the data. This defaults to GT_TYPE_NONE.
static const UT_StringHolder theEmptyString
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
int64 getI64(GT_Offset offset, int index=0) const override
uint8 getU8(GT_Offset offset, int index=0) const override
static const UT_OptionsHolder theEmptyOptions
Definition: UT_Options.h:1168
bool operator<(const GU_TetrahedronFacet &a, const GU_TetrahedronFacet &b)
int64 GT_Offset
Definition: GT_Types.h:129
GT_Size getTupleSize() const override
Number of elements for each array element.
long long int64
Definition: SYS_Types.h:116
virtual void getIndexedDicts(UT_Array< UT_OptionsHolder > &dicts, UT_IntArray &indices) const =0
GT_Size entries() const override
Number of entries in the array.
GT_Size getDictIndexCount() const override
GT_Dict getDict(GT_Offset offset, int index=0) const override
GLint j
Definition: glad.h:2733
int64 GT_Size
Definition: GT_Types.h:128
GLsizeiptr size
Definition: glcorearb.h:664
int64 getMemoryUsage() const override
GT_Storage getStorage() const override
Type of data stored in the array.
ImageBuf OIIO_API resize(const ImageBuf &src, string_view filtername="", float filterwidth=0.0f, ROI roi={}, int nthreads=0)
fpreal32 getF32(GT_Offset offset, int index=0) const override
int32 getI32(GT_Offset offset, int index=0) const override
GLuint index
Definition: glcorearb.h:786
fpreal16 getF16(GT_Offset offset, int index=0) const override
GT_Size getStringIndexCount() const override
void setDict(GT_Size offset, int index, const GT_Dict &dict)
virtual int32 getI32(GT_Offset offset, int idx=0) const =0
unsigned int uint
Definition: SYS_Types.h:45
int UT_IndexedHashMapItemId
Each item in the shared map is assigned a unique id.
GT_Offset getStringIndex(GT_Offset, int) const override
GLenum src
Definition: glcorearb.h:1793