VTK  9.1.0
vtkGenericDataArrayLookupHelper.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkGenericDataArrayLookupHelper.h
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14=========================================================================*/
22#ifndef vtkGenericDataArrayLookupHelper_h
23#define vtkGenericDataArrayLookupHelper_h
24
25#include "vtkIdList.h"
26#include <algorithm>
27#include <cmath>
28#include <limits>
29#include <unordered_map>
30#include <vector>
31
32namespace detail
33{
34template <typename T, bool>
35struct has_NaN;
36
37template <typename T>
38struct has_NaN<T, true>
39{
40 static bool isnan(T x) { return std::isnan(x); }
41};
42
43template <typename T>
44struct has_NaN<T, false>
45{
46 static bool isnan(T) { return false; }
47};
48
49template <typename T>
50bool isnan(T x)
51{
52 // Select the correct partially specialized type.
54}
55} // namespace detail
56
57template <class ArrayTypeT>
59{
60public:
61 typedef ArrayTypeT ArrayType;
62 typedef typename ArrayType::ValueType ValueType;
63
65
67
68 void SetArray(ArrayTypeT* array)
69 {
70 if (this->AssociatedArray != array)
71 {
72 this->ClearLookup();
73 this->AssociatedArray = array;
74 }
75 }
76
78 {
79 this->UpdateLookup();
80 auto indices = FindIndexVec(elem);
81 if (indices == nullptr)
82 {
83 return -1;
84 }
85 return indices->front();
86 }
87
89 {
90 ids->Reset();
91 this->UpdateLookup();
92 auto indices = FindIndexVec(elem);
93 if (indices)
94 {
95 ids->Allocate(static_cast<vtkIdType>(indices->size()));
96 for (auto index : *indices)
97 {
98 ids->InsertNextId(index);
99 }
100 }
101 }
102
104
108 {
109 this->ValueMap.clear();
110 this->NanIndices.clear();
111 }
113
114private:
116 void operator=(const vtkGenericDataArrayLookupHelper&) = delete;
117
118 void UpdateLookup()
119 {
120 if (!this->AssociatedArray || (this->AssociatedArray->GetNumberOfTuples() < 1) ||
121 (!this->ValueMap.empty() || !this->NanIndices.empty()))
122 {
123 return;
124 }
125
126 vtkIdType num = this->AssociatedArray->GetNumberOfValues();
127 this->ValueMap.reserve(num);
128 for (vtkIdType i = 0; i < num; ++i)
129 {
130 auto value = this->AssociatedArray->GetValue(i);
131 if (::detail::isnan(value))
132 {
133 NanIndices.push_back(i);
134 }
135 this->ValueMap[value].push_back(i);
136 }
137 }
138
139 // Return a pointer to the relevant vector of indices if specified value was
140 // found in the array.
141 std::vector<vtkIdType>* FindIndexVec(ValueType value)
142 {
143 std::vector<vtkIdType>* indices{ nullptr };
144 if (::detail::isnan(value) && !this->NanIndices.empty())
145 {
146 indices = &this->NanIndices;
147 }
148 const auto& pos = this->ValueMap.find(value);
149 if (pos != this->ValueMap.end())
150 {
151 indices = &pos->second;
152 }
153 return indices;
154 }
155
156 ArrayTypeT* AssociatedArray{ nullptr };
157 std::unordered_map<ValueType, std::vector<vtkIdType>> ValueMap;
158 std::vector<vtkIdType> NanIndices;
159};
160
161#endif
162// VTK-HeaderTest-Exclude: vtkGenericDataArrayLookupHelper.h
internal class used by vtkGenericDataArray to support LookupValue.
void LookupValue(ValueType elem, vtkIdList *ids)
void ClearLookup()
Release any allocated memory for internal data-structures.
list of point or cell ids
Definition: vtkIdList.h:140
vtkIdType InsertNextId(const vtkIdType vtkid)
Add the id specified to the end of the list.
Definition: vtkIdList.h:341
int Allocate(const vtkIdType sz, const int strategy=0)
Allocate a capacity for sz ids in the list and set the number of stored ids in the list to 0.
void Reset()
Reset to an empty state but retain previously allocated memory.
Definition: vtkIdList.h:254
@ value
Definition: vtkX3D.h:226
int vtkIdType
Definition: vtkType.h:332