VTK  9.7.1
vtkBitArray.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
19 #ifndef vtkBitArray_h
20 #define vtkBitArray_h
21 
22 #include "vtkBuffer.h" // For vtkBuffer
23 #include "vtkCommonCoreModule.h" // For export macro
24 #include "vtkDataArray.h"
25 
26 #include <cassert> // for assert
27 #include <vector> // for vector
28 
29 VTK_ABI_NAMESPACE_BEGIN
30 class vtkBitArrayLookup;
31 
32 class VTKCOMMONCORE_EXPORT vtkBitArray : public vtkDataArray
33 {
34 public:
36  {
38  VTK_DATA_ARRAY_DELETE = vtkAbstractArray::VTK_DATA_ARRAY_DELETE,
39  VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
40  VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
41  };
42  using ArrayTypeTag = std::integral_constant<int, vtkArrayTypes::VTK_BIT_ARRAY>;
43  using DataTypeTag = std::integral_constant<int, VTK_BIT>;
44  using ValueType = unsigned char;
45 
46  static vtkBitArray* New();
47  vtkTypeMacro(vtkBitArray, vtkDataArray);
48  void PrintSelf(ostream& os, vtkIndent indent) override;
49 
57 
68  vtkTypeBool ReserveTuples(vtkIdType numTuples) override;
69 
70  // satisfy vtkDataArray API
71  int GetArrayType() const override { return vtkBitArray::ArrayTypeTag::value; }
72  int GetDataType() const override { return vtkBitArray::DataTypeTag::value; }
73  int GetDataTypeSize() const override { return 0; }
74 
79  bool SetNumberOfValues(vtkIdType number) override;
80 
89  void SetTuple(vtkIdType i, vtkIdType j, vtkAbstractArray* source) override;
90 
98 
106  void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override;
107 
116  vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override;
117 
125  void InsertTuples(
126  vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
127 
136 
141  double* GetTuple(vtkIdType i) override;
142 
146  void GetTuple(vtkIdType i, double* tuple) override;
147 
149 
154  void SetTuple(vtkIdType i, const float* tuple) override;
155  void SetTuple(vtkIdType i, const double* tuple) override;
157 
159 
165  void InsertTuple(vtkIdType i, const float* tuple) override;
166  void InsertTuple(vtkIdType i, const double* tuple) override;
168 
170 
175  vtkIdType InsertNextTuple(const float* tuple) override;
176  vtkIdType InsertNextTuple(const double* tuple) override;
178 
180 
187  void RemoveTuple(vtkIdType id) override;
189 
191 
199  double GetComponent(vtkIdType tupleIdx, int compIdx) override;
200  void SetComponent(vtkIdType i, int j, double c) override;
202 
207  void Squeeze() override;
208 
212  ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
213  VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
214  VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents());
215 
217 
220  void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
221  VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
222  VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents());
224 
226 
229  void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
230  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples());
232 
234 
237  void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
238  VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples());
240 
244  int GetValue(vtkIdType id) const;
245 
252  void SetValue(vtkIdType id, int value);
253 
259  void InsertValue(vtkIdType id, int i);
260 
264  vtkVariant GetVariantValue(vtkIdType idx) override;
265 
271  void SetVariantValue(vtkIdType idx, vtkVariant value) override;
272 
278  void InsertVariantValue(vtkIdType idx, vtkVariant value) override;
279 
280  vtkIdType InsertNextValue(int i);
281 
288  void InsertComponent(vtkIdType i, int j, double c) override;
289 
293  ValueType* GetPointer(vtkIdType id) { return this->Buffer->GetBuffer() + id / 8; }
294 
300  ValueType* WritePointer(vtkIdType id, vtkIdType number);
301 
302  VTK_DEPRECATED_IN_9_7_0("Use vtkBitArray::WritePointer(valueIdx, numValues) or "
303  "vtkAbstractArray::SetNumberOf[Values/Tuples]() instead")
304  void* WriteVoidPointer(vtkIdType id, vtkIdType number) override
305  {
306  return this->WritePointer(id, number);
307  }
308 
309  void* GetVoidPointer(vtkIdType id) override { return static_cast<void*>(this->GetPointer(id)); }
310 
316 #ifdef __VTK_WRAP__
317  vtkAbstractBuffer* GetBuffer() { return this->Buffer; }
318 #else
319  vtkBuffer<ValueType>* GetBuffer() { return this->Buffer; }
320 #endif // __VTK_WRAP__
321 
323 
326  void DeepCopy(vtkDataArray* da) override;
327  void DeepCopy(vtkAbstractArray* aa) override { this->Superclass::DeepCopy(aa); }
329 
333  void ShallowCopy(vtkDataArray* da) override;
335 
337 
348 #ifndef __VTK_WRAP__
349  void SetArray(
350  ValueType* array, vtkIdType size, int save, int deleteMethod = VTK_DATA_ARRAY_DELETE);
351 #endif
352  void SetVoidArray(void* array, vtkIdType size, int save) override
353  {
354  this->SetArray(static_cast<ValueType*>(array), size, save);
355  }
356  void SetVoidArray(void* array, vtkIdType size, int save, int deleteMethod) override
357  {
358  this->SetArray(static_cast<ValueType*>(array), size, save, deleteMethod);
359  }
361 
368  void SetArrayFreeFunction(void (*callback)(void*)) override;
369 
373  VTK_DEPRECATED_IN_9_7_0("Use vtk::DataArrayValueRange, or the array directly")
374  VTK_NEWINSTANCE vtkArrayIterator* NewIterator() override;
375 
377 
380  vtkIdType LookupValue(vtkVariant value) override;
381  void LookupValue(vtkVariant value, vtkIdList* ids) override;
382  vtkIdType LookupValue(int value);
383  void LookupValue(int value, vtkIdList* ids);
385 
394  void DataChanged() override;
395 
401  void ClearLookup() override;
402 
403 protected:
404  vtkBitArray();
405  ~vtkBitArray() override;
406 
418  virtual void InitializeUnusedBitsInLastByte();
419 
423  bool ReallocateTuples(vtkIdType numTuples);
424 
425  vtkBuffer<ValueType>* Buffer; // pointer to data
426  std::vector<double> LegacyTuple;
427 
431  VTK_DEPRECATED_IN_9_7_0("Use ReserveTuples")
432  ValueType* ResizeAndExtend(vtkIdType size)
433  {
434  if (!this->ReserveTuples(size / this->NumberOfComponents + 1))
435  {
436  return nullptr;
437  }
438  return this->Buffer->GetBuffer();
439  }
440 
441 private:
442  // hide superclass' DeepCopy() from the user and the compiler
443  void DeepCopy(vtkDataArray& da) { this->vtkDataArray::DeepCopy(&da); }
444 
445  vtkBitArray(const vtkBitArray&) = delete;
446  void operator=(const vtkBitArray&) = delete;
447 
448  vtkBitArrayLookup* Lookup;
449  void UpdateLookup();
450 };
451 
452 // Declare vtkArrayDownCast implementations for vtkBitArray:
454 
456 {
457  const auto bitsetDiv = std::div(id, static_cast<vtkIdType>(8));
458  const vtkIdType &bitsetId = bitsetDiv.quot, &bitId = bitsetDiv.rem;
459  ValueType mask = 0x80 >> bitId; // NOLINT(clang-analyzer-core.BitwiseShift)
460  this->Buffer->GetBuffer()[bitsetId] =
461  static_cast<ValueType>((value != 0) ? (this->Buffer->GetBuffer()[bitsetId] | mask)
462  : (this->Buffer->GetBuffer()[bitsetId] & (~mask)));
463  this->DataChanged();
464 }
465 
466 inline void vtkBitArray::InsertValue(vtkIdType valueIdx, int value)
467 {
468  if (valueIdx >= this->Capacity)
469  {
470  if (!this->ReserveTuples((valueIdx + 1) / this->NumberOfComponents + 1))
471  {
472  return;
473  }
474  }
475  this->SetValue(valueIdx, value);
476  if (valueIdx > this->MaxId)
477  {
478  this->MaxId = valueIdx;
480  }
481 }
482 
484 {
485  return vtkVariant(this->GetValue(id));
486 }
487 
489 {
490  this->SetValue(id, value.ToInt());
491 }
492 
494 {
495  this->InsertValue(id, value.ToInt());
496 }
497 
499 {
500  this->InsertValue(this->MaxId + 1, i);
501  return this->MaxId;
502 }
503 VTK_ABI_NAMESPACE_END
504 #endif
vtkBuffer< ValueType > * Buffer
Definition: vtkBitArray.h:425
int GetDataType() const override
Return the underlying data type.
Definition: vtkBitArray.h:72
int GetDataTypeSize() const override
Return the size of the underlying data type.
Definition: vtkBitArray.h:73
int GetArrayType() const override
Method for type-checking in FastDownCast implementations.
Definition: vtkBitArray.h:71
virtual void DeepCopy(vtkAbstractArray *da)
Deep copy of data.
void SetVariantValue(vtkIdType idx, vtkVariant value) override
Set a value in the array from a variant.
Definition: vtkBitArray.h:488
virtual void ShallowCopy(vtkDataArray *other)
Create a shallow copy of other into this, if possible.
int GetNumberOfComponents() const
Set/Get the dimension (n) of the components.
Abstract superclass for all arrays.
virtual double * GetTuple(vtkIdType tupleIdx)=0
Get the data tuple at tupleIdx.
void SetTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray *source) override
See documentation from parent class.
void InsertTuple(vtkIdType dstTupleIdx, vtkIdType srcTupleIdx, vtkAbstractArray *source) override
See documentation from parent class.
virtual vtkTypeBool ReserveTuples(vtkIdType numTuples)=0
Reserve the array to the requested number of tuples and preserve data.
int vtkIdType
Definition: vtkType.h:363
void DeepCopy(vtkAbstractArray *aa) override
Deep copy of data.
void InsertValue(vtkIdType id, int i)
Inserts values and checks to make sure there is enough memory.
Definition: vtkBitArray.h:466
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
A type representing the union of many types.
Definition: vtkVariant.h:52
void SetValue(vtkIdType id, int value)
Set the data at a particular index.
Definition: vtkBitArray.h:455
int ToInt(bool *valid) const
Convert the variant to a numeric type: If it holds a numeric, cast to the appropriate type...
int vtkTypeBool
Definition: vtkABI.h:64
void * GetVoidPointer(vtkIdType id) override
Return a void pointer.
Definition: vtkBitArray.h:309
vtkIdType GetNumberOfValues() const
Get the total number of values in the array.
virtual void SetComponent(vtkIdType tupleIdx, int compIdx, double value)
Set the data component at the location specified by tupleIdx and compIdx to value.
vtkTypeBool ReserveTuples(vtkIdType numTuples) override
Reserve the array to the requested number of tuples and preserve data.
ScalarType * GetBuffer()
Access the buffer as a scalar pointer.
Definition: vtkBuffer.h:43
a simple class to control print indentation
Definition: vtkIndent.h:28
void save(Archiver &ar, const std::string &str, const unsigned int version)
vtkArrayDownCast_FastCastMacro(vtkBitArray)
list of point or cell ids
Definition: vtkIdList.h:25
void InsertVariantValue(vtkIdType idx, vtkVariant value) override
Inserts values from a variant and checks to ensure there is enough memory.
Definition: vtkBitArray.h:493
abstract superclass for arrays of numeric data
Definition: vtkDataArray.h:44
void InsertTuplesStartingAt(vtkIdType dstStart, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
void DataChanged() override
Tell the array explicitly that the data has changed.
static vtkDataArray * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkDataArray.
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:356
Abstract superclass to iterate over elements in an vtkAbstractArray.
int GetValue(vtkIdType id) const
Get the data at a particular index.
vtkIdType InsertNextTuple(vtkIdType srcTupleIdx, vtkAbstractArray *source) override
See documentation from parent class.
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
#define VTK_NEWINSTANCE
unsigned char ValueType
Definition: vtkBitArray.h:44
std::integral_constant< int, VTK_OPAQUE > DataTypeTag
virtual void InitializeUnusedBitsInLastByte()
This method should be called whenever MaxId needs to be changed, as this method fills the unused bits...
void DeepCopy(vtkAbstractArray *aa) override
Deep copy of another bit array.
Definition: vtkBitArray.h:327
virtual double GetComponent(vtkIdType tupleIdx, int compIdx)
Return the data component at the location specified by tupleIdx and compIdx.
dynamic, self-adjusting array of bits
Definition: vtkBitArray.h:32
virtual void SetArrayFreeFunction(void(*callback)(void *))=0
This method allows the user to specify a custom free function to be called when the array is dealloca...
Abstract base class for vtkBuffer providing buffer protocol support.
vtkIdType InsertNextValue(int i)
Definition: vtkBitArray.h:498
std::integral_constant< int, vtkArrayTypes::VTK_ABSTRACT_ARRAY > ArrayTypeTag
virtual void RemoveTuple(vtkIdType tupleIdx)=0
These methods remove tuples from the data array.
vtkVariant GetVariantValue(vtkIdType idx) override
Get a value in the array as a variant.
Definition: vtkBitArray.h:483
void InsertTuples(vtkIdList *dstIds, vtkIdList *srcIds, vtkAbstractArray *source) override
See documentation from parent class.
virtual bool SetNumberOfValues(vtkIdType numValues)
Specify the number of values (tuples * components) for this object to hold.
virtual void Squeeze()=0
Free any unnecessary memory.
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
virtual void ShallowCopy(vtkAbstractArray *src)
This method will copy the data from the source array to this array.
#define VTK_EXPECTS(x)
#define VTK_DEPRECATED_IN_9_7_0(reason)
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:352
vtkBuffer< ValueType > * GetBuffer()
Return the underlying buffer object.
Definition: vtkBitArray.h:319