VTK: Common/Core/vtkGenericDataArrayLookupHelper.h Source File

| VTK 9.3.1

vtkGenericDataArrayLookupHelper.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
10 #ifndef vtkGenericDataArrayLookupHelper_h
11 #define vtkGenericDataArrayLookupHelper_h
12
13 #include "vtkIdList.h"
14 #include <algorithm>
15 #include <cmath>
16 #include <limits>
17 #include <unordered_map>
18 #include <vector>
19
20 namespace vtkGenericDataArrayLookupHelper_detail
21 {
22 VTK_ABI_NAMESPACE_BEGIN
23 template <typename T, bool>
24 struct has_NaN;
25
26 template <typename T>
27 struct has_NaN<T, true>
28 {
29 static bool isnan(T x) { return std::isnan(x); }
30 };
31
32 template <typename T>
33 struct has_NaN<T, false>
34 {
35 static bool isnan(T) { return false; }
36 };
37
38 template <typename T>
39 bool isnan(T x)
40 {
41 // Select the correct partially specialized type.
42 return has_NaN<T, std::numeric_limits<T>::has_quiet_NaN>::isnan(x);
43 }
44 VTK_ABI_NAMESPACE_END
45 } // namespace detail
46
47 VTK_ABI_NAMESPACE_BEGIN
48 template <class ArrayTypeT>
49 class vtkGenericDataArrayLookupHelper
50 {
51 public:
52 typedef ArrayTypeT ArrayType;
53 typedef typename ArrayType::ValueType ValueType;
54
55 vtkGenericDataArrayLookupHelper() = default;
56
57 ~vtkGenericDataArrayLookupHelper() { this->ClearLookup(); }
58
59 void SetArray(ArrayTypeT* array)
60 {
61 if (this->AssociatedArray != array)
62 {
63 this->ClearLookup();
64 this->AssociatedArray = array;
65 }
66 }
67
68 vtkIdType LookupValue(ValueType elem)
69 {
70 this->UpdateLookup();
71 auto indices = FindIndexVec(elem);
72 if (indices == nullptr)
73 {
74 return -1;
75 }
76 return indices->front();
77 }
78
79 void LookupValue(ValueType elem, vtkIdList* ids)
80 {
81 ids->Reset();
82 this->UpdateLookup();
83 auto indices = FindIndexVec(elem);
84 if (indices)
85 {
86 ids->Allocate(static_cast<vtkIdType>(indices->size()));
87 for (auto index : *indices)
88 {
89 ids->InsertNextId(index);
90 }
91 }
92 }
93
95
98 void ClearLookup()
99 {
100 this->ValueMap.clear();
101 this->NanIndices.clear();
102 }
104
105 private:
106 vtkGenericDataArrayLookupHelper(const vtkGenericDataArrayLookupHelper&) = delete;
107 void operator=(const vtkGenericDataArrayLookupHelper&) = delete;
108
109 void UpdateLookup()
110 {
111 if (!this->AssociatedArray || (this->AssociatedArray->GetNumberOfTuples() < 1) ||
112 (!this->ValueMap.empty() || !this->NanIndices.empty()))
113 {
114 return;
115 }
116
117 vtkIdType num = this->AssociatedArray->GetNumberOfValues();
118 this->ValueMap.reserve(num);
119 for (vtkIdType i = 0; i < num; ++i)
120 {
121 auto value = this->AssociatedArray->GetValue(i);
122 if (vtkGenericDataArrayLookupHelper_detail::isnan(value))
123 {
124 NanIndices.push_back(i);
125 }
126 this->ValueMap[value].push_back(i);
127 }
128 }
129
130 // Return a pointer to the relevant vector of indices if specified value was
131 // found in the array.
132 std::vector<vtkIdType>* FindIndexVec(ValueType value)
133 {
134 std::vector<vtkIdType>* indices{ nullptr };
135 if (vtkGenericDataArrayLookupHelper_detail::isnan(value) && !this->NanIndices.empty())
136 {
137 indices = &this->NanIndices;
138 }
139 const auto& pos = this->ValueMap.find(value);
140 if (pos != this->ValueMap.end())
141 {
142 indices = &pos->second;
143 }
144 return indices;
145 }
146
147 ArrayTypeT* AssociatedArray{ nullptr };
148 std::unordered_map<ValueType, std::vector<vtkIdType>> ValueMap;
149 std::vector<vtkIdType> NanIndices;
150 };
151
152 VTK_ABI_NAMESPACE_END
153 #endif
154 // VTK-HeaderTest-Exclude: vtkGenericDataArrayLookupHelper.h
vtkGenericDataArrayLookupHelper
internal class used by vtkGenericDataArray to support LookupValue.
Definition: vtkGenericDataArrayLookupHelper.h:50
vtkGenericDataArrayLookupHelper::~vtkGenericDataArrayLookupHelper
~vtkGenericDataArrayLookupHelper()
Definition: vtkGenericDataArrayLookupHelper.h:57
vtkGenericDataArrayLookupHelper::LookupValue
void LookupValue(ValueType elem, vtkIdList *ids)
Definition: vtkGenericDataArrayLookupHelper.h:79
vtkGenericDataArrayLookupHelper::ArrayType
ArrayTypeT ArrayType
Definition: vtkGenericDataArrayLookupHelper.h:52
vtkGenericDataArrayLookupHelper::vtkGenericDataArrayLookupHelper
vtkGenericDataArrayLookupHelper()=default
vtkGenericDataArrayLookupHelper::ClearLookup
void ClearLookup()
Release any allocated memory for internal data-structures.
Definition: vtkGenericDataArrayLookupHelper.h:98
vtkGenericDataArrayLookupHelper::SetArray
void SetArray(ArrayTypeT *array)
Definition: vtkGenericDataArrayLookupHelper.h:59
vtkGenericDataArrayLookupHelper::LookupValue
vtkIdType LookupValue(ValueType elem)
Definition: vtkGenericDataArrayLookupHelper.h:68
vtkGenericDataArrayLookupHelper::ValueType
ArrayType::ValueType ValueType
Definition: vtkGenericDataArrayLookupHelper.h:53
vtkIdList
list of point or cell ids
Definition: vtkIdList.h:32
vtkIdList::Allocate
int Allocate(vtkIdType sz, int strategy=0)
Allocate a capacity for sz ids in the list and set the number of stored ids in the list to 0.
vtkIdList::InsertNextId
vtkIdType InsertNextId(vtkIdType vtkid)
Add the id specified to the end of the list.
Definition: vtkIdList.h:235
vtkIdList::Reset
void Reset()
Reset to an empty state but retain previously allocated memory.
Definition: vtkIdList.h:143
vtkGenericDataArrayLookupHelper_detail
Definition: vtkGenericDataArrayLookupHelper.h:21
vtkGenericDataArrayLookupHelper_detail::isnan
bool isnan(T x)
Definition: vtkGenericDataArrayLookupHelper.h:39
vtkX3D::value
@ value
Definition: vtkX3D.h:220
vtkX3D::index
@ index
Definition: vtkX3D.h:246
vtkGenericDataArrayLookupHelper_detail::has_NaN< T, false >::isnan
static bool isnan(T)
Definition: vtkGenericDataArrayLookupHelper.h:35
vtkGenericDataArrayLookupHelper_detail::has_NaN< T, true >::isnan
static bool isnan(T x)
Definition: vtkGenericDataArrayLookupHelper.h:29
vtkGenericDataArrayLookupHelper_detail::has_NaN
Definition: vtkGenericDataArrayLookupHelper.h:24
vtkIdList.h
vtkIdType
int vtkIdType
Definition: vtkType.h:315