So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
Access.hpp
1 /*
2  * This file is part of bogus, a C++ sparse block matrix library.
3  *
4  * Copyright 2013 Gilles Daviet <gdaviet@gmail.com>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
10 
11 
12 #ifndef BOGUS_BLOCK_ACCESS_HPP
13 #define BOGUS_BLOCK_ACCESS_HPP
14 
15 #include "Constants.hpp"
16 #include "Traits.hpp"
17 
18 #include "../Utils/CppTools.hpp"
19 
20 namespace bogus {
21 
23 template < typename SelfTransposeT >
24 inline typename EnableIf< BlockTraits< SelfTransposeT >::is_self_transpose, const SelfTransposeT& >::ReturnType
25 transpose_block( const SelfTransposeT &block ) { return block ; }
26 
28 template< typename BlockType >
29 inline typename EnableIf< !BlockTraits< BlockType >::is_self_transpose, typename BlockType::ConstTransposeReturnType >::ReturnType
30 transpose_block ( const BlockType& block )
31 {
32  return block.transpose() ;
33 }
34 
36 
42 template< typename BlockType,
43  bool IsSelfTranspose = BlockTraits< BlockType >::is_self_transpose,
44  bool DefinesConstTranspose = HasConstTransposeReturnType< BlockType >::Value,
45  bool DefinesTransposeTraits = HasReturnType< BlockTransposeTraits< BlockType > >::Value >
47  enum { is_defined= 0 } ;
48 } ;
49 
50 // Self-transpose
51 template< typename BlockType, bool DCT, bool DTT >
52 struct BlockTranspose< BlockType, true, DCT, DTT > {
53  typedef const BlockType& ReturnType ;
54  enum { is_defined = 1 } ;
55 } ;
56 // ConstTransposeReturnType
57 template< typename BlockType, bool DTT >
58 struct BlockTranspose< BlockType, false, true, DTT > {
59  typedef typename BlockType::ConstTransposeReturnType ReturnType ;
60  enum { is_defined = 1 } ;
61 } ;
62 // BlockTransposeTraits
63 template< typename BlockType >
64 struct BlockTranspose< BlockType, false, false, true > {
65  typedef typename BlockTransposeTraits< BlockType >::ReturnType ReturnType ;
66  enum { is_defined = 1 } ;
67 } ;
68 
69 template < typename BlockType >
71 {
72  enum {
74  } ;
75 } ;
76 
77 
79 template < bool DoTranspose >
80 struct TransposeIf {
81  template < typename BlockT >
82  inline static const BlockT& get( const BlockT& src )
83  { return src ; }
84 } ;
85 template < >
86 struct TransposeIf< true > {
87  template < typename BlockT >
88  inline static typename BlockTranspose< BlockT >::ReturnType get( const BlockT& src )
89  { return transpose_block( src ) ; }
90 } ;
91 
93 // Case with runtime test
94 template < bool RuntimeTest, bool >
96  enum { runtime_test = 1, runtime_transpose = 0 } ;
97 
98  template < typename RhsT, typename ResT, typename Scalar >
99  inline static void assign( const RhsT& rhs, ResT &res, const Scalar scale, bool doTranspose )
100  {
101  if( doTranspose ) res = scale * transpose_block( rhs ) ;
102  else res = scale * rhs ;
103  }
104 } ;
105 
106 // Case with compile-time check
107 template < bool CompileTimeTranspose >
108 struct BlockTransposeOption< false, CompileTimeTranspose >
109 {
110  enum { runtime_test = 0, runtime_transpose = 1 } ;
111 
112  template < typename RhsT, typename ResT, typename Scalar >
113  inline static void assign( const RhsT& rhs, ResT &res, const Scalar scale, bool )
114  {
115  res = scale * TransposeIf< CompileTimeTranspose >::get( rhs ) ;
116  }
117 } ;
118 
119 
121 template< typename BlockT, bool Transpose_ = false >
122 struct BlockDims
123 {
124  typedef BlockTraits< BlockT > Traits ;
126 
127  enum { Rows = Dims::First,
128  Cols = Dims::Second } ;
129 } ;
130 
132 template < int DimensionAtCompileTime, typename VectorType, typename Index >
133 struct Segmenter
134 {
135  enum { dimension = DimensionAtCompileTime } ;
136 
137  typedef typename VectorType::template NRowsBlockXpr< dimension >::Type
138  ReturnType ;
139  typedef typename VectorType::template ConstNRowsBlockXpr< dimension >::Type
140  ConstReturnType ;
141 
142  Segmenter( VectorType &vec, const Index* ) : m_vec( vec ) {}
143 
144  inline ReturnType operator[]( const Index inner )
145  {
146  return m_vec.template middleRows< dimension >( dimension*inner ) ;
147  }
148 
149  inline ConstReturnType operator[]( const Index inner ) const
150  {
151  return m_vec.template middleRows< dimension >( dimension*inner ) ;
152  }
153 
154 private:
155  VectorType & m_vec ;
156 } ;
157 
158 template < typename VectorType, typename Index >
159 struct Segmenter< internal::DYNAMIC, VectorType, Index >
160 {
161  typedef typename VectorType::RowsBlockXpr ReturnType ;
162  typedef typename VectorType::ConstRowsBlockXpr ConstReturnType ;
163 
164  Segmenter( VectorType &vec, const Index* offsets ) : m_vec( vec ), m_offsets( offsets ) { }
165 
166  inline ReturnType operator[]( const Index inner )
167  {
168  return m_vec.middleRows( m_offsets[ inner ], m_offsets[ inner + 1 ] - m_offsets[ inner ] ) ;
169  }
170 
171  inline ConstReturnType operator[]( const Index inner ) const
172  {
173  return m_vec.middleRows( m_offsets[ inner ], m_offsets[ inner + 1 ] - m_offsets[ inner ] ) ;
174  }
175 
176 
177 private:
178  VectorType & m_vec ;
179  const Index* m_offsets ;
180 } ;
181 
182 }
183 
184 #endif
EnableIf< BlockTraits< SelfTransposeT >::is_self_transpose, const SelfTransposeT & >::ReturnType transpose_block(const SelfTransposeT &block)
Specialization of transpose_block() for self-adjoint types.
Definition: Access.hpp:25
Access to the dimensions of a block.
Definition: Access.hpp:122
Defines the transpose type of a BlockType using self-introspection.
Definition: Access.hpp:46
Utility struct to handle both compile-time and runtime optionally transposed ops. ...
Definition: Access.hpp:95
Definition: Traits.hpp:29
Utility struct for expressing a compile-time conditional transpose of a block.
Definition: Access.hpp:80
Definition: Access.hpp:70
Definition: CppTools.hpp:49
Access to segment of a vector corresponding to a given block-row.
Definition: Access.hpp:133
Defines the return type of an associated transpose_block( const BlockType&amp; ) function.
Definition: Traits.hpp:55
Definition: CppTools.hpp:75