customOperators.hpp
1 /*---------------------------------------------------------------------------*\
2  *
3  * mimmo
4  *
5  * Copyright (C) 2015-2021 OPTIMAD engineering Srl
6  *
7  * -------------------------------------------------------------------------
8  * License
9  * This file is part of mimmo.
10  *
11  * mimmo 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  * mimmo 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 mimmo. If not, see <http://www.gnu.org/licenses/>.
22  *
23  \ *---------------------------------------------------------------------------*/
24 
25 #ifndef __CUSTOMOPERATORS_HH__
26 #define __CUSTOMOPERATORS_HH__
27 
28 #include <cmath>
29 #include <vector>
30 #include <array>
31 #include <algorithm>
32 
33 #include "mimmoTypeDef.hpp"
34 #include <bitpit_patchkernel.hpp>
35 
36 
37 
38 std::array<double, 3> operator-(const bitpit::Vertex &v1, const bitpit::Vertex &v2);
39 
45 int countSubstring(const std::string& str, const std::string& sub);
46 
47 
52 template <class T>
53 void freeContainer (std::vector<T> & t){
54  t.swap(std::vector<T>());
55 }
56 
62 template <class T>
63 T summing_Vector(std::vector<T> & t){
64  T result = t[0];
65  std::size_t size = t.size();
66  for(int i=1; i<size; ++i){
67  result += t[i];
68  }
69  return result;
70 }
71 
90 bool inline logical_summing(bool &root, bool & target, bool * policy){
91  bool result;
92  if(policy[0]) result = target;
93  if(policy[1]) result = root + target;
94 
95  if(policy[2]) {
96  result = root - target;
97  //further check if root is 0 and target is 1.
98  // -1 is read as 1; I want to keep him as 0;
99  if(result) result = result - target;
100  }
101  return result;
102 };
103 
122 int inline logical_summing(int &root, int &target, bool policy){
123  bool result;
124  if(policy){
125  result = (bool)root + (bool)target;
126  }else{
127  result = (bool)root - (bool)target;
128  //further check if root is 0 and target is 1.
129  // -1 is read as 1; I want to keep him as 0;
130  if(result) result = result - (bool)target;
131  }
132  return (int)result;
133 };
134 
140 template <class T>
141 T getSign (T & t){
142  T result = 1;
143  if(t < 0) result= -1 * result;
144  return result;
145 }
146 
154 template <class T>
155 int inline posVectorFind( std::vector<T> &vect, T& target){
156  int pos = -1;
157  typename std::vector< T >::iterator it_find = std::find(vect.begin(),vect.end(),target);
158  if (it_find!=vect.end()) {
159  pos = std::distance(vect.begin(),it_find);
160  }
161 
162 return pos;
163 };
164 
172 template <class T, size_t d >
173 int inline posVectorFind( std::array<T, d> &vect, T& target){
174  int pos = -1;
175  typename std::array< T,d >::iterator it_find = std::find(vect.begin(),vect.end(),target);
176  if (it_find!=vect.end()){
177  pos = std::distance(vect.begin(),it_find);
178  }
179  return pos;
180 };
181 
189 template <class T>
190 bool inline checkVectorFind( std::vector<T> &vect, T& target){
191  bool check = false;
192  typename std::vector< T >::iterator it_find = std::find(vect.begin(),vect.end(),target);
193  if (it_find!=vect.end()) check=true;
194  return check;
195 };
196 
204 template <class T, size_t d>
205 bool inline checkVectorFind( std::array<T,d> &vect, T& target){
206  bool check = false;
207  typename std::array< T,d >::iterator it_find = std::find(vect.begin(),vect.end(),target);
208  if (it_find!=vect.end()) check=true;
209  return check;
210 };
211 
217 template <class T, size_t d>
218 std::vector<T> inline conVect(std::array<T, d> & origin ){
219  std::vector<T> result(d);
220  for(size_t i=0; i<d; ++i)
221  result[i] = origin[i];
222  return result;
223 }
224 
230 template <class T, size_t d>
231 std::array<T,d> inline conArray(std::vector<T> & origin ){
232  std::array<T,d> result;
233  for(size_t i=0; i<d; ++i)
234  result[i] = origin[i];
235  return result;
236 }
237 
243 template <class T, size_t d>
244 std::vector< std::vector < T > > inline conVect(std::vector< std::array<T, d> > & origin ){
245  std::vector<std::vector<T> > result(origin.size());
246  for(size_t i=0; i<origin.size(); ++i)
247  result[i] = conVect(origin[i]);
248  return result;
249 }
250 
256 template <class T, size_t d>
257 std::vector< std::array< T, d > > inline conArray(std::vector<std::vector<T> > & origin ){
258  std::vector< std::array<T,d> > result(origin.size());
259  for(size_t i=0; i<origin.size(); ++i)
260  result[i] = conArray< T, d>(origin[i]);
261  return result;
262 }
263 
271 template <class T>
272 T findPosition(T & value, std::vector<T> & source){
273  T result = -1;
274  typename std::vector< T >::iterator itV = std::find(source.begin(), source.end(), value);
275  if(itV != source.end())
276  result = std::distance(source.begin(), itV);
277  return result;
278 };
279 
280 
299 template <class T>
300 std::vector< T > getVectorSubset(int i, int j, std::vector<T> & source){
301  std::vector < T> result;
302  int counter = 0;
303  if(i >= (int)source.size() || j < 0) return result;
304 
305  result.resize(source.size());
306 
307  // check your controlling indices
308  int maxElement = source.size() -1;
309  i = std::max(std::min(i,maxElement), 0);
310  j = std::max(std::min(j,maxElement), 0);
311 
312  if(i>=j){
313  for(int k=i; k<source.size(); ++k ){
314  result[counter] = source[k];
315  ++counter;
316  }
317  for(int k=0; k<j; ++k ){
318  result[counter] = source[k];
319  ++counter;
320  }
321  }else{
322  for(int k=i; k<j; ++k ){
323  result[counter] = source[k];
324  ++counter;
325  }
326  }
327  result.resize(counter);
328  return result;
329 }
330 
349 template <class T>
350 void fillVectorSubset(int i, int j, std::vector<T> & source, T cValue){
351  if(i >= source.size() || j < 0) return;
352 
353  // check your controlling indices
354  int maxElement = source.size() -1;
355  i = std::max(std::min(i,maxElement), 0);
356  j = std::max(std::min(j,maxElement), 0);
357 
358  if(i>=j){
359  for(int k=i; k<source.size(); ++k ){
360  source[k] = cValue;
361  }
362  for(int k=0; k<j; ++k ){
363  source[k] = cValue;
364  }
365  }else{
366  for(int k=i; k<j; ++k ){
367  source[k] = cValue;
368  }
369  }
370  return;
371 }
372 
378 #endif //__CUSTOMOPERATORS_HH__
std::array< double, 3 > operator-(const bitpit::Vertex &v1, const bitpit::Vertex &v2)
std::vector< T > getVectorSubset(int i, int j, std::vector< T > &source)
T summing_Vector(std::vector< T > &t)
bool checkVectorFind(std::vector< T > &vect, T &target)
bool logical_summing(bool &root, bool &target, bool *policy)
T findPosition(T &value, std::vector< T > &source)
void fillVectorSubset(int i, int j, std::vector< T > &source, T cValue)
std::vector< T > conVect(std::array< T, d > &origin)
int countSubstring(const std::string &str, const std::string &sub)
void freeContainer(std::vector< T > &t)
int posVectorFind(std::vector< T > &vect, T &target)
T getSign(T &t)
std::array< T, d > conArray(std::vector< T > &origin)