/* -*- c++ -*- (enables emacs c++ mode) */ /*=========================================================================== Copyright (C) 2000-2017 Yves Renard This file is a part of GetFEM++ GetFEM++ is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version along with the GCC Runtime Library Exception either version 3.1 or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License and GCC Runtime Library Exception for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. As a special exception, you may use this file as it is a part of a free software library without restriction. Specifically, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other files to produce an executable, this file does not by itself cause the resulting executable to be covered by the GNU Lesser General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU Lesser General Public License. ===========================================================================*/ /** @file gmm_algobase.h @author Yves Renard @date September 28, 2000. @brief Miscelleanous algorithms on containers. */ #ifndef GMM_ALGOBASE_H__ #define GMM_ALGOBASE_H__ #include "gmm_std.h" #include "gmm_except.h" #include namespace gmm { /* ********************************************************************* */ /* Definitition de classes de comparaison. */ /* retournant un int. */ /* ********************************************************************* */ template struct less : public std::binary_function { inline int operator()(const T& x, const T& y) const { return (x < y) ? -1 : ((y < x) ? 1 : 0); } }; template<> struct less : public std::binary_function { int operator()(int x, int y) const { return x-y; } }; template<> struct less : public std::binary_function { int operator()(char x, char y) const { return int(x-y); } }; template<> struct less : public std::binary_function { int operator()(short x, short y) const { return int(x-y); } }; template<> struct less : public std::binary_function { int operator()(unsigned char x, unsigned char y) const { return int(x)-int(y); } }; template struct greater : public std::binary_function { inline int operator()(const T& x, const T& y) const { return (y < x) ? -1 : ((x < y) ? 1 : 0); } }; template<> struct greater : public std::binary_function { int operator()(int x, int y) const { return y-x; } }; template<> struct greater : public std::binary_function { int operator()(char x, char y) const { return int(y-x); } }; template<> struct greater : public std::binary_function { int operator()(short x, short y) const { return int(y-x); } }; template<> struct greater : public std::binary_function { int operator()(unsigned char x, unsigned char y) const { return int(y)-int(x); } }; template inline T my_abs(T a) { return (a < T(0)) ? T(-a) : a; } template struct approx_less : public std::binary_function { double eps; inline int operator()(const T &x, const T &y) const { if (my_abs(x - y) <= eps) return 0; if (x < y) return -1; return 1; } approx_less(double e = 1E-13) { eps = e; } }; template struct approx_greater : public std::binary_function { double eps; inline int operator()(const T &x, const T &y) const { if (my_abs(x - y) <= eps) return 0; if (x > y) return -1; return 1; } approx_greater(double e = 1E-13) { eps = e; } }; template int lexicographical_compare(ITER1 b1, const ITER1 &e1, ITER2 b2, const ITER2 &e2, const COMP &c) { int i; for ( ; b1 != e1 && b2 != e2; ++b1, ++b2) if ((i = c(*b1, *b2)) != 0) return i; if (b1 != e1) return 1; if (b2 != e2) return -1; return 0; } template > struct lexicographical_less : public std::binary_function { COMP c; int operator()(const CONT &x, const CONT &y) const { return gmm::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), c); } lexicographical_less(const COMP &d = COMP()) { c = d; } }; template > struct lexicographical_greater : public std::binary_function { COMP c; int operator()(const CONT &x, const CONT &y) const { return -gmm::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end(), c); } lexicographical_greater(const COMP &d = COMP()) { c = d; } }; /* ********************************************************************* */ /* "Virtual" iterators on sequences. */ /* The class T represent a class of sequence. */ /* ********************************************************************* */ template struct sequence_iterator { typedef T value_type; typedef value_type* pointer; typedef value_type& reference; typedef const value_type& const_reference; typedef std::forward_iterator_tag iterator_category; T Un; sequence_iterator(T U0 = T(0)) { Un = U0; } sequence_iterator &operator ++() { ++Un; return *this; } sequence_iterator operator ++(int) { sequence_iterator tmp = *this; (*this)++; return tmp; } const_reference operator *() const { return Un; } reference operator *() { return Un; } bool operator ==(const sequence_iterator &i) const { return (i.Un==Un);} bool operator !=(const sequence_iterator &i) const { return (i.Un!=Un);} }; /* ********************************************************************* */ /* generic algorithms. */ /* ********************************************************************* */ template ITER2 copy_n(ITER1 first, SIZE count, ITER2 result) { for ( ; count > 0; --count, ++first, ++result) *result = *first; return result; } template typename std::iterator_traits::value_type mean_value(ITER first, const ITER &last) { GMM_ASSERT2(first != last, "mean value of empty container"); size_t n = 1; typename std::iterator_traits::value_type res = *first++; while (first != last) { res += *first; ++first; ++n; } res /= float(n); return res; } template typename CONT::value_type mean_value(const CONT &c) { return mean_value(c.begin(), c.end()); } template /* hum ... */ void minmax_box(typename std::iterator_traits::value_type &pmin, typename std::iterator_traits::value_type &pmax, ITER first, const ITER &last) { typedef typename std::iterator_traits::value_type PT; if (first != last) { pmin = pmax = *first; ++first; } while (first != last) { typename PT::const_iterator b = (*first).begin(), e = (*first).end(); typename PT::iterator b1 = pmin.begin(), b2 = pmax.begin(); while (b != e) { *b1 = std::min(*b1, *b); *b2 = std::max(*b2, *b); ++b; ++b1; ++b2; } } } template struct sorted_indexes_aux { const VEC &v; public: sorted_indexes_aux(const VEC& v_) : v(v_) {} template bool operator()(const IDX &ia, const IDX &ib) const { return v[ia] < v[ib]; } }; template void sorted_indexes(const VEC &v, IVEC &iv) { iv.clear(); iv.resize(v.size()); for (size_t i=0; i < v.size(); ++i) iv[i] = i; std::sort(iv.begin(), iv.end(), sorted_indexes_aux(v)); } } #endif /* GMM_ALGOBASE_H__ */