LocARNA-1.9.2
src/LocARNA/sparse_vector.hh
00001 #ifndef SPARSE_VECTOR_HH
00002 #define SPARSE_VECTOR_HH
00003 
00004 #ifdef HAVE_CONFIG_H
00005 #include <config.h>
00006 #endif
00007 
00008 #include <iosfwd>
00009 
00010 #include "aux.hh"
00011 
00012 namespace LocARNA {
00013 
00025     template <typename T>
00026     class SparseVector {
00027     public:
00028         typedef T value_t; 
00029 
00030         typedef size_t size_type; 
00031 
00032         typedef size_type key_t; 
00033 
00034     protected:
00035         typedef
00036             typename unordered_map<key_t, value_t>::type map_t; 
00037         map_t the_map_; 
00038         value_t def_;   
00039 
00040     public:
00046         typedef typename map_t::const_iterator const_iterator;
00047 
00056         class element {
00057         private:
00058             SparseVector<T> *m_;
00059             key_t k_;
00060 
00061         public:
00070             element(SparseVector<T> *m, key_t k) : m_(m), k_(k) {}
00071 
00079             operator value_t() {
00080                 typename map_t::const_iterator it = m_->the_map_.find(k_);
00081                 if (it == m_->the_map_.end())
00082                     return m_->def_;
00083                 else
00084                     return it->second;
00085             }
00086 
00098             element
00099             operator+=(const value_t &x) {
00100                 const_iterator it = m_->the_map_.find(k_);
00101                 if (it == m_->the_map_.end())
00102                     m_->the_map_[k_] = m_->def_ + x;
00103                 else
00104                     m_->the_map_[k_] += x;
00105 
00106                 return *this;
00107             }
00108 
00121             element &
00122             operator=(const value_t &x) {
00123                 if (x == m_->def_) {
00124                     m_->the_map_.erase(k_);
00125                 } else {
00126                     // the following replaces m_->the_map_[k_] = x;
00127                     // but never calls the default constructor for value_t
00128 
00129                     typename map_t::iterator it = m_->the_map_.find(k_);
00130                     if (it != m_->the_map_.end()) {
00131                         it->second = x;
00132                     } else {
00133                         m_->the_map_.insert(typename map_t::value_type(k_, x));
00134                     }
00135                 }
00136                 return *this;
00137             }
00138         };
00139 
00145         explicit SparseVector(const value_t &def) : the_map_(), def_(def) {}
00146 
00154         element operator[](size_type i) { return element(this, key_t(i)); }
00155 
00163         const value_t &operator[](size_type i) const {
00164             const_iterator it = the_map_.find(key_t(i));
00165             if (it == the_map_.end())
00166                 return def_;
00167             else
00168                 return it->second;
00169         }
00170 
00183         void
00184         set(size_type i, const value_t &val) {
00185             typename map_t::iterator it = the_map_.find(key_t(i));
00186             if (it != the_map_.end()) {
00187                 it->second = val;
00188             } else {
00189                 the_map_.insert(typename map_t::value_type(key_t(i), val));
00190             }
00191         }
00192 
00198         void
00199         reset(size_type i) {
00200             typename map_t::iterator it = the_map_.find(key_t(i));
00201             if (it != the_map_.end()) {
00202                 the_map_.erase(key_t(i));
00203             }
00204         }
00205 
00210         size_type
00211         size() const {
00212             return the_map_.size();
00213         }
00214 
00220         bool
00221         empty() const {
00222             return the_map_.empty();
00223         }
00224 
00228         void
00229         clear() {
00230             the_map_.clear();
00231         }
00232 
00240         const_iterator
00241         begin() const {
00242             return the_map_.begin();
00243         }
00244 
00251         const_iterator
00252         end() const {
00253             return the_map_.end();
00254         }
00255     };
00256 
00265     template <class T>
00266     inline std::ostream &
00267     operator<<(std::ostream &out, const SparseVector<T> &v) {
00268         for (typename SparseVector<T>::const_iterator it = v.begin();
00269              v.end() != it; ++it) {
00270             out << it->first << ":" << it->second << " ";
00271         }
00272         out << std::endl;
00273         return out;
00274     }
00275 
00276 } // end namespace LocARNA
00277 
00278 #endif // SPARSE_VECTOR_HH
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends