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 
18 #include <UT/UT_Assert.h>
19 #include <UT/UT_IndexedHashSet.h>
21 #include <UT/UT_NonCopyable.h>
22 #include <UT/UT_Options.h>
23 
25 {
26 public:
27  GT_DAIndexedDict(GT_Size array_size, int tuple_size=1);
28  ~GT_DAIndexedDict() override;
29 
31 
32  const char* className() const override
33  {
34  return "GT_DAIndexedDict";
35  }
36  GT_DataArrayHandle harden() const override
37  {
38  return GT_DataArrayHandle(SYSconst_cast(this));
39  }
40  GT_Storage getStorage() const override
41  {
42  return GT_STORE_DICT;
43  }
44  GT_Size getTupleSize() const override { return myTupleSize; }
45  GT_Size entries() const override { return mySize; }
46  GT_Type getTypeInfo() const override { return GT_TYPE_NONE; }
47  int64 getMemoryUsage() const override
48  {
49 #if 0
50  return myDictionaries.getMemoryUsage() +
51  sizeof(int32)*mySize*myTupleSize;
52 #else
53  return sizeof(int32)*mySize*myTupleSize;
54 #endif
55  }
56 
57  uint8 getU8(GT_Offset offset, int index=0) const override
58  {
59  UT_ASSERT_P(offset >= 0 && offset < mySize);
60  UT_ASSERT_P(index >= 0 && index < myTupleSize);
61  offset = offset*myTupleSize + index;
62  return myData[offset];
63  }
64  int32 getI32(GT_Offset offset, int index=0) const override
65  {
66  UT_ASSERT_P(offset >= 0 && offset < mySize);
67  UT_ASSERT_P(index >= 0 && index < myTupleSize);
68  offset = offset*myTupleSize + index;
69  return myData[offset];
70  }
71  int64 getI64(GT_Offset offset, int index=0) const override
72  {
73  UT_ASSERT_P(offset >= 0 && offset < mySize);
74  UT_ASSERT_P(index >= 0 && index < myTupleSize);
75  offset = offset*myTupleSize + index;
76  return myData[offset];
77  }
78  fpreal16 getF16(GT_Offset offset, int index=0) const override
79  {
80  UT_ASSERT_P(offset >= 0 && offset < mySize);
81  UT_ASSERT_P(index >= 0 && index < myTupleSize);
82  offset = offset*myTupleSize + index;
83  return myData[offset];
84  }
85  fpreal32 getF32(GT_Offset offset, int index=0) const override
86  {
87  UT_ASSERT_P(offset >= 0 && offset < mySize);
88  UT_ASSERT_P(index >= 0 && index < myTupleSize);
89  offset = offset*myTupleSize + index;
90  return myData[offset];
91  }
92  fpreal64 getF64(GT_Offset offset, int index=0) const override
93  {
94  UT_ASSERT_P(offset >= 0 && offset < mySize);
95  UT_ASSERT_P(index >= 0 && index < myTupleSize);
96  offset = offset*myTupleSize + index;
97  return myData[offset];
98  }
99 
100  GT_String getS(GT_Offset, int) const override { return UT_StringHolder::theEmptyString; }
101  GT_Size getStringIndexCount() const override { return -1; }
102  GT_Offset getStringIndex(GT_Offset, int) const override { return -1; }
103  void getIndexedStrings(UT_StringArray &, UT_IntArray &) const override {}
104 
105  GT_Size getDictIndexCount() const override
106  {
107  return myDictionaries.getItemIdUpperBound() + 1;
108  }
109  GT_Offset getDictIndex(GT_Offset offset, int idx) const override
110  {
111  return getI32(offset, idx);
112  }
113  void getIndexedDicts(
115  UT_IntArray &indices) const override;
116 
117  GT_Dict getDict(GT_Offset offset, int index = 0) const override
118  {
119  UT_ASSERT_P(offset >= 0 && offset < mySize);
120  UT_ASSERT_P(index >= 0 && index < myTupleSize);
121  offset = offset * myTupleSize + index;
122  UT_IndexedHashSetItemId id = myData[offset];
123  return (id >= 0) ? *myDictionaries.get(id)
125  }
126 
128  int index,
129  const GT_Dict &dict)
130  {
131  UT_ASSERT_P(offset >= 0 && offset < mySize);
132  UT_ASSERT_P(index >= 0 && index < myTupleSize);
133  if (!dict || dict.isEmpty())
134  freeDictionary(offset, index);
135  else
136  {
137  offset = offset * myTupleSize + index;
138  UT_IndexedHashSetItemId old_id = myData[offset];
139  myData[offset] = myDictionaries.add(dict);
140  myDictionaries.remove(old_id);
141  }
142  }
143 
144 private:
145  void resize(GT_Size size)
146  {
147  if (!size)
148  {
149  GT_Memory::Free(
150  GT_MEM_DATA, myData,
151  mySize * myTupleSize * sizeof(int32));
152  myDictionaries.clear();
153  }
154  else
155  {
156  for (GT_Size i = size; i < mySize; ++i)
157  {
158  for (GT_Size j = 0; j < myTupleSize; ++j)
159  freeDictionary(i, j);
160  }
161 
162  myData = (int32 *)GT_Memory::Realloc(
163  GT_MEM_DATA, myData,
164  mySize * myTupleSize * sizeof(int32),
165  size * myTupleSize * sizeof(int32));
166 
167  for (GT_Size i = mySize; i < size; ++i)
168  {
169  for (GT_Size j = 0; j < myTupleSize; ++j)
170  myData[i * myTupleSize + j] = -1;
171  }
172  mySize = size;
173  }
174  }
175 
176  void freeDictionary(GT_Size offset, int index)
177  {
178  UT_ASSERT_P(offset >= 0 && offset < mySize);
179  UT_ASSERT_P(index >= 0 && index < myTupleSize);
180  offset = offset*myTupleSize + index;
181  if (myData[offset] >= 0)
182  {
183  myDictionaries.remove(myData[offset]);
184  myData[offset] = -1;
185  }
186  }
187 
189  int32 *myData;
190  GT_Size mySize;
191  GT_Size myTupleSize;
192 };
193 
194 #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:981
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
exint UT_IndexedHashSetItemId
Each item in the shared map is assigned a unique id.
float fpreal32
Definition: SYS_Types.h:200
double fpreal64
Definition: SYS_Types.h:201
unsigned char uint8
Definition: SYS_Types.h:36
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:41
UT_IntrusivePtr< GT_DataArray > GT_DataArrayHandle
Definition: GT_DataArray.h:33
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:164
GT_Type getTypeInfo() const override
Return "type" information for the data. This defaults to GT_TYPE_NONE.
static const UT_StringHolder theEmptyString
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:1242
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
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
GT_Offset getStringIndex(GT_Offset, int) const override