Rheolef  7.2
an efficient C++ finite element environment
 
Loading...
Searching...
No Matches
pair_set.h
Go to the documentation of this file.
1#ifndef _RHEOLEF_PAIR_SET_H
2#define _RHEOLEF_PAIR_SET_H
3//
4// This file is part of Rheolef.
5//
6// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
7//
8// Rheolef is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 2 of the License, or
11// (at your option) any later version.
12//
13// Rheolef is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17//
18// You should have received a copy of the GNU General Public License
19// along with Rheolef; if not, write to the Free Software
20// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21//
22// =========================================================================
23// same as index_set, but with a pair<size_t,T> instead of a size_t
24// wrapper around map<size_t,T> with goodies such as union as a+b
25//
26// motivation: this class is useful for disarray<pair_set,M>
27// to send/receive variable-sized lists via MPI correctly
28// disarray<pair_set,M> is used by the asr class
29// send & receive of such lists are used to assembly a global matrix locally
30// when computing the csr*csr product
31//
32// author: Pierre.Saramito@imag.fr
33//
34// date: 19 mai 2012
35//
36#include "rheolef/communicator.h"
37#include "rheolef/pretty_name.h"
38#include "rheolef/msg_util.h"
39
40#pragma GCC diagnostic push
41#pragma GCC diagnostic ignored "-Weffc++"
42#pragma GCC diagnostic ignored "-Wparentheses"
43#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
44#ifdef _RHEOLEF_HAVE_MPI
45#include "rheolef/mpi_pair_datatype.h"
46#endif // _RHEOLEF_HAVE_MPI
47#include <boost/serialization/map.hpp>
48#include <boost/serialization/base_object.hpp>
49#pragma GCC diagnostic pop
50
51namespace rheolef {
52
53/*Class:
54NAME: pair_set - a set of (index,value) pair (@PACKAGE@-@VERSION@)
55SYNOPSIS:
56 A class for: l = @{(0,3.3),...(7,8.2)@} i.e. a wrapper for STL @code{map<size_t,T>} with
57 some assignment operators, such as l1 += l2.
58 This class is suitable for use with the @code{disarray<T>} class,
59 as @code{disarray<pair_set>} (@pxref{disarray class}).
60TODO:
61 template <T,A> with A=std::allocator or heap_allocator
62 difficulty: get heap_allocator<T> or std::allocator<T> and then requires
63 heap_allocator<pair<size_t,T>> or std::allocator<>pair<size_t,T>>
64 for std::map
65
66AUTHOR: Pierre.Saramito@imag.fr
67DATE: date: 19 may 2012
68End:
69*/
70//<verbatim:
71template<class T, class A = std::allocator<std::pair<std::size_t,T> > >
72class pair_set: public std::map<std::size_t, T, std::less<std::size_t> > {
73 // TODO: use A extra-arg for std::map for heap_allocator
74public:
75
76// typedefs:
77
78 typedef std::size_t size_type;
79 typedef std::pair<size_type,T> pair_type;
80 typedef std::pair<const size_type,T> const_pair_type;
82 typedef std::map<size_type, T, std::less<size_type> > // TODO: use allocator_type for heap_allocator
84#ifdef TODO
85 typedef typename base::allocator_type allocator_type;
86#endif // TODO
88 typedef typename base::iterator iterator;
89 typedef typename base::const_iterator const_iterator;
90
91// allocators:
92
93 pair_set (const A& alloc = A());
94 pair_set (const pair_set<T,A>& x, const A& alloc = A());
96 void clear ();
97
98// basic algebra: semantic of a sparse vector
99
100 pair_set<T,A>& operator+= (const pair_type& x); // c := a union {x}
101 template<class B>
102 pair_set<T,A>& operator+= (const pair_set<T,B>& b); // c := a union b
103
104// boost mpi:
105
106 template <class Archive>
107 void serialize (Archive& ar, const unsigned int version);
108};
109// io:
110template <class T, class A>
111std::istream& operator>> (std::istream& is, pair_set<T,A>& a);
112template <class T, class A>
113std::ostream& operator<< (std::ostream& os, const pair_set<T,A>& b);
114//>verbatim:
115
116namespace details {
117// for boost mpi and disarray<pair_set>:
118template <class T, class A>
119struct default_set_op_traits<pair_set<T,A> > {
120 using type = details::generic_set_plus_op;
121};
122template <class T, class A>
123struct is_container<pair_set<T,A> > : std::true_type {
124 typedef std::true_type type;
125};
126#ifdef _RHEOLEF_HAVE_MPI
127// convert boost::mpl::false_ and true_ to std::false_type and true_type...
128template <class T, class A>
130 : std::conditional<
131 boost::mpi::is_mpi_datatype<T>::value
132 ,std::true_type
133 ,std::false_type
134 >::type
135{
136 typedef
137 typename std::conditional<
138 boost::mpi::is_mpi_datatype<T>::value
139 ,std::true_type
140 ,std::false_type>
141 ::type
143};
144#endif // _RHEOLEF_HAVE_MPI
145} // namespace details
146// -------------------------------------------------------------------
147// inlined
148// -------------------------------------------------------------------
149template <class T, class A>
150inline
152 : base (std::less<size_type>()) // TODO: use alloc extra-arg for base=std::map for heap_allocator
153{
154}
155template <class T, class A>
156inline
157void
159{
160 base::clear();
161}
162template <class T, class A>
163inline
166{
167 iterator p = base::find(x.first);
168 if (p == base::end()) {
169 // insert a new element
170 base::insert (x);
171 } else {
172 // increment an existing element
173 (*p).second += x.second;
174 }
175 return *this;
176}
177template <class T, class A>
178template <class Archive>
179void
180pair_set<T,A>::serialize (Archive& ar, const unsigned int version)
181{
182 ar & boost::serialization::base_object<base>(*this);
183}
184// -------------------------------------------------------------------
185// not inlined
186// -------------------------------------------------------------------
187template <class T, class A>
188pair_set<T,A>::pair_set (const pair_set& a, const A& alloc)
189 : base(std::less<size_type>()) // TODO: use a.get_allocator() extra-arg for base=std::map for heap_allocator
190{
191 for (const_iterator iter = a.base::begin(), last = a.base::end(); iter != last; iter++) {
192 base::insert (*iter);
193 }
194}
195template <class T, class A>
196pair_set<T,A>&
198{
199 base::clear();
200 for (const_iterator iter = a.base::begin(), last = a.base::end(); iter != last; iter++) {
201 base::insert (*iter);
202 }
203 return *this;
204}
205template <class T, class A>
206template <class B>
209{
210 for (typename pair_set<T,B>::const_iterator iter = b.begin(), last = b.end(); iter != last; iter++) {
211 operator+= (*iter);
212 }
213 return *this;
214}
215template <class T, class A>
216std::istream&
217operator>> (std::istream& is, pair_set<T,A>& a)
218{
219 typedef typename pair_set<T,A>::size_type size_type;
220 typedef typename pair_set<T,A>::pair_type pair_type;
221 size_type n;
222 is >> n;
223 a.clear();
224 for (size_type i = 0; i < n; i++) {
225 pair_type xi;
226 is >> xi.first >> xi.second;
227 a.insert (xi);
228 }
229 return is;
230}
231template <class T, class A>
232std::ostream&
233operator<< (std::ostream& os, const pair_set<T,A>& a)
234{
235 typedef typename pair_set<T,A>::size_type size_type;
237 os << a.size() << "\t";
238 for (const_iterator iter = a.begin(), last = a.end(); iter != last; iter++) {
239 os << " " << (*iter).first << " " << (*iter).second;
240 }
241 return os;
242}
243
244} // namespace rheolef
245#endif // _RHEOLEF_PAIR_SET_H
field::size_type size_type
Definition branch.cc:430
pair_set(const pair_set< T, A > &x, const A &alloc=A())
std::map< size_type, T, std::less< size_type > > base
Definition pair_set.h:83
pair_type value_type
Definition pair_set.h:81
base::iterator iterator
Definition pair_set.h:88
pair_set(const A &alloc=A())
Definition pair_set.h:151
pair_set< T, A > & operator+=(const pair_type &x)
Definition pair_set.h:165
std::pair< const size_type, T > const_pair_type
Definition pair_set.h:80
void serialize(Archive &ar, const unsigned int version)
Definition pair_set.h:180
std::pair< size_type, T > pair_type
Definition pair_set.h:79
std::size_t size_type
Definition pair_set.h:78
base::const_iterator const_iterator
Definition pair_set.h:89
pair_set< T, A > & operator=(const pair_set< T, A > &x)
Definition pair_set.h:197
base::allocator_type allocator_type
Definition pair_set.h:85
Expr1::float_type T
Definition field_expr.h:230
This file is part of Rheolef.
std::ostream & operator<<(std::ostream &os, const catchmark &m)
Definition catchmark.h:99
std::enable_if< details::is_rheolef_arithmetic< U >::value, ad3_basic< T > & >::type operator+=(ad3_basic< T > &a, const U &b)
Definition ad3.h:286
std::istream & operator>>(std::istream &is, const catchmark &m)
Definition catchmark.h:88
STL namespace.
Definition sphere.icc:25
std::conditional< boost::mpi::is_mpi_datatype< T >::value, std::true_type, std::false_type >::type type
Definition pair_set.h:142