From 71c4918f05a4b380dfaebfabcc1847c1c6d497dd Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Sun, 27 May 2012 15:34:44 -0400 Subject: clean up --- utils/fast_sparse_vector.h | 86 +++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 43 deletions(-) (limited to 'utils/fast_sparse_vector.h') diff --git a/utils/fast_sparse_vector.h b/utils/fast_sparse_vector.h index 3cc48f8e..e86cbdc1 100644 --- a/utils/fast_sparse_vector.h +++ b/utils/fast_sparse_vector.h @@ -30,7 +30,7 @@ // to just set it #define L2_CACHE_LINE 128 -// this should just be a typedef to pair on the new c++ +// this should just be a typedef to pair on the new c++ // I have to avoid this since I want to use unions and c++-98 // does not let unions have types with constructors in them // this type bypasses default constructors. use with caution! @@ -38,32 +38,32 @@ // does anything template struct PairIntT { - const PairIntT& operator=(const std::pair& v) { + const PairIntT& operator=(const std::pair& v) { std::memcpy(this, &v, sizeof(PairIntT)); return *this; } - operator const std::pair&() const { - return *reinterpret_cast*>(this); + operator const std::pair&() const { + return *reinterpret_cast*>(this); } - int& first() { - return reinterpret_cast*>(this)->first; + unsigned& first() { + return reinterpret_cast*>(this)->first; } T& second() { - return reinterpret_cast*>(this)->second; + return reinterpret_cast*>(this)->second; } - const int& first() const { - return reinterpret_cast*>(this)->first; + const unsigned& first() const { + return reinterpret_cast*>(this)->first; } const T& second() const { - return reinterpret_cast*>(this)->second; + return reinterpret_cast*>(this)->second; } private: // very bad way of bypassing the default constructor on T - char data_[sizeof(std::pair)]; + char data_[sizeof(std::pair)]; }; -BOOST_STATIC_ASSERT(sizeof(PairIntT) == sizeof(std::pair)); +BOOST_STATIC_ASSERT(sizeof(PairIntT) == sizeof(std::pair)); -template +template class FastSparseVector { public: struct const_iterator { @@ -79,17 +79,17 @@ class FastSparseVector { } const bool local_; const PairIntT* local_it_; - typename std::map::const_iterator remote_it_; - const std::pair& operator*() const { + typename std::map::const_iterator remote_it_; + const std::pair& operator*() const { if (local_) - return *reinterpret_cast*>(local_it_); + return *reinterpret_cast*>(local_it_); else return *remote_it_; } - const std::pair* operator->() const { + const std::pair* operator->() const { if (local_) - return reinterpret_cast*>(local_it_); + return reinterpret_cast*>(local_it_); else return &*remote_it_; } @@ -118,17 +118,17 @@ class FastSparseVector { } FastSparseVector(const FastSparseVector& other) { std::memcpy(this, &other, sizeof(FastSparseVector)); - if (is_remote_) data_.rbmap = new std::map(*data_.rbmap); + if (is_remote_) data_.rbmap = new std::map(*data_.rbmap); } - FastSparseVector(std::pair* first, std::pair* last) { + FastSparseVector(std::pair* first, std::pair* last) { const ptrdiff_t n = last - first; if (n <= LOCAL_MAX) { is_remote_ = false; local_size_ = n; - std::memcpy(data_.local, first, sizeof(std::pair) * n); + std::memcpy(data_.local, first, sizeof(std::pair) * n); } else { is_remote_ = true; - data_.rbmap = new std::map(first, last); + data_.rbmap = new std::map(first, last); } } void erase(int k) { @@ -150,31 +150,31 @@ class FastSparseVector { clear(); std::memcpy(this, &other, sizeof(FastSparseVector)); if (is_remote_) - data_.rbmap = new std::map(*data_.rbmap); + data_.rbmap = new std::map(*data_.rbmap); return *this; } T const& get_singleton() const { assert(size()==1); return begin()->second; } - bool nonzero(int k) const { + bool nonzero(unsigned k) const { return static_cast(value(k)); } - inline void set_value(int k, const T& v) { + inline void set_value(unsigned k, const T& v) { get_or_create_bin(k) = v; } - inline T& add_value(int k, const T& v) { + inline T& add_value(unsigned k, const T& v) { return get_or_create_bin(k) += v; } - inline T get(int k) const { + inline T get(unsigned k) const { return value(k); } - inline T value(int k) const { + inline T value(unsigned k) const { if (is_remote_) { - typename std::map::const_iterator it = data_.rbmap->find(k); + typename std::map::const_iterator it = data_.rbmap->find(k); if (it != data_.rbmap->end()) return it->second; } else { - for (int i = 0; i < local_size_; ++i) { + for (unsigned i = 0; i < local_size_; ++i) { const PairIntT& p = data_.local[i]; if (p.first() == k) return p.second(); } @@ -256,8 +256,8 @@ class FastSparseVector { } inline FastSparseVector& operator*=(const T& scalar) { if (is_remote_) { - const typename std::map::iterator end = data_.rbmap->end(); - for (typename std::map::iterator it = data_.rbmap->begin(); it != end; ++it) + const typename std::map::iterator end = data_.rbmap->end(); + for (typename std::map::iterator it = data_.rbmap->begin(); it != end; ++it) it->second *= scalar; } else { for (int i = 0; i < local_size_; ++i) @@ -267,8 +267,8 @@ class FastSparseVector { } inline FastSparseVector& operator/=(const T& scalar) { if (is_remote_) { - const typename std::map::iterator end = data_.rbmap->end(); - for (typename std::map::iterator it = data_.rbmap->begin(); it != end; ++it) + const typename std::map::iterator end = data_.rbmap->end(); + for (typename std::map::iterator it = data_.rbmap->begin(); it != end; ++it) it->second /= scalar; } else { for (int i = 0; i < local_size_; ++i) @@ -300,7 +300,7 @@ class FastSparseVector { T dot(const std::vector& v) const { T res = T(); for (const_iterator it = begin(), e = end(); it != e; ++it) - if (it->first < v.size()) res += it->second * v[it->first]; + if (static_cast(it->first) < v.size()) res += it->second * v[it->first]; return res; } T dot(const FastSparseVector& other) const { @@ -330,11 +330,11 @@ class FastSparseVector { v.resize(i+1); return v[i]; } - inline T& get_or_create_bin(int k) { + inline T& get_or_create_bin(unsigned k) { if (is_remote_) { return (*data_.rbmap)[k]; } else { - for (int i = 0; i < local_size_; ++i) + for (unsigned i = 0; i < local_size_; ++i) if (data_.local[i].first() == k) return data_.local[i].second(); } assert(!is_remote_); @@ -353,17 +353,17 @@ class FastSparseVector { void swap_local_rbmap() { if (is_remote_) { // data is in rbmap, move to local assert(data_.rbmap->size() < LOCAL_MAX); - const std::map* m = data_.rbmap; + const std::map* m = data_.rbmap; local_size_ = m->size(); int i = 0; - for (typename std::map::const_iterator it = m->begin(); + for (typename std::map::const_iterator it = m->begin(); it != m->end(); ++it) { data_.local[i] = *it; ++i; } is_remote_ = false; } else { // data is local, move to rbmap - std::map* m = new std::map(&data_.local[0], &data_.local[local_size_]); + std::map* m = new std::map(&data_.local[0], &data_.local[local_size_]); data_.rbmap = m; is_remote_ = true; } @@ -371,7 +371,7 @@ class FastSparseVector { union { PairIntT local[LOCAL_MAX]; - std::map* rbmap; + std::map* rbmap; } data_; unsigned char local_size_; bool is_remote_; @@ -399,8 +399,8 @@ class FastSparseVector { void load(Archive & ar, const unsigned int version) { (void) version; this->clear(); - int sz; ar & sz; - for (int i = 0; i < sz; ++i) { + unsigned sz; ar & sz; + for (unsigned i = 0; i < sz; ++i) { std::pair wire_pair; ar & wire_pair; this->set_value(FD::Convert(wire_pair.first), wire_pair.second); -- cgit v1.2.3