00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef OPENMS_DATASTRUCTURES_SPARSEVECTOR_H
00036 #define OPENMS_DATASTRUCTURES_SPARSEVECTOR_H
00037
00038 #include <map>
00039 #include <algorithm>
00040 #include <stdexcept>
00041 #include <cassert>
00042 #include <cmath>
00043 #include <sstream>
00044 #include <OpenMS/CONCEPT/Exception.h>
00045
00046 #include <iostream>
00047
00048 namespace OpenMS
00049 {
00059 template <typename Value>
00060 class SparseVector
00061 {
00062
00063 public:
00064
00065
00066 class SparseVectorConstIterator;
00067 class SparseVectorIterator;
00068 class SparseVectorReverseIterator;
00069 class SparseVectorConstReverseIterator;
00070 class ValueProxy;
00071
00072
00073 typedef SparseVectorConstIterator const_iterator;
00074 typedef SparseVectorConstReverseIterator const_reverse_iterator;
00075 typedef SparseVectorIterator iterator;
00076 typedef SparseVectorReverseIterator reverse_iterator;
00077
00078
00079 typedef typename std::map<size_t, Value>::difference_type difference_type;
00080 typedef typename std::map<size_t, Value>::size_type size_type;
00081 typedef typename std::map<size_t, Value>::allocator_type allocator_type;
00082 typedef Value value_type;
00083 typedef Value * pointer;
00084 typedef ValueProxy & reference;
00085 typedef const ValueProxy & const_reference;
00086
00087
00088 typedef typename std::map<size_t, Value>::const_iterator map_const_iterator;
00089 typedef typename std::map<size_t, Value>::iterator map_iterator;
00090 typedef typename std::map<size_t, Value>::const_reverse_iterator reverse_map_const_iterator;
00091 typedef typename std::map<size_t, Value>::reverse_iterator reverse_map_iterator;
00092
00093 typedef SparseVectorConstIterator ConstIterator;
00094 typedef SparseVectorConstReverseIterator ConstReverseIterator;
00095 typedef SparseVectorIterator Iterator;
00096 typedef SparseVectorReverseIterator ReverseIterator;
00097
00098
00099 void print() const
00100 {
00101 std::cout << std::endl;
00102 for (map_const_iterator it = values_.begin(); it != values_.end(); ++it)
00103 {
00104 std::cout << it->first << ": " << it->second << std::endl;
00105 }
00106 }
00107
00109 SparseVector() :
00110 values_(), size_(0), sparse_element_(0)
00111 {
00112 }
00113
00115 SparseVector(Value se) :
00116 values_(), size_(0), sparse_element_(se)
00117 {
00118 }
00119
00121 SparseVector(size_type size, Value value, Value se = 0) :
00122 values_(), size_(size), sparse_element_(se)
00123 {
00124 if (value != sparse_element_)
00125 {
00126 map_iterator i = values_.begin();
00127 for (size_type s = 0; s < size; ++s)
00128 {
00129
00130 i = values_.insert(i, std::make_pair(s, value));
00131 }
00132 }
00133 }
00134
00136 SparseVector(const SparseVector & source) :
00137 values_(source.values_), size_(source.size_), sparse_element_(source.sparse_element_)
00138 {
00139 }
00140
00142 SparseVector & operator=(const SparseVector & source)
00143 {
00144 if (this != &source)
00145 {
00146 values_ = source.values_;
00147 size_ = source.size_;
00148 sparse_element_ = source.sparse_element_;
00149 }
00150 return *this;
00151 }
00152
00154 ~SparseVector()
00155 {
00156 }
00157
00159 bool operator==(const SparseVector & rhs) const
00160 {
00161 return (values_ == rhs.values_) && (size_ == rhs.size_) && (sparse_element_ == rhs.sparse_element_);
00162 }
00163
00165 bool operator<(const SparseVector & rhs) const
00166 {
00167 return values_ < rhs.values_;
00168 }
00169
00171 size_type nonzero_size() const
00172 {
00173 return values_.size();
00174 }
00175
00177 size_type size() const
00178 {
00179 return size_;
00180 }
00181
00183 bool empty() const
00184 {
00185 return size() == 0;
00186 }
00187
00189 void push_back(Value value)
00190 {
00191 operator[](size_++) = value;
00192 }
00193
00199 Value at(size_type pos) const
00200 {
00201 if (pos >= size_)
00202 {
00203 throw Exception::OutOfRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
00204 }
00205 else
00206 {
00207 return operator[](pos);
00208 }
00209 }
00210
00212 const Value operator[](size_type pos) const
00213 {
00214 assert(pos < size_);
00215 return (Value)ValueProxy(const_cast<SparseVector &>(*this), pos);
00216 }
00217
00219 ValueProxy operator[](size_type pos)
00220 {
00221 assert(pos < size_);
00222 return ValueProxy(*this, pos);
00223 }
00224
00226 void clear()
00227 {
00228 values_.clear();
00229 size_ = 0;
00230 }
00231
00233 void resize(size_type newsize)
00234 {
00235
00236
00237 if (newsize < size_)
00238 {
00239 for (map_iterator mit = values_.begin(); mit != values_.end(); )
00240 {
00241 if (mit->first >= newsize)
00242 {
00243 size_type nextvalue = (++mit)->first;
00244 values_.erase(--mit);
00245 mit = values_.find(nextvalue);
00246 }
00247 else
00248 {
00249 ++mit;
00250 }
00251 }
00252 }
00253 size_ = newsize;
00254 }
00255
00261 void erase(SparseVectorIterator it)
00262 {
00263 if (it.position() >= size_)
00264 {
00265 throw Exception::OutOfRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
00266 }
00267
00268
00269 bool update = false;
00270 map_iterator mit = values_.find(it.position());
00271 map_iterator mit_next;
00272 if (mit != values_.end())
00273 {
00274 mit_next = mit;
00275 ++mit_next;
00276 values_.erase(mit);
00277 update = true;
00278 }
00279 else
00280 {
00281 mit_next = values_.lower_bound(it.position());
00282 update = true;
00283 }
00284
00285
00286 if (update) update_(mit_next, 1);
00287
00288 --size_;
00289 }
00290
00295 void erase(SparseVectorIterator first, SparseVectorIterator last)
00296 {
00297 if (first.position() >= size_ || last.position() > size_ || last.position() < first.position())
00298 {
00299 throw Exception::OutOfRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
00300 }
00301
00302 size_type amount_deleted = last.position() - first.position();
00303 map_iterator mfirst = values_.lower_bound(first.position());
00304 map_iterator mlast = values_.lower_bound(last.position());
00305
00306 if (mfirst == values_.begin())
00307 {
00308 values_.erase(mfirst, mlast);
00309 update_(values_.begin(), amount_deleted);
00310 }
00311 else
00312 {
00313 map_iterator start_it = mfirst;
00314 --start_it;
00315 values_.erase(mfirst, mlast);
00316 ++start_it;
00317 update_(start_it, amount_deleted);
00318 }
00319
00320 size_ -= amount_deleted;
00321 }
00322
00324 SparseVectorIterator getMinElement()
00325 {
00326 switch (size_)
00327 {
00328 case 0:
00329 break;
00330
00331 case 1:
00332 return begin();
00333
00334 break;
00335
00336 default:
00337 if (values_.empty())
00338 {
00339
00340 return begin();
00341 }
00342 bool first_sparse_found = false;
00343 size_type pos = 0;
00344 map_iterator lowest = values_.begin();
00345 map_iterator second = values_.begin();
00346 map_iterator first = second++;
00347 map_iterator last = values_.end();
00348
00349 if (lowest->first > 0)
00350 {
00351 first_sparse_found = true;
00352 }
00353
00354 while (second != last)
00355 {
00356 if (second->second < lowest->second)
00357 {
00358 lowest = second;
00359 }
00360 if (size_ > values_.size() && !first_sparse_found)
00361 {
00362 if ((second->first) - (first->first) > 1)
00363 {
00364 pos = first->first + 1;
00365 first_sparse_found = true;
00366 }
00367 }
00368 ++first; ++second;
00369 }
00370
00371 if (size_ == values_.size() || lowest->second < SparseVector::sparse_element_)
00372 {
00373 return SparseVectorIterator(*this, lowest->first);
00374 }
00375 else
00376 {
00377 if (!first_sparse_found)
00378 {
00379 return SparseVectorIterator(*this, first->first + 1);
00380 }
00381 return SparseVectorIterator(*this, pos);
00382 }
00383 break;
00384 }
00385 return end();
00386
00387 }
00388
00390 iterator begin()
00391 {
00392 return SparseVectorIterator(*this, 0);
00393 }
00394
00396 iterator end()
00397 {
00398 return SparseVectorIterator(*this, this->size());
00399 }
00400
00402 reverse_iterator rbegin()
00403 {
00404 return SparseVectorReverseIterator(*this, this->size());
00405 }
00406
00408 reverse_iterator rend()
00409 {
00410 return SparseVectorReverseIterator(*this, 0);
00411 }
00412
00414 const_iterator begin() const
00415 {
00416 return SparseVectorConstIterator(*this, 0);
00417 }
00418
00420 const_iterator end() const
00421 {
00422 return SparseVectorConstIterator(*this, this->size());
00423 }
00424
00426 const_reverse_iterator rbegin() const
00427 {
00428 return SparseVectorConstIterator(*this, this->size());
00429 }
00430
00432 const_reverse_iterator rend() const
00433 {
00434 return SparseVectorConstIterator(*this, 0);
00435 }
00436
00437 private:
00439 std::map<size_type, Value> values_;
00440
00442 size_type size_;
00443
00444 protected:
00445
00447 Value sparse_element_;
00448
00450 void update_(map_iterator it, Size amount_deleted)
00451 {
00452 while (it != values_.end())
00453 {
00454 size_type tmp_index = it->first;
00455 Value tmp_value = it->second;
00456 if (it != values_.begin())
00457 {
00458
00459 map_iterator tmp_it = it;
00460 --tmp_it;
00461 values_.erase(it);
00462 it = values_.insert(tmp_it, std::make_pair(tmp_index - amount_deleted, tmp_value));
00463 }
00464 else
00465 {
00466
00467 values_.erase(it);
00468 it = values_.insert(std::make_pair(tmp_index - amount_deleted, tmp_value)).first;
00469 }
00470 ++it;
00471 }
00472 }
00473
00474 public:
00475
00480 class ValueProxy
00481 {
00482
00483 public:
00484
00486 ValueProxy(SparseVector & vec, size_type index) :
00487 vec_(vec), index_(index)
00488 {
00489 }
00490
00491
00492
00494 operator double() const
00495 {
00496 double value = vec_.sparse_element_;
00497 map_const_iterator cmit = vec_.values_.find(index_);
00498 if (cmit != vec_.values_.end())
00499 {
00500 value = cmit->second;
00501 }
00502 return value;
00503 }
00504
00506 operator int() const
00507 {
00508 int value = vec_.sparse_element_;
00509 map_const_iterator cmit = vec_.values_.find(index_);
00510 if (cmit != vec_.values_.end())
00511 {
00512 value = cmit->second;
00513 }
00514 return value;
00515 }
00516
00518 operator float() const
00519 {
00520 float value = vec_.sparse_element_;
00521 map_const_iterator cmit = vec_.values_.find(index_);
00522 if (cmit != vec_.values_.end())
00523 {
00524 value = cmit->second;
00525 }
00526 return value;
00527 }
00528
00529
00530
00532 ValueProxy & operator=(const ValueProxy & rhs)
00533 {
00534 if ((this != &rhs) && (vec_ == rhs.vec_))
00535 {
00536
00537 map_const_iterator cmit = rhs.vec_.values_.find(rhs.index_);
00538 if (cmit != rhs.vec_.values_.end())
00539 {
00540 vec_.values_[rhs.index_] = cmit->second;
00541 }
00542
00543 else
00544 {
00545 map_iterator mit = vec_.values_.find(rhs.index_);
00546 if (mit != vec_.values_.end())
00547 {
00548 vec_.values_.erase(mit);
00549 }
00550 }
00551 index_ = rhs.index_;
00552 }
00553 return *this;
00554 }
00555
00557 ValueProxy & operator=(Value val)
00558 {
00559 if (val != vec_.sparse_element_)
00560 {
00561 vec_.values_[index_] = val;
00562 }
00563 else
00564 {
00565 map_iterator mit = vec_.values_.find(index_);
00566 if (mit != vec_.values_.end())
00567 {
00568 vec_.values_.erase(mit);
00569 }
00570 }
00571 return *this;
00572 }
00573
00575 bool operator!=(const ValueProxy & other)
00576 {
00577
00578 return (index_ != other.index_) || (&vec_ != &other.vec_);
00579 }
00580
00582 bool operator==(const ValueProxy & other)
00583 {
00584 return !(this != other);
00585 }
00586
00588 bool operator<(const ValueProxy & other)
00589 {
00590 return (Value) * this < (Value)other;
00591 }
00592
00594 bool operator>(const ValueProxy & other)
00595 {
00596 return (Value) * this > (Value)other;
00597 }
00598
00600 bool operator<=(const ValueProxy & other)
00601 {
00602 return (Value) * this <= (Value)other;
00603 }
00604
00606 bool operator>=(const ValueProxy & other)
00607 {
00608 return (Value) * this >= (Value)other;
00609 }
00610
00611 private:
00612
00614 SparseVector & vec_;
00615
00617 size_type index_;
00618
00619 };
00620
00625 class SparseVectorIterator
00626 {
00627 friend class SparseVector<Value>;
00628 friend class SparseVectorConstIterator;
00629
00630 public:
00631
00633 SparseVectorIterator(const SparseVectorIterator & source) :
00634 position_(source.position_),
00635 vector_(source.vector_),
00636 valit_(source.valit_)
00637 {
00638 }
00639
00641 virtual ~SparseVectorIterator()
00642 {
00643 }
00644
00646 SparseVectorIterator & operator=(const SparseVectorIterator & source)
00647 {
00648 if (this != &source)
00649 {
00650 position_ = source.position_;
00651 vector_ = source.vector_;
00652 valit_ = source.valit_;
00653 }
00654 return *this;
00655 }
00656
00658 SparseVectorIterator & operator++()
00659 {
00660 ++position_;
00661 return *this;
00662 }
00663
00665 SparseVectorIterator operator++(int)
00666 {
00667 SparseVectorIterator tmp(*this);
00668 ++position_;
00669 return tmp;
00670 }
00671
00673 SparseVectorIterator & operator--()
00674 {
00675 --position_;
00676 return *this;
00677 }
00678
00680 SparseVectorIterator operator--(int)
00681 {
00682 SparseVectorIterator tmp(*this);
00683 --position_;
00684 return tmp;
00685 }
00686
00688 ValueProxy operator*()
00689 {
00690 assert(position_ < vector_.size_);
00691 return ValueProxy(this->vector_, position_);
00692 }
00693
00695 const Value operator*() const
00696 {
00697 assert(position_ < vector_.size_);
00698 return (Value)ValueProxy(this->vector_, position_);
00699 }
00700
00702 ValueProxy operator[](size_type n)
00703 {
00704 position_ += n;
00705 assert(position_ < vector_.size_);
00706 return ValueProxy(this->vector_, position_);
00707 }
00708
00710 SparseVectorIterator & operator+=(const size_type rhs)
00711 {
00712 position_ += rhs;
00713 return *this;
00714 }
00715
00717 SparseVectorIterator & operator-=(const size_type rhs)
00718 {
00719 position_ -= rhs;
00720 return *this;
00721 }
00722
00724 SparseVectorIterator operator+(const size_type rhs) const
00725 {
00726 return SparseVectorIterator(vector_, position_ + rhs);
00727 }
00728
00730 difference_type operator+(const SparseVectorIterator rhs) const
00731 {
00732 return position_ + rhs.position();
00733 }
00734
00736 SparseVectorIterator operator-(const size_type rhs) const
00737 {
00738 return SparseVectorIterator(vector_, position_ - rhs);
00739 }
00740
00742 difference_type operator-(const SparseVectorIterator rhs) const
00743 {
00744 return position_ - rhs.position();
00745 }
00746
00748 bool operator!=(const SparseVectorIterator & other)
00749 {
00750 return position_ != other.position_ || &vector_ != &other.vector_;
00751 }
00752
00754 bool operator==(const SparseVectorIterator & other)
00755 {
00756 return !(*this != other);
00757 }
00758
00760 bool operator<(const SparseVectorIterator & other)
00761 {
00762 return position_ < other.position();
00763 }
00764
00766 bool operator>(const SparseVectorIterator & other)
00767 {
00768 return position_ > other.position();
00769 }
00770
00772 bool operator<=(const SparseVectorIterator & other)
00773 {
00774 return position_ <= other.position();
00775 }
00776
00778 bool operator>=(const SparseVectorIterator & other)
00779 {
00780 return position_ >= other.position();
00781 }
00782
00784 SparseVectorIterator & hop()
00785 {
00786
00787
00788 if (position_ != valit_->first)
00789 {
00790 valit_ = vector_.values_.upper_bound(position_);
00791 }
00792 else
00793 {
00794 ++valit_;
00795 }
00796
00797 if (valit_ == vector_.values_.end())
00798 {
00799 position_ = vector_.size_;
00800 }
00801 else
00802 {
00803 position_ = valit_->first;
00804 }
00805 return *this;
00806 }
00807
00809 size_type position() const
00810 {
00811 return position_;
00812 }
00813
00814 protected:
00815
00817 SparseVectorIterator(SparseVector & vector, size_type position) :
00818 position_(position),
00819 vector_(vector),
00820 valit_(vector.values_.begin())
00821 {
00822 }
00823
00825 size_type position_;
00826
00828 SparseVector & vector_;
00829
00831 map_const_iterator valit_;
00832
00833 private:
00834
00836 SparseVectorIterator();
00837
00838 };
00839
00844 class SparseVectorReverseIterator
00845 {
00846 friend class SparseVector<Value>;
00847 friend class SparseVectorConstReverseIterator;
00848
00849 public:
00850
00852 SparseVectorReverseIterator(const SparseVectorReverseIterator & source) :
00853 position_(source.position_),
00854 vector_(source.vector_),
00855 valrit_(source.valrit_)
00856 {
00857 }
00858
00860 virtual ~SparseVectorReverseIterator()
00861 {
00862 }
00863
00865 SparseVectorReverseIterator & operator=(const SparseVectorReverseIterator & source)
00866 {
00867 if (this != &source)
00868 {
00869 position_ = source.position_;
00870 vector_ = source.vector_;
00871 valrit_ = source.valrit_;
00872 }
00873 return *this;
00874 }
00875
00877 SparseVectorReverseIterator & operator++()
00878 {
00879 --position_;
00880 return *this;
00881 }
00882
00884 SparseVectorReverseIterator operator++(int)
00885 {
00886 SparseVectorReverseIterator tmp(*this);
00887 --position_;
00888 return tmp;
00889 }
00890
00892 SparseVectorReverseIterator & operator--()
00893 {
00894 ++position_;
00895 return *this;
00896 }
00897
00899 SparseVectorReverseIterator operator--(int)
00900 {
00901 SparseVectorReverseIterator tmp(*this);
00902 ++position_;
00903 return tmp;
00904 }
00905
00907 Value operator*()
00908 {
00909 assert(position_ <= vector_.size_);
00910 assert(position_ != 0);
00911 return ValueProxy(this->vector_, position_ - 1);
00912 }
00913
00915 ValueProxy operator[](size_type n)
00916 {
00917 position_ -= n;
00918 assert(position_ < vector_.size_);
00919 return ValueProxy(this->vector_, position_);
00920 }
00921
00923 SparseVectorReverseIterator & operator+=(const size_type rhs)
00924 {
00925 position_ -= rhs;
00926 return *this;
00927 }
00928
00930 SparseVectorReverseIterator & operator-=(const size_type rhs)
00931 {
00932 position_ += rhs;
00933 return *this;
00934 }
00935
00937 SparseVectorReverseIterator operator+(const size_type rhs) const
00938 {
00939 return SparseVectorReverseIterator(vector_, position_ - rhs);
00940 }
00941
00943 difference_type operator+(const SparseVectorReverseIterator rhs) const
00944 {
00945 return position_ + rhs.position();
00946 }
00947
00949 SparseVectorReverseIterator operator-(const size_type rhs) const
00950 {
00951 return SparseVectorReverseIterator(vector_, position_ + rhs);
00952 }
00953
00955 difference_type operator-(const SparseVectorReverseIterator rhs) const
00956 {
00957
00958 return -1 * (position_ - rhs.position());
00959 }
00960
00962 bool operator!=(const SparseVectorReverseIterator & other)
00963 {
00964 return position_ != other.position_ || &vector_ != &other.vector_;
00965 }
00966
00968 bool operator==(const SparseVectorReverseIterator & other)
00969 {
00970 return !(*this != other);
00971 }
00972
00974 bool operator<(const SparseVectorReverseIterator & other)
00975 {
00976 return !(this->position() < other.position());
00977 }
00978
00980 bool operator>(const SparseVectorReverseIterator & other)
00981 {
00982 return !(this->position() > other.position());
00983 }
00984
00986 bool operator<=(const SparseVectorReverseIterator & other)
00987 {
00988 return !(this->position() <= other.position());
00989 }
00990
00992 bool operator>=(const SparseVectorReverseIterator & other)
00993 {
00994 return !(this->position() >= other.position());
00995 }
00996
00998 SparseVectorReverseIterator & rhop()
00999 {
01000 assert(valrit_ != reverse_map_const_iterator(vector_.values_.rend()));
01001
01002 if (position_ - 1 != valrit_->first)
01003 {
01004 valrit_ = reverse_map_const_iterator(--(vector_.values_.find(position_ - 1)));
01005 }
01006 else
01007 {
01008 ++valrit_;
01009 }
01010
01011 if (valrit_ == reverse_map_const_iterator(vector_.values_.rend()))
01012 {
01013 position_ = 0;
01014 }
01015 else
01016 {
01017 position_ = valrit_->first + 1;
01018 }
01019 return *this;
01020 }
01021
01023 size_type position() const
01024 {
01025 return position_;
01026 }
01027
01028 public:
01029
01031 SparseVectorReverseIterator(SparseVector & vector, size_type position) :
01032 position_(position),
01033 vector_(vector),
01034 valrit_(vector.values_.rbegin())
01035 {
01036 }
01037
01038 protected:
01039
01041 size_type position_;
01042
01043 private:
01045 SparseVector & vector_;
01046
01048 reverse_map_const_iterator valrit_;
01049
01051 SparseVectorReverseIterator();
01052
01053
01054
01055 };
01056
01058 class SparseVectorConstIterator
01059 {
01060 friend class SparseVector<Value>;
01061 friend class SparseVectorIterator;
01062
01063 public:
01064
01066 SparseVectorConstIterator(const SparseVectorConstIterator & source) :
01067 position_(source.position_),
01068 vector_(source.vector_),
01069 valit_(source.valit_)
01070 {
01071 }
01072
01074 SparseVectorConstIterator(const SparseVectorIterator & source) :
01075 position_(source.position_),
01076 vector_(source.vector_),
01077 valit_(source.valit_)
01078 {
01079 }
01080
01082 virtual ~SparseVectorConstIterator()
01083 {
01084 }
01085
01087 SparseVectorConstIterator & operator=(const SparseVectorConstIterator & source)
01088 {
01089 if (this != &source)
01090 {
01091 position_ = source.position_;
01092 const_cast<SparseVector &>(this->vector_) = source.vector_;
01093 valit_ = source.valit_;
01094 }
01095 return *this;
01096 }
01097
01099 SparseVectorConstIterator & operator++()
01100 {
01101 assert(position_ <= vector_.size_);
01102 ++position_;
01103 return *this;
01104 }
01105
01107 SparseVectorConstIterator operator++(int)
01108 {
01109 SparseVectorConstIterator tmp(*this);
01110 ++position_;
01111 assert(position_ <= vector_.size_);
01112 return tmp;
01113 }
01114
01116 SparseVectorConstIterator & operator--()
01117 {
01118 assert(position_ <= vector_.size_);
01119 --position_;
01120 return *this;
01121 }
01122
01124 SparseVectorConstIterator operator--(int)
01125 {
01126 SparseVectorConstIterator tmp(*this);
01127 --position_;
01128 assert(position_ <= vector_.size_);
01129 return tmp;
01130 }
01131
01133 const Value operator*() const
01134 {
01135 assert(position_ < vector_.size_);
01136 return (Value)ValueProxy(const_cast<SparseVector &>(this->vector_), position_);
01137 }
01138
01139
01140 const ValueProxy operator[](size_type n) const
01141 {
01142 position_ += n;
01143 assert(position_ < vector_.size_);
01144 return ValueProxy(const_cast<SparseVector &>(this->vector_), position_);
01145 }
01146
01148 SparseVectorConstIterator & operator+=(const size_type rhs)
01149 {
01150 position_ += rhs;
01151 return *this;
01152 }
01153
01155 SparseVectorConstIterator & operator-=(const size_type rhs)
01156 {
01157 position_ -= rhs;
01158 return *this;
01159 }
01160
01162 SparseVectorConstIterator operator+(const size_type rhs) const
01163 {
01164 return SparseVectorConstIterator(const_cast<SparseVector &>(this->vector_), position_ + rhs);
01165 }
01166
01168 SparseVectorConstIterator operator-(const size_type rhs) const
01169 {
01170 return SparseVectorConstIterator(const_cast<SparseVector &>(this->vector_), position_ - rhs);
01171 }
01172
01174 bool operator!=(const SparseVectorConstIterator & other)
01175 {
01176 return position_ != other.position_ || &vector_ != &other.vector_;
01177 }
01178
01180 bool operator==(const SparseVectorConstIterator & other)
01181 {
01182 return !(*this != other);
01183 }
01184
01186 bool operator<(const SparseVectorConstIterator & other)
01187 {
01188 return this->position() < other.position();
01189 }
01190
01192 bool operator>(const SparseVectorConstIterator & other)
01193 {
01194 return this->position() > other.position();
01195 }
01196
01198 bool operator<=(const SparseVectorConstIterator & other)
01199 {
01200 return this->position() <= other.position();
01201 }
01202
01204 bool operator>=(const SparseVectorConstIterator & other)
01205 {
01206 return this->position() >= other.position();
01207 }
01208
01210 SparseVectorConstIterator & hop()
01211 {
01212 assert(valit_ != vector_.values_.end());
01213
01214 if (position_ != valit_->first)
01215 {
01216 valit_ = vector_.values_.upper_bound(position_);
01217 }
01218 else
01219 {
01220 ++valit_;
01221 }
01222
01223 if (valit_ == vector_.values_.end())
01224 {
01225 position_ = vector_.size_;
01226 }
01227 else
01228 {
01229 position_ = valit_->first;
01230 }
01231 return *this;
01232 }
01233
01235 size_type position() const
01236 {
01237 return position_;
01238 }
01239
01240 protected:
01242 SparseVectorConstIterator();
01243
01245 SparseVectorConstIterator(const SparseVector & vector, size_type position) :
01246 position_(position),
01247 vector_(vector),
01248 valit_(vector.values_.begin())
01249 {
01250 }
01251
01252 private:
01254 mutable size_type position_;
01255
01257 const SparseVector & vector_;
01258
01260 map_const_iterator valit_;
01261
01262 };
01263
01265 class SparseVectorConstReverseIterator
01266 {
01267 friend class SparseVector<Value>;
01268
01269 public:
01270
01272 SparseVectorConstReverseIterator(const SparseVectorConstIterator & source) :
01273 position_(source.position_),
01274 vector_(source.vector_),
01275 valrit_(source.valrit_)
01276 {
01277 }
01278
01280 SparseVectorConstReverseIterator(const SparseVectorReverseIterator & source) :
01281 position_(source.position_),
01282 vector_(source.vector_),
01283 valrit_(source.valrit_)
01284 {
01285 }
01286
01288 virtual ~SparseVectorConstReverseIterator()
01289 {
01290 }
01291
01293 SparseVectorConstReverseIterator & operator=(const SparseVectorConstReverseIterator & source)
01294 {
01295 if (this != &source)
01296 {
01297 position_ = source.position_;
01298 const_cast<SparseVector &>(this->vector_) = source.vector_;
01299 valrit_ = source.valrit_;
01300 }
01301 return *this;
01302 }
01303
01305 SparseVectorConstReverseIterator & operator++()
01306 {
01307
01308 --position_;
01309 return *this;
01310 }
01311
01313 SparseVectorConstReverseIterator operator++(int)
01314 {
01315 SparseVectorConstIterator tmp(*this);
01316 --position_;
01317
01318 return tmp;
01319 }
01320
01322 SparseVectorConstReverseIterator & operator--()
01323 {
01324
01325 ++position_;
01326 return *this;
01327 }
01328
01330 SparseVectorConstReverseIterator operator--(int)
01331 {
01332 SparseVectorConstIterator tmp(*this);
01333 ++position_;
01334
01335 return tmp;
01336 }
01337
01339 ValueProxy operator*()
01340 {
01341 assert(position_ <= vector_.size_);
01342 assert(position_ != 0);
01343 return ValueProxy(const_cast<SparseVector &>(this->vector_), position_ - 1);
01344 }
01345
01347 SparseVectorConstReverseIterator & rhop()
01348 {
01349 assert(valrit_ != vector_.values_.rend());
01350
01351 if (position_ - 1 != valrit_->first)
01352 {
01353 valrit_ = reverse_map_const_iterator(--(vector_.values_.find(position_ - 1)));
01354 }
01355 else
01356 {
01357 ++valrit_;
01358 }
01359
01360 if (valrit_ == reverse_map_const_iterator(vector_.values_.rend()))
01361 {
01362 position_ = 0;
01363 }
01364 else
01365 {
01366 position_ = valrit_->first + 1;
01367 }
01368 return *this;
01369 }
01370
01372 size_type position() const
01373 {
01374 return position_;
01375 }
01376
01378 bool operator!=(const SparseVectorConstReverseIterator & other)
01379 {
01380 return position_ != other.position_ || &vector_ != &other.vector_;
01381 }
01382
01383 protected:
01384
01386 SparseVectorConstReverseIterator();
01387
01389 SparseVectorConstReverseIterator(const SparseVector & vector, size_type position) :
01390 position_(position), vector_(vector), valrit_(vector.values_.rbegin())
01391 {
01392 }
01393
01394 private:
01395
01396
01397 mutable size_type position_;
01398
01400 const SparseVector & vector_;
01401
01402
01403 reverse_map_const_iterator valrit_;
01404
01405 };
01406
01407
01408 };
01409
01410 }
01411 #endif //OPENMS_DATASTRUCTURES_SPARSEVECTOR_H