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
vtkBitArray::SetVoidArray
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
vtkAbstractArray::Squeeze
virtual void Squeeze()=0
Free any unnecessary memory.
vtkBitArray::RemoveFirstTuple
void RemoveFirstTuple() override
These methods remove tuples from the data array.
vtkAbstractArray::DeleteMethod
DeleteMethod
Definition: vtkAbstractArray.h:322
vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE
@ VTK_DATA_ARRAY_ALIGNED_FREE
Definition: vtkAbstractArray.h:325
vtkBitArray::SetArrayFreeFunction
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...
vtkBitArray::InsertTuples
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...
vtkBitArray::SetComponent
void SetComponent(vtkIdType i, int j, double c) override
Set the data component at the ith tuple and jth component location.
vtkBitArray::Squeeze
void Squeeze() override
Free any unneeded memory.
Definition: vtkBitArray.h:383
vtkX3D::value
@ value
Definition: vtkX3D.h:226
vtkAbstractArray::VTK_DATA_ARRAY_DELETE
@ VTK_DATA_ARRAY_DELETE
Definition: vtkAbstractArray.h:324
vtkIdType
int vtkIdType
Definition: vtkType.h:332
vtkBitArray::SetArray
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.
vtkAbstractArray::Size
vtkIdType Size
Definition: vtkAbstractArray.h:677
vtkAbstractArray::VTK_DATA_ARRAY_FREE
@ VTK_DATA_ARRAY_FREE
Definition: vtkAbstractArray.h:323
vtkBitArray::GetValue
int GetValue(vtkIdType id) const
Get the data at a particular index.
vtkBitArray::SetTuple
void SetTuple(vtkIdType i, const double *tuple) override
Set the tuple value at the ith location in the array.
vtkBitArray::InsertNextTuple
vtkIdType InsertNextTuple(vtkIdType j, vtkAbstractArray *source) override
Insert the jth tuple in the source array, at the end in this array.
vtkDataArray
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:159
vtkBitArray::Tuple
double * Tuple
Definition: vtkBitArray.h:322
vtkBitArray::GetPointer
unsigned char * GetPointer(vtkIdType id)
Direct manipulation of the underlying data.
Definition: vtkBitArray.h:212
vtkBitArray::LookupValue
void LookupValue(vtkVariant value, vtkIdList *ids) override
Return the indices where a specific value appears.
vtkBitArray::~vtkBitArray
~vtkBitArray() override
vtkBitArray::GetTuple
double * GetTuple(vtkIdType i) override
Get a pointer to a tuple at the ith location.
vtkBitArray::GetDataTypeSize
int GetDataTypeSize() const override
Return the size of the underlying data type.
Definition: vtkBitArray.h:61
vtkBitArray::WritePointer
unsigned char * WritePointer(vtkIdType id, vtkIdType number)
Get the address of a particular data index.
vtkAbstractArray::InsertVariantValue
virtual void InsertVariantValue(vtkIdType valueIdx, vtkVariant value)=0
Insert a value into the array from a variant.
vtkBitArray::DeepCopy
void DeepCopy(vtkDataArray *da) override
Deep copy of another bit array.
vtkBitArray::LookupValue
vtkIdType LookupValue(vtkVariant value) override
Return the indices where a specific value appears.
vtkBitArray::TupleSize
int TupleSize
Definition: vtkBitArray.h:321
vtkBitArray::Array
unsigned char * Array
Definition: vtkBitArray.h:317
vtkBitArray::InsertValue
void InsertValue(vtkIdType id, int i)
Inserts values and checks to make sure there is enough memory.
Definition: vtkBitArray.h:346
vtkBitArray::DataChanged
void DataChanged() override
Tell the array explicitly that the data has changed.
vtkBitArray::SetValue
void SetValue(vtkIdType id, int value)
Set the data at a particular index.
Definition: vtkBitArray.h:338
vtkBitArray::SetNumberOfTuples
void SetNumberOfTuples(vtkIdType number) override
Set the number of n-tuples in the array.
vtkBitArray::GetDataType
int GetDataType() const override
Return the underlying data type.
Definition: vtkBitArray.h:60
vtkBitArray::InsertTuple
void InsertTuple(vtkIdType i, vtkIdType j, vtkAbstractArray *source) override
Insert the jth tuple in the source array, at ith location in this array.
vtkBitArray::New
static vtkBitArray * New()
vtkBitArray::InsertVariantValue
void InsertVariantValue(vtkIdType idx, vtkVariant value) override
Inserts values from a variant and checks to ensure there is enough memory.
Definition: vtkBitArray.h:371
vtkBitArray::InsertTuple
void InsertTuple(vtkIdType i, const double *tuple) override
Insert (memory allocation performed) the tuple into the ith location in the array.
vtkBitArray::InsertTuples
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,...
vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
@ VTK_DATA_ARRAY_USER_DEFINED
Definition: vtkAbstractArray.h:326
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:113
vtkBitArray::vtkBitArray
vtkBitArray()
save
void save(Archiver &ar, const std::string &str, const unsigned int vtkNotUsed(version))
Definition: vtkVariantBoostSerialization.h:64
vtkBitArray::GetVoidPointer
void * GetVoidPointer(vtkIdType id) override
Return a void pointer.
Definition: vtkBitArray.h:226
vtkVariant
A atomic type representing the union of many types.
Definition: vtkVariant.h:155
vtkIdList
list of point or cell ids
Definition: vtkIdList.h:140
vtkX3D::size
@ size
Definition: vtkX3D.h:259
vtkBitArray::Resize
vtkTypeBool Resize(vtkIdType numTuples) override
Resize the array while conserving the data.
vtkBitArray::WriteVoidPointer
void * WriteVoidPointer(vtkIdType id, vtkIdType number) override
Get the address of a particular data index.
Definition: vtkBitArray.h:221
vtkBitArray::InsertNextValue
vtkIdType InsertNextValue(int i)
Definition: vtkBitArray.h:376
vtkBitArray::ResizeAndExtend
unsigned char * ResizeAndExtend(vtkIdType sz)
vtkAbstractArray::SetVariantValue
virtual void SetVariantValue(vtkIdType valueIdx, vtkVariant value)=0
Set a value in the array from a variant.
vtkBitArray::SetVoidArray
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
vtkBitArray::InsertComponent
void InsertComponent(vtkIdType i, int j, double c) override
Insert the data component at ith tuple and jth component location.
vtkBitArray::InsertTuple
void InsertTuple(vtkIdType i, const float *tuple) override
Insert (memory allocation performed) the tuple into the ith location in the array.
vtkAbstractArray
Abstract superclass for all arrays.
Definition: vtkAbstractArray.h:76
vtkBitArray::RemoveTuple
void RemoveTuple(vtkIdType id) override
These methods remove tuples from the data array.
vtkBitArray::LookupValue
vtkIdType LookupValue(int value)
Return the indices where a specific value appears.
vtkDataArray.h
vtkVariant::ToInt
int ToInt(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type.
vtkBitArray::Allocate
vtkTypeBool Allocate(vtkIdType sz, vtkIdType ext=1000) override
Allocate memory for this array.
vtkAbstractArray::DeepCopy
virtual void DeepCopy(vtkAbstractArray *da)
Deep copy of data.
vtkArrayIterator
Abstract superclass to iterate over elements in an vtkAbstractArray.
Definition: vtkArrayIterator.h:50
vtkBitArray::LookupValue
void LookupValue(int value, vtkIdList *ids)
Return the indices where a specific value appears.
vtkBitArray::DeepCopy
void DeepCopy(vtkAbstractArray *aa) override
Deep copy of data.
Definition: vtkBitArray.h:232
vtkBitArray::SetTuple
void SetTuple(vtkIdType i, vtkIdType j, vtkAbstractArray *source) override
Set the tuple at the ith location using the jth tuple in the source array.
vtkBitArray::ClearLookup
void ClearLookup() override
Delete the associated fast lookup data structure on this array, if it exists.
vtkBitArray::GetTuple
void GetTuple(vtkIdType i, double *tuple) override
Copy the tuple value into a user-provided array.
vtkBitArray
dynamic, self-adjusting array of bits
Definition: vtkBitArray.h:34
source
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Definition: vtkBoostGraphAdapter.h:998
vtkBitArray::InitializeUnusedBitsInLastByte
virtual void InitializeUnusedBitsInLastByte()
This method should be called whenever MaxId needs to be changed, as this method fills the unused bits...
vtkBitArray::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
VTK_BIT
#define VTK_BIT
Definition: vtkType.h:44
vtkBitArray::InsertNextTuple
vtkIdType InsertNextTuple(const float *tuple) override
Insert (memory allocation performed) the tuple onto the end of the array.
VTK_NEWINSTANCE
#define VTK_NEWINSTANCE
Definition: vtkWrappingHints.h:44
vtkAbstractArray::MaxId
vtkIdType MaxId
Definition: vtkAbstractArray.h:678
vtkBitArray::SetNumberOfValues
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...
vtkBitArray::SetTuple
void SetTuple(vtkIdType i, const float *tuple) override
Set the tuple value at the ith location in the array.
vtkDataArray::DeepCopy
void DeepCopy(vtkAbstractArray *aa) override
Deep copy of data.
vtkBitArray::InsertNextTuple
vtkIdType InsertNextTuple(const double *tuple) override
Insert (memory allocation performed) the tuple onto the end of the array.
vtkTypeBool
int vtkTypeBool
Definition: vtkABI.h:69
vtkBitArray::NewIterator
vtkArrayIterator * NewIterator() override
Returns a new vtkBitArrayIterator instance.
vtkBitArray::Initialize
void Initialize() override
Release storage and reset array to initial state.
vtkBitArray::SetVariantValue
void SetVariantValue(vtkIdType idx, vtkVariant value) override
Set a value in the array from a variant.
Definition: vtkBitArray.h:366
vtkBitArray::RemoveLastTuple
void RemoveLastTuple() override
These methods remove tuples from the data array.