VTK  9.6.20260318
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
26#include <cassert> // for assert
27#include <vector> // for vector
28
29VTK_ABI_NAMESPACE_BEGIN
30class vtkBitArrayLookup;
31
32class VTKCOMMONCORE_EXPORT vtkBitArray : public vtkDataArray
33{
34public:
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
62 vtkTypeBool Allocate(vtkIdType sz, vtkIdType ext = 1000) override;
63
74 vtkTypeBool ReserveTuples(vtkIdType numTuples) override;
75
76 // satisfy vtkDataArray API
77 int GetArrayType() const override { return vtkBitArray::ArrayTypeTag::value; }
78 int GetDataType() const override { return vtkBitArray::DataTypeTag::value; }
79 int GetDataTypeSize() const override { return 0; }
80
85 bool SetNumberOfValues(vtkIdType number) override;
86
96
104
112 void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override;
113
122 vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override;
123
132 vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
133
142
147 double* GetTuple(vtkIdType i) override;
148
152 void GetTuple(vtkIdType i, double* tuple) override;
153
155
160 void SetTuple(vtkIdType i, const float* tuple) override;
161 void SetTuple(vtkIdType i, const double* tuple) override;
163
165
171 void InsertTuple(vtkIdType i, const float* tuple) override;
172 void InsertTuple(vtkIdType i, const double* tuple) override;
174
176
181 vtkIdType InsertNextTuple(const float* tuple) override;
182 vtkIdType InsertNextTuple(const double* tuple) override;
184
186
193 void RemoveTuple(vtkIdType id) override;
195
197
205 double GetComponent(vtkIdType tupleIdx, int compIdx) override;
206 void SetComponent(vtkIdType i, int j, double c) override;
208
213 void Squeeze() override;
214
218 ValueType GetTypedComponent(vtkIdType tupleIdx, int comp) const
219 VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
220 VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents());
221
223
226 void SetTypedComponent(vtkIdType tupleIdx, int comp, ValueType value)
227 VTK_EXPECTS(0 <= tupleIdx && GetNumberOfComponents() * tupleIdx + comp < GetNumberOfValues())
228 VTK_EXPECTS(0 <= comp && comp < GetNumberOfComponents());
230
232
235 void GetTypedTuple(vtkIdType tupleIdx, ValueType* tuple) const
236 VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples());
238
240
243 void SetTypedTuple(vtkIdType tupleIdx, const ValueType* tuple)
244 VTK_EXPECTS(0 <= tupleIdx && tupleIdx < GetNumberOfTuples());
246
250 int GetValue(vtkIdType id) const;
251
258 void SetValue(vtkIdType id, int value);
259
265 void InsertValue(vtkIdType id, int i);
266
271
277 void SetVariantValue(vtkIdType idx, vtkVariant value) override;
278
284 void InsertVariantValue(vtkIdType idx, vtkVariant value) override;
285
287
294 void InsertComponent(vtkIdType i, int j, double c) override;
295
299 ValueType* GetPointer(vtkIdType id) { return this->Buffer->GetBuffer() + id / 8; }
300
307
308 void* WriteVoidPointer(vtkIdType id, vtkIdType number) override
309 {
310 return this->WritePointer(id, number);
311 }
312
313 void* GetVoidPointer(vtkIdType id) override { return static_cast<void*>(this->GetPointer(id)); }
314
320#ifdef __VTK_WRAP__
321 vtkAbstractBuffer* GetBuffer() { return this->Buffer; }
322#else
324#endif // __VTK_WRAP__
325
327
330 void DeepCopy(vtkDataArray* da) override;
331 void DeepCopy(vtkAbstractArray* aa) override { this->Superclass::DeepCopy(aa); }
333
337 void ShallowCopy(vtkDataArray* da) override;
339
341
352#ifndef __VTK_WRAP__
354 ValueType* array, vtkIdType size, int save, int deleteMethod = VTK_DATA_ARRAY_DELETE);
355#endif
356 void SetVoidArray(void* array, vtkIdType size, int save) override
357 {
358 this->SetArray(static_cast<ValueType*>(array), size, save);
359 }
360 void SetVoidArray(void* array, vtkIdType size, int save, int deleteMethod) override
361 {
362 this->SetArray(static_cast<ValueType*>(array), size, save, deleteMethod);
363 }
364
365
372 void SetArrayFreeFunction(void (*callback)(void*)) override;
373
377 VTK_DEPRECATED_IN_9_7_0("Use vtk::DataArrayValueRange, or the array directly")
379
381
385 void LookupValue(vtkVariant value, vtkIdList* ids) override;
387 void LookupValue(int value, vtkIdList* ids);
389
398 void DataChanged() override;
399
405 void ClearLookup() override;
406
407protected:
409 ~vtkBitArray() override;
410
423
428 bool AllocateTuples(vtkIdType numTuples);
429
434
435 vtkBuffer<ValueType>* Buffer; // pointer to data
436 std::vector<double> LegacyTuple;
437
441 VTK_DEPRECATED_IN_9_7_0("Use ReserveTuples")
443 {
444 if (!this->ReserveTuples(size / this->NumberOfComponents + 1))
445 {
446 return nullptr;
447 }
448 return this->Buffer->GetBuffer();
449 }
450
451private:
452 // hide superclass' DeepCopy() from the user and the compiler
453 void DeepCopy(vtkDataArray& da) { this->vtkDataArray::DeepCopy(&da); }
454
455 vtkBitArray(const vtkBitArray&) = delete;
456 void operator=(const vtkBitArray&) = delete;
457
458 vtkBitArrayLookup* Lookup;
459 void UpdateLookup();
460};
461
462// Declare vtkArrayDownCast implementations for vtkBitArray:
464
465inline void vtkBitArray::SetValue(vtkIdType id, int value)
466{
467 const auto bitsetDiv = std::div(id, static_cast<vtkIdType>(8));
468 const vtkIdType &bitsetId = bitsetDiv.quot, &bitId = bitsetDiv.rem;
469 ValueType mask = 0x80 >> bitId; // NOLINT(clang-analyzer-core.BitwiseShift)
470 this->Buffer->GetBuffer()[bitsetId] =
471 static_cast<ValueType>((value != 0) ? (this->Buffer->GetBuffer()[bitsetId] | mask)
472 : (this->Buffer->GetBuffer()[bitsetId] & (~mask)));
473 this->DataChanged();
474}
475
476inline void vtkBitArray::InsertValue(vtkIdType valueIdx, int value)
477{
478 if (valueIdx >= this->Capacity)
479 {
480 if (!this->ReserveTuples((valueIdx + 1) / this->NumberOfComponents + 1))
481 {
482 return;
483 }
484 }
485 this->SetValue(valueIdx, value);
486 if (valueIdx > this->MaxId)
487 {
488 this->MaxId = valueIdx;
490 }
491}
492
494{
495 return vtkVariant(this->GetValue(id));
496}
497
499{
500 this->SetValue(id, value.ToInt());
501}
502
504{
505 this->InsertValue(id, value.ToInt());
506}
507
509{
510 this->InsertValue(this->MaxId + 1, i);
511 return this->MaxId;
512}
513VTK_ABI_NAMESPACE_END
514#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:33
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:44
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.
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.
bool AllocateTuples(vtkIdType numTuples)
Allocate space for numTuples.
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:42
void SetValue(vtkIdType id, int value)
Set the data at a particular index.
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 ShallowCopy(vtkDataArray *da) override
Shallow copy of another bit array.
void GetTuple(vtkIdType i, double *tuple) override
Copy the tuple value into a user-provided array.
std::vector< double > LegacyTuple
void SetTuple(vtkIdType i, const double *tuple) override
Set the tuple value at the ith location in the array.
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.
void SetTuple(vtkIdType i, const float *tuple) override
Set the tuple value at the ith location in the array.
vtkIdType InsertNextTuple(const float *tuple) override
Insert (memory allocation performed) the tuple onto the end of the array.
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 InsertTuple(vtkIdType i, const float *tuple) override
Insert (memory allocation performed) the tuple into the ith location in the array.
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:43
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:79
int GetArrayType() const override
Method for type-checking in FastDownCast implementations.
Definition vtkBitArray.h:77
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:37
@ VTK_DATA_ARRAY_DELETE
Definition vtkBitArray.h:38
@ VTK_DATA_ARRAY_USER_DEFINED
Definition vtkBitArray.h:40
@ VTK_DATA_ARRAY_ALIGNED_FREE
Definition vtkBitArray.h:39
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:78
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_EXPECTS(x)
#define VTK_NEWINSTANCE