VTK  9.1.0
vtkBitArray.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkBitArray.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
25 #ifndef vtkBitArray_h
26 #define vtkBitArray_h
27 
28 #include "vtkCommonCoreModule.h" // For export macro
29 #include "vtkDataArray.h"
30 
31 class vtkBitArrayLookup;
32 
33 class VTKCOMMONCORE_EXPORT vtkBitArray : public vtkDataArray
34 {
35 public:
37  {
39  VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
40  VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
41  VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
42  };
43 
44  static vtkBitArray* New();
45  vtkTypeMacro(vtkBitArray, vtkDataArray);
46  void PrintSelf(ostream& os, vtkIndent indent) override;
47 
52  vtkTypeBool Allocate(vtkIdType sz, vtkIdType ext = 1000) override;
53 
57  void Initialize() override;
58 
59  // satisfy vtkDataArray API
60  int GetDataType() const override { return VTK_BIT; }
61  int GetDataTypeSize() const override { return 0; }
62 
66  void SetNumberOfTuples(vtkIdType number) override;
67 
72  bool SetNumberOfValues(vtkIdType number) override;
73 
81 
87 
93  void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override;
94 
101  vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
102 
109 
114  double* GetTuple(vtkIdType i) override;
115 
119  void GetTuple(vtkIdType i, double* tuple) override;
120 
122 
125  void SetTuple(vtkIdType i, const float* tuple) override;
126  void SetTuple(vtkIdType i, const double* tuple) override;
128 
130 
134  void InsertTuple(vtkIdType i, const float* tuple) override;
135  void InsertTuple(vtkIdType i, const double* tuple) override;
137 
139 
142  vtkIdType InsertNextTuple(const float* tuple) override;
143  vtkIdType InsertNextTuple(const double* tuple) override;
145 
147 
152  void RemoveTuple(vtkIdType id) override;
153  void RemoveFirstTuple() override;
154  void RemoveLastTuple() override;
156 
163  void SetComponent(vtkIdType i, int j, double c) override;
164 
168  void Squeeze() override;
169 
173  vtkTypeBool Resize(vtkIdType numTuples) override;
174 
178  int GetValue(vtkIdType id) const;
179 
184  void SetValue(vtkIdType id, int value);
185 
189  void InsertValue(vtkIdType id, int i);
190 
194  void SetVariantValue(vtkIdType idx, vtkVariant value) override;
195 
199  void InsertVariantValue(vtkIdType idx, vtkVariant value) override;
200 
201  vtkIdType InsertNextValue(int i);
202 
207  void InsertComponent(vtkIdType i, int j, double c) override;
208 
212  unsigned char* GetPointer(vtkIdType id) { return this->Array + id / 8; }
213 
219  unsigned char* WritePointer(vtkIdType id, vtkIdType number);
220 
221  void* WriteVoidPointer(vtkIdType id, vtkIdType number) override
222  {
223  return this->WritePointer(id, number);
224  }
225 
226  void* GetVoidPointer(vtkIdType id) override { return static_cast<void*>(this->GetPointer(id)); }
227 
231  void DeepCopy(vtkDataArray* da) override;
232  void DeepCopy(vtkAbstractArray* aa) override { this->Superclass::DeepCopy(aa); }
233 
235 
246 #ifndef __VTK_WRAP__
247  void SetArray(
248  unsigned char* array, vtkIdType size, int save, int deleteMethod = VTK_DATA_ARRAY_DELETE);
249 #endif
250  void SetVoidArray(void* array, vtkIdType size, int save) override
251  {
252  this->SetArray(static_cast<unsigned char*>(array), size, save);
253  }
254  void SetVoidArray(void* array, vtkIdType size, int save, int deleteMethod) override
255  {
256  this->SetArray(static_cast<unsigned char*>(array), size, save, deleteMethod);
257  }
259 
266  void SetArrayFreeFunction(void (*callback)(void*)) override;
267 
272 
274 
278  void LookupValue(vtkVariant value, vtkIdList* ids) override;
280  void LookupValue(int value, vtkIdList* ids);
282 
291  void DataChanged() override;
292 
298  void ClearLookup() override;
299 
300 protected:
302  ~vtkBitArray() override;
303 
316 
317  unsigned char* Array; // pointer to data
318  unsigned char* ResizeAndExtend(vtkIdType sz);
319  // function to resize data
320 
321  int TupleSize; // used for data conversion
322  double* Tuple;
323 
324  void (*DeleteFunction)(void*);
325 
326 private:
327  // hide superclass' DeepCopy() from the user and the compiler
328  void DeepCopy(vtkDataArray& da) { this->vtkDataArray::DeepCopy(&da); }
329 
330 private:
331  vtkBitArray(const vtkBitArray&) = delete;
332  void operator=(const vtkBitArray&) = delete;
333 
334  vtkBitArrayLookup* Lookup;
335  void UpdateLookup();
336 };
337 
339 {
340  this->Array[id / 8] =
341  static_cast<unsigned char>((value != 0) ? (this->Array[id / 8] | (0x80 >> id % 8))
342  : (this->Array[id / 8] & (~(0x80 >> id % 8))));
343  this->DataChanged();
344 }
345 
346 inline void vtkBitArray::InsertValue(vtkIdType id, int i)
347 {
348  if (id >= this->Size)
349  {
350  if (!this->ResizeAndExtend(id + 1))
351  {
352  return;
353  }
354  }
355  this->Array[id / 8] =
356  static_cast<unsigned char>((i != 0) ? (this->Array[id / 8] | (0x80 >> id % 8))
357  : (this->Array[id / 8] & (~(0x80 >> id % 8))));
358  if (id > this->MaxId)
359  {
360  this->MaxId = id;
362  }
363  this->DataChanged();
364 }
365 
367 {
368  this->SetValue(id, value.ToInt());
369 }
370 
372 {
373  this->InsertValue(id, value.ToInt());
374 }
375 
377 {
378  this->InsertValue(this->MaxId + 1, i);
379  this->DataChanged();
380  return this->MaxId;
381 }
382 
383 inline void vtkBitArray::Squeeze()
384 {
385  this->ResizeAndExtend(this->MaxId + 1);
386 }
387 #endif
Abstract superclass for all arrays.
virtual void DeepCopy(vtkAbstractArray *da)
Deep copy of data.
virtual void SetVariantValue(vtkIdType valueIdx, vtkVariant value)=0
Set a value in the array from a variant.
virtual void InsertVariantValue(vtkIdType valueIdx, vtkVariant value)=0
Insert a value into the array from a variant.
virtual void Squeeze()=0
Free any unnecessary memory.
Abstract superclass to iterate over elements in an vtkAbstractArray.
dynamic, self-adjusting array of bits
Definition: vtkBitArray.h:34
int GetValue(vtkIdType id) const
Get the data at a particular index.
vtkIdType InsertNextTuple(vtkIdType j, vtkAbstractArray *source) override
Insert the jth tuple in the source array, at the end in this array.
vtkIdType InsertNextTuple(const double *tuple) override
Insert (memory allocation performed) the tuple onto the end of the array.
void DataChanged() override
Tell the array explicitly that the data has changed.
unsigned char * GetPointer(vtkIdType id)
Direct manipulation of the underlying data.
Definition: vtkBitArray.h:212
double * GetTuple(vtkIdType i) override
Get a pointer to a tuple at the ith location.
void InsertTuples(vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray *source) override
Copy n consecutive tuples starting at srcStart from the source array to this array,...
void SetNumberOfTuples(vtkIdType number) override
Set the number of n-tuples in the array.
void SetVariantValue(vtkIdType idx, vtkVariant value) override
Set a value in the array from a variant.
Definition: vtkBitArray.h:366
void SetValue(vtkIdType id, int value)
Set the data at a particular index.
Definition: vtkBitArray.h:338
vtkTypeBool Allocate(vtkIdType sz, vtkIdType ext=1000) override
Allocate memory for this array.
void InsertTuple(vtkIdType i, const double *tuple) override
Insert (memory allocation performed) the tuple into the ith location in the array.
void GetTuple(vtkIdType i, double *tuple) override
Copy the tuple value into a user-provided array.
void LookupValue(vtkVariant value, vtkIdList *ids) override
Return the indices where a specific value appears.
void SetTuple(vtkIdType i, const double *tuple) override
Set the tuple value at the ith location in the array.
unsigned char * ResizeAndExtend(vtkIdType sz)
void InsertComponent(vtkIdType i, int j, double c) override
Insert the data component at ith tuple and jth component location.
void LookupValue(int value, vtkIdList *ids)
Return the indices where a specific value appears.
void DeepCopy(vtkAbstractArray *aa) override
Deep copy of data.
Definition: vtkBitArray.h:232
void SetComponent(vtkIdType i, int j, double c) override
Set the data component at the ith tuple and jth component location.
void SetTuple(vtkIdType i, const float *tuple) override
Set the tuple value at the ith location in the array.
vtkArrayIterator * NewIterator() override
Returns a new vtkBitArrayIterator instance.
vtkIdType InsertNextTuple(const float *tuple) override
Insert (memory allocation performed) the tuple onto the end of the array.
vtkTypeBool Resize(vtkIdType numTuples) override
Resize the array while conserving the data.
void InsertValue(vtkIdType id, int i)
Inserts values and checks to make sure there is enough memory.
Definition: vtkBitArray.h:346
void RemoveFirstTuple() override
These methods remove tuples from the data array.
double * Tuple
Definition: vtkBitArray.h:322
void InsertTuple(vtkIdType i, const float *tuple) override
Insert (memory allocation performed) the tuple into the ith location in the array.
void Initialize() override
Release storage and reset array to initial state.
bool SetNumberOfValues(vtkIdType number) override
In addition to setting the number of values, this method also sets the unused bits of the last byte o...
void SetVoidArray(void *array, vtkIdType size, int save, int deleteMethod) override
This method lets the user specify data to be held by the array.
Definition: vtkBitArray.h:254
unsigned char * WritePointer(vtkIdType id, vtkIdType number)
Get the address of a particular data index.
void SetArrayFreeFunction(void(*callback)(void *)) override
This method allows the user to specify a custom free function to be called when the array is dealloca...
vtkIdType InsertNextValue(int i)
Definition: vtkBitArray.h:376
void InsertTuple(vtkIdType i, vtkIdType j, vtkAbstractArray *source) override
Insert the jth tuple in the source array, at ith location in this array.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void SetVoidArray(void *array, vtkIdType size, int save) override
This method lets the user specify data to be held by the array.
Definition: vtkBitArray.h:250
void RemoveTuple(vtkIdType id) override
These methods remove tuples from the data array.
void * GetVoidPointer(vtkIdType id) override
Return a void pointer.
Definition: vtkBitArray.h:226
void * WriteVoidPointer(vtkIdType id, vtkIdType number) override
Get the address of a particular data index.
Definition: vtkBitArray.h:221
~vtkBitArray() override
unsigned char * Array
Definition: vtkBitArray.h:317
int GetDataTypeSize() const override
Return the size of the underlying data type.
Definition: vtkBitArray.h:61
void RemoveLastTuple() override
These methods remove tuples from the data array.
void SetTuple(vtkIdType i, vtkIdType j, vtkAbstractArray *source) override
Set the tuple at the ith location using the jth tuple in the source array.
void DeepCopy(vtkDataArray *da) override
Deep copy of another bit array.
void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source) override
Copy the tuples indexed in srcIds from the source array to the tuple locations indexed by dstIds in t...
void SetArray(unsigned char *array, vtkIdType size, int save, int deleteMethod=VTK_DATA_ARRAY_DELETE)
This method lets the user specify data to be held by the array.
int GetDataType() const override
Return the underlying data type.
Definition: vtkBitArray.h:60
vtkIdType LookupValue(int value)
Return the indices where a specific value appears.
vtkIdType LookupValue(vtkVariant value) override
Return the indices where a specific value appears.
void InsertVariantValue(vtkIdType idx, vtkVariant value) override
Inserts values from a variant and checks to ensure there is enough memory.
Definition: vtkBitArray.h:371
void Squeeze() override
Free any unneeded memory.
Definition: vtkBitArray.h:383
virtual void InitializeUnusedBitsInLastByte()
This method should be called whenever MaxId needs to be changed, as this method fills the unused bits...
void ClearLookup() override
Delete the associated fast lookup data structure on this array, if it exists.
static vtkBitArray * New()
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:50
void DeepCopy(vtkAbstractArray *aa) override
Deep copy of data.
list of point or cell ids
Definition: vtkIdList.h:31
a simple class to control print indentation
Definition: vtkIndent.h:34
A atomic type representing the union of many types.
Definition: vtkVariant.h:66
int ToInt(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
@ value
Definition: vtkX3D.h:226
@ size
Definition: vtkX3D.h:259
int vtkTypeBool
Definition: vtkABI.h:69
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
int vtkIdType
Definition: vtkType.h:332
#define VTK_BIT
Definition: vtkType.h:44
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))
#define VTK_NEWINSTANCE