Rheolef  7.1
an efficient C++ finite element environment
msg_util.h
Go to the documentation of this file.
1 #ifndef _RHEO_MSG_UTIL_H
2 #define _RHEO_MSG_UTIL_H
3 //
24 // message exchange basic algorithms
25 // implemented with MPI
26 //
27 // author: Pierre.Saramito@imag.fr
28 //
29 // date: 17 december 1998
30 //
31 # include "rheolef/distributed.h"
32 namespace rheolef {
33 
34 // bugs in MPI...
35 #ifndef RHEO_MPI_PRUDENCE
36 #define RHEO_MPI_PRUDENCE 0
37 #endif
38 
39 // there is a non-standard SGI extension like that:
40 template <class T1, class T2>
41 struct select1st : std::unary_function<std::pair<T1,T2>, T1> {
42  T1 operator() (const std::pair<T1,T2>& x) const { return x.first; }
43 };
44 template <class T1, class T2>
45 struct select2nd : std::unary_function<std::pair<T1,T2>, T2> {
46  T2 operator() (const std::pair<T1,T2>& x) const { return x.second; }
47 };
48 
49 // always true predicate
50 template <class T>
51 struct always_true : std::unary_function<T,bool> {
52  bool operator()(const T& x) const { return true; }
53 };
54 // operator :=
55 template <class T1, class T2>
56 struct set_op : std::binary_function<T1,T2,T1> {
57  T1& operator()(T1& x, const T2& y) const { return x = y; }
58 };
59 // operator +=
60 template <class T1, class T2>
61 struct set_add_op : std::binary_function<T1,T2,T1> {
62  T1& operator()(T1& x, const T2& y) const { return x += y; }
63 };
64 // dummy iterator, when an iterator is required but
65 // we have no usage.
66 template <class T, class Distance = std::ptrdiff_t>
67 class dummy_iterator : public std::iterator<std::input_iterator_tag, T, Distance, const T*, const T&> {
68 public:
70  dummy_iterator<T,Distance> operator++(int) { return *this; }
71  T& operator*() { return _dummy; }
72  const T& operator*() const { return _dummy; }
74 protected:
76 };
77 // index iterator, simulates array[i] = i
78 template <class Size, class Distance = std::ptrdiff_t>
79 class index_iterator : public std::iterator<std::input_iterator_tag, Size, Distance, const Size*, const Size&> {
80 public:
81  index_iterator& operator++() { _i++; return *this; }
84  _i++;
85  return tmp;
86  }
87  const Size& operator*() const { return _i; }
88  const Size& operator[](const Size& i) const { return i; }
90  return x._i == _i; }
92  return !(x._i == _i); }
93  index_iterator(Size i0 = 0) : _i(i0) {}
94 protected:
95  Size _i;
96 };
97 
98 // f1(pair x) = x.first
99 template <class Pair>
100 struct first_op
101 : public std::unary_function<Pair,typename Pair::first_type> {
102  typename std::unary_function<Pair,typename Pair::first_type>::result_type
103  operator()(const Pair& x) const {
104  return x.first; }
105 };
106 // f2(pair x) = x.second
107 template <class Pair>
108 struct second_op
109  : public std::unary_function<Pair, typename Pair::second_type> {
110  typename std::unary_function<Pair,typename Pair::second_type>::result_type
111  operator()(const Pair& x) const {
112  return x.second; }
113 };
114 // pair<const uint, T> and pair<uint, T> are not compatible
115 // for some C++; so convert it explicitly:
116 template<class Pair1, class Pair2>
117 struct pair_identity : public std::unary_function<Pair1, Pair2> {
118  Pair2 operator()(const Pair1& x) const {
119  return Pair2(x.first, x.second); }
120 };
121 // wrapper iterator class that applies an operator
122 template <class Iterator, class Operator>
123 class apply_iterator : public std::iterator_traits<Iterator> {
124 public:
125  typedef typename Operator::result_type value_type;
126  apply_iterator(Iterator i1, Operator op1)
127  : i(i1), op(op1) {}
129  i++; return *this; }
131  apply_iterator t = *this; i++; return t; }
132  value_type operator*() const { return op(*i); }
133  bool operator== (apply_iterator<Iterator,Operator> b) const{ return (i == b.i); }
134  bool operator!= (apply_iterator<Iterator,Operator> b) const{ return (i != b.i); }
135 protected:
136  Iterator i;
137  Operator op;
138 };
139 template <class Iterator, class Operator>
140 inline
142 make_apply_iterator(Iterator i, Operator op) {
143  return apply_iterator<Iterator,Operator>(i,op);
144 }
145 // some c++ cannot convert pair<const I,T> to pair<I,T>:
146 template <class InputIterator, class OutputIterator>
147 OutputIterator
148 msg_pair_copy(InputIterator input, InputIterator last,
149 OutputIterator result) {
150  while (input != last) {
151  (*result).first = (*input).first;
152  (*result++).second = (*input++).second;
153  }
154  return result;
155 }
156 } // namespace rheolef
157 #endif // _RHEO_MSG_UTIL_H
rheolef::set_op::operator()
T1 & operator()(T1 &x, const T2 &y) const
Definition: msg_util.h:57
rheolef::index_iterator::index_iterator
index_iterator(Size i0=0)
Definition: msg_util.h:93
rheolef::pair_identity
Definition: msg_util.h:117
rheolef::select2nd::operator()
T2 operator()(const std::pair< T1, T2 > &x) const
Definition: msg_util.h:46
rheolef::dummy_iterator::operator*
T & operator*()
Definition: msg_util.h:71
rheolef::dummy_iterator::operator++
dummy_iterator< T, Distance > operator++(int)
Definition: msg_util.h:70
mkgeo_ball.b
int b
Definition: mkgeo_ball.sh:152
rheolef::first_op
Definition: msg_util.h:101
rheolef::apply_iterator::i
Iterator i
Definition: msg_util.h:136
rheolef::second_op
Definition: msg_util.h:109
rheolef::index_iterator
Definition: msg_util.h:79
rheolef::apply_iterator
Definition: msg_util.h:123
rheolef::dummy_iterator::_dummy
T _dummy
Definition: msg_util.h:75
rheolef::always_true::operator()
bool operator()(const T &x) const
Definition: msg_util.h:52
rheolef::apply_iterator::operator*
value_type operator*() const
Definition: msg_util.h:132
rheolef::apply_iterator::operator++
apply_iterator operator++(int)
Definition: msg_util.h:130
rheolef::index_iterator::operator++
index_iterator operator++(int)
Definition: msg_util.h:82
rheolef::first_op::operator()
std::unary_function< Pair, typename Pair::first_type >::result_type operator()(const Pair &x) const
Definition: msg_util.h:103
rheolef::dummy_iterator
Definition: msg_util.h:67
rheolef::always_true
Definition: msg_util.h:51
rheolef::pair_identity::operator()
Pair2 operator()(const Pair1 &x) const
Definition: msg_util.h:118
rheolef::select2nd
Definition: msg_util.h:45
rheolef::index_iterator::operator++
index_iterator & operator++()
Definition: msg_util.h:81
rheolef::dummy_iterator::dummy_iterator
dummy_iterator()
Definition: msg_util.h:73
rheolef::index_iterator::_i
Size _i
Definition: msg_util.h:95
rheolef::select1st
Definition: msg_util.h:41
rheolef::index_iterator::operator*
const Size & operator*() const
Definition: msg_util.h:87
rheolef::set_add_op::operator()
T1 & operator()(T1 &x, const T2 &y) const
Definition: msg_util.h:62
rheolef::apply_iterator::operator++
apply_iterator & operator++()
Definition: msg_util.h:128
rheolef
This file is part of Rheolef.
Definition: compiler_eigen.h:37
rheolef::apply_iterator::apply_iterator
apply_iterator(Iterator i1, Operator op1)
Definition: msg_util.h:126
rheolef::second_op::operator()
std::unary_function< Pair, typename Pair::second_type >::result_type operator()(const Pair &x) const
Definition: msg_util.h:111
rheolef::msg_pair_copy
OutputIterator msg_pair_copy(InputIterator input, InputIterator last, OutputIterator result)
Definition: msg_util.h:148
rheolef::index_iterator::operator==
bool operator==(const index_iterator< Size, Distance > &x) const
Definition: msg_util.h:89
rheolef::apply_iterator::op
Operator op
Definition: msg_util.h:137
rheolef::apply_iterator::operator==
bool operator==(apply_iterator< Iterator, Operator > b) const
Definition: msg_util.h:133
rheolef::make_apply_iterator
apply_iterator< Iterator, Operator > make_apply_iterator(Iterator i, Operator op)
Definition: msg_util.h:142
rheolef::apply_iterator::value_type
Operator::result_type value_type
Definition: msg_util.h:125
rheolef::index_iterator::operator!=
bool operator!=(const index_iterator< Size, Distance > &x) const
Definition: msg_util.h:91
rheolef::dummy_iterator::operator++
dummy_iterator< T, Distance > & operator++()
Definition: msg_util.h:69
rheolef::set_op
Definition: msg_util.h:56
rheolef::apply_iterator::operator!=
bool operator!=(apply_iterator< Iterator, Operator > b) const
Definition: msg_util.h:134
rheolef::set_add_op
Definition: msg_util.h:61
mkgeo_ball.tmp
tmp
Definition: mkgeo_ball.sh:380
rheolef::select1st::operator()
T1 operator()(const std::pair< T1, T2 > &x) const
Definition: msg_util.h:42
rheolef::dummy_iterator::operator*
const T & operator*() const
Definition: msg_util.h:72
rheolef::index_iterator::operator[]
const Size & operator[](const Size &i) const
Definition: msg_util.h:88
T
Expr1::float_type T
Definition: field_expr.h:261