~ubuntu-branches/ubuntu/trusty/rheolef/trusty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef _RHEOLEF_VEC_CONCAT_H
#define _RHEOLEF_VEC_CONCAT_H
///
/// This file is part of Rheolef.
///
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
///
/// Rheolef is free software; you can redistribute it and/or modify
/// it under the terms of the GNU General Public License as published by
/// the Free Software Foundation; either version 2 of the License, or
/// (at your option) any later version.
///
/// Rheolef 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 General Public License for more details.
///
/// You should have received a copy of the GNU General Public License
/// along with Rheolef; if not, write to the Free Software
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
///
/// =========================================================================
// build vec from initializer lists
//
#include "rheolef/vec.h"

namespace rheolef {

template <class T, class M>
struct vec_concat_value {
// typedef:
 typedef enum { scalar, vector} variant_type;
// allocators:
 vec_concat_value (const T& x)        : s(x), v(),  variant(scalar) {}
 vec_concat_value (const vec<T,M>& x) : s(),  v(x), variant(vector) {}
// io/debug:
 friend std::ostream& operator<< (std::ostream& o, const vec_concat_value<T,M>& x) {
  if (x.variant == scalar) return o << "s"; else return o << "v";
 }
// data:
public:
 T            s;
 vec<T,M>     v;
 variant_type variant;
};

template <class T, class M>
struct vec_concat {
// typedef:
 typedef typename vec<T,M>::size_type   size_type;
 typedef vec_concat_value<T,M>          value_type;

// allocators:

 vec_concat () : _l() {}

#ifdef TO_CLEAN
 vec_concat (const std::initializer_list<vec<T,M> >& il) : _l() {
#ifdef _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
    typedef typename std::initializer_list<vec<T,M> >::const_iterator const_iterator;
#else // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
    typedef const vec<T,M>* const_iterator;
#endif // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
    for(const_iterator iter = il.begin(); iter != il.end(); ++iter) {
        _l.push_back(value_type(*iter));
    }
 }
#endif // TO_CLEAN

 vec_concat (const std::initializer_list<value_type>& il) : _l() {
#ifdef _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
    typedef typename std::initializer_list<value_type>::const_iterator const_iterator;
#else // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
    typedef const value_type* const_iterator;
#endif // _RHEOLEF_HAVE_STD_INITIALIZER_ITERATOR
    for(const_iterator iter = il.begin(); iter != il.end(); ++iter) {
        _l.push_back(*iter);
    }
 }
 friend std::ostream& operator<< (std::ostream& o, const vec_concat<T,M>& x) {
    std::cout << "{";
    for(typename std::list<value_type>::const_iterator iter = x._l.begin(); iter != x._l.end(); ++iter) {
        std::cout << *iter << " ";
    }
    return std::cout << "}";
  }
  vec<T,M> build_vec() const;
// data:
protected:
 std::list<value_type> _l;
};
template <class T, class M>
vec<T,M>
vec_concat<T,M>::build_vec() const
{
  // ------------------------------------
  // first pass: compute the vector size
  // ------------------------------------
  size_type s_sz = 0;
  size_type v_sz = 0;
  size_type v_dis_sz = 0;
  communicator comm;
  for(typename std::list<value_type>::const_iterator iter = _l.begin(); iter != _l.end(); ++iter) {
    const vec_concat_value<T,M>& x = *iter;
    if (x.variant == value_type::vector) {
      comm      = x.v.ownership().comm();
      v_sz     += x.v.ownership().size();
      v_dis_sz += x.v.ownership().dis_size();
    } else {
      s_sz++;
    }
  }
  v_dis_sz += s_sz;
  if (comm.rank() == comm.size() - 1) { v_sz += s_sz; }
  distributor ownership (v_dis_sz, comm, v_sz);
  // ------------------------
  // second pass: copy values
  // ------------------------
  vec<T,M> u (ownership);
  size_type my_proc = comm.rank();
  size_type last_proc = comm.size() - 1;
  typename vec<T,M>::iterator pu = u.begin();
  for(typename std::list<value_type>::const_iterator iter = _l.begin(); iter != _l.end(); ++iter) {
    const vec_concat_value<T,M>& x = *iter;
    if (x.variant == value_type::vector) {
      for (typename vec<T,M>::const_iterator pv = x.v.begin(), lastv = x.v.end(); pv != lastv; pv++) {
        *pu++ = *pv;
      }
    } else {
      if (my_proc == last_proc) {
        *pu++ = x.s;
      }
    }
  }
  return u;
}
#ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST
template <class T, class M>
inline
vec<T,M>::vec (const std::initializer_list<vec_concat_value<T,M> >& init_list)
{
  vec_concat<T,M> vc (init_list);
  vec<T,M>::operator= (vc.build_vec());
}
template <class T, class M>
inline
vec<T,M>&
vec<T,M>::operator= (const std::initializer_list<vec_concat_value<T,M> >& init_list)
{
  vec_concat<T,M> vc (init_list);
  vec<T,M>::operator= (vc.build_vec());
  return *this;
}
#endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST

} // namespace rheolef
#endif // _RHEOLEF_VEC_CONCAT_H