Viskores  1.0
StableSortIndices.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_worklet_SortAndUniqueIndices_h
19 #define viskores_worklet_SortAndUniqueIndices_h
20 
25 
26 namespace viskores
27 {
28 namespace worklet
29 {
30 
34 {
36 
37  // Allows Sort to be called on an array that indexes into KeyPortal.
38  // If the values compare equal, the indices are compared to stabilize the
39  // result.
40  template <typename KeyPortalType>
42  {
43  using KeyType = typename KeyPortalType::ValueType;
44 
45  const KeyPortalType KeyPortal;
46 
48  IndirectSortPredicate(const KeyPortalType& keyPortal)
49  : KeyPortal(keyPortal)
50  {
51  }
52 
53  template <typename IndexType>
54  VISKORES_EXEC bool operator()(const IndexType& a, const IndexType& b) const
55  {
56  // If the values compare equal, compare the indices as well so we get
57  // consistent outputs.
58  const KeyType valueA = this->KeyPortal.Get(a);
59  const KeyType valueB = this->KeyPortal.Get(b);
60  if (valueA < valueB)
61  {
62  return true;
63  }
64  else if (valueB < valueA)
65  {
66  return false;
67  }
68  else
69  {
70  return a < b;
71  }
72  }
73  };
74 
75  // Allows you to pass an IndirectSortPredicate to a device algorithm without knowing the device.
76  template <typename KeyArrayType>
78  {
79  const KeyArrayType KeyArray;
80 
81  VISKORES_CONT IndirectSortPredicateExecObject(const KeyArrayType& keyArray)
82  : KeyArray(keyArray)
83  {
84  }
85 
86  template <typename Device>
88  Device,
89  viskores::cont::Token& token) const
90  {
91  auto keyPortal = this->KeyArray.PrepareForInput(Device(), token);
93  }
94  };
95 
96  // Allows Unique to be called on an array that indexes into KeyPortal.
97  template <typename KeyPortalType>
99  {
100  const KeyPortalType KeyPortal;
101 
103  IndirectUniquePredicate(const KeyPortalType& keyPortal)
104  : KeyPortal(keyPortal)
105  {
106  }
107 
108  template <typename IndexType>
109  VISKORES_EXEC bool operator()(const IndexType& a, const IndexType& b) const
110  {
111  return this->KeyPortal.Get(a) == this->KeyPortal.Get(b);
112  }
113  };
114 
115  // Allows you to pass an IndirectUniquePredicate to a device algorithm without knowing the device.
116  template <typename KeyArrayType>
118  {
119  const KeyArrayType KeyArray;
120 
121  VISKORES_CONT IndirectUniquePredicateExecObject(const KeyArrayType& keyArray)
122  : KeyArray(keyArray)
123  {
124  }
125 
126  template <typename Device>
128  Device,
129  viskores::cont::Token& token) const
130  {
131  auto keyPortal = this->KeyArray.PrepareForInput(Device(), token);
133  }
134  };
135 
147  template <typename KeyType, typename Storage>
150  IndexArrayType& indices)
151  {
153  using SortPredicate = IndirectSortPredicateExecObject<KeyArrayType>;
154 
156 
157  viskores::cont::Algorithm::Sort(device, indices, SortPredicate(keys));
158  }
159 
170  template <typename KeyType, typename Storage>
172  IndexArrayType& indices)
173  {
175  }
176 
181  template <typename KeyType, typename Storage>
185  {
186  // Generate the initial index array
187  IndexArrayType indices;
188  {
190  viskores::cont::Algorithm::Copy(device, indicesSrc, indices);
191  }
192 
193  StableSortIndices::Sort(device, keys, indices);
194 
195  return indices;
196  }
197 
202  template <typename KeyType, typename Storage>
205  {
207  }
208 
213  template <typename KeyType, typename Storage>
216  IndexArrayType& indices)
217  {
219  using UniquePredicate = IndirectUniquePredicateExecObject<KeyArrayType>;
220 
221  viskores::cont::Algorithm::Unique(device, indices, UniquePredicate(keys));
222  }
223 
228  template <typename KeyType, typename Storage>
230  IndexArrayType& indices)
231  {
233  }
234 };
235 }
236 } // end namespace viskores::worklet
237 
238 #endif // viskores_worklet_SortAndUniqueIndices_h
viskores::worklet::StableSortIndices::Unique
static void Unique(const viskores::cont::ArrayHandle< KeyType, Storage > &keys, IndexArrayType &indices)
Reduces the array returned by Sort so that the mapped keys are unique.
Definition: StableSortIndices.h:229
viskores::worklet::StableSortIndices::IndirectSortPredicate::KeyPortal
const KeyPortalType KeyPortal
Definition: StableSortIndices.h:45
ArrayHandle.h
viskores::cont::Algorithm::Unique
static void Unique(viskores::cont::DeviceAdapterId devId, viskores::cont::ArrayHandle< T, Storage > &values)
Definition: Algorithm.h:1098
viskores::worklet::StableSortIndices::IndirectUniquePredicate::IndirectUniquePredicate
IndirectUniquePredicate(const KeyPortalType &keyPortal)
Definition: StableSortIndices.h:103
viskores::worklet::StableSortIndices::IndirectUniquePredicate::operator()
bool operator()(const IndexType &a, const IndexType &b) const
Definition: StableSortIndices.h:109
viskores::worklet::StableSortIndices::Sort
static void Sort(const viskores::cont::ArrayHandle< KeyType, Storage > &keys, IndexArrayType &indices)
Permutes the indices array so that it will map keys into a stable sorted order.
Definition: StableSortIndices.h:171
viskores::cont::Algorithm::Sort
static void Sort(viskores::cont::DeviceAdapterId devId, viskores::cont::ArrayHandle< T, Storage > &values)
Definition: Algorithm.h:999
viskores::worklet::StableSortIndices::IndirectUniquePredicate
Definition: StableSortIndices.h:98
viskores::cont::ArrayHandle< viskores::Id >
viskores::worklet::StableSortIndices::IndirectSortPredicateExecObject::KeyArray
const KeyArrayType KeyArray
Definition: StableSortIndices.h:79
viskores::cont::DeviceAdapterTagAny
Tag for a device adapter used to specify that any device may be used for an operation.
Definition: DeviceAdapterTag.h:194
viskores::worklet::StableSortIndices::IndirectSortPredicate::KeyType
typename KeyPortalType::ValueType KeyType
Definition: StableSortIndices.h:43
viskores::worklet::StableSortIndices::IndirectUniquePredicateExecObject
Definition: StableSortIndices.h:117
viskores::worklet::StableSortIndices::Sort
static IndexArrayType Sort(const viskores::cont::ArrayHandle< KeyType, Storage > &keys)
Returns an index array that maps the keys array into a stable sorted ordering.
Definition: StableSortIndices.h:203
viskores::worklet::StableSortIndices::Sort
static IndexArrayType Sort(viskores::cont::DeviceAdapterId device, const viskores::cont::ArrayHandle< KeyType, Storage > &keys)
Returns an index array that maps the keys array into a stable sorted ordering.
Definition: StableSortIndices.h:182
viskores::cont::Algorithm::Copy
static bool Copy(viskores::cont::DeviceAdapterId devId, const viskores::cont::ArrayHandle< T, CIn > &input, viskores::cont::ArrayHandle< U, COut > &output)
Definition: Algorithm.h:422
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores::worklet::StableSortIndices
Produces an ArrayHandle<viskores::Id> index array that stable-sorts and optionally uniquifies an inpu...
Definition: StableSortIndices.h:33
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
viskores::worklet::StableSortIndices::IndirectSortPredicateExecObject::PrepareForExecution
IndirectSortPredicate< typename KeyArrayType::ReadPortalType > PrepareForExecution(Device, viskores::cont::Token &token) const
Definition: StableSortIndices.h:87
Algorithm.h
ArrayHandleIndex.h
viskores::worklet::StableSortIndices::IndirectSortPredicateExecObject::IndirectSortPredicateExecObject
IndirectSortPredicateExecObject(const KeyArrayType &keyArray)
Definition: StableSortIndices.h:81
viskores::cont::ArrayHandle::GetNumberOfValues
viskores::Id GetNumberOfValues() const
Returns the number of entries in the array.
Definition: ArrayHandle.h:482
VISKORES_ASSERT
#define VISKORES_ASSERT(condition)
Definition: Assert.h:51
viskores::worklet::StableSortIndices::Sort
static void Sort(viskores::cont::DeviceAdapterId device, const viskores::cont::ArrayHandle< KeyType, Storage > &keys, IndexArrayType &indices)
Permutes the indices array so that it will map keys into a stable sorted order.
Definition: StableSortIndices.h:148
viskores::worklet::StableSortIndices::Unique
static void Unique(viskores::cont::DeviceAdapterId device, const viskores::cont::ArrayHandle< KeyType, Storage > &keys, IndexArrayType &indices)
Reduces the array returned by Sort so that the mapped keys are unique.
Definition: StableSortIndices.h:214
viskores::cont::DeviceAdapterId
An object used to specify a device.
Definition: DeviceAdapterTag.h:66
viskores::worklet::StableSortIndices::IndirectUniquePredicate::KeyPortal
const KeyPortalType KeyPortal
Definition: StableSortIndices.h:100
viskores::worklet::StableSortIndices::IndirectSortPredicate::operator()
bool operator()(const IndexType &a, const IndexType &b) const
Definition: StableSortIndices.h:54
viskores::worklet::StableSortIndices::IndirectUniquePredicateExecObject::PrepareForExecution
IndirectUniquePredicate< typename KeyArrayType::ReadPortalType > PrepareForExecution(Device, viskores::cont::Token &token) const
Definition: StableSortIndices.h:127
viskores::worklet::StableSortIndices::IndirectSortPredicateExecObject
Definition: StableSortIndices.h:77
viskores::worklet::StableSortIndices::IndirectUniquePredicateExecObject::IndirectUniquePredicateExecObject
IndirectUniquePredicateExecObject(const KeyArrayType &keyArray)
Definition: StableSortIndices.h:121
ExecutionObjectBase.h
viskores::cont::Token
A token to hold the scope of an ArrayHandle or other object.
Definition: Token.h:43
viskores::cont::ExecutionObjectBase
Base ExecutionObjectBase for execution objects to inherit from so that you can use an arbitrary objec...
Definition: ExecutionObjectBase.h:39
viskores::worklet::StableSortIndices::IndirectSortPredicate
Definition: StableSortIndices.h:41
VISKORES_EXEC
#define VISKORES_EXEC
Definition: ExportMacros.h:59
viskores::worklet::StableSortIndices::IndirectUniquePredicateExecObject::KeyArray
const KeyArrayType KeyArray
Definition: StableSortIndices.h:119
viskores::worklet::StableSortIndices::IndirectSortPredicate::IndirectSortPredicate
IndirectSortPredicate(const KeyPortalType &keyPortal)
Definition: StableSortIndices.h:48
viskores::cont::ArrayHandleIndex
An implicit array handle containing the its own indices.
Definition: ArrayHandleIndex.h:64