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