Logo  0.95.0-final
Finite Element Embedded Library and Language in C++
Feel++ Feel++ on Github Feel++ community
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
feelalg/traits.hpp
Go to the documentation of this file.
1 /* -*- mode: c++; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; show-trailing-whitespace: t -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
2 
3  This file is part of the Feel library
4 
5  Author(s): Christophe Prud'homme <christophe.prudhomme@feelpp.org>
6  Date: 2005-08-17
7 
8  Copyright (C) 2005,2006 EPFL
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License as published by the Free Software Foundation; either
13  version 3.0 of the License, or (at your option) any later version.
14 
15  This library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public
21  License along with this library; if not, write to the Free Software
22  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 */
29 #ifndef __FEELALG_TRAITS_HPP
30 #define __FEELALG_TRAITS_HPP 1
31 
32 #include <boost/preprocessor/comparison/less.hpp>
33 #include <boost/preprocessor/comparison/equal.hpp>
34 #include <boost/preprocessor/logical/and.hpp>
35 #include <boost/preprocessor/control/if.hpp>
36 #include <boost/preprocessor/list/at.hpp>
37 #include <boost/preprocessor/list/cat.hpp>
38 #include <boost/preprocessor/list/for_each_product.hpp>
39 #include <boost/preprocessor/logical/or.hpp>
40 #include <boost/preprocessor/tuple/to_list.hpp>
41 #include <boost/preprocessor/tuple/eat.hpp>
42 #include <boost/preprocessor/facilities/empty.hpp>
43 #include <boost/preprocessor/punctuation/comma.hpp>
44 #include <boost/preprocessor/facilities/identity.hpp>
45 #include <boost/preprocessor/arithmetic/add.hpp>
46 #include <boost/preprocessor/list/filter.hpp>
47 
48 #include <boost/numeric/ublas/vector.hpp>
49 #include <boost/numeric/ublas/matrix.hpp>
50 #include <boost/numeric/ublas/matrix_sparse.hpp>
51 
52 
53 #include <feel/feelcore/feel.hpp>
54 #include <feel/feelcore/traits.hpp>
55 
56 
57 namespace Feel
58 {
60 namespace glas
61 {
62 namespace ublas = boost::numeric::ublas;
63 
64 struct vector_tag {};
65 struct matrix_tag {};
66 
74 template<typename T>
75 struct traits
76 {
77  typedef T self_type;
78  typedef typename self_type::value_type value_type;
79 };
80 
81 /*
82  * Vector
83  */
84 # define FEELPP_GLAS_TRAITS_VECTOR_TYPE(T) BOOST_PP_TUPLE_ELEM(5, 0 , T)
85 # define FEELPP_GLAS_TRAITS_VECTOR_ITERATOR(T) BOOST_PP_TUPLE_ELEM(5, 1 , T)
86 # define FEELPP_GLAS_TRAITS_VECTOR_CONST_ITERATOR(T) BOOST_PP_TUPLE_ELEM(5, 2 , T)
87 # define FEELPP_GLAS_TRAITS_VECTOR_SIZE(T) BOOST_PP_TUPLE_ELEM(5, 3 , T)
88 # define FEELPP_GLAS_TRAITS_VECTOR_RESIZE(T) BOOST_PP_TUPLE_ELEM(5, 4 , T)
89 #
90 # define FEELPP_GLAS_TRAITS_VECTOR_TYPES \
91  BOOST_PP_TUPLE_TO_LIST( \
92  2, \
93  ( \
94  ( ublas::vector, iterator, const_iterator, size, resize ), \
95  ( std::vector , iterator, const_iterator, size, resize ) \
96  ) \
97  ) \
98 
99 #
100 # /* Generates code for all integral types. */
101 # define FEELPP_GLAS_TRAITS_VECTOR_OP(_, T) \
102  FEELPP_GLAS_TRAITS_VECTOR_OP_CODE T \
103 
104 #
105 #define FEELPP_GLAS_TRAITS_VECTOR_OP_CODE(T,V) \
106 template<> \
107 struct traits<FEELPP_GLAS_TRAITS_VECTOR_TYPE( V )<FEELPP_TRAITS_TYPE( T )> > \
108 { \
109  typedef FEELPP_TRAITS_TYPE( T ) value_type; \
110  typedef FEELPP_GLAS_TRAITS_VECTOR_TYPE( V )<FEELPP_TRAITS_TYPE( T )> self_type; \
111  typedef self_type::FEELPP_GLAS_TRAITS_VECTOR_ITERATOR( V ) iterator; \
112  typedef self_type::FEELPP_GLAS_TRAITS_VECTOR_CONST_ITERATOR( V ) const_iterator; \
113  typedef vector_tag type_tag; \
114  static const bool is_vector = true; \
115  static const bool is_matrix = false; \
116  \
117 }; \
118 traits<FEELPP_GLAS_TRAITS_VECTOR_TYPE( V )<FEELPP_TRAITS_TYPE( T )> >::const_iterator \
119 begin( FEELPP_GLAS_TRAITS_VECTOR_TYPE( V )<FEELPP_TRAITS_TYPE( T )> const& t ) \
120 { \
121  return t.begin(); \
122 } \
123 traits<FEELPP_GLAS_TRAITS_VECTOR_TYPE( V )<FEELPP_TRAITS_TYPE( T )> >::iterator \
124 begin( FEELPP_GLAS_TRAITS_VECTOR_TYPE( V )<FEELPP_TRAITS_TYPE( T )>& t ) \
125 { \
126  return t.begin(); \
127 } \
128 traits<FEELPP_GLAS_TRAITS_VECTOR_TYPE( V )<FEELPP_TRAITS_TYPE( T )> >::const_iterator \
129 end( FEELPP_GLAS_TRAITS_VECTOR_TYPE( V )<FEELPP_TRAITS_TYPE( T )> const& t ) \
130 { \
131  return t.end(); \
132 } \
133 traits<FEELPP_GLAS_TRAITS_VECTOR_TYPE( V )<FEELPP_TRAITS_TYPE( T )> >::iterator \
134 end( FEELPP_GLAS_TRAITS_VECTOR_TYPE( V )<FEELPP_TRAITS_TYPE( T )>& t ) \
135 { \
136  return t.end(); \
137 } \
138 inline \
139 size_type size( FEELPP_GLAS_TRAITS_VECTOR_TYPE( V )<FEELPP_TRAITS_TYPE( T )> const& t ) \
140 { \
141  return t.FEELPP_GLAS_TRAITS_VECTOR_SIZE(V)(); \
142 } \
143 inline \
144 void resize( FEELPP_GLAS_TRAITS_VECTOR_TYPE( V )<FEELPP_TRAITS_TYPE( T )>& t, size_type newsize ) \
145 { \
146  return t.FEELPP_GLAS_TRAITS_VECTOR_RESIZE(V)( newsize ); \
147 } \
148 
149 
150 
154 BOOST_PP_LIST_FOR_EACH_PRODUCT( FEELPP_GLAS_TRAITS_VECTOR_OP, 2, ( FEELPP_TRAITS_TYPES, FEELPP_GLAS_TRAITS_VECTOR_TYPES ) );
155 
156 /*
157  * Matrices
158  */
159 #
160 # define FEELPP_GLAS_TRAITS_MATRIX_TYPE(T) BOOST_PP_TUPLE_ELEM(5, 0 , T)
161 # define FEELPP_GLAS_TRAITS_MATRIX_SIZE1(T) BOOST_PP_TUPLE_ELEM(5, 1 , T)
162 # define FEELPP_GLAS_TRAITS_MATRIX_SIZE2(T) BOOST_PP_TUPLE_ELEM(5, 2 , T)
163 # define FEELPP_GLAS_TRAITS_MATRIX_RESIZE(T) BOOST_PP_TUPLE_ELEM(5, 3 , T)
164 # define FEELPP_GLAS_TRAITS_MATRIX_NNZ(T) BOOST_PP_TUPLE_ELEM(5, 4 , T)
165 #
166 #define UBLAS_MATRIX_ROW( T ) ublas::matrix<T, ublas::row_major>
167 #define UBLAS_MATRIX_COL( T ) ublas::matrix<T, ublas::column_major>
168 #define UBLAS_MATRIX_SPARSE_ROW( T ) ublas::compressed_matrix<T, ublas::row_major>
169 #define UBLAS_MATRIX_SPARSE_COL( T ) ublas::compressed_matrix<T, ublas::column_major>
170 #
171 # define FEELPP_GLAS_TRAITS_MATRIX_TYPES \
172  BOOST_PP_TUPLE_TO_LIST( \
173  4, \
174  ( \
175  ( UBLAS_MATRIX_ROW , size1, size2, resize, boost::none_t ), \
176  ( UBLAS_MATRIX_COL , size1, size2, resize, boost::none_t ), \
177  ( UBLAS_MATRIX_SPARSE_ROW, size1, size2, resize, nnz ), \
178  ( UBLAS_MATRIX_SPARSE_COL, size1, size2, resize, nnz ) \
179  ) \
180  ) \
181 
182 #
183 # /* Generates code for all integral types. */
184 # define FEELPP_GLAS_TRAITS_MATRIX_OP(_, T) \
185  FEELPP_GLAS_TRAITS_MATRIX_OP_CODE T \
186 
187 #
188 #define FEELPP_GLAS_TRAITS_MATRIX_OP_CODE(T,V) \
189 template<> \
190 struct traits<FEELPP_GLAS_TRAITS_MATRIX_TYPE( V )( FEELPP_TRAITS_TYPE( T ) ) > \
191 { \
192  typedef FEELPP_TRAITS_TYPE( T ) value_type; \
193  typedef FEELPP_GLAS_TRAITS_MATRIX_TYPE( V )( FEELPP_TRAITS_TYPE( T ) ) self_type; \
194  typedef matrix_tag type_tag; \
195  static const bool is_vector = false; \
196  static const bool is_matrix = true; \
197 }; \
198 inline \
199 size_type nrows( FEELPP_GLAS_TRAITS_MATRIX_TYPE( V )( FEELPP_TRAITS_TYPE( T ) ) const& t ) \
200 { \
201  return t.FEELPP_GLAS_TRAITS_MATRIX_SIZE1(V)(); \
202 } \
203 inline \
204 size_type ncols( FEELPP_GLAS_TRAITS_MATRIX_TYPE( V )( FEELPP_TRAITS_TYPE( T ) ) const& t ) \
205 { \
206  return t.FEELPP_GLAS_TRAITS_MATRIX_SIZE2(V)(); \
207 } \
208 inline \
209 void resize( FEELPP_GLAS_TRAITS_MATRIX_TYPE( V )( FEELPP_TRAITS_TYPE( T ) )& t, size_type newsize1, size_type newsize2 ) \
210 { \
211  return t.FEELPP_GLAS_TRAITS_MATRIX_RESIZE(V)( newsize1, newsize2 ); \
212 } \
213 
214 
215 
219 BOOST_PP_LIST_FOR_EACH_PRODUCT( FEELPP_GLAS_TRAITS_MATRIX_OP, 2, ( FEELPP_TRAITS_TYPES, FEELPP_GLAS_TRAITS_MATRIX_TYPES ) );
220 
221 } // glas
223 } // Feel
224 
225 #endif /*__FEELALG_TRAITS_HPP */

Generated on Fri Oct 25 2013 14:24:26 for Feel++ by doxygen 1.8.4