35 #ifndef OPENMS_DATASTRUCTURES_SPARSEVECTOR_H 36 #define OPENMS_DATASTRUCTURES_SPARSEVECTOR_H 40 #include <OpenMS/config.h> 61 template <
typename Value>
82 typedef typename std::map<size_t, Value>::size_type
size_type;
103 std::cout << std::endl;
104 for (map_const_iterator it =
values_.begin(); it !=
values_.end(); ++it)
106 std::cout << it->first <<
": " << it->second << std::endl;
126 #pragma clang diagnostic push 127 #pragma clang diagnostic ignored "-Wfloat-equal" 129 #pragma clang diagnostic pop 131 map_iterator i =
values_.begin();
132 for (size_type s = 0; s <
size; ++s)
135 i =
values_.insert(i, std::make_pair(s, value));
204 Value
at(size_type pos)
const 220 return (Value)ValueProxy(const_cast<SparseVector&>(*
this), pos);
227 return ValueProxy(*
this, pos);
246 if (mit->first >= newsize)
248 size_type nextvalue = (++mit)->first;
268 if (it.position() >=
size_)
275 map_iterator mit =
values_.find(it.position());
276 map_iterator mit_next;
286 mit_next =
values_.lower_bound(it.position());
291 if (update)
update_(mit_next, 1);
300 void erase(SparseVectorIterator first, SparseVectorIterator last)
302 if (first.position() >=
size_ || last.position() >
size_ || last.position() < first.position())
307 size_type amount_deleted = last.position() - first.position();
308 map_iterator mfirst =
values_.lower_bound(first.position());
309 map_iterator mlast =
values_.lower_bound(last.position());
318 map_iterator start_it = mfirst;
322 update_(start_it, amount_deleted);
325 size_ -= amount_deleted;
347 bool first_sparse_found =
false;
349 map_iterator lowest =
values_.begin();
350 map_iterator second =
values_.begin();
351 map_iterator first = second++;
352 map_iterator last =
values_.end();
354 if (lowest->first > 0)
356 first_sparse_found =
true;
359 while (second != last)
361 if (second->second < lowest->second)
367 if ((second->first) - (first->first) > 1)
369 pos = first->first + 1;
370 first_sparse_found =
true;
378 return SparseVectorIterator(*
this, lowest->first);
382 if (!first_sparse_found)
384 return SparseVectorIterator(*
this, first->first + 1);
386 return SparseVectorIterator(*
this, pos);
397 return SparseVectorIterator(*
this, 0);
403 return SparseVectorIterator(*
this, this->
size());
409 return SparseVectorReverseIterator(*
this, this->
size());
415 return SparseVectorReverseIterator(*
this, 0);
421 return SparseVectorConstIterator(*
this, 0);
425 const_iterator
end()
const 427 return SparseVectorConstIterator(*
this, this->
size());
433 return SparseVectorConstIterator(*
this, this->
size());
437 const_reverse_iterator
rend()
const 439 return SparseVectorConstIterator(*
this, 0);
457 while (it != values_.end())
459 size_type tmp_index = it->first;
460 Value tmp_value = it->second;
461 if (it != values_.begin())
464 map_iterator tmp_it = it;
467 it = values_.insert(tmp_it, std::make_pair(tmp_index - amount_deleted, tmp_value));
473 it = values_.insert(std::make_pair(tmp_index - amount_deleted, tmp_value)).first;
505 value = cmit->second;
517 value = cmit->second;
529 value = cmit->second;
539 if ((
this != &rhs) && (
vec_ == rhs.
vec_))
589 return !(
this != other);
595 return (Value) *
this < (Value)other;
601 return (Value) *
this > (Value)other;
607 return (Value) *
this <= (Value)other;
613 return (Value) *
this >= (Value)other;
639 position_(source.position_),
640 vector_(source.vector_),
641 valit_(source.valit_)
695 assert(position_ < vector_.size_);
702 assert(position_ < vector_.size_);
703 return (Value)
ValueProxy(this->vector_, position_);
710 assert(position_ < vector_.size_);
761 return !(*
this != other);
767 return position_ < other.
position();
773 return position_ > other.
position();
779 return position_ <= other.
position();
785 return position_ >= other.
position();
793 if (position_ != valit_->first)
795 valit_ = vector_.values_.upper_bound(position_);
802 if (valit_ == vector_.values_.end())
804 position_ = vector_.size_;
808 position_ = valit_->first;
825 valit_(vector.values_.
begin())
858 position_(source.position_),
859 vector_(source.vector_),
860 valrit_(source.valrit_)
914 assert(position_ <= vector_.size_);
915 assert(position_ != 0);
916 return ValueProxy(this->vector_, position_ - 1);
923 assert(position_ < vector_.size_);
963 return -1 * (position_ - rhs.
position());
975 return !(*
this != other);
981 return !(this->position() < other.
position());
987 return !(this->position() > other.
position());
993 return !(this->position() <= other.
position());
999 return !(this->position() >= other.
position());
1007 if (position_ - 1 != valrit_->first)
1022 position_ = valrit_->first + 1;
1037 position_(position),
1039 valrit_(vector.values_.
rbegin())
1072 position_(source.position_),
1073 vector_(source.vector_),
1074 valit_(source.valit_)
1080 position_(source.position_),
1081 vector_(source.vector_),
1082 valit_(source.valit_)
1094 if (
this != &source)
1106 assert(position_ <= vector_.size_);
1116 assert(position_ <= vector_.size_);
1123 assert(position_ <= vector_.size_);
1133 assert(position_ <= vector_.size_);
1140 assert(position_ < vector_.size_);
1141 return (Value)
ValueProxy(const_cast<SparseVector&>(this->vector_), position_);
1148 assert(position_ < vector_.size_);
1149 return ValueProxy(const_cast<SparseVector&>(this->vector_), position_);
1187 return !(*
this != other);
1193 return this->position() < other.
position();
1199 return this->position() > other.
position();
1205 return this->position() <= other.
position();
1211 return this->position() >= other.
position();
1217 assert(valit_ != vector_.values_.end());
1219 if (position_ != valit_->first)
1221 valit_ = vector_.values_.upper_bound(position_);
1228 if (valit_ == vector_.values_.end())
1230 position_ = vector_.size_;
1234 position_ = valit_->first;
1251 position_(position),
1253 valit_(vector.values_.
begin())
1278 position_(source.position_),
1279 vector_(source.vector_),
1280 valrit_(source.valrit_)
1286 position_(source.position_),
1287 vector_(source.vector_),
1288 valrit_(source.valrit_)
1300 if (
this != &source)
1346 assert(position_ <= vector_.size_);
1347 assert(position_ != 0);
1348 return ValueProxy(const_cast<SparseVector&>(this->vector_), position_ - 1);
1354 assert(valrit_ != vector_.values_.rend());
1356 if (position_ - 1 != valrit_->first)
1371 position_ = valrit_->first + 1;
1395 position_(position), vector_(vector), valrit_(vector.values_.
rbegin())
1416 #endif //OPENMS_DATASTRUCTURES_SPARSEVECTOR_H SparseVectorIterator(const SparseVectorIterator &source)
copy constructor
Definition: SparseVector.h:638
ValueProxy & operator=(const ValueProxy &rhs)
assignment operator, ditches the sparse elements
Definition: SparseVector.h:537
size_type position() const
find out at what position the iterator is; useful in combination with hop()
Definition: SparseVector.h:814
SparseVector(Value se)
constructor with chosen sparse element
Definition: SparseVector.h:117
reverse_iterator rend()
rend iterator
Definition: SparseVector.h:413
SparseVectorConstReverseIterator(const SparseVectorConstIterator &source)
copy constructor
Definition: SparseVector.h:1277
SparseVectorConstReverseIterator & operator++()
postincrement operator
Definition: SparseVector.h:1310
map_const_iterator valit_
the position in the underlying map of SparseVector
Definition: SparseVector.h:1265
bool operator!=(const SparseVectorIterator &other)
inequality operator
Definition: SparseVector.h:753
SparseVectorReverseIterator & operator++()
prefix increment
Definition: SparseVector.h:882
const ValueProxy & const_reference
Definition: SparseVector.h:87
ValueProxy operator*()
dereference operator
Definition: SparseVector.h:693
SparseVectorConstIterator operator--(int)
immediate increment operator
Definition: SparseVector.h:1129
std::map< size_t, Value >::const_iterator map_const_iterator
Definition: SparseVector.h:90
size_type position_
the position in the referred SparseVector
Definition: SparseVector.h:830
void clear()
removes all elements
Definition: SparseVector.h:231
SparseVector(const SparseVector &source)
copy constructor
Definition: SparseVector.h:141
SparseVectorConstIterator & operator++()
postincrement operator
Definition: SparseVector.h:1104
Out of range exception.
Definition: Exception.h:320
const_iterator end() const
const end iterator
Definition: SparseVector.h:425
ValueProxy & operator=(Value val)
assignment operator, ditches the sparse elements
Definition: SparseVector.h:562
SparseVectorIterator & operator++()
prefix increment
Definition: SparseVector.h:663
ValueProxy operator[](size_type pos)
ValueProxy handles the conversion and the writing ( if != sparseElement )
Definition: SparseVector.h:224
ValueProxy operator*()
dereference operator
Definition: SparseVector.h:1344
class ValueProxy allows the SparseVector to differentiate between writing and reading, so zeros can be ignored See "more effective c++" section 30
Definition: SparseVector.h:485
ValueProxy & reference
Definition: SparseVector.h:86
SparseVectorConstIterator operator+(const size_type rhs) const
binary arithmetic +
Definition: SparseVector.h:1167
SparseVector implementation. The container will not actually store a specified type of element - the ...
Definition: SparseVector.h:62
const_reverse_iterator rend() const
const end reverse_iterator
Definition: SparseVector.h:437
SparseVectorReverseIterator operator--(int)
postfix decrement
Definition: SparseVector.h:904
SparseVectorIterator operator+(const size_type rhs) const
binary arithmetic +
Definition: SparseVector.h:729
difference_type operator+(const SparseVectorIterator rhs) const
binary arithmetic +
Definition: SparseVector.h:735
std::map< size_t, Value >::reverse_iterator reverse_map_iterator
Definition: SparseVector.h:93
SparseVectorConstIterator operator-(const size_type rhs) const
binary arithmetic -
Definition: SparseVector.h:1173
std::map< size_t, Value >::iterator map_iterator
Definition: SparseVector.h:91
Value operator*()
dereference operator
Definition: SparseVector.h:912
bool operator==(const ValueProxy &other)
equality operator
Definition: SparseVector.h:587
SparseVectorIterator & operator-=(const size_type rhs)
compound assignment -
Definition: SparseVector.h:722
bool operator<(const ValueProxy &other)
less than operator
Definition: SparseVector.h:593
bool empty() const
true if the container is empty
Definition: SparseVector.h:188
SparseVectorConstReverseIterator(const SparseVectorReverseIterator &source)
copy constructor from SparseVector::SparseVectorIterator
Definition: SparseVector.h:1285
SparseVectorConstReverseIterator operator++(int)
immediate increment operator
Definition: SparseVector.h:1318
size_type position() const
find out at what position the iterator is, useful in combination with hop()
Definition: SparseVector.h:1240
const_iterator for SparseVector
Definition: SparseVector.h:1063
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
SparseVectorReverseIterator & operator-=(const size_type rhs)
compound assignment -
Definition: SparseVector.h:935
map_const_iterator valit_
the position in the underlying map of SparseVector
Definition: SparseVector.h:836
bool operator>=(const SparseVectorConstIterator &other)
greater or equal than operator
Definition: SparseVector.h:1209
SparseVectorConstIterator & operator=(const SparseVectorConstIterator &source)
assignment operator
Definition: SparseVector.h:1092
const_iterator begin() const
const begin iterator
Definition: SparseVector.h:419
SparseVectorConstReverseIterator & operator=(const SparseVectorConstReverseIterator &source)
assignment operator
Definition: SparseVector.h:1298
SparseVectorReverseIterator reverse_iterator
Definition: SparseVector.h:78
bool operator!=(const SparseVectorReverseIterator &other)
inequality operator
Definition: SparseVector.h:967
ValueProxy(SparseVector &vec, size_type index)
public constructor
Definition: SparseVector.h:491
SparseVectorIterator & operator+=(const size_type rhs)
compound assignment +
Definition: SparseVector.h:715
ValueProxy operator[](size_type n)
indexing
Definition: SparseVector.h:920
size_type nonzero_size() const
number of nonzero elements, i.e. the space actually used
Definition: SparseVector.h:176
bool operator!=(const SparseVectorConstReverseIterator &other)
inequality operator
Definition: SparseVector.h:1383
bool operator>=(const SparseVectorReverseIterator &other)
greater or equal than operator
Definition: SparseVector.h:997
SparseVector & vector_
referred sparseVector
Definition: SparseVector.h:1050
SparseVectorConstIterator(const SparseVectorIterator &source)
copy constructor from SparseVector::SparseVectorIterator
Definition: SparseVector.h:1079
difference_type operator+(const SparseVectorReverseIterator rhs) const
binary arithmetic +
Definition: SparseVector.h:948
SparseVectorReverseIterator & rhop()
go to the next nonempty position
Definition: SparseVector.h:1003
void push_back(Value value)
push_back (see stl vector docs)
Definition: SparseVector.h:194
bool operator<=(const SparseVectorIterator &other)
less or equal than operator
Definition: SparseVector.h:777
SparseVectorReverseIterator(const SparseVectorReverseIterator &source)
copy constructor
Definition: SparseVector.h:857
void resize(size_type newsize)
resizes the vector to param newsize
Definition: SparseVector.h:238
size_type size_
size including sparse elements
Definition: SparseVector.h:447
random access iterator for SparseVector including the hop() function to jump to the next non-sparse e...
Definition: SparseVector.h:630
size_type position_
position in referred SparseVector
Definition: SparseVector.h:1259
bool operator!=(const ValueProxy &other)
inequality operator
Definition: SparseVector.h:580
SparseVectorConstIterator(const SparseVectorConstIterator &source)
copy constructor
Definition: SparseVector.h:1071
virtual ~SparseVectorIterator()
destructor
Definition: SparseVector.h:646
SparseVectorConstIterator(const SparseVector &vector, size_type position)
detailed constructor
Definition: SparseVector.h:1250
std::map< size_t, Value >::size_type size_type
Definition: SparseVector.h:82
SparseVectorIterator operator-(const size_type rhs) const
binary arithmetic -
Definition: SparseVector.h:741
const Value operator*() const
const dereference operator
Definition: SparseVector.h:700
iterator end()
end iterator
Definition: SparseVector.h:401
SparseVectorConstIterator & operator-=(const size_type rhs)
compound assignment -
Definition: SparseVector.h:1160
SparseVectorIterator getMinElement()
gets an Iterator to the element (including sparseElements) with the minimal value ...
Definition: SparseVector.h:329
bool operator<(const SparseVectorConstIterator &other)
less than operator
Definition: SparseVector.h:1191
bool operator<(const SparseVector &rhs) const
less than operator
Definition: SparseVector.h:170
const_reverse_iterator rbegin() const
const begin reverse_iterator
Definition: SparseVector.h:431
const SparseVector & vector_
reference to the vector operating on
Definition: SparseVector.h:1405
const_reverse_iterator for SparseVector
Definition: SparseVector.h:1270
bool operator>(const SparseVectorReverseIterator &other)
greater than operator
Definition: SparseVector.h:985
size_type position_
Definition: SparseVector.h:1402
void print() const
Definition: SparseVector.h:101
SparseVectorIterator & operator=(const SparseVectorIterator &source)
assignment operator
Definition: SparseVector.h:651
size_type position() const
find out at what position the iterator is, useful in combination with hop()
Definition: SparseVector.h:1377
void erase(SparseVectorIterator it)
Definition: SparseVector.h:266
Value at(size_type pos) const
Definition: SparseVector.h:204
virtual ~SparseVectorConstReverseIterator()
destructor
Definition: SparseVector.h:1293
SparseVectorConstReverseIterator(const SparseVector &vector, size_type position)
detailed constructor
Definition: SparseVector.h:1394
void erase(SparseVectorIterator first, SparseVectorIterator last)
Definition: SparseVector.h:300
const SparseVector & vector_
referring to this SparseVector
Definition: SparseVector.h:1262
bool operator==(const SparseVectorReverseIterator &other)
equality operator
Definition: SparseVector.h:973
SparseVectorReverseIterator operator+(const size_type rhs) const
binary arithmetic +
Definition: SparseVector.h:942
SparseVectorIterator & hop()
go to the next nonempty position
Definition: SparseVector.h:789
bool operator==(const SparseVectorConstIterator &other)
equality operator
Definition: SparseVector.h:1185
bool operator>(const SparseVectorConstIterator &other)
greater than operator
Definition: SparseVector.h:1197
SparseVector & operator=(const SparseVector &source)
assignment operator
Definition: SparseVector.h:147
SparseVectorConstIterator const_iterator
Definition: SparseVector.h:72
SparseVectorConstReverseIterator operator--(int)
immediate decrement operator
Definition: SparseVector.h:1335
SparseVectorIterator iterator
Definition: SparseVector.h:77
SparseVectorReverseIterator operator-(const size_type rhs) const
binary arithmetic -
Definition: SparseVector.h:954
const ValueProxy operator[](size_type n) const
Definition: SparseVector.h:1145
iterator begin()
begin iterator
Definition: SparseVector.h:395
SparseVectorConstReverseIterator & rhop()
go to the next nonempty position
Definition: SparseVector.h:1352
bool operator>=(const ValueProxy &other)
greater or equal than operator
Definition: SparseVector.h:611
SparseVector()
default constructor
Definition: SparseVector.h:111
SparseVectorReverseIterator & operator+=(const size_type rhs)
compound assignment +
Definition: SparseVector.h:928
bool operator==(const SparseVector &rhs) const
equality operator
Definition: SparseVector.h:164
SparseVectorIterator(SparseVector &vector, size_type position)
Definition: SparseVector.h:822
bool operator>=(const SparseVectorIterator &other)
greater or equal than operator
Definition: SparseVector.h:783
SparseVectorReverseIterator ReverseIterator
Definition: SparseVector.h:98
SparseVectorConstReverseIterator const_reverse_iterator
Definition: SparseVector.h:76
bool operator==(const SparseVectorIterator &other)
equality operator
Definition: SparseVector.h:759
SparseVectorIterator operator++(int)
postfix increment
Definition: SparseVector.h:670
SparseVectorConstIterator operator++(int)
immediate increment operator
Definition: SparseVector.h:1112
SparseVectorConstReverseIterator ConstReverseIterator
Definition: SparseVector.h:96
size_type size() const
size of the represented vector
Definition: SparseVector.h:182
SparseVectorConstIterator & hop()
go to the next nonempty position
Definition: SparseVector.h:1215
reverse_map_const_iterator valrit_
Definition: SparseVector.h:1408
size_type position_
the position in the referred SparseVector
Definition: SparseVector.h:1046
const Value operator*() const
dereference operator
Definition: SparseVector.h:1138
size_type position() const
find out at what position the iterator is; useful in combination with hop()
Definition: SparseVector.h:1028
SparseVector & vec_
the referring SparseVector
Definition: SparseVector.h:619
Value * pointer
Definition: SparseVector.h:85
Value value_type
Definition: SparseVector.h:84
SparseVectorIterator Iterator
Definition: SparseVector.h:97
SparseVectorReverseIterator & operator=(const SparseVectorReverseIterator &source)
assignment operator
Definition: SparseVector.h:870
difference_type operator-(const SparseVectorReverseIterator rhs) const
binary arithmetic -
Definition: SparseVector.h:960
bool operator<=(const ValueProxy &other)
less or equal than operator
Definition: SparseVector.h:605
void update_(map_iterator it, Size amount_deleted)
Updates position of it and all larger elements.
Definition: SparseVector.h:455
size_t Size
Size type e.g. used as variable which can hold result of size()
Definition: Types.h:128
bool operator<(const SparseVectorIterator &other)
less than operator
Definition: SparseVector.h:765
bool operator<=(const SparseVectorConstIterator &other)
less or equal than operator
Definition: SparseVector.h:1203
virtual ~SparseVectorConstIterator()
destructor
Definition: SparseVector.h:1087
difference_type operator-(const SparseVectorIterator rhs) const
binary arithmetic -
Definition: SparseVector.h:747
size_type index_
the reference into the SparseVector
Definition: SparseVector.h:622
reverse_map_const_iterator valrit_
the position in the underlying map of SparseVector
Definition: SparseVector.h:1053
bool operator>(const ValueProxy &other)
greater than operator
Definition: SparseVector.h:599
SparseVector(size_type size, Value value, Value se=0)
detailed constructor, use with filling element value is discouraged unless it is the same as sparse e...
Definition: SparseVector.h:123
SparseVectorConstIterator ConstIterator
Definition: SparseVector.h:95
SparseVectorReverseIterator(SparseVector &vector, size_type position)
detailed constructor
Definition: SparseVector.h:1036
bool operator<(const SparseVectorReverseIterator &other)
less than operator
Definition: SparseVector.h:979
std::map< size_t, Value >::allocator_type allocator_type
Definition: SparseVector.h:83
ValueProxy operator[](size_type n)
indexing
Definition: SparseVector.h:707
SparseVectorConstIterator & operator--()
postincrement operator
Definition: SparseVector.h:1121
SparseVectorIterator & operator--()
prefix decrement
Definition: SparseVector.h:678
std::map< size_type, Value > values_
underlying map
Definition: SparseVector.h:444
std::map< size_t, Value >::const_reverse_iterator reverse_map_const_iterator
Definition: SparseVector.h:92
SparseVectorConstIterator & operator+=(const size_type rhs)
compound assignment +
Definition: SparseVector.h:1153
SparseVectorIterator operator--(int)
postfix decrement
Definition: SparseVector.h:685
SparseVectorConstReverseIterator & operator--()
postdecrement operator
Definition: SparseVector.h:1327
Value sparse_element_
sparse element
Definition: SparseVector.h:452
reverse_iterator rbegin()
rbegin iterator
Definition: SparseVector.h:407
random access reverse iterator for SparseVector including the hop() function to jump to the next non-...
Definition: SparseVector.h:849
const Value operator[](size_type pos) const
ValueProxy handles the conversion to int and ,the writing ( if != sparseElement ) ...
Definition: SparseVector.h:217
virtual ~SparseVectorReverseIterator()
destructor
Definition: SparseVector.h:865
SparseVector & vector_
the referred SparseVector
Definition: SparseVector.h:833
bool operator>(const SparseVectorIterator &other)
greater than operator
Definition: SparseVector.h:771
bool operator!=(const SparseVectorConstIterator &other)
inequality operator
Definition: SparseVector.h:1179
SparseVectorReverseIterator & operator--()
prefix decrement
Definition: SparseVector.h:897
std::map< size_t, Value >::difference_type difference_type
Definition: SparseVector.h:81
SparseVectorReverseIterator operator++(int)
postfix increment
Definition: SparseVector.h:889
bool operator<=(const SparseVectorReverseIterator &other)
less or equal than operator
Definition: SparseVector.h:991
~SparseVector()
destructor
Definition: SparseVector.h:159