iRoCS Toolbox  1.1.0
SparseVector.hh
Go to the documentation of this file.
1 /**************************************************************************
2  *
3  * Copyright (C) 2015 Thorsten Falk
4  *
5  * Image Analysis Lab, University of Freiburg, Germany
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  **************************************************************************/
22 
23 #ifndef ATBSPARSEVECTOR_LEGACY_HH
24 #define ATBSPARSEVECTOR_LEGACY_HH
25 
26 #ifdef HAVE_CONFIG_H
27 #include <config.hh>
28 #endif
29 
30 #include <map>
31 #include <blitz/array.h>
32 
33 namespace atb
34 {
35 
36  class SparseVectorError : public std::exception
37  {
38  public:
39 
40  SparseVectorError(char const *message)
41  : _message(message)
42  {}
43 
45  {}
46 
47  char const *what() const throw()
48  {
49  return _message;
50  }
51 
52  private:
53 
54  char const *_message;
55  };
56 
57  template <typename T>
59  {
60 
61  public:
62 
63  static T zero;
64  typedef typename std::map<size_t,T>::const_iterator const_iterator;
65  typedef typename std::map<size_t,T>::const_iterator iterator;
66 
67  SparseVector();
68  SparseVector(size_t size);
69  SparseVector(SparseVector const &v);
70  SparseVector(blitz::Array<T,1> const &v);
71  template<int Dim>
72  SparseVector(blitz::TinyVector<T,Dim> const &v);
73  ~SparseVector();
74 
75  void resize(size_t size);
76  size_t size() const;
77 
78  bool nonZero(size_t c) const;
79 
80  T const &operator()(size_t c) const;
81  T &operator()(size_t c);
82 
83  size_t nNonZeroEntries() const;
84  void clear();
85  const_iterator begin() const;
86  const_iterator end() const;
87  iterator begin();
88  iterator end();
89  const_iterator erase(size_t c);
90 
91  blitz::Array<T,1> toBlitz() const;
92 
93  void operator+=(SparseVector<T> const &v);
94  void operator-=(SparseVector<T> const &v);
95  void operator*=(T const &alpha);
96  void operator/=(T const &alpha);
97 
98  private:
99 
100  size_t _size;
101  std::map<size_t,T> _data;
102 
103  };
104 
105  template<typename T>
106  T dot(SparseVector<T> const &v1, SparseVector<T> const &v2);
107  template<typename T>
108  T dot(blitz::Array<T,1> const &v1, SparseVector<T> const &v2);
109  template<typename T>
110  T dot(SparseVector<T> const &v1, blitz::Array<T,1> const &v2);
111  template<typename T, int Dim>
112  T dot(blitz::TinyVector<T,Dim> const &v1, SparseVector<T> const &v2);
113  template<typename T, int Dim>
114  T dot(SparseVector<T> const &v1, blitz::TinyVector<T,Dim> const &v2);
115 
116  template<typename T>
117  SparseVector<T> operator*(SparseVector<T> const &v, T const &alpha);
118 
119  template<typename T>
120  SparseVector<T> operator*(T const &alpha, SparseVector<T> const &v);
121 
122 }
123 
124 #include "SparseVector.icc"
125 
126 #endif
char const * what() const
Definition: SparseVector.hh:47
std::map< size_t, T >::const_iterator const_iterator
Definition: SparseVector.hh:64
std::map< size_t, T >::const_iterator iterator
Definition: SparseVector.hh:65
SparseVectorError(char const *message)
Definition: SparseVector.hh:40
Polynomial< CoeffT > operator*(const Polynomial< CoeffT > &p1, const Polynomial< CoeffT > &p2)
Multiplication operator.
double dot(blitz::Array< DataT, 1 > const &vec1, blitz::Array< DataT, 1 > const &vec2)
Calculation of the inner product (dot product) of the given vectors.