RINGMesh  Version 5.0.0
A programming library for geological model meshes
algorithm.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2017, Association Scientifique pour la Geologie et ses
3  * Applications (ASGA). 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  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of ASGA nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ASGA BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  * http://www.ring-team.org
28  *
29  * RING Project
30  * Ecole Nationale Superieure de Geologie - GeoRessources
31  * 2 Rue du Doyen Marcel Roubault - TSA 70605
32  * 54518 VANDOEUVRE-LES-NANCY
33  * FRANCE
34  */
35 
36 #pragma once
37 
38 #include <algorithm>
39 #include <vector>
40 
48 namespace RINGMesh
49 {
54  template < typename T, typename container >
55  index_t find( const container& in, const T& value )
56  {
57  auto it = std::find( in.begin(), in.end(), value );
58  if( it == in.end() )
59  {
60  return NO_ID;
61  }
62  else
63  {
64  return static_cast< index_t >( it - in.begin() );
65  }
66  }
67 
72  template < typename T, typename container >
73  index_t find_sorted( const container& in, const T& value )
74  {
75  auto low = std::lower_bound( in.begin(), in.end(), value );
76  if( low == in.end() || value < *low )
77  {
78  return NO_ID;
79  }
80  else
81  {
82  return static_cast< index_t >( low - in.begin() );
83  }
84  }
85 
86  template < typename T, typename container >
87  bool contains( const container& in, const T& value )
88  {
89  return find( in, value ) != NO_ID;
90  }
91 
92  template < typename T, typename container >
93  bool contains_sorted( const container& in, const T& value )
94  {
95  return find_sorted( in, value ) != NO_ID;
96  }
97 
103  template < typename T1, typename T2 >
104  void indirect_sort( std::vector< T1 >& input, std::vector< T2 >& output )
105  {
106  if( input.size() < 2 )
107  {
108  return;
109  }
110  for( auto it1 : range( input.size() - 1 ) )
111  {
112  index_t ref_index = it1;
113  T1 ref_value = input[it1];
114  for( auto it2 : range( it1 + 1, input.size() ) )
115  {
116  index_t new_index = it2;
117  T1 new_value = input[it2];
118  if( ref_value > new_value )
119  {
120  ref_value = new_value;
121  ref_index = new_index;
122  }
123  }
124  std::iter_swap( input.begin() + it1, input.begin() + ref_index );
125  std::iter_swap( output.begin() + it1, output.begin() + ref_index );
126  }
127  }
128 
134  template < typename CONTAINER, typename CMP >
135  void sort_unique( CONTAINER& container, const CMP& cmp )
136  {
137  std::sort( container.begin(), container.end(), cmp );
138  container.erase( std::unique( container.begin(), container.end(), cmp ),
139  container.end() );
140  }
141 
146  template < typename CONTAINER >
147  void sort_unique( CONTAINER& container )
148  {
149  std::sort( container.begin(), container.end() );
150  container.erase( std::unique( container.begin(), container.end() ),
151  container.end() );
152  }
153 } // namespace RINGMesh
index_t find_sorted(const container &in, const T &value)
Returns the position of the first entity matching.
Definition: algorithm.h:73
void sort_unique(CONTAINER &container, const CMP &cmp)
Sorts a container and suppresses all duplicated entities.
Definition: algorithm.h:135
index_t find(const container &in, const T &value)
Returns the position of the first entity matching.
Definition: algorithm.h:55
bool contains_sorted(const container &in, const T &value)
Definition: algorithm.h:93
bool contains(const container &in, const T &value)
Definition: algorithm.h:87
Classes to build GeoModel from various inputs.
Definition: algorithm.h:48
void indirect_sort(std::vector< T1 > &input, std::vector< T2 > &output)
Bubble sorting of input and output vectors according to values of input.
Definition: algorithm.h:104