28 #ifndef SHARK_LINALG_BLAS_VECTOR_SPARSE_HPP 29 #define SHARK_LINALG_BLAS_VECTOR_SPARSE_HPP 56 template<
class T,
class I>
57 class compressed_vector:
public vector_container<compressed_vector<T, I> > {
59 typedef T& true_reference;
60 typedef compressed_vector<T, I> self_type;
63 typedef std::size_t size_type;
64 typedef std::ptrdiff_t difference_type;
66 typedef value_type scalar_type;
78 const_reference value()
const {
79 return const_cast<self_type const&
>(m_vector)(m_i);
81 value_type& ref()
const {
83 index_type
const* start = m_vector.
indices();
84 index_type
const*
end = start + m_vector.
nnz();
85 index_type
const *pos = std::lower_bound(start,end,m_i);
87 if (pos != end&& *pos == m_i)
88 return m_vector.m_values[pos-start];
92 return *m_vector.
set_element(posIter, m_i, m_vector.m_zero);
99 m_vector(m), m_i(i) {}
107 return ref() = v.value();
147 :m_size(size), m_nnz(0), m_indices(non_zeros,0), m_values(non_zeros),m_zero(0){}
150 :m_size(ae().size()), m_nnz(0), m_indices(non_zeros,0), m_values(non_zeros),m_zero(0)
160 return m_indices.size();
175 return& m_indices[0];
180 return& m_indices[0];
200 m_indices.resize(non_zeros);
201 m_values.resize(non_zeros);
207 std::size_t pos = lower_bound(i);
208 if (pos ==
nnz() || m_indices[pos] != i)
210 return m_values [pos];
233 m_indices = v.m_indices;
234 m_values = v.m_values;
254 m_indices.swap(v.m_indices);
255 m_values.swap(v.m_values);
263 typedef compressed_storage_iterator<value_type const, index_type const>
const_iterator;
264 typedef compressed_storage_iterator<value_type, index_type const>
iterator;
270 const_iterator
end()
const {
283 iterator
set_element(iterator pos, size_type index, value_type value) {
286 if(pos !=
end() && pos.index() == index){
291 difference_type arrayPos = pos -
begin();
297 m_values.begin()+arrayPos,m_values.begin() + m_nnz , m_values.begin() + m_nnz +1
300 m_indices.begin()+arrayPos,m_indices.begin() + m_nnz , m_indices.begin() + m_nnz +1
303 m_values[arrayPos] = value;
304 m_indices[arrayPos] = index;
314 difference_type startPos = start -
begin();
315 difference_type endPos = end -
begin();
319 m_values.begin()+endPos,m_values.begin() + m_nnz, m_values.begin() + startPos
322 m_indices.begin()+endPos,m_indices.begin() + m_nnz , m_indices.begin() + startPos
324 m_nnz -= endPos - startPos;
331 difference_type arrayPos = pos -
begin();
332 if(arrayPos == m_nnz-1){
338 m_values.begin()+arrayPos+1,m_values.begin() + m_nnz , m_values.begin() + arrayPos
341 m_indices.begin()+arrayPos+1,m_indices.begin() + m_nnz , m_indices.begin() + arrayPos
348 template<
class Archive>
350 boost::serialization::collection_size_type s(m_size);
351 ar & boost::serialization::make_nvp(
"size",s);
352 if (Archive::is_loading::value) {
356 ar & boost::serialization::make_nvp(
"nnz", m_nnz);
357 ar & boost::serialization::make_nvp(
"indices", m_indices);
358 ar & boost::serialization::make_nvp(
"values", m_values);
362 std::size_t lower_bound( index_type t)
const{
365 return std::lower_bound(begin, end, t)-
begin;
370 std::vector<index_type> m_indices;
371 std::vector<value_type> m_values;
376 struct vector_temporary_type<T,sparse_bidirectional_iterator_tag>{
380 template<
class T,
class I>
384 template<
class T,
class I>