ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkPriorityQueueContainer.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkPriorityQueueContainer_h
19#define itkPriorityQueueContainer_h
20
21#include "itkVectorContainer.h"
22#include "itkIntTypes.h"
23#include "itkNumericTraits.h"
24
25#include <functional>
26#include <queue>
27#include <vector>
28
29namespace itk
30{
31// first define a common interface all the wrapper will have to abide to
32// this will let us define our own wrapper with different behavior.
33// As an example we define below a wrapper for a min sorted or max sorted
34// queue.
35template <typename TElement, typename TElementIdentifier = IdentifierType>
36class ITK_TEMPLATE_EXPORT ElementWrapperInterface
37{
38public:
39 using ElementType = TElement;
40 using ElementIdentifierType = TElementIdentifier;
41
43
45 virtual ~ElementWrapperInterface() = default;
47
48 [[nodiscard]] virtual ElementIdentifierType
49 GetLocation(const ElementType & element) const = 0;
50
51 virtual void
52 SetLocation(ElementType & element, const ElementIdentifierType & identifier) = 0;
53
54 [[nodiscard]] virtual bool
55 is_less(const ElementType & element1, const ElementType & element2) const = 0;
56
57 [[nodiscard]] virtual bool
58 is_greater(const ElementType & element1, const ElementType & element2) const = 0;
59};
60// ------------------------------------------------------------------------
61
62
63// ------------------------------------------------------------------------
64// If you want to manage the items outside the queue for example, if you don't
65// want the queue to manage the items memory, then you can use this wrapper
66// around pointers to items. It follows the ElementWrapperInterface and thus
67// can be used in the queue.
68//
69template <typename TElementWrapperPointer, typename TElementIdentifier = IdentifierType>
70class ITK_TEMPLATE_EXPORT ElementWrapperPointerInterface
71{
72public:
73 using ElementWrapperPointerType = TElementWrapperPointer;
74 using ElementIdentifierType = TElementIdentifier;
75
77
81
82 TElementIdentifier
83 GetLocation(const ElementWrapperPointerType & element) const;
84
85 void
87
88 virtual bool
89 is_less(const ElementWrapperPointerType & element1, const ElementWrapperPointerType & element2) const;
90
91 virtual bool
92 is_greater(const ElementWrapperPointerType & element1, const ElementWrapperPointerType & element2) const;
93};
94// ------------------------------------------------------------------------
95
96// ------------------------------------------------------------------------
97// To follow ITK rule, we template the ElementWrapperType priority and the element
98// identifier type.
99// For example, as we want to use this for decimation, the element will be some
100// kind of cell or point pointer, the priority will be whatever you want it to
101// be as long as you define the comparison operators, and the identifier will
102// set according to the size of the vector you want to create.
103//
104// this implementation is used for min sorted priorityqueue
105template <typename TElement, typename TElementPriority = double, typename TElementIdentifier = IdentifierType>
106class ITK_TEMPLATE_EXPORT MinPriorityQueueElementWrapper
107 : public ElementWrapperInterface<MinPriorityQueueElementWrapper<TElement, TElementPriority, TElementIdentifier>,
108 TElementIdentifier>
109{
110public:
113 TElementIdentifier>;
114 using ElementType = TElement;
115 using ElementPriorityType = TElementPriority;
116 using ElementIdentifierType = TElementIdentifier;
117
121
123
125
126 bool
128
129 bool
130 operator<(const MinPriorityQueueElementWrapper & other) const;
131
132 bool
134
136 GetLocation(const MinPriorityQueueElementWrapper & element) const override;
137
138 void
139 SetLocation(MinPriorityQueueElementWrapper & element, const ElementIdentifierType & identifier) override;
140
141 // still virtual to be able to overload it in the Max flavor
142 bool
144 const MinPriorityQueueElementWrapper & element2) const override;
145
146 bool
148 const MinPriorityQueueElementWrapper & element2) const override;
149};
150// ------------------------------------------------------------------------
151
152
153// ------------------------------------------------------------------------
154// this implementation is used for max sorted priorityqueue
155// most of the job is already done, just need to overload the less
156// and greater ops.
157template <typename TElement, typename TElementPriority = double, typename TElementIdentifier = IdentifierType>
158class ITK_TEMPLATE_EXPORT MaxPriorityQueueElementWrapper
159 : public MinPriorityQueueElementWrapper<TElement, TElementPriority, TElementIdentifier>
160{
161public:
162 using ElementType = TElement;
163 using ElementPriorityType = TElementPriority;
164 using ElementIdentifierType = TElementIdentifier;
165
168
170
171 virtual bool
173
174 bool
175 is_less(const Superclass & element1, const Superclass & element2) const override;
176
177 virtual bool
179
180 bool
181 is_greater(const Superclass & element1, const Superclass & element2) const override;
182};
183// ------------------------------------------------------------------------
184
185
186// ------------------------------------------------------------------------
187// finally, implement the priority queue itself on top of an
188// itk::VectorContainer
189template <typename TElementWrapper,
190 typename TElementWrapperInterface,
191 typename TElementPriority = double,
192 typename TElementIdentifier = IdentifierType>
193class ITK_TEMPLATE_EXPORT PriorityQueueContainer : public VectorContainer<TElementIdentifier, TElementWrapper>
194{
195public:
200
201 using ElementIdentifierType = TElementIdentifier;
202 using ElementWrapperType = TElementWrapper;
203 using ElementInterfaceType = TElementWrapperInterface;
204
206
207public:
209 ~PriorityQueueContainer() override = default;
210
211 template <typename TInputIterator>
212 PriorityQueueContainer(TInputIterator first, TInputIterator last)
213 : Superclass()
214 {
215 TInputIterator it = first;
216 while (it != last)
217 {
218 this->Push(*it);
219 ++it;
220 }
221 }
222
223public:
224 itkNewMacro(Self);
225 itkOverrideGetNameOfClassMacro(PriorityQueueContainer);
226
227 // void Reserve( ElementIdentifier NbOfElementsToStore )
228 //{ this->Superclass->Reserve( NbOfElementsToStore ); }
229 // void Squeeze( ) { this->Superclass->Squeeze( ); }
230 void
232 [[nodiscard]] bool
233 Empty() const;
234 void
236
237 const ElementWrapperType &
238 Peek() const;
239
240 void
242
246 bool
247 Update(const ElementWrapperType & element);
248
252 bool
254
255protected:
256 // One instance of the interface to deal with the functions calls
258
259 inline ElementWrapperType &
261 {
262 return this->operator[](identifier);
263 }
264
265 inline const ElementWrapperType &
267 {
268 return this->operator[](identifier);
269 }
270
271 inline void
273 {
274 this->operator[](identifier) = element;
275 m_Interface.SetLocation(element, identifier);
276 }
277
278 inline ElementIdentifierType
279 GetParent(const ElementIdentifierType & identifier) const
280 {
281 return (identifier - 1) >> 1;
282 }
283
284 inline ElementIdentifierType
285 GetLeft(const ElementIdentifierType & identifier) const
286 {
287 return (identifier << 1) + 1;
288 }
289
290 inline ElementIdentifierType
291 GetRight(const ElementIdentifierType & identifier) const
292 {
293 return (identifier << 1) + 2;
294 }
295
296 inline bool
298 {
299 return iId > 0;
300 }
301
302 void
304
305
306 void
308};
309// ------------------------------------------------------------------------
310
311} // namespace itk
312
313#include "itkPriorityQueueContainer.hxx"
314#endif
virtual bool is_greater(const ElementType &element1, const ElementType &element2) const =0
virtual ~ElementWrapperInterface()=default
ITK_DEFAULT_COPY_AND_MOVE(ElementWrapperInterface)
virtual void SetLocation(ElementType &element, const ElementIdentifierType &identifier)=0
virtual bool is_less(const ElementType &element1, const ElementType &element2) const =0
virtual ElementIdentifierType GetLocation(const ElementType &element) const =0
virtual ~ElementWrapperPointerInterface()=default
ITK_DEFAULT_COPY_AND_MOVE(ElementWrapperPointerInterface)
virtual bool is_less(const ElementWrapperPointerType &element1, const ElementWrapperPointerType &element2) const
void SetLocation(ElementWrapperPointerType &element, const ElementIdentifierType &identifier)
TElementIdentifier GetLocation(const ElementWrapperPointerType &element) const
virtual bool is_greater(const ElementWrapperPointerType &element1, const ElementWrapperPointerType &element2) const
static const ElementIdentifierType m_ElementNotFound
bool is_greater(const Superclass &element1, const Superclass &element2) const override
MinPriorityQueueElementWrapper< ElementType, ElementPriorityType, ElementIdentifierType > Superclass
bool is_less(const Superclass &element1, const Superclass &element2) const override
MaxPriorityQueueElementWrapper(ElementType element, ElementPriorityType priority)
virtual bool is_greater(const MaxPriorityQueueElementWrapper &element1, const MaxPriorityQueueElementWrapper &element2) const
virtual bool is_less(const MaxPriorityQueueElementWrapper &element1, const MaxPriorityQueueElementWrapper &element2) const
bool is_greater(const MinPriorityQueueElementWrapper &element1, const MinPriorityQueueElementWrapper &element2) const override
bool operator>(const MinPriorityQueueElementWrapper &other) const
ElementWrapperInterface< MinPriorityQueueElementWrapper< TElement, TElementPriority, TElementIdentifier >, TElementIdentifier > Superclass
bool operator==(const MinPriorityQueueElementWrapper &other) const
bool is_less(const MinPriorityQueueElementWrapper &element1, const MinPriorityQueueElementWrapper &element2) const override
ElementIdentifierType GetLocation(const MinPriorityQueueElementWrapper &element) const override
void SetLocation(MinPriorityQueueElementWrapper &element, const ElementIdentifierType &identifier) override
MinPriorityQueueElementWrapper(ElementType element, ElementPriorityType priority)
ElementWrapperType & GetElementAtLocation(const ElementIdentifierType &identifier)
void UpdateDownTree(const ElementIdentifierType &identifier)
TElementWrapperInterface ElementInterfaceType
ElementIdentifierType GetParent(const ElementIdentifierType &identifier) const
SmartPointer< const Self > ConstPointer
VectorContainer< TElementIdentifier, TElementWrapper > Superclass
bool HasParent(const ElementIdentifierType &iId) const
bool DeleteElement(const ElementWrapperType &element)
void UpdateUpTree(const ElementIdentifierType &identifier)
ElementIdentifierType GetRight(const ElementIdentifierType &identifier) const
bool Update(const ElementWrapperType &element)
~PriorityQueueContainer() override=default
void SetElementAtLocation(const ElementIdentifierType &identifier, ElementWrapperType &element)
const ElementWrapperType & Peek() const
ElementIdentifierType GetLeft(const ElementIdentifierType &identifier) const
PriorityQueueContainer(TInputIterator first, TInputIterator last)
const ElementWrapperType & GetElementAtLocation(const ElementIdentifierType &identifier) const
Implements transparent reference counting.
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
SizeValueType IdentifierType
Definition itkIntTypes.h:95