28 #ifndef REMORA_SPARSE_HPP
29#define REMORA_SPARSE_HPP
32#include "detail/storage.hpp"
33#include "detail/traits.hpp"
42template<
class T,
class I = std::
size_t>
class compressed_vector;
43template<
class T,
class I = std::
size_t>
class compressed_vector_adaptor;
44template<
class T,
class I = std::
size_t,
class Orientation = row_major>
class compressed_matrix;
62template<
class T,
class I>
63class compressed_vector
64:
public vector_container<compressed_vector<T, I>, cpu_tag >
65,
public detail::BaseSparseVector<detail::VectorStorage<T,I > >{
69 typedef T
const& const_reference;
72 typedef detail::compressed_vector_reference<compressed_vector const> const_closure_type;
73 typedef detail::compressed_vector_reference<compressed_vector> closure_type;
74 typedef sparse_vector_storage<T,I> storage_type;
75 typedef sparse_vector_storage<T const,I const> const_storage_type;
76 typedef elementwise<sparse_tag> evaluation_category;
80 : detail::BaseSparseVector<detail::VectorStorage<T,I> >(
81 detail::VectorStorage<T, I>(),0,0
83 explicit compressed_vector(size_type size, size_type non_zeros = 0)
84 : detail::BaseSparseVector<detail::VectorStorage<T,I> >(
85 detail::VectorStorage<T, I>(),size,non_zeros
88 compressed_vector(vector_expression<E, cpu_tag>
const& e, size_type non_zeros = 0)
89 : detail::BaseSparseVector<detail::VectorStorage<T,I> >(
90 detail::VectorStorage<T, I>(),e().size(),non_zeros
95 void resize(size_type size,
bool keep =
false){
96 this->do_resize(size, keep);
100 storage_type raw_storage(){
101 return this->m_manager.m_storage;
105 const_storage_type raw_storage()
const{
106 return this->m_manager.m_storage;
109 typename device_traits<cpu_tag>::queue_type& queue()
const{
110 return device_traits<cpu_tag>::default_queue();
113 friend void swap(compressed_vector& v1, compressed_vector& v2){
114 std::swap(v1.m_size, v2.m_size);
115 v1.m_indices.swap(v2.m_indices);
116 v1.m_values.swap(v2.m_values);
117 v1.m_storage.swap(v2.m_storage);
121 compressed_vector& operator = (compressed_vector
const& v) =
default;
123 compressed_vector& operator = (vector_container<C, cpu_tag>
const& v) {
124 this->resize(v().size(),
false);
129 compressed_vector& operator = (vector_expression<AE, cpu_tag>
const& ae) {
130 compressed_vector temporary(ae, this->nnz_capacity());
136 template<
class Archive>
137 void serialize(Archive& ar,
const unsigned int ) {
138 ar &
static_cast< detail::BaseSparseVector<detail::VectorStorage<T,I >
>& >(*this);
155template<
class T,
class I>
156class compressed_vector_adaptor
157:
public vector_expression<compressed_vector_adaptor<T, I>, cpu_tag>
158,
public detail::BaseSparseVector<detail::VectorStorageReference<T,I > >{
160 typedef typename std::remove_const<T>::type value_type;
161 typedef typename std::remove_const<I>::type size_type;
162 typedef T
const& const_reference;
163 typedef T& reference;
165 typedef detail::compressed_vector_reference<compressed_vector_adaptor const> const_closure_type;
166 typedef detail::compressed_vector_reference<compressed_vector_adaptor> closure_type;
167 typedef sparse_vector_storage<T,I> storage_type;
168 typedef sparse_vector_storage<T const,I const> const_storage_type;
169 typedef elementwise<sparse_tag> evaluation_category;
174 explicit compressed_vector_adaptor(size_type size, storage_type storage)
175 : detail::BaseSparseVector<detail::VectorStorageReference<T,I> >(
176 detail::VectorStorageReference<T,I>(storage), size, storage.nnz
181 compressed_vector_adaptor(vector_expression<E, cpu_tag>
const& e)
182 : detail::BaseSparseVector<detail::VectorStorageReference<T,I> >(
183 detail::VectorStorageReference<T,I>(e().raw_storage()), e().size(), e().raw_storage().nnz
187 storage_type raw_storage(){
188 return this->m_manager.m_storage;
192 const_storage_type raw_storage()
const{
193 return this->m_manager.m_storage;
196 typename device_traits<cpu_tag>::queue_type& queue()
const{
197 return device_traits<cpu_tag>::default_queue();
201 compressed_vector_adaptor& operator = (compressed_vector_adaptor
const& v) {
202 kernels::assign(*
this,
typename vector_temporary<compressed_vector_adaptor>::type(v));
206 compressed_vector_adaptor& operator = (vector_expression<AE, cpu_tag>
const& ae) {
207 kernels::assign(*
this,
typename vector_temporary<AE>::type(ae));
212template<
class T,
class I,
class Orientation>
213class compressed_matrix:
public matrix_container<compressed_matrix<T, I, Orientation>, cpu_tag >{
216 typedef T value_type;
217 typedef T
const& const_reference;
218 typedef T& reference;
220 typedef detail::compressed_matrix_proxy<compressed_matrix<T, I, Orientation>
const, Orientation> const_closure_type;
221 typedef detail::compressed_matrix_proxy<compressed_matrix<T, I, Orientation>, Orientation> closure_type;
222 typedef sparse_matrix_storage<T, I> storage_type;
223 typedef sparse_matrix_storage<T const, I const> const_storage_type;
224 typedef elementwise<sparse_tag> evaluation_category;
225 typedef Orientation orientation;
227 compressed_matrix():m_impl(detail::MatrixStorage<T,I>(0,0)){}
229 compressed_matrix(size_type rows, size_type cols, size_type non_zeros = 0)
230 :m_impl(detail::MatrixStorage<T,I>(orientation::index_M(rows,cols),orientation::index_m(rows,cols)),non_zeros){}
233 compressed_matrix(matrix_expression<E, cpu_tag>
const& m, size_type non_zeros = 0)
235 detail::MatrixStorage<T,I>(orientation::index_M(m().size1(),m().size2()),orientation::index_m(m().size1(),m().size2())),
242 compressed_matrix operator=(matrix_container<E, cpu_tag>
const& m){
243 resize(m.size1(),m.size2());
244 return assign(*
this,m);
247 compressed_matrix& operator=(matrix_expression<E, cpu_tag>
const& m){
248 compressed_matrix temporary(m);
249 m_impl = std::move(temporary.m_impl);
254 size_type size1()
const {
255 return orientation::index_M(m_impl.major_size(), m_impl.minor_size());
259 size_type size2()
const {
260 return orientation::index_m(m_impl.major_size(), m_impl.minor_size());
264 std::size_t nnz_capacity()
const{
265 return m_impl.nnz_capacity();
268 std::size_t nnz_reserved()
const {
269 return m_impl.nnz_reserved();
272 std::size_t major_capacity(size_type i)
const{
273 return m_impl.major_capacity(i);
276 std::size_t major_nnz(size_type i)
const {
277 return m_impl.major_nnz(i);
281 void set_nnz(std::size_t non_zeros) {
282 m_impl.set_nnz(non_zeros);
285 void set_major_nnz(size_type i,std::size_t non_zeros) {
286 m_impl.set_major_nnz(i,non_zeros);
289 const_storage_type raw_storage()
const{
290 return m_impl.raw_storage();
292 storage_type raw_storage(){
293 return m_impl.raw_storage();
296 typename device_traits<cpu_tag>::queue_type& queue()
const{
297 return device_traits<cpu_tag>::default_queue();
300 void reserve(std::size_t non_zeros) {
301 m_impl.reserve(non_zeros);
304 void major_reserve(size_type i, std::size_t non_zeros,
bool exact_size =
false) {
305 m_impl.major_reserve(i, non_zeros, exact_size);
308 void resize(size_type rows, size_type columns){
309 m_impl.resize(orientation::index_M(rows,columns),orientation::index_m(rows,columns));
312 typedef typename detail::compressed_matrix_impl<detail::MatrixStorage<T,I> >::const_major_iterator const_major_iterator;
313 typedef typename detail::compressed_matrix_impl<detail::MatrixStorage<T,I> >::major_iterator major_iterator;
315 const_major_iterator major_begin(size_type i)
const {
316 return m_impl.cmajor_begin(i);
319 const_major_iterator major_end(size_type i)
const{
320 return m_impl.cmajor_end(i);
323 major_iterator major_begin(size_type i) {
324 return m_impl.major_begin(i);
327 major_iterator major_end(size_type i) {
328 return m_impl.major_end(i);
331 major_iterator set_element(major_iterator pos, size_type index, value_type value){
332 return m_impl.set_element(pos, index, value);
335 major_iterator clear_range(major_iterator start, major_iterator end) {
336 return m_impl.clear_range(start,end);
340 for(std::size_t i = 0; i != m_impl.major_size(); ++i){
341 clear_range(major_begin(i),major_end(i));
345 template<
class Archive>
346 void serialize(Archive &ar,
const unsigned int ) {
351 detail::compressed_matrix_impl<detail::MatrixStorage<T,I> > m_impl;
482struct matrix_row_optimizer<detail::compressed_matrix_proxy<M, row_major> >{
483 typedef compressed_matrix_row<detail::compressed_matrix_proxy<M, row_major> > type;
485 static type create(detail::compressed_matrix_proxy<M, row_major>
const& m, std::size_t i){
493template<
class M,
class Orientation>
494struct matrix_transpose_optimizer<detail::compressed_matrix_proxy<M, Orientation> >{
495 typedef detail::compressed_matrix_proxy<M, typename Orientation::transposed_orientation> type;
497 static type create(detail::compressed_matrix_proxy<M, Orientation>
const& m){
498 return type(m.matrix(), m.start(), m.end());
504struct matrix_rows_optimizer<detail::compressed_matrix_proxy<M, row_major> >{
505 typedef detail::compressed_matrix_proxy<M, row_major> type;
507 static type create(detail::compressed_matrix_proxy<M, row_major>
const& m,
508 std::size_t start, std::size_t end
510 return type(m.matrix(), start + m.start(), end+m.start());
515template<
class T,
class O>
516struct matrix_temporary_type<T,O,sparse_tag, cpu_tag> {
517 typedef compressed_matrix<T,std::size_t, O> type;
521struct vector_temporary_type<T,sparse_tag, cpu_tag>{
522 typedef compressed_vector<T> type;