Geogram Version 1.10.1
A programming library of geometric algorithms
Loading...
Searching...
No Matches
algorithm.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40#ifndef GEOGRAM_BASIC_ALGORITHM
41#define GEOGRAM_BASIC_ALGORITHM
42
45#include <algorithm>
46#include <random>
47
48#ifdef GEO_PARALLEL_STL
49#include <execution>
50#endif
51
57namespace GEO {
58
70 bool GEOGRAM_API uses_parallel_algorithm(size_t size=0);
71
84 template <typename ITERATOR>
85 inline void sort(
86 const ITERATOR& begin, const ITERATOR& end
87 ) {
88#ifdef GEO_PARALLEL_STL
89 if(uses_parallel_algorithm(size_t(end - begin))) {
90 std::sort(std::execution::par, begin, end);
91 } else
92#endif
93 {
94 std::sort(begin, end);
95 }
96 }
97
117 template <typename ITERATOR, typename CMP>
118 inline void sort(
119 const ITERATOR& begin, const ITERATOR& end, const CMP& cmp
120 ) {
121#ifdef GEO_PARALLEL_STL
122 if(uses_parallel_algorithm(size_t(end - begin))) {
123 std::sort(std::execution::par, begin, end, cmp);
124 } else
125#endif
126 {
127 std::sort(begin, end, cmp);
128 }
129 }
130
131
136 template <typename VECTOR> inline void sort_unique(VECTOR& v) {
137 std::sort(v.begin(), v.end());
138 // Note that std::unique leaves a 'queue' of duplicated elements
139 // at the end of the vector, and returns an iterator that
140 // indicates where to stop.
141 v.erase(
142 std::unique(v.begin(), v.end()), v.end()
143 );
144 }
145
152 template <typename ITERATOR> inline void sort_3(ITERATOR items) {
153 if (items[0]> items[1]) {
154 std::swap(items[0], items[1]);
155 }
156 if (items[1]> items[2]) {
157 std::swap(items[1], items[2]);
158 }
159 if (items[0]> items[1]) {
160 std::swap(items[0], items[1]);
161 }
162 }
163
170 template <typename ITERATOR> inline void sort_4(ITERATOR items) {
171 if (items[1] < items[0]) {
172 std::swap(items[0], items[1]);
173 }
174 if (items[3] < items[2]) {
175 std::swap(items[2], items[3]);
176 }
177 if (items[2] < items[0]) {
178 std::swap(items[0], items[2]);
179 std::swap(items[1], items[3]);
180 }
181 if (items[2] < items[1]) {
182 std::swap(items[1], items[2]);
183 }
184 if (items[3] < items[2]) {
185 std::swap(items[2], items[3]);
186 }
187 }
188
196 template <typename ITERATOR>
197 inline void random_shuffle(const ITERATOR& begin, const ITERATOR& end) {
199 std::mt19937 urng{Numeric::uint32(seed)};
200 std::shuffle(begin, end, urng);
201 }
202
203}
204
205#endif
Common include file, providing basic definitions. Should be included before anything else by all head...
int32_t int32
Definition numeric.h:133
uint32_t uint32
Definition numeric.h:145
int32 random_int32()
Returns a 32 bits integer between 0 and RAND_MAX.
Global Vorpaline namespace.
Definition basic.h:55
void sort_unique(VECTOR &v)
Sorts a vector and suppresses all duplicated elements.
Definition algorithm.h:136
void sort_4(ITERATOR items)
Specialized sort routine for 4 elements.
Definition algorithm.h:170
void sort(const ITERATOR &begin, const ITERATOR &end)
Sorts elements in parallel.
Definition algorithm.h:85
bool uses_parallel_algorithm(size_t size=0)
Checks whether parallel algorithms are used.
void sort_3(ITERATOR items)
Specialized sort routine for 3 elements.
Definition algorithm.h:152
void random_shuffle(const ITERATOR &begin, const ITERATOR &end)
Applies a random permutation to a sequence.
Definition algorithm.h:197
Types and functions for numbers manipulation.