Loading...
Searching...
No Matches
SortAlgorithms.hpp
1/*---------------------------------------------------------------------------*\
2 *
3 * bitpit
4 *
5 * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6 *
7 * -------------------------------------------------------------------------
8 * License
9 * This file is part of bitpit.
10 *
11 * bitpit is free software: you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License v3 (LGPL)
13 * as published by the Free Software Foundation.
14 *
15 * bitpit is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22 *
23\*---------------------------------------------------------------------------*/
24
25// ========================================================================== //
26// - SORTING ALGORITHMS - //
27// //
28// Functions for data sorting. //
29// ========================================================================== //
30// INFO //
31// Author : Alessandro Alaia //
32// Version : v2.0 //
33// //
34// All rights reserved. //
35// ========================================================================== //
36# ifndef __BITPIT_SORT_ALGORITHMS_HPP__
37# define __BITPIT_SORT_ALGORITHMS_HPP__
38
39// ========================================================================== //
40// INCLUDES //
41// ========================================================================== //
42
43// Standard Template Library
44# include <cmath>
45# include <array>
46# include <vector>
47# include <string>
48# include <iostream>
49
50// Classes
51// none
52
53// bitpit
54# include "Operators.hpp"
55
56namespace bitpit{
57
58// KdTree ------------------------------------------------------------------- //
59template <class T, class T1 = int>
60class KdNode {
61
62 // Members ========================================================== //
63 public:
64 int lchild_;
65 int rchild_;
67 T1 label;
69 // Constructor ====================================================== //
70 public:
71 KdNode( // default constructor for KdNode variables
72 void // (input) none
73 );
74
75 // Methods ============================================================== //
76 public:
77 void reset( // Reset the node
78 void // (input) none
79 );
80};
81
82template <int d, class T, class T1 = int>
83class KdTree {
84
85 // Members ============================================================== //
86 public:
87 int MAXSTK;
88 int n_nodes;
89 std::vector< KdNode<T, T1> > nodes;
91 // Constructors ========================================================= //
92 public:
93 KdTree( // Default constructor for KdTree
94 int stack_size = 10 // (input/optional stack size)
95 );
96
97 // Methods ============================================================== //
98 public:
99 void clear( // Clear kd-tree content
100 void // (input) none
101 );
102 int exist( // Check if element exist in the kd-tree
103 const T * // (input) pointer to element to be tested
104 );
105 int exist( // Check if element exist in the kd-tree
106 const T *, // (input) pointer to element to be tested
107 T1 & // (input/output) label of the kd node matching test object
108 );
109
110 template <class T2>
111 int hNeighbor( // Check if a kd-node exists in the h-neighborhood of a given item
112 const T *, // (input) pointer to element to be tested
113 T2 , // (input) radius of ball
114 bool ,
115 int n = 0, // (input/optional) root for binary search algorithm
116 int l = 0 // (input/optional) level of root on binary tree
117 );
118
119 template <class T2>
120 void hNeighbors( // Check if a kd-node exists in the h-neighborhood of a given item
121 const T *, // (input) pointer to element to be tested
122 T2 , // (input) radius of ball
123 std::vector<T1> *, // (output) pointer to container of labels of h-neighbors
124 std::vector<T1> *,
125 int n = 0, // (input/optional) root for binary search algorithm
126 int l = 0 // (input/optional) level of root on binary tree
127 );
128
129 template <class T2>
130 int hNeighbor( // Check if a kd-node exists in the h-neighborhood of a given item
131 const T *, // (input) pointer to element to be tested
132 T1 &, // (input/output) label of the kd node matching test object
133 T2 , // (input) radius of ball
134 int n = 0, // (input/optional) root for binary search algorithm
135 int l = 0 // (input/optional) level of root on binary tree
136 );
137
138 void insert( // Insert new element in the kd-tree
139 T * // (input) pointer to element to be inserted
140 );
141 void insert( // Insert new element in the kd-tree
142 T *, // (input) pointer to element to be inserted
143 T1 & // (input) label of the new element
144 );
145 private:
146 void increaseStack( // Increase stack size
147 void // )input) none
148 );
149 void decreaseStack( // Decrease stack size
150 void // )input) none
151 );
152
153 template <class T2>
154 T2 distance( // Evalautes the the distance between two vertices
155 const T &P1,
156 const T &P2
157 ) const;
158
159};
160
161// min PQUEUE --------------------------------------------------------------- //
162template <class T, class T1 = T>
164
165 // Members ============================================================== //
166 public:
167 int MAXSTK;
169 std::vector< T > keys;
170 std::vector< T1 > labels;
171 std::vector< std::array<int,2> > *map;
173 private:
174 bool use_labels;
176 // Constructor ========================================================== //
177 public:
178 MinPQueue( // Default constructor for min priority queue
179 bool a = false, // (input/optional) flag for key labelling
180 std::vector< std::array<int,2> > *b = NULL // (input/optional) pointer to user-defined map
181 );
182 MinPQueue( // Default constructor for min priority queue
183 int , // (input) stack size
184 bool a = false, // (input/optional) flag for key labelling
185 std::vector< std::array<int,2> > *b = NULL // (input/optional) pointer to user-defined map
186 );
187
188 // Destructor =========================================================== //
189 public:
190 ~MinPQueue( // Standard destructor for min priority queues
191 void // (input) none
192 );
193
194 // Methods ============================================================== //
195 public:
196 void clear( // Clear min heap content
197 void // (input) none
198 );
199 void extract( // Extract root from the min-heap data structure
200 T & // (input/output) root value
201 );
202 void extract( // Extract root from the min-heap data structure
203 T &, // (input/output) root value
204 T1 & // (input/output) root label
205 );
206 void insert( // Insert a new key
207 T & // (input) new key to be inserted
208 );
209 void insert( // Insert a new key
210 T &, // (input) new key to be inserted
211 T1 & // (input) label
212 );
213 void modify( // Modify key value
214 int , // (input) index of key to be modified
215 T & // (input) new key value
216 );
217 void modify( // Modify key value
218 int , // (input) index of key to be modified
219 T &, // (input) new key value
220 T1 & // (input) label attached to the new key
221 );
222 void buildHeap( // Build min-heap
223 void // (input) none
224 );
225 void display( // Display min-heap content
226 std::ostream & // (input) output stream
227 );
228 private:
229 void increaseSTACK( // Increase stack size
230 void // (input) none
231 );
232 void decreaseSTACK( // Decrease stack size
233 void // (input) none
234 );
235 void heapify( // Restore min heap condition on spacified element
236 int // (input) position of element in stack
237 );
238
239};
240
241// max PQUEUE --------------------------------------------------------------- //
242template <class T, class T1 = T>
244
245 // Members ============================================================== //
246 public:
247 int MAXSTK;
249 std::vector< T > keys;
250 std::vector< T1 > labels;
251 std::vector< std::array<int,2> > *map;
252 private:
253 bool use_labels;
255 // Constructor ========================================================== //
256 public:
257 MaxPQueue( // Default constructor for min priority queue
258 bool a = false, // (input/optional) flag for key labelling
259 std::vector< std::array<int,2> > *b = NULL // (input/optional) pointer to user-defined map
260 );
261 MaxPQueue( // Default constructor for min priority queue
262 int , // (input) stack size
263 bool a = false, // (input/optional) flag for key labelling
264 std::vector< std::array<int,2> > *b = NULL // (input/optional) pointer to user-defined map
265 );
266
267 // Destructor =========================================================== //
268 public:
269 ~MaxPQueue( // Standard destructor for min priority queues
270 void // (input) none
271 );
272
273 // Methods ============================================================== //
274 public:
275 void clear( // Clear max heap content
276 void // (input) none
277 );
278 void extract( // Extract root from the min-heap data structure
279 T & // (input/output) root value
280 );
281 void extract( // Extract root from the min-heap data structure
282 T &, // (input/output) root value
283 T1 & // (input/output) root label
284 );
285 void insert( // Insert a new key
286 T & // (input) new key to be inserted
287 );
288 void insert( // Insert a new key
289 T &, // (input) new key to be inserted
290 T1 & // (input) label
291 );
292 void modify( // Modify key value
293 int , // (input) index of key to be modified
294 T & // (input) new key value
295 );
296 void modify( // Modify key value
297 int , // (input) index of key to be modified
298 T &, // (input) new key value
299 T1 & // (input) label attached to the new key
300 );
301 void buildHeap( // Build min-heap
302 void
303 );
304 void display( // Display min-heap content
305 std::ostream & // (input) output stream
306 );
307 private:
308 void increaseSTACK( // Increase stack size
309 void // (input) none
310 );
311 void decreaseSTACK( // Decrease stack size
312 void // (input) none
313 );
314 void heapify( // Restore min heap condition on spacified element
315 int // (input) position of element in stack
316 );
317
318};
319
320// LIFO stack --------------------------------------------------------------- //
321template <class T>
323
324 // Members ============================================================== //
325 public:
326 int MAXSTK;
327 int TOPSTK;
328 std::vector<T> STACK;
330 // Constructor ========================================================== //
331 public:
332 LIFOStack( // Standard constructor for LIFO stack
333 void // (input) none
334 );
335 LIFOStack( // Custom constructor #1 for LIFO stack
336 int // (input) maximal stack size
337 );
338 LIFOStack( // Custom constructor #2 for LIFO stack
339 std::vector<T> & // (input) items to be added in the LIFO stack
340 );
341
342 // Destructor =========================================================== //
343 public:
344 ~LIFOStack( // Standard destructor for LIFO stack
345 void // (input) none
346 );
347
348 // Methods ============================================================== //
349 public:
350 void clear( // Clear stack content
351 void // (input) none
352 );
353 void increaseSTACK( // Increase stack size
354 void // (input) none
355 );
356 void decreaseSTACK( // Decrease stack size
357 void // (output) none
358 );
359 T pop( // Pop last item from stack
360 void // (input) none
361 );
362 void push( // Pusk item into the stack
363 const T & // (input) item to be pushed into the stack list
364 );
365 void push( // Pusk items into the stack
366 const std::vector<T> & // (input) items to be pushed into the stack list
367 );
368 void display( // Display LIFO stack infos
369 std::ostream & // (input) output stream
370 );
371};
372
373// ========================================================================== //
374// FUNCTIONS PROTOTYPES //
375// ========================================================================== //
376//None
377
378}
379
380// ========================================================================== //
381// TEMPLATES //
382// ========================================================================== //
383# include "LIFOStack.tpp"
384# include "PQueue.tpp"
385# include "KdTree.tpp"
386
387
388
389# endif
class for kd-tree node.
void reset(void)
Definition KdTree.tpp:98
class for kd-tree data structure.
KdTree(int stack_size=10)
Definition KdTree.tpp:162
std::vector< KdNode< T, T1 > > nodes
int exist(const T *)
Definition KdTree.tpp:235
void insert(T *)
Definition KdTree.tpp:488
void clear(void)
Definition KdTree.tpp:196
int hNeighbor(const T *, T2, bool, int n=0, int l=0)
Definition KdTree.tpp:361
void hNeighbors(const T *, T2, std::vector< T1 > *, std::vector< T1 > *, int n=0, int l=0)
Definition KdTree.tpp:733
class for Last In First Out stack
void increaseSTACK(void)
void push(const T &)
void display(std::ostream &)
void decreaseSTACK(void)
std::vector< T > STACK
class for max priority queue.
void insert(T &)
Definition PQueue.tpp:1437
void modify(int, T &)
Definition PQueue.tpp:1559
void extract(T &)
Definition PQueue.tpp:1332
void clear(void)
Definition PQueue.tpp:1041
std::vector< std::array< int, 2 > > * map
std::vector< T1 > labels
void display(std::ostream &)
Definition PQueue.tpp:1621
MaxPQueue(bool a=false, std::vector< std::array< int, 2 > > *b=NULL)
Definition PQueue.tpp:887
void buildHeap(void)
Definition PQueue.tpp:1221
std::vector< T > keys
class for min priority queue.
void display(std::ostream &)
Definition PQueue.tpp:787
void buildHeap(void)
Definition PQueue.tpp:387
void clear(void)
Definition PQueue.tpp:223
void insert(T &)
Definition PQueue.tpp:587
std::vector< T > keys
std::vector< std::array< int, 2 > > * map
void modify(int, T &)
Definition PQueue.tpp:725
std::vector< T1 > labels
MinPQueue(bool a=false, std::vector< std::array< int, 2 > > *b=NULL)
Definition PQueue.tpp:85
void extract(T &)
Definition PQueue.tpp:482
--- layout: doxygen_footer ---