VTK  9.5.20251019
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 "vtkCommonCoreModule.h" // For export macro
23#include "vtkDataArray.h"
24
25#include <cassert> // for assert
26
27VTK_ABI_NAMESPACE_BEGIN
28class vtkBitArrayLookup;
29
30class VTKCOMMONCORE_EXPORT vtkBitArray : public vtkDataArray
31{
32public:
34 {
37 VTK_DATA_ARRAY_ALIGNED_FREE = vtkAbstractArray::VTK_DATA_ARRAY_ALIGNED_FREE,
38 VTK_DATA_ARRAY_USER_DEFINED = vtkAbstractArray::VTK_DATA_ARRAY_USER_DEFINED
39 };
40 using ArrayTypeTag = std::integral_constant<int, vtkArrayTypes::BitArray>;
41 using DataTypeTag = std::integral_constant<int, VTK_BIT>;
42 using ValueType = unsigned char;
43
44 static vtkBitArray* New();
45 vtkTypeMacro(vtkBitArray, vtkDataArray);
46 void PrintSelf(ostream& os, vtkIndent indent) override;
47
55
60 vtkTypeBool Allocate(vtkIdType sz, vtkIdType ext = 1000) override;
61
65 void Initialize() override;
66
67 // satisfy vtkDataArray API
68 int GetArrayType() const override { return vtkBitArray::ArrayTypeTag::value; }
69 int GetDataType() const override { return vtkBitArray::DataTypeTag::value; }
70 int GetDataTypeSize() const override { return 0; }
71
75 void SetNumberOfTuples(vtkIdType number) override;
76
81 bool SetNumberOfValues(vtkIdType number) override;
82
92
100
108 void InsertTuples(vtkIdList* dstIds, vtkIdList* srcIds, vtkAbstractArray* source) override;
109
118 vtkIdType dstStart, vtkIdList* srcIds, vtkAbstractArray* source) override;
119
128 vtkIdType dstStart, vtkIdType n, vtkIdType srcStart, vtkAbstractArray* source) override;
129
138
143 double* GetTuple(vtkIdType i) override;
144
148 void GetTuple(vtkIdType i, double* tuple) override;
149
151
156 void SetTuple(vtkIdType i, const float* tuple) override;
157 void SetTuple(vtkIdType i, const double* tuple) override;
159
161
167 void InsertTuple(vtkIdType i, const float* tuple) override;
168 void InsertTuple(vtkIdType i, const double* tuple) override;
170
172
177 vtkIdType InsertNextTuple(const float* tuple) override;
178 vtkIdType InsertNextTuple(const double* tuple) override;
180
182
189 void RemoveTuple(vtkIdType id) override;
190 void RemoveFirstTuple() override;
191 void RemoveLastTuple() override;
193
202 void SetComponent(vtkIdType i, int j, double c) override;
203
207 void Squeeze() override;
208
212 vtkTypeBool Resize(vtkIdType numTuples) override;
213
217 int GetValue(vtkIdType id) const;
218
225 void SetValue(vtkIdType id, int value);
226
232 void InsertValue(vtkIdType id, int i);
233
239 void SetVariantValue(vtkIdType idx, vtkVariant value) override;
240
246 void InsertVariantValue(vtkIdType idx, vtkVariant value) override;
247
248 vtkIdType InsertNextValue(int i);
249
256 void InsertComponent(vtkIdType i, int j, double c) override;
257
261 ValueType* GetPointer(vtkIdType id) { return this->Array + id / 8; }
262
269
270 void* WriteVoidPointer(vtkIdType id, vtkIdType number) override
271 {
272 return this->WritePointer(id, number);
273 }
274
275 void* GetVoidPointer(vtkIdType id) override { return static_cast<void*>(this->GetPointer(id)); }
276
280 void DeepCopy(vtkDataArray* da) override;
281 void DeepCopy(vtkAbstractArray* aa) override { this->Superclass::DeepCopy(aa); }
282
284
295#ifndef __VTK_WRAP__
297 ValueType* array, vtkIdType size, int save, int deleteMethod = VTK_DATA_ARRAY_DELETE);
298#endif
299 void SetVoidArray(void* array, vtkIdType size, int save) override
300 {
301 this->SetArray(static_cast<ValueType*>(array), size, save);
302 }
303 void SetVoidArray(void* array, vtkIdType size, int save, int deleteMethod) override
304 {
305 this->SetArray(static_cast<ValueType*>(array), size, save, deleteMethod);
306 }
308
315 void SetArrayFreeFunction(void (*callback)(void*)) override;
316
321
323
327 void LookupValue(vtkVariant value, vtkIdList* ids) override;
329 void LookupValue(int value, vtkIdList* ids);
331
340 void DataChanged() override;
341
347 void ClearLookup() override;
348
349protected:
351 ~vtkBitArray() override;
352
365
366 ValueType* Array; // pointer to data
368 // function to resize data
369
370 int TupleSize; // used for data conversion
371 double* Tuple;
372
373 void (*DeleteFunction)(void*);
374
375private:
376 // hide superclass' DeepCopy() from the user and the compiler
377 void DeepCopy(vtkDataArray& da) { this->vtkDataArray::DeepCopy(&da); }
378
379 vtkBitArray(const vtkBitArray&) = delete;
380 void operator=(const vtkBitArray&) = delete;
381
382 vtkBitArrayLookup* Lookup;
383 void UpdateLookup();
384};
385
386// Declare vtkArrayDownCast implementations for vtkBitArray:
388
389inline void vtkBitArray::SetValue(vtkIdType id, int value)
390{
391 const auto bitsetDiv = std::div(id, static_cast<vtkIdType>(8));
392 const vtkIdType &bitsetId = bitsetDiv.quot, &bitId = bitsetDiv.rem;
393 ValueType mask = 0x80 >> bitId; // NOLINT(clang-analyzer-core.BitwiseShift)
394 this->Array[bitsetId] = static_cast<ValueType>(
395 (value != 0) ? (this->Array[bitsetId] | mask) : (this->Array[bitsetId] & (~mask)));
396 this->DataChanged();
397}
398
399inline void vtkBitArray::InsertValue(vtkIdType id, int value)
400{
401 if (id >= this->Size)
402 {
403 if (!this->ResizeAndExtend(id + 1))
404 {
405 return;
406 }
407 }
408 this->SetValue(id, value);
409 if (id > this->MaxId)
410 {
411 this->MaxId = id;
413 }
414 this->DataChanged();
415}
416
418{
419 this->SetValue(id, value.ToInt());
420}
421
423{
424 this->InsertValue(id, value.ToInt());
425}
426
428{
429 this->InsertValue(this->MaxId + 1, i);
430 this->DataChanged();
431 return this->MaxId;
432}
433
435{
436 this->ResizeAndExtend(this->MaxId + 1);
437}
438VTK_ABI_NAMESPACE_END
439#endif
Abstract superclass for all arrays.
Abstract superclass to iterate over elements in an vtkAbstractArray.
dynamic, self-adjusting array of bits
Definition vtkBitArray.h:31
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:42
vtkIdType InsertNextTuple(vtkIdType j, vtkAbstractArray *source) override
Insert the jth tuple in the source array, at the end in this array.
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.
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.
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 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.
void InsertComponent(vtkIdType i, int j, double c) override
Insert the data component at ith tuple and jth component location.
ValueType * WritePointer(vtkIdType id, vtkIdType number)
Get the address of a particular data index.
void LookupValue(int value, vtkIdList *ids)
Return the indices where a specific value appears.
void DeepCopy(vtkAbstractArray *aa) override
Deep copy of data.
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.
vtkIdType InsertNextTuple(const float *tuple) override
Insert (memory allocation performed) the tuple onto the end of the array.
vtkArrayIterator * NewIterator() override
Returns a new vtkBitArrayIterator instance.
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.
ValueType * GetPointer(vtkIdType id)
Direct manipulation of the underlying data.
void RemoveFirstTuple() override
These methods remove tuples from the data array.
double * Tuple
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.
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.
ValueType * 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:41
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.
~vtkBitArray() override
int GetDataTypeSize() const override
Return the size of the underlying data type.
Definition vtkBitArray.h:70
void RemoveLastTuple() override
These methods remove tuples from the data array.
int GetArrayType() const override
Method for type-checking in FastDownCast implementations.
Definition vtkBitArray.h:68
void SetTuple(vtkIdType i, vtkIdType j, vtkAbstractArray *source) override
Set the tuple at the ith location using the jth tuple in the source array.
std::integral_constant< int, vtkArrayTypes::BitArray > ArrayTypeTag
Definition vtkBitArray.h:40
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.
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...
int GetDataType() const override
Return the underlying data type.
Definition vtkBitArray.h:69
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.
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 unneeded memory.
ValueType * ResizeAndExtend(vtkIdType sz)
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.
abstract superclass for arrays of numeric data
void DeepCopy(vtkAbstractArray *aa) override
Deep copy of data.
list of point or cell ids
Definition vtkIdList.h:133
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 *)
int vtkIdType
Definition vtkType.h:367
void save(Archiver &ar, const std::string &str, const unsigned int version)
#define VTK_NEWINSTANCE