So-Bogus
A c++ sparse block matrix library aimed at Second Order cone problems
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
CompoundSparseBlockIndex.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_COMPOUND_SPARSE_BLOCK_INDEX_HPP
13 #define BOGUS_COMPOUND_SPARSE_BLOCK_INDEX_HPP
14 
15 #include <vector>
16 
17 #include "SparseBlockIndex.hpp"
18 
19 namespace bogus
20 {
21 
22 template < typename FirstIndexType, typename SecondIndexType, bool NativeOrder >
24  CompoundSparseBlockIndex< FirstIndexType, SecondIndexType, NativeOrder > >
25 {
26  typedef typename FirstIndexType::Index Index ;
27  typedef typename FirstIndexType::BlockPtr BlockPtr ;
28 
30  typedef typename Base::InnerOffsetsType InnerOffsetsType ;
31  typedef typename Base::InnerIterator InnerIterator ;
32  using Base::valid ;
33 
36  : Base( index1.valid && index2.valid ),
37  first( index1.derived() ), second( index2.derived() ),
38  innerOffsets( index1.innerOffsetsArray() )
39  {
40  assert( first.outerSize() == first.outerSize() ) ;
41  assert( second.innerSize() == second.innerSize() ) ;
42  }
43 
44  Index outerSize() const { return first.outerSize() ; }
45  Index nonZeros() const { return first.nonZeros() + second.nonZeros() ; }
46 
47  const InnerOffsetsType& innerOffsetsArray() const { return innerOffsets ; }
48 
49  Index size( Index outerIdx ) const
50  {
51  return first.size( outerIdx ) + second.size( outerIdx ) ;
52  }
53 
54  const FirstIndexType& first ;
55  const SecondIndexType& second ;
56  const InnerOffsetsType &innerOffsets ;
57 } ;
58 
59 template < typename FirstIndexType, typename SecondIndexType, bool NativeOrder >
60 struct SparseBlockIndexTraits< CompoundSparseBlockIndex< FirstIndexType, SecondIndexType, NativeOrder > >
61 {
62  typedef typename FirstIndexType::Index Index ;
63  typedef typename FirstIndexType::BlockPtr BlockPtr ;
64 
66 
67  struct InnerIterator
68  {
69  InnerIterator( const SparseBlockIndexType& index, Index outer )
70  : m_it1( index.first, outer ), m_it2( index.second, outer ), m_it2_begin( m_it2 )
71  {
72  }
73 
74  operator bool() const
75  {
76  return m_it1 || m_it2 ;
77  }
78 
79  InnerIterator& operator++()
80  {
81  if( m_it1 ) ++ m_it1 ;
82  else ++ m_it2 ;
83  return *this ;
84  }
85  InnerIterator& operator--()
86  {
87  if( m_it2 == m_it2_begin ) --m_it1 ;
88  else --m_it2 ;
89 
90  return *this ;
91  }
92 
93  Index inner() const {
94  return m_it1 ? m_it1.inner() : m_it2.inner() ;
95  }
96  BlockPtr ptr() const {
97  return m_it1 ? m_it1.ptr() : m_it2.ptr() ;
98  }
99 
100  InnerIterator end() const
101  {
102  return InnerIterator( *this ).toEnd() ;
103  }
104 
105  InnerIterator& toEnd() const
106  {
107  m_it1.toEnd() ;
108  m_it2.toEnd() ;
109  return *this ;
110  }
111 
112  bool after( Index outer ) const
113  {
114  return NativeOrder ? ( inner() > outer ) : ( inner() < outer ) ;
115  }
116 
117  private:
118  typename FirstIndexType::InnerIterator m_it1 ;
119  typename SecondIndexType::InnerIterator m_it2 ;
120  typename SecondIndexType::InnerIterator m_it2_begin ;
121  } ;
122 
123 } ;
124 
125 
126 }
127 
128 #endif
129 
Definition: SparseBlockIndex.hpp:29
const InnerOffsetsType & innerOffsetsArray() const
Definition: SparseBlockIndex.hpp:24
bool valid
Whether this index is currently valid.
Definition: SparseBlockIndex.hpp:40
std::vector< Index > InnerOffsetsType
Type of the array encoding the size of each block of the inner dimension.
Definition: SparseBlockIndex.hpp:37
Definition: CompoundSparseBlockIndex.hpp:23