So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
CppTools.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 #ifndef BOGUS_CPP_TOOLS_HPP
12 #define BOGUS_CPP_TOOLS_HPP
13 
14 #include <iostream>
15 #include <vector>
16 #include <cassert>
17 
18 namespace bogus
19 {
20 
21 #ifndef BOGUS_HAS_CPP11
22 #define BOGUS_HAS_CPP11 (__cplusplus >= 201103L)
23 #endif
24 
25 // NULLPTR
26 #if BOGUS_HAS_CPP11
27 #define BOGUS_NULL_PTR( Type ) nullptr
28 #else
29 #define BOGUS_NULL_PTR( Type ) (static_cast<Type*>(0))
30 #endif
31 
32 // Swap template parameters if DoSwap is true
33 
34 template < bool DoSwap, typename First_, typename Second_ >
35 struct TypeSwapIf
36 {
37  typedef First_ First ;
38  typedef Second_ Second ;
39 } ;
40 
41 template < typename First_, typename Second_ >
42 struct TypeSwapIf< true, First_, Second_ >
43 {
44  typedef First_ Second ;
45  typedef Second_ First ;
46 } ;
47 
48 template < bool DoSwap, int First_, int Second_ >
49 struct SwapIf
50 {
51  enum { First = First_, Second = Second_ } ;
52 } ;
53 
54 template < int First_, int Second_ >
55 struct SwapIf< true, First_, Second_ >
56 {
57  enum { First = Second_, Second = First_ } ;
58 } ;
59 
60 // Is Same
61 template < typename T1, typename T2>
62 struct IsSame
63 {
64  enum { Value = 0 } ;
65 } ;
66 template < typename T1 >
67 struct IsSame< T1, T1 >
68 {
69  enum { Value = 1 } ;
70 } ;
71 
72 // Enable if (for SFINAE use )
73 
74 template < bool Condition, typename ReturnType_ = void >
75 struct EnableIf
76 {
77 } ;
78 
79 template < typename ReturnType_ >
80 struct EnableIf< true, ReturnType_ >
81 {
82  typedef ReturnType_ ReturnType ;
83 } ;
84 
85 template < bool Condition, typename ReturnType_ = void >
86 struct DisableIf
87 {
88 } ;
89 
90 template < typename ReturnType_ >
91 struct DisableIf< false, ReturnType_ >
92 {
93  typedef ReturnType_ ReturnType ;
94 } ;
95 
96 // Warning : HasXXX< T > does NOT work for reference typedefs !
97 #define BOGUS_DEFINE_HAS_TYPE( TypeName ) \
98  template < typename BaseType > \
99  struct Has##TypeName \
100  { \
101  private: \
102  enum { True = 1, False = 2 } ; \
103  typedef char TrueType[ True ] ; \
104  typedef char FalseType[ False ] ; \
105  template <typename T > struct Some { typedef int Type ; } ; \
106  \
107  template< typename T > \
108  static const TrueType& check( typename Some<typename T::TypeName>::Type ) ; \
109  template< typename > \
110  static const FalseType& check( ... ) ; \
111  public: \
112  enum { Value = ( True == sizeof( check< BaseType >( 0 ) ) ) } ;\
113  }
114 
115 BOGUS_DEFINE_HAS_TYPE( ReturnType ) ;
116 BOGUS_DEFINE_HAS_TYPE( ConstTransposeReturnType ) ;
117 BOGUS_DEFINE_HAS_TYPE( Base ) ;
118 
119 // Static assertions
120 
121 template < bool Assertion >
123 {
124  enum {
125  BLOCKS_MUST_BE_SQUARE_OR_HAVE_DYNAMIC_DIMENSIONS,
126  BLOCKS_MUST_HAVE_FIXED_DIMENSIONS,
127  MATRICES_ORDERING_IS_INCONSISTENT,
128  TRANSPOSE_MAKES_NO_SENSE_IN_THIS_CONTEXT,
129  TRANSPOSE_IS_NOT_DEFINED_FOR_THIS_BLOCK_TYPE,
130  OPERANDS_HAVE_INCONSISTENT_FLAGS,
131  UNORDERED_INSERTION_WITH_COMPRESSED_INDEX,
132  NOT_IMPLEMENTED
133  } ;
134 } ;
135 
136 template < >
137 struct StaticAssert< false >
138 {
139 } ;
140 
141 #define BOGUS_STATIC_ASSERT( test, message ) (void) ::bogus::StaticAssert< test >::message
142 
144 template< typename Element >
146 {
147 public:
148 
150  enum { is_mutable = 0 } ;
151 
152  ConstMappedArray ( )
153  : m_data( 0 ), m_size( 0 )
154  {}
155 
156  ConstMappedArray ( const Element* data, std::size_t size )
157  : m_data( data ), m_size( size )
158  {}
159 
160  void setData( const Element* data, std::size_t size )
161  {
162  m_data = data ;
163  m_size = size ;
164  }
165 
166  const Element* data() const { return m_data ; }
167 
168  inline std::size_t size() const { return m_size ; }
169  inline bool empty() const { return 0 == m_size ; }
170 
171  const Element* begin() const { return data() ; }
172  const Element* end() const { return data() + size() ; }
173 
174  const Element& operator[]( std::size_t idx ) const
175  { return m_data[idx] ; }
176 
177  inline void resize( std::size_t s)
178  { assert( !m_data || m_size == s ) ; (void) s ; }
179  inline void reserve( std::size_t )
180  { }
181  inline void clear( )
182  { }
183  inline void assign( std::size_t s, const Element&)
184  { assert( !m_data || m_size == s ) ; (void) s ; }
185 
186 private:
187  const Element* m_data ;
188  std::size_t m_size ;
189 } ;
190 
191 template< typename Element >
192 inline const Element* data_pointer( const ConstMappedArray< Element >& vec )
193 {
194  return vec.data() ;
195 }
196 
198 template <typename T, typename Allocator>
199 inline const T* data_pointer( const std::vector<T, Allocator>& vec )
200 {
201 #if BOGUS_HAS_CPP11
202  return vec.data() ;
203 #else
204  return vec.empty() ? BOGUS_NULL_PTR( const T ) : &vec[0] ;
205 #endif
206 }
207 
209 template <typename T, typename Allocator>
210 inline T* data_pointer( std::vector<T, Allocator>& vec )
211 {
212 #if BOGUS_HAS_CPP11
213  return vec.data() ;
214 #else
215  return vec.empty() ? BOGUS_NULL_PTR( T ) : &vec[0] ;
216 #endif
217 }
218 
219 
220 template< typename Scalar>
222  const Scalar s ;
223  explicit ConstantArray( Scalar s_=1 ) : s(s_) {}
224 
225  inline bool has_element( int ) const { return true ; }
226  inline Scalar element( int ) const { return s ; }
227  inline Scalar operator[]( int ) const { return s ; }
228 };
229 template< typename Scalar>
230 inline ConstantArray<Scalar> make_constant_array( Scalar s) { return ConstantArray<Scalar>(s) ; }
231 
232 } //namespace bogus
233 
234 
235 #endif
236 
237 
238 
Definition: CppTools.hpp:86
Definition: CppTools.hpp:122
Definition: CppTools.hpp:35
Definition: CppTools.hpp:221
Definition: CppTools.hpp:49
Definition: CppTools.hpp:62
Definition: CppTools.hpp:75
Const mapped array, used for Mapped Block Matrices.
Definition: CppTools.hpp:145