HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_PackedArrayOfArrays.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: UT_PackedArrayOfArrays.h (UT Library, C++)
7  *
8  * COMMENTS: An array of arrays that is more efficiently
9  * laid out than the UT_Array<UT_Array<T> >
10  * Only allows appending, however.
11  */
12 
13 #pragma once
14 
15 #ifndef __UT_PACKED_ARRAY_OF_ARRAYS_H__
16 #define __UT_PACKED_ARRAY_OF_ARRAYS_H__
17 
18 #include "UT_API.h"
19 
20 #include "UT_Array.h"
21 #include "UT_IteratorRange.h"
22 
23 template <typename T>
25 {
26 public:
28 
30  {
31  }
32 
33  void clear()
34  { myOffsets.clear(); myData.clear(); }
35  bool isEmpty() const
36  { return size() == 0; }
37 
38  /// Returns the number of arrays in this structure.
39  exint size() const { return SYSmax(myOffsets.size() - 1, 0); }
40  exint entries() const { return size(); }
41 
42  int64 getMemoryUsage(bool inclusive = false) const
43  {
44  return (inclusive ? sizeof(*this) : 0) +
45  myOffsets.getMemoryUsage(false) +
46  myData.getMemoryUsage(false);
47  }
48 
49  /// Adds a new array to the end of our list of arrays.
50  void append(const UT_Array<T> &arr)
51  { append(arr.array(), arr.entries()); }
52  void append(const T *data, exint len)
53  {
54  if (myOffsets.isEmpty())
55  myOffsets.append(0);
56  for (exint i = 0; i < len; i++)
57  myData.append(data[i]);
58  myOffsets.append(myData.entries());
59  }
60  void append(const UT_PackedArrayOfArrays<T> &arr, exint idx)
61  { append(arr.arrayData(idx), arr.arrayLen(idx)); }
62 
63  /// Appends an array of arrays to our list.
65  {
66  for (exint i = 0; i < arr.entries(); i++)
67  append(arr, i);
68  }
69 
70  /// Appends a new array and returns a pointer to the resulting
71  /// data so it can be filled in.
73  {
74  if (myOffsets.isEmpty())
75  myOffsets.append(0);
76  exint base = myOffsets.last();
77  myOffsets.append(base+len);
78  myData.bumpSize(base+len);
79  return myData.array() + base;
80  }
81 
82  /// From the given idxth array, return the idx_in_array element
83  const T &operator()(exint idx, exint idx_in_array) const
84  {
85  UT_ASSERT_P(idx >= 0 && idx < size());
86  exint base = myOffsets(idx);
87  UT_ASSERT_P(base+idx_in_array < myOffsets(idx+1));
88  return myData(base+idx_in_array);
89  }
90 
91  /// Extracts an array into a UT_Array.
92  void extract(UT_Array<T> &result, exint idx) const
93  {
94  UT_ASSERT_P(idx >= 0 && idx < size());
95  exint base = myOffsets(idx);
96  exint len = myOffsets(idx+1)-base;
97  result.entries(len);
98  for (exint i = 0; i < len; i++)
99  result(i) = myData(base+i);
100  }
101 
103  {
104  // This is less than equal to as it is valid to
105  // point to the final element if the corresponding
106  // array is empty!
107  UT_ASSERT_P(myOffsets(idx) <= myData.size());
108  return myData.array() + myOffsets(idx);
109  }
110  const T *arrayData(exint idx) const
111  {
112  UT_ASSERT_P(myOffsets(idx) <= myData.size());
113  return myData.array() + myOffsets(idx);
114  }
115  exint arrayLen(exint idx) const
116  {
117  return myOffsets(idx+1)-myOffsets(idx);
118  }
119 
120  /// @{
121  /// Returns an iterator range for the specified array.
123  { return UTmakeRange(arrayData(idx), arrayData(idx + 1)); }
125  { return UTmakeRange(arrayData(idx), arrayData(idx + 1)); }
126  /// @}
127 
128  /// Decreases, but never expands, to the given maxsize.
129  void truncate(exint maxsize)
130  {
131  if (maxsize >= 0 && size() > maxsize)
132  {
133  if (maxsize == 0)
134  clear();
135  else
136  {
137  myOffsets.setSize(maxsize+1);
138  myData.setSize(myOffsets(maxsize));
139  }
140  }
141  }
142 
143  /// These adjust the capacity without adjusting the entries
144  /// and are useful for pre-allocing the amount when known
145  /// in advance.
146  void setDataCapacity(exint capacity)
147  { myData.setCapacity(capacity); }
149  { myData.setCapacityIfNeeded(capacity); }
150  void setArrayCapacity(exint capacity)
151  { myOffsets.setCapacity(capacity); }
153  { myOffsets.setCapacityIfNeeded(capacity); }
154 
155  /// The raw offset table stores the end of each array
156  /// in the data array.
157  /// Each array has [rawOffsets(idx) ... rawOffsets(idx+1))
158  UT_Array<exint> &rawOffsets() { return myOffsets; }
159  const UT_Array<exint> &rawOffsets() const { return myOffsets; }
160 
161  /// All of the array data is in one contiguous block.
162  UT_Array<T> &rawData() { return myData; }
163  const UT_Array<T> &rawData() const { return myData; }
164 
165 protected:
168 };
169 
170 #endif
171 
#define SYSmax(a, b)
Definition: SYS_Math.h:1367
void setDataCapacity(exint capacity)
const T & operator()(exint idx, exint idx_in_array) const
From the given idxth array, return the idx_in_array element.
void truncate(exint maxsize)
Decreases, but never expands, to the given maxsize.
UT_Array< exint > & rawOffsets()
UT_IteratorRange< IterT > UTmakeRange(IterT &&b, IterT &&e)
const UT_Array< T > & rawData() const
T * array()
Definition: UT_Array.h:614
UT_IteratorRange< const T * > arrayRange(exint idx) const
png_uint_32 i
Definition: png.h:2877
void append(const UT_PackedArrayOfArrays< T > &arr, exint idx)
GLsizeiptr size
Definition: glcorearb.h:663
void append(const T *data, exint len)
void setArrayCapacity(exint capacity)
const UT_Array< exint > & rawOffsets() const
long long int64
Definition: SYS_Types.h:107
const T * arrayData(exint idx) const
int64 exint
Definition: SYS_Types.h:116
void concat(const UT_PackedArrayOfArrays< T > &arr)
Appends an array of arrays to our list.
#define UT_ASSERT_P(ZZ)
Definition: UT_Assert.h:125
UT_Array< T > & rawData()
All of the array data is in one contiguous block.
void setArrayCapacityIfNeeded(exint capacity)
GLboolean * data
Definition: glcorearb.h:130
void extract(UT_Array< T > &result, exint idx) const
Extracts an array into a UT_Array.
exint size() const
Returns the number of arrays in this structure.
exint entries() const
Alias of size(). size() is preferred.
Definition: UT_Array.h:453
void setDataCapacityIfNeeded(exint capacity)
int64 getMemoryUsage(bool inclusive=false) const
#define UT_API_TMPL
Definition: UT_API.h:14
void append(const UT_Array< T > &arr)
Adds a new array to the end of our list of arrays.
exint arrayLen(exint idx) const
UT_IteratorRange< T * > arrayRange(exint idx)