VTK  9.7.20260813
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
18
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#include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
26
27#include <cassert> // for assert
28#include <vector> // for vector
29
30VTK_ABI_NAMESPACE_BEGIN
31class vtkBitArrayLookup;
32
33class VTKCOMMONCORE_EXPORT VTK_MARSHALAUTO vtkBitArray : public vtkDataArray
34{
35public:
43 using ArrayTypeTag = std::integral_constant<int, vtkArrayTypes::VTK_BIT_ARRAY>;
44 using DataTypeTag = std::integral_constant<int, VTK_BIT>;
45 using ValueType = unsigned char;
46
47 static vtkBitArray* New();
48 vtkTypeMacro(vtkBitArray, vtkDataArray);
49 void PrintSelf(ostream& os, vtkIndent indent) override;
50
58
69 vtkTypeBool ReserveTuples(vtkIdType numTuples) override;
70
71 // satisfy vtkDataArray API
72 int GetArrayType() const override { return vtkBitArray::ArrayTypeTag::value; }
73 int GetDataType() const override { return vtkBitArray::DataTypeTag::value; }
74 int GetDataTypeSize() const override { return 0; }
75
80 bool SetNumberOfValues(vtkIdType number) override;
81
91
99
107 void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override;
108
117 vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override;
118
127 vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
128
137
143 double* GetTuple(vtkIdType i) override;
144
149 void GetTuple(vtkIdType i, double* tuple) override;
150
152
158 void SetTuple(vtkIdType i, const float* tuple) override;
160 void SetTuple(vtkIdType i, const double* tuple) override;
162
164
170 void InsertTuple(vtkIdType i, const float* tuple) override;
171 void InsertTuple(vtkIdType i, const double* tuple) override;
173
175
180 vtkIdType InsertNextTuple(const float* tuple) override;
181 vtkIdType InsertNextTuple(const double* tuple) override;
183
185
192 void RemoveTuple(vtkIdType id) override;
194
196
204 double GetComponent(vtkIdType tupleIdx, int compIdx) override;
205 void SetComponent(vtkIdType i, int j, double c) override;
207
212 void Squeeze() override;
213
217 ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
218 VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
219 VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents());
220
222
225 void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
226 VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
227 VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents());
229
231
234 void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const VTK_EXPECTS(
235 0 <= tupleIdx && tupleIdx < GetNumberOfTuples()) VTK_SIZEHINT(tuple, GetNumberOfComponents());
237
239
242 void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple) VTK_EXPECTS(
243 0 <= tupleIdx && tupleIdx < GetNumberOfTuples()) VTK_SIZEHINT(tuple, GetNumberOfComponents());
245
250 int GetValue(vtkIdType id) const;
251
259 void SetValue(vtkIdType id, int value);
260
266 void InsertValue(vtkIdType id, int i);
267
272
278 void SetVariantValue(vtkIdType idx, vtkVariant value) override;
279
285 void InsertVariantValue(vtkIdType idx, vtkVariant value) override;
286
288
295 void InsertComponent(vtkIdType i, int j, double c) override;
296
300 VTK_ZEROCOPY ValueType* GetPointer(vtkIdType id) { return this->Buffer->GetBuffer() + id / 8; }
301
308
309 VTK_DEPRECATED_IN_9_7_0("Use vtkBitArray::WritePointer(valueIdx, numValues) or "
310 "vtkAbstractArray::SetNumberOf[Values/Tuples]() instead")
311 void* WriteVoidPointer(vtkIdType id, vtkIdType number) override
312 {
313 return this->WritePointer(id, number);
314 }
315
316 void* GetVoidPointer(vtkIdType id) override { return static_cast<void*>(this->GetPointer(id)); }
317
323#ifdef __VTK_WRAP__
324 vtkAbstractBuffer* GetBuffer() { return this->Buffer; }
325#else
327#endif // __VTK_WRAP__
328
330
333 void DeepCopy(vtkDataArray* da) override;
334 void DeepCopy(vtkAbstractArray* aa) override { this->Superclass::DeepCopy(aa); }
336
340 void ShallowCopy(vtkDataArray* da) override;
342
344
355#ifndef __VTK_WRAP__
357 ValueType* array, vtkIdType size, int save, int deleteMethod = VTK_DATA_ARRAY_DELETE);
358#else
359 // the wrappers do not handle a default argument, so the overloads are spelled out for them.
360 void SetArray(VTK_ZEROCOPY ValueType* array, vtkIdType size, int save);
361 void SetArray(VTK_ZEROCOPY ValueType* array, vtkIdType size, int save, int deleteMethod);
362#endif
363 void SetVoidArray(void* array, vtkIdType size, int save) override
364 {
365 this->SetArray(static_cast<ValueType*>(array), size, save);
366 }
367 void SetVoidArray(void* array, vtkIdType size, int save, int deleteMethod) override
368 {
369 this->SetArray(static_cast<ValueType*>(array), size, save, deleteMethod);
370 }
371
372
379 void SetArrayFreeFunction(void (*callback)(void*)) override;
380
384 VTK_DEPRECATED_IN_9_7_0("Use vtk::DataArrayValueRange, or the array directly")
386
388
392 void LookupValue(vtkVariant value, vtkIdList* ids) override;
394 void LookupValue(int value, vtkIdList* ids);
396
405 void DataChanged() override;
406
412 void ClearLookup() override;
413
414protected:
416 ~vtkBitArray() override;
417
430
435
436 vtkBuffer<ValueType>* Buffer; // pointer to data
437 std::vector<double> LegacyTuple;
438
442 VTK_DEPRECATED_IN_9_7_0("Use ReserveTuples")
444 {
445 if (!this->ReserveTuples(size / this->NumberOfComponents + 1))
446 {
447 return nullptr;
448 }
449 return this->Buffer->GetBuffer();
450 }
451
452private:
453 // hide superclass' DeepCopy() from the user and the compiler
454 void DeepCopy(vtkDataArray& da) { this->vtkDataArray::DeepCopy(&da); }
455
456 vtkBitArray(const vtkBitArray&) = delete;
457 void operator=(const vtkBitArray&) = delete;
458
459 vtkBitArrayLookup* Lookup;
460 void UpdateLookup();
461};
462
463// Declare vtkArrayDownCast implementations for vtkBitArray:
465
466inline void vtkBitArray::SetValue(vtkIdType id, int value)
467{
468 const auto bitsetDiv = std::div(id, static_cast<vtkIdType>(8));
469 const vtkIdType &bitsetId = bitsetDiv.quot, &bitId = bitsetDiv.rem;
470 ValueType mask = 0x80 >> bitId; // NOLINT(clang-analyzer-core.BitwiseShift)
471 this->Buffer->GetBuffer()[bitsetId] =
472 static_cast<ValueType>((value != 0) ? (this->Buffer->GetBuffer()[bitsetId] | mask)
473 : (this->Buffer->GetBuffer()[bitsetId] & (~mask)));
474 this->DataChanged();
475}
476
477inline void vtkBitArray::InsertValue(vtkIdType valueIdx, int value)
478{
479 if (valueIdx >= this->Capacity)
480 {
481 if (!this->ReserveTuples((valueIdx + 1) / this->NumberOfComponents + 1))
482 {
483 return;
484 }
485 }
486 this->SetValue(valueIdx, value);
487 if (valueIdx > this->MaxId)
488 {
489 this->MaxId = valueIdx;
491 }
492}
493
495{
496 return vtkVariant(this->GetValue(id));
497}
498
500{
501 this->SetValue(id, value.ToInt());
502}
503
505{
506 this->InsertValue(id, value.ToInt());
507}
508
510{
511 this->InsertValue(this->MaxId + 1, i);
512 return this->MaxId;
513}
514VTK_ABI_NAMESPACE_END
515#endif
int GetNumberOfComponents() const
Set/Get the dimension (n) of the components.
vtkIdType GetNumberOfTuples() const
Get the number of complete tuples (a component group) in the array.
vtkIdType GetNumberOfValues() const
Get the total number of values in the array.
virtual void ShallowCopy(vtkAbstractArray *src)
This method will copy the data from the source array to this array.
Abstract base class for vtkBuffer providing buffer protocol support.
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.
void * WriteVoidPointer(vtkIdType id, vtkIdType number) override
Get the address of a particular data index.
unsigned char ValueType
Definition vtkBitArray.h:45
vtkIdType InsertNextTuple(vtkIdType j, vtkAbstractArray *source) override
Insert the jth tuple in the source array, at the end in this array.
void GetTypedTuple(vtkIdType tupleIdx, ValueType *tuple) const
Copy the tuple at tupleIdx into tuple.
static vtkBitArray * FastDownCast(vtkAbstractArray *source)
Perform a fast, safe cast from a vtkAbstractArray to a vtkBitArray.
void DataChanged() override
Tell the array explicitly that the data has changed.
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 SetVariantValue(vtkIdType idx, vtkVariant value) override
Set a value in the array from a variant.
std::integral_constant< int, vtkArrayTypes::VTK_BIT_ARRAY > ArrayTypeTag
Definition vtkBitArray.h:43
void SetValue(vtkIdType id, int value)
Set the data at a particular index.
void ShallowCopy(vtkDataArray *da) override
Shallow copy of another bit array.
std::vector< double > LegacyTuple
void InsertComponent(vtkIdType i, int j, double c) override
Insert the data component at ith tuple and jth component location.
vtkBuffer< ValueType > * GetBuffer()
Return the underlying buffer object.
ValueType * WritePointer(vtkIdType id, vtkIdType number)
Get the address of a particular data index.
void DeepCopy(vtkAbstractArray *aa) override
Deep copy of another bit array.
double GetComponent(vtkIdType tupleIdx, int compIdx) override
Set/Get the data component at the ith tuple and jth component location.
void SetComponent(vtkIdType i, int j, double c) override
Set/Get the data component at the ith tuple and jth component location.
bool ReallocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
vtkArrayIterator * NewIterator() override
Returns a new vtkBitArrayIterator instance.
vtkBuffer< ValueType > * Buffer
void SetTypedTuple(vtkIdType tupleIdx, const ValueType *tuple)
Set this array's tuple at tupleIdx to the values in tuple.
void InsertValue(vtkIdType id, int i)
Inserts values and checks to make sure there is enough memory.
ValueType * GetPointer(vtkIdType id)
Direct manipulation of the underlying data.
void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
Set component comp of the tuple at tupleIdx to value.
void InsertTuplesStartingAt(vtkIdType dstStart, vtkIdList *srcIds, vtkAbstractArray *source) override
Copy the tuples indexed in srcIds from the source array to the tuple locations starting at index dstS...
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.
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...
std::integral_constant< int, VTK_BIT > DataTypeTag
Definition vtkBitArray.h:44
vtkIdType InsertNextValue(int i)
double * GetTuple(vtkIdType i) override
Get a pointer to a tuple at the ith location.
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.
void RemoveTuple(vtkIdType id) override
These methods remove tuples from the data array.
void * GetVoidPointer(vtkIdType id) override
Return a void pointer.
int GetDataTypeSize() const override
Return the size of the underlying data type.
Definition vtkBitArray.h:74
int GetArrayType() const override
Method for type-checking in FastDownCast implementations.
Definition vtkBitArray.h:72
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 SetArray(ValueType *array, vtkIdType size, int save, int deleteMethod=VTK_DATA_ARRAY_DELETE)
This method lets the user specify data to be held by the array.
@ VTK_DATA_ARRAY_FREE
Definition vtkBitArray.h:38
@ VTK_DATA_ARRAY_DELETE
Definition vtkBitArray.h:39
@ VTK_DATA_ARRAY_USER_DEFINED
Definition vtkBitArray.h:41
@ VTK_DATA_ARRAY_ALIGNED_FREE
Definition vtkBitArray.h:40
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...
vtkTypeBool ReserveTuples(vtkIdType numTuples) override
Reserve the array to the requested number of tuples and preserve data.
int GetDataType() const override
Return the underlying data type.
Definition vtkBitArray.h:73
vtkVariant GetVariantValue(vtkIdType idx) override
Get a value in the array as a variant.
vtkIdType LookupValue(vtkVariant value) override
Return the indices where a specific value appears.
static vtkBitArray * New()
void InsertVariantValue(vtkIdType idx, vtkVariant value) override
Inserts values from a variant and checks to ensure there is enough memory.
void Squeeze() override
Free any unnecessary memory.
ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
Get component comp of the tuple at tupleIdx.
virtual void InitializeUnusedBitsInLastByte()
This method should be called whenever MaxId needs to be changed, as this method fills the unused bits...
ValueType * ResizeAndExtend(vtkIdType size)
Function to resize data.
void ClearLookup() override
Delete the associated fast lookup data structure on this array, if it exists.
internal storage class used by vtkSOADataArrayTemplate, vtkAOSDataArrayTemplate, and others.
Definition vtkBuffer.h:32
void DeepCopy(vtkAbstractArray *aa) override
Deep copy of data.
list of point or cell ids
Definition vtkIdList.h:135
a simple class to control print indentation
Definition vtkIndent.h:108
A type representing the union of many types.
Definition vtkVariant.h:162
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
#define vtkArrayDownCast_FastCastMacro(ArrayT)
This macro is used to tell vtkArrayDownCast to use FastDownCast instead of SafeDownCast.
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
#define vtkDataArray
#define VTK_DEPRECATED_IN_9_7_0(reason)
int vtkIdType
Definition vtkType.h:363
void save(Archiver &ar, const std::string &str, const unsigned int version)
#define VTK_MARSHAL_EXCLUDE_REASON_IS_REDUNDANT
#define VTK_SIZEHINT(...)
#define VTK_EXPECTS(x)
#define VTK_MARSHALAUTO
#define VTK_ZEROCOPY
#define VTK_MARSHALEXCLUDE(reason)
#define VTK_NEWINSTANCE