12 #ifndef COMPRESSED_SPARSE_BLOCK_INDEX_HPP
13 #define COMPRESSED_SPARSE_BLOCK_INDEX_HPP
15 #include "SparseBlockIndex.hpp"
16 #include "../Utils/CppTools.hpp"
22 template<
typename Index_,
typename BlockPtr_,
template <
typename>
class ArrayType >
25 typedef Index_ Index ;
26 typedef BlockPtr_ BlockPtr ;
30 typedef typename Base::InnerIterator InnerIterator ;
33 typedef typename ArrayType< Index >::Type
Inner ;
34 typedef typename ArrayType< Index >::Type
Outer ;
36 InnerOffsetsType innerOffsets ;
47 void resizeOuter( Index size )
49 outer.assign( size+1, 0 ) ;
51 void reserve( Index nnz)
53 inner.reserve( nnz ) ;
56 Index outerSize( )
const {
return outer.size() - 1 ; }
57 Index nonZeros()
const {
return inner.size() ; }
60 const InnerOffsetsType& innerOffsetsArray()
const {
return innerOffsets ; }
64 template <
bool Ordered >
65 void insert( Index outIdx, Index inIdx, BlockPtr ptr )
67 BOGUS_STATIC_ASSERT( Ordered, UNORDERED_INSERTION_WITH_COMPRESSED_INDEX
70 valid &= ( ptr == (BlockPtr) ( inner.size() ) )
71 && ( 0 == outer[ outIdx+1 ] || inIdx > inner.back() ) ;
73 inner.push_back( inIdx ) ;
75 void insertBack( Index outIdx, Index inIdx, BlockPtr ptr )
76 { insert< true >( outIdx, inIdx, ptr ) ; }
87 for(
unsigned i = 1 ; i < outer.size() ; ++i )
89 outer[i] += outer[i-1] ;
95 outer.assign( outer.size(), 0 ) ;
101 SparseBlockIndex &operator=(
const SparseBlockIndex &compressed )
103 if( &compressed !=
this )
105 outer = compressed.outer ;
106 inner = compressed.inner ;
107 if( !compressed.innerOffsets.empty() )
108 innerOffsets = compressed.innerOffsets ;
109 valid = compressed.valid ;
114 template <
typename SourceDerived >
115 SparseBlockIndex &operator=(
const SparseBlockIndexBase< SourceDerived > &source )
117 resizeOuter( source.outerSize() ) ;
118 reserve( source.nonZeros() ) ;
121 if( source.hasInnerOffsets() ) {
122 innerOffsets.resize( source.innerOffsetsArray().size() ) ;
123 std::copy( source.innerOffsetsArray().begin(), source.innerOffsetsArray().end(), innerOffsets.begin() ) ;
125 valid = source.valid ;
127 for(
typename SourceDerived::Index i = 0 ; i < source.outerSize() ; ++i )
129 for(
typename SourceDerived::InnerIterator it( source.derived(), i ) ;
132 insertBack( i, it.inner(), it.ptr() ) ;
141 SparseBlockIndex & move( SparseBlockIndex &compressed )
143 if( &compressed !=
this )
147 inner.swap( compressed.inner );
148 outer.swap( compressed.outer );
150 if( !compressed.innerOffsets.empty() )
151 innerOffsets.swap( compressed.innerOffsets ) ;
152 valid = compressed.valid ;
153 compressed.valid = false ;
159 template <
typename SourceDerived >
160 SparseBlockIndex &move(
const SparseBlockIndexBase< SourceDerived > &source )
162 return ( *
this = source ) ;
165 Index size(
const Index outerIdx )
const
167 return outer[ outerIdx + 1 ] - outer[ outerIdx ] ;
172 const Index* rowIndex()
const {
return data_pointer(outer) ; }
173 const Index* columns()
const {
return data_pointer(inner) ; }
176 const Index* outerIndexPtr()
const {
return data_pointer(outer) ; }
177 const Index* innerIndexPtr()
const {
return data_pointer(inner) ; }
181 template <
typename Index_,
typename BlockPtr_,
template <
typename>
class ArrayType >
184 typedef Index_ Index;
185 typedef BlockPtr_ BlockPtr;
194 typedef std::random_access_iterator_tag iterator_category;
195 typedef Index value_type;
196 typedef ptrdiff_t difference_type;
197 typedef const Index* pointer;
198 typedef const Index& reference;
200 InnerIterator( ) : m_inner( BOGUS_NULL_PTR(
const Index) ) { }
203 : m_it( index.
outer[ outer ] ), m_end( index.
outer[ outer + 1] ),
204 m_inner( data_pointer( index.
inner ) )
208 operator bool()
const
210 return m_it != m_end ;
213 InnerIterator& operator++()
218 InnerIterator& operator--()
224 InnerIterator& operator+= (
const std::size_t n )
226 m_it = std::min( m_it + (Index) n, m_end ) ;
230 difference_type operator- (
const InnerIterator& other )
const
232 return ( (difference_type) m_it ) - ( difference_type ) other.m_it ;
235 Index operator* ()
const
240 InnerIterator end()
const
242 return InnerIterator( *this ).toEnd() ;
245 InnerIterator& toEnd()
251 bool after( Index outer )
const
253 return inner() > outer ;
256 Index inner()
const {
return m_inner[ m_it ] ; }
257 BlockPtr ptr()
const {
return m_it ; }
259 BlockPtr rawIndex()
const {
return m_it ; }
265 const Index* m_inner ;
Definition: SparseBlockIndex.hpp:29
Uncompressed sparse block index.
Definition: SparseBlockIndex.hpp:86
Outer outer
Vector encoding the start and end of inner vectors.
Definition: CompressedSparseBlockIndex.hpp:41
ArrayType< Inner >::Type Outer
Vector of inner vectors.
Definition: SparseBlockIndex.hpp:100
Definition: SparseBlockIndex.hpp:24
Compressed index, compatible with usual BSR/BSC formats.
Definition: CompressedSparseBlockIndex.hpp:23
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
Inner inner
Vector of inner indices.
Definition: CompressedSparseBlockIndex.hpp:39
void finalize()
Finalizes the outer indices vector.
Definition: CompressedSparseBlockIndex.hpp:85
void insert(Index outIdx, Index inIdx, BlockPtr ptr)
Definition: CompressedSparseBlockIndex.hpp:65
std::vector< std::pair< Index, BlockPtr > > Inner
Vector of ( inner index ; block pointer ) tuples encoding an inner vector.
Definition: SparseBlockIndex.hpp:98