12 #ifndef BOGUS_SPARSEBLOCKINDEX_HPP
13 #define BOGUS_SPARSEBLOCKINDEX_HPP
23 template<
typename Derived >
28 template<
typename Derived >
32 typedef typename Traits::Index Index ;
33 typedef typename Traits::InnerIterator InnerIterator ;
46 const Derived& derived()
const ;
62 Index
nonZeros()
const {
return derived().nonZeros() ; }
73 Index
size(
const Index outerIdx )
const ;
75 InnerIterator
begin(
const Index outerIdx )
const ;
77 InnerIterator
last(
const Index outerIdx )
const ;
79 InnerIterator
end(
const Index outerIdx )
const ;
84 template <
bool Compressed,
typename Index_,
typename BlockPtr_ = Index_,
85 template <
typename>
class ArrayType = ResizableSequenceContainer >
89 typedef Index_ Index ;
90 typedef BlockPtr_ BlockPtr ;
94 typedef typename Base::InnerIterator InnerIterator ;
98 typedef std::vector < std::pair< Index, BlockPtr > >
Inner ;
100 typedef typename ArrayType< Inner >::Type
Outer ;
102 InnerOffsetsType innerOffsets ;
110 void resizeOuter( Index size )
112 outer.resize( size ) ;
114 void reserve( Index )
118 Index outerSize( )
const {
return outer.size() ; }
119 const InnerOffsetsType& innerOffsetsArray()
const {
return innerOffsets ; }
121 template <
bool Ordered >
122 void insert( Index outIdx, Index inIdx, BlockPtr ptr )
124 outer[ outIdx ].push_back( std::make_pair( inIdx, ptr ) ) ;
127 void insertBack( Index outIdx, Index inIdx, BlockPtr ptr )
128 { insert< true >( outIdx, inIdx, ptr ) ; }
132 if( ordered ) return ;
134 #ifndef BOGUS_DONT_PARALLELIZE
135 #pragma omp parallel for
137 for(
int i = 0 ; i < (Index) outer.size() ; ++i )
139 std::sort( outer[i].
begin(), outer[i].
end() ) ;
145 std::vector< Inner >( outer.size() ).swap( outer ) ;
150 SparseBlockIndex &operator=(
const SparseBlockIndex &o )
156 if( !o.innerOffsets.empty() )
157 innerOffsets = o.innerOffsets ;
162 template <
typename SourceDerived >
163 SparseBlockIndex &operator=(
const SparseBlockIndexBase< SourceDerived > &source ) ;
165 SparseBlockIndex & move( SparseBlockIndex &uncompressed )
167 if( &uncompressed !=
this )
169 outer.swap( uncompressed.outer ) ;
170 if( !uncompressed.innerOffsets.empty() )
171 innerOffsets.swap( uncompressed.innerOffsets ) ;
172 valid = uncompressed.valid ;
173 uncompressed.valid = false ;
178 template <
typename SourceDerived >
179 SparseBlockIndex &move(
const SparseBlockIndexBase< SourceDerived > &source )
181 return ( *
this = source ) ;
184 template <
bool Symmetric,
typename SourceDerived >
185 SparseBlockIndex& setToTranspose(
const SparseBlockIndexBase< SourceDerived > &source )
188 resizeOuter( source.innerSize() ) ;
189 valid = source.valid ;
191 for(
typename SourceDerived::Index i = 0 ; i < source.outerSize() ; ++i )
194 for(
typename SourceDerived::InnerIterator it( source.derived(), i ) ;
195 it && ( !Symmetric || it.inner() < i ) ; ++ it )
197 insertBack( it.inner(), i, it.ptr() ) ;
205 Index size(
const Index outerIdx )
const
207 return outer[ outerIdx ].size() ;
210 Index nonZeros()
const
213 for(
unsigned i = 0 ; i < outer.size() ; ++i )
214 nnz += outer[i].size() ;
219 void changePtr(
const InnerIterator& it, BlockPtr ptr )
221 const_cast< BlockPtr&
>( it.asStdIterator()->second ) = ptr ;
226 template <
bool Compressed,
typename Index_,
typename BlockPtr_,
template <
typename>
class ArrayType >
229 typedef Index_ Index;
230 typedef BlockPtr_ BlockPtr;
239 typedef std::random_access_iterator_tag iterator_category;
240 typedef Index value_type;
241 typedef std::ptrdiff_t difference_type;
242 typedef const Index* pointer;
243 typedef const Index& reference;
248 : m_it( index.outer[ outer ].begin() ), m_end( index.outer[ outer ].end() )
252 operator bool()
const
254 return m_it != m_end ;
257 InnerIterator& operator++()
262 InnerIterator& operator--()
268 InnerIterator& operator+= (
const std::size_t n )
274 difference_type operator- (
const InnerIterator& other )
const
276 return m_it - other.m_it ;
279 Index operator* ()
const
284 InnerIterator end()
const
286 return InnerIterator( *this ).toEnd() ;
289 InnerIterator& toEnd()
295 bool after( Index outer )
const
297 return inner() > outer ;
300 Index inner()
const {
return m_it->first ; }
301 BlockPtr ptr()
const {
return m_it->second ; }
304 typename SparseBlockIndexType::Inner::const_iterator asStdIterator()
const {
return m_it ; }
307 typename SparseBlockIndexType::Inner::const_iterator m_it ;
308 typename SparseBlockIndexType::Inner::const_iterator m_end ;
317 #endif // SPARSEBLOCKINDEX_HPP
Definition: SparseBlockIndex.hpp:29
Uncompressed sparse block index.
Definition: SparseBlockIndex.hpp:86
Index size(const Index outerIdx) const
Returns the number of blocks at the outer index outerIdx.
const Index * innerOffsetsData() const
Same as innerOffsetsArray, but returns a pointer instead. Assumes hasInnerOffsets() ...
Definition: SparseBlockIndex.hpp:65
const InnerOffsetsType & innerOffsetsArray() const
ArrayType< Inner >::Type Outer
Vector of inner vectors.
Definition: SparseBlockIndex.hpp:100
InnerIterator end(const Index outerIdx) const
Returns an iterator to the end of outerIdx.
Definition: SparseBlockIndex.hpp:24
bool valid
Whether this index is currently valid.
Definition: SparseBlockIndex.hpp:40
InnerIterator last(const Index outerIdx) const
Returns an iterator to the last non-empty block of outerIdx.
Index nonZeros() const
Returns the total number of nonZeros in this index.
Definition: SparseBlockIndex.hpp:62
std::vector< Index > InnerOffsetsType
Type of the array encoding the size of each block of the inner dimension.
Definition: SparseBlockIndex.hpp:37
Index outerSize() const
Number of elemnts of the major indexing direction.
bool hasInnerOffsets() const
Returns whether the innerOffsetsArray() has been filled.
Index innerSize() const
Number of elements of the minor indexing direction.
InnerIterator begin(const Index outerIdx) const
Returns an iterator to the first non-empty block of outerIdx.
std::vector< std::pair< Index, BlockPtr > > Inner
Vector of ( inner index ; block pointer ) tuples encoding an inner vector.
Definition: SparseBlockIndex.hpp:98