VTK
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 <algorithm>
26#include "vtkIdList.h"
27
28namespace detail
29{
30 // this can be removed when C++11 is required.
31 template< class T > struct remove_const { typedef T type; };
32 template< class T > struct remove_const<const T> { typedef T type; };
33}
34
35template <class ArrayTypeT>
37{
38public:
39 typedef ArrayTypeT ArrayType;
40 typedef typename ArrayType::ValueType ValueType;
41
42 // Constructor.
44 : AssociatedArray(NULL),
45 SortedArray(NULL)
46 {
47 }
49 {
50 this->ClearLookup();
51 }
52
53 void SetArray(ArrayTypeT *array)
54 {
55 if (this->AssociatedArray != array)
56 {
57 this->ClearLookup();
58 this->AssociatedArray = array;
59 }
60 }
61
63 {
64 this->UpdateLookup();
65 ValueWithIndex temp;
66 temp.Value = elem;
67 ValueWithIndex* pos =
68 std::lower_bound(this->SortedArray,
69 this->SortedArray + this->SortedArraySize, temp);
70 if (pos == (this->SortedArray + this->SortedArraySize))
71 {
72 return -1;
73 }
74 if (pos->Value != elem)
75 {
76 return -1;
77 }
78 return pos->Index;
79 }
80
82 {
83 this->UpdateLookup();
84 ValueWithIndex temp;
85 temp.Value = elem;
86 std::pair<ValueWithIndex*, ValueWithIndex*> range =
87 std::equal_range(this->SortedArray,
88 this->SortedArray + this->SortedArraySize, temp);
89 while (range.first != range.second)
90 {
91 // assert(range.first->Value == elem);
92 ids->InsertNextId(range.first->Index);
93 ++range.first;
94 }
95 }
96
98
102 {
103 free(this->SortedArray);
104 this->SortedArray = NULL;
105 this->SortedArraySize = 0;
106 }
108
109private:
111 void operator=(const vtkGenericDataArrayLookupHelper&) VTK_DELETE_FUNCTION;
112
113 struct ValueWithIndex
114 {
116 vtkIdType Index;
117 inline bool operator<(const ValueWithIndex& other) const
118 {
119 return this->Value < other.Value;
120 }
121 };
122
123 void UpdateLookup()
124 {
125 if (!this->AssociatedArray || this->SortedArray)
126 {
127 return;
128 }
129
130 int numComps = this->AssociatedArray->GetNumberOfComponents();
131 this->SortedArraySize =
132 this->AssociatedArray->GetNumberOfTuples() * numComps;
133
134 if (this->SortedArraySize == 0)
135 {
136 return;
137 }
138
139 this->SortedArray = reinterpret_cast<ValueWithIndex*>(
140 malloc(this->SortedArraySize * sizeof(ValueWithIndex)));
141 for (vtkIdType cc = 0, max = this->AssociatedArray->GetNumberOfValues();
142 cc < max; ++cc)
143 {
144 ValueWithIndex& item = this->SortedArray[cc];
145 item.Value = this->AssociatedArray->GetValue(cc);
146 item.Index = cc;
147 }
148 std::sort(this->SortedArray, this->SortedArray + this->SortedArraySize);
149 }
150
151 ArrayTypeT *AssociatedArray;
152 ValueWithIndex* SortedArray;
153 vtkIdType SortedArraySize;
154};
155
156#endif
157// 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:37
vtkIdType InsertNextId(const vtkIdType vtkid)
Add the id specified to the end of the list.
Definition: vtkIdList.h:182
@ range
Definition: vtkX3D.h:238
@ type
Definition: vtkX3D.h:516
int vtkIdType
Definition: vtkType.h:287
VTKCOMMONCORE_EXPORT bool operator<(const vtkUnicodeString &lhs, const vtkUnicodeString &rhs)
#define max(a, b)