SparseVector.h
Go to the documentation of this file.
1 #ifndef _utl_SparseVector_h_
2 #define _utl_SparseVector_h_
3 
4 #include <map>
5 #include <numeric>
6 
7 #include <boost/lambda/lambda.hpp>
8 
9 
10 namespace utl {
11 
35  template<typename T = double>
36  class SparseVector {
37  public:
38  typedef int IndexType;
39 
40  private:
41  typedef std::map<IndexType, T> MapType;
42  typedef typename MapType::value_type MapValueType;
43  typedef typename MapType::iterator MapIterator;
44  typedef typename MapType::const_iterator MapConstIterator;
45 
46  public:
47  template<class IteratorType, typename ValueType>
49  public:
50  IteratorTransformer(const IteratorType& it)
51  : fIterator(it) { }
52 
53  IndexType GetIndex() const { return fIterator->first; }
54 
55  ValueType& operator()() const { return fIterator->second; }
56 
57  ValueType& operator*() const { return fIterator->second; }
58 
59  IteratorTransformer& operator++() { ++fIterator; return *this; }
60 
61  bool operator==(const IteratorTransformer& it) const { return fIterator == it.fIterator; }
62 
63  bool operator!=(const IteratorTransformer& it) const { return fIterator != it.fIterator; }
64 
65  private:
66  IteratorType fIterator;
67  };
68 
69  SparseVector(const T& init = T())
70  : fInitializer(init) { }
71 
72  template<typename U>
73  explicit
76  {
78  it != v.SparseEnd(); ++it)
79  (*this)(it.GetIndex()) = it();
80  }
81 
82  unsigned int GetNonEmptySize() const { return fVector.size(); }
83 
84  const T& GetInitializer() const { return fInitializer; }
85 
86  void Clear() { fVector.clear(); }
87 
89  const T&
90  operator[](const IndexType index)
91  const
92  {
93  const MapConstIterator it = fVector.find(index);
94  return (it == fVector.end()) ? fInitializer : it->second;
95  }
96 
98  T&
99  operator()(const IndexType index)
100  {
101  return fVector.insert(MapValueType(index, fInitializer)).first->second;
102  }
103 
104  const T&
105  operator()(const IndexType index)
106  const
107  {
108  const MapConstIterator it = fVector.find(index);
109  return (it == fVector.end()) ? fInitializer : it->second;
110  }
111 
112  bool IsEmpty() const { return fVector.empty(); }
113 
114  bool
115  IsEmpty(const IndexType index)
116  const
117  {
118  const MapConstIterator it = fVector.find(index);
119  return it == fVector.end();
120  }
121 
123  unsigned int
125  {
126  unsigned int erased = 0;
127  for (MapIterator it = fVector.begin();
128  it != fVector.end(); )
129  if (it->second != fInitializer)
130  ++it;
131  else {
132  Erase(it++);
133  ++erased;
134  }
135  return erased;
136  }
137 
138  bool
140  const
141  {
142  return fInitializer == v.fInitializer &&
143  fVector == v.fVector;
144  }
145 
146  bool operator!=(const SparseVector<T>& v) const { return !operator==(v); }
147 
148  template<typename U>
149  bool
151  const
152  {
153  if (fInitializer != v.GetInitializer())
154  return false;
155  ConstIterator it1 = SparseBegin();
156  typename SparseVector<U>::ConstIterator it2 = v.SparseBegin();
157  while (it1 != SparseEnd() && it2 != v.SparseEnd()) {
158  if (it1.GetIndex() != it2.GetIndex() ||
159  it1() != it2())
160  return false;
161  ++it1;
162  ++it2;
163  }
164 
165  if (it1 != SparseEnd() || it2 != v.SparseEnd())
166  return false;
167 
168  return true;
169  }
170 
171  template<typename U>
172  bool operator!=(const SparseVector<U>& v) const
173  { return !operator==(v); }
174 
177 
178  Iterator SparseBegin() { return Iterator(fVector.begin()); }
179 
180  ConstIterator SparseBegin() const { return ConstIterator(fVector.begin()); }
181 
182  Iterator SparseEnd() { return Iterator(fVector.end()); }
183 
184  ConstIterator SparseEnd() const { return ConstIterator(fVector.end()); }
185 
186  Iterator Find(const IndexType index)
187  { return Iterator(fVector.find(index)); }
188 
189  ConstIterator Find(const IndexType index) const
190  { return ConstIterator(fVector.find(index)); }
191 
193  template<typename U>
194  SparseVector&
195  operator*=(const U& value)
196  {
197  for (MapIterator it = fVector.begin(); it != fVector.end(); ++it)
198  it->second *= value;
199  return *this;
200  }
201 
203  template<typename U>
204  SparseVector&
206  {
207  for (typename SparseVector<U>::ConstIterator it = m.SparseBegin();
208  it != m.SparseEnd(); ++it)
209  (*this)(it.GetIndex()) += it();
210  return *this;
211  }
212 
214  template<typename U>
215  SparseVector&
217  {
218  for (typename SparseVector<U>::ConstIterator it = m.SparseBegin();
219  it != m.SparseEnd(); ++it)
220  (*this)(it.GetIndex()) -= it();
221  return *this;
222  }
223 
224  private:
225 
226  void Erase(const MapIterator it) { fVector.erase(it); }
227 
228  const T fInitializer;
230  };
231 
232 
234  template<typename T, typename U>
235  typename boost::lambda::return_type_2<
236  boost::lambda::arithmetic_action<boost::lambda::multiply_action>,
239  >::type
241  {
242  typedef typename boost::lambda::return_type_2<
243  boost::lambda::arithmetic_action<boost::lambda::multiply_action>,
246  >::type Result;
247 
248  Result r = Result();
249 
250  typename SparseVector<T>::ConstIterator aIt = a.SparseBegin();
251  typename SparseVector<U>::ConstIterator bIt = b.SparseBegin();
252  if (aIt == a.SparseEnd() || bIt == b.SparseEnd())
253  return r;
254  typename SparseVector<T>::IndexType ai;
255  typename SparseVector<U>::IndexType bi;
256  do {
257  ai = aIt.GetIndex();
258  bi = bIt.GetIndex();
259  if (ai < bi) {
260  ++aIt;
261  continue;
262  }
263  if (bi < ai) {
264  ++bIt;
265  continue;
266  }
267  r += aIt() * bIt();
268  ++aIt;
269  ++bIt;
270  } while (aIt != a.SparseEnd() && bIt != b.SparseEnd());
271 
272  return r;
273  }
274 
275 
276  // output stuff
277 
278  template<class Stream, typename T>
279  Stream&
280  Output(Stream& s, const SparseVector<T>& v,
281  const unsigned int size, const char separator = ' ')
282  {
283  if (!size)
284  return s;
285 
286  if (v.IsEmpty(0))
287  s << '.';
288  else
289  s << v[0];
290 
291  for (unsigned int i = 1; i < size; ++i)
292  if (v.IsEmpty(i))
293  s << separator << '.';
294  else
295  s << separator << v[i];
296 
297  return s;
298  }
299 
300 
301 }
302 
303 
304 namespace boost {
305 
306  namespace lambda {
307 
308  template<typename T, typename U>
309  class plain_return_type_2<
310  arithmetic_action<multiply_action>,
311  utl::SparseVector<T>,
313  > {
314  private:
315  // delegate to the action result of (T, Action, U)
316  typedef typename
317  return_type_2<
318  arithmetic_action<multiply_action>,
319  T,
320  U
322 
323  public:
324  typedef res_type type;
325  };
326 
327  }
328 
329 }
330 
331 
332 #include <utl/SparseMatrixVectorOp.h>
333 
334 
335 #endif
ConstIterator Find(const IndexType index) const
Definition: SparseVector.h:189
bool operator!=(const SparseVector< T > &v) const
Definition: SparseVector.h:146
bool operator==(const SparseVector< U > &v) const
Definition: SparseVector.h:150
Sparse container class for vectorial data.
Definition: SparseVector.h:36
ConstIterator SparseBegin() const
Definition: SparseVector.h:180
MapType::const_iterator MapConstIterator
Definition: SparseVector.h:44
void Erase(const MapIterator it)
Definition: SparseVector.h:226
SparseVector & operator-=(const SparseVector< U > &m)
vector subtraction
Definition: SparseVector.h:216
IteratorTransformer< MapIterator, T > Iterator
Definition: SparseVector.h:175
bool operator!=(const IteratorTransformer &it) const
Definition: SparseVector.h:63
T & operator()(const IndexType index)
creating element access
Definition: SparseVector.h:99
MapType::iterator MapIterator
Definition: SparseVector.h:43
bool IsEmpty() const
Definition: SparseVector.h:112
IteratorTransformer< MapConstIterator, const T > ConstIterator
Definition: SparseVector.h:176
unsigned int Reclaim()
remove &quot;empty&quot; elements (that are equal to the initializer)
Definition: SparseVector.h:124
Iterator Find(const IndexType index)
Definition: SparseVector.h:186
#define U
unsigned int GetNonEmptySize() const
Definition: SparseVector.h:82
constexpr double s
Definition: AugerUnits.h:163
SparseVector & operator*=(const U &value)
multiplication with scalar
Definition: SparseVector.h:195
ConstIterator SparseEnd() const
Definition: SparseVector.h:184
IteratorTransformer(const IteratorType &it)
Definition: SparseVector.h:50
Vector operator*(const double d, const Vector &v)
Definition: OperationsVV.h:38
Iterator SparseEnd()
Definition: SparseVector.h:182
Iterator SparseBegin()
Definition: SparseVector.h:178
const T & GetInitializer() const
Definition: SparseVector.h:84
bool operator==(const SparseVector< T > &v) const
Definition: SparseVector.h:139
const T & operator()(const IndexType index) const
Definition: SparseVector.h:105
const T & operator[](const IndexType index) const
noncreating element access
Definition: SparseVector.h:90
bool operator!=(const SparseVector< U > &v) const
Definition: SparseVector.h:172
std::map< IndexType, T > MapType
Definition: SparseVector.h:41
bool operator==(const IteratorTransformer &it) const
Definition: SparseVector.h:61
Stream & Output(Stream &s, const SparseMatrix< T > &m, const typename SparseMatrix< T >::IndexType rows, const typename SparseMatrix< T >::IndexType columns, const char separator= ' ')
Definition: SparseMatrix.h:386
IteratorTransformer & operator++()
Definition: SparseVector.h:59
constexpr double m
Definition: AugerUnits.h:121
SparseVector & operator+=(const SparseVector< U > &m)
vector addition
Definition: SparseVector.h:205
MapType::value_type MapValueType
Definition: SparseVector.h:42

, generated on Tue Sep 26 2023.