Toast++  1.0.2 (r.539)
Forward and inverse modelling in optical tomography
spvector.h
1 // -*-C++-*-
2 // ==========================================================================
3 // Module mathlib
4 // File spvector.h
5 // Declaration of template class TSparseVector ('template sparse vector')
6 //
7 // The following typedefs for specific template types have been defined
8 // for convenience:
9 // RSparseVector = TSparseVector<double> ('real')
10 // FSparseVector = TSparseVector<float> ('float')
11 // CSparseVector = TSparseVector<complex> ('complex')
12 // ISparseVector = TSparseVector<int> ('integer')
13 // SparseVector = TSparseVector<double> for backward compatibility
14 // ==========================================================================
15 
16 #ifndef __SPVECTOR_H
17 #define __SPVECTOR_H
18 
19 template<class VT> class TSparseMatrix;
20 
21 // ==========================================================================
22 // struct for defining a sparse vector data block
23 
24 template<class VT>
25 struct spvecbase {
26  VT *data; // data array
27  int *index; // index array
28  int lsize; // logical vector size
29  int psize; // physical vector size (= number of used entries)
30  int bufsize; // buffer size (= total number of entries)
31  int nref; // reference counter
32 };
33 
34 // ==========================================================================
35 // class TSparseVector
36 
37 template<class VT> class TSparseVector {
38 public:
39  TSparseVector ();
40 
41  TSparseVector (int ldim);
42  // creates a sparse vector with logical dimension ldim and physical
43  // dimension 0
44 
45  TSparseVector (int ldim, int pdim);
46  // creates a sparse vector with logical dimension ldim and physical
47  // dimension pdim (all entries unused)
48 
49  TSparseVector (const TSparseVector<VT> &vec);
50  // copy constructor; *this is created as a copy of, not a reference to
51  // `vec'
52 
53  ~TSparseVector ();
54 
55  operator TVector<VT> ();
56  // cast to full vector
57 
58  void Copy (const TSparseVector<VT> &vec);
59  void Copy (const TVector<VT> &vec);
60  // copy from sparse or full vector
61 
62  void New (int ldim, int nbuf = 0);
63  // reallocates vector; sets logical size to `ldim' and allocates memory
64  // for `nbuf' entries
65 
66  void Allocate (int nbuf); // previously `Initialise'
67  // reallocates memory for `nbuf' entries, but keeps logical size
68 
69  void Relink (const TSparseVector<VT> &vec);
70 
71  void Shrink ();
72  // removes all unused entries from the vector
73 
74  void Initialise (int nz, int *idx);
75  // initialise vector to `nz' entries and index list `idx'
76  // values are set to zero
77 
78  void Clear ();
79  // sets all entries to zero but without deallocating space
80 
81  TSparseVector &operator*= (const VT &vt);
82  // multiplication with scalar
83 
84  VT operator& (const TVector<VT> &vec) const;
85  // scalar product with a full vector
86 
87  friend VT iprod (const TSparseVector<VT> &v1, const TSparseVector<VT> &v2,
88  int from = 0, int to = -1);
89  // scalar product of two sparse vectors with optional range limit
90  // The range specification [from,to] denotes logical indices
91 
92  int LDim() const { return base->lsize; }
93  // returns logical dimension of vector
94 
95  int PDim() const { return base->psize; }
96  // returns physical dimension of data array
97 
98  VT Get (int i) const; // i: logical index
99 
100  void Put (int i, VT val);
101 
102  void Add (int i, VT val);
103 
104  void Erase (int i);
105  // deletes entry for logical index i
106 
107  inline int PIndex (int i) const;
108  // returns physical index to logical index `i', or -1 if `i' is not
109  // in the index list
110 
111  int Insert (int i);
112  // inserts a new entry for logical index `i' and returns the
113  // corresponding physical index
114  // assumes that an entry for `i' is not already present
115 
116  void SparseOutput (ostream &os) const;
117 
118 // conditional inline functions
119 #ifdef MATH_DEBUG
120  VT &operator[] (int i) const; // i: physical index
121 
122  int LIndex (int i) const;
123  // returns the logical index of physical index `i'.
124 
125  void SetIndex (int pi, int li);
126  // sets index at position pi to logical index li.
127  // Warning: this is a low-level routine. Use it only when you know
128  // what you are doing.
129 #else
130  VT &operator[] (int i) const { return data[i]; }
131 
132  int LIndex (int i) const { return index[i]; }
133 
134  void SetIndex (int pi, int li) { index[pi] = li; }
135  // sets index at position pi to logical index li.
136  // Warning: this is a low-level routine. Use it only when you know
137  // what you are doing.
138 #endif
139 
140  // friends
141  friend TVector<VT> ToVector (const TSparseVector<VT> &vec);
142  friend ostream &operator<< (ostream &os, const TSparseVector<VT> &vec);
143 
144 private:
145  spvecbase<VT> *base;
146  VT *data;
147  int *index;
148 
149  friend VT iprod_sorted (const TSparseVector<VT> &v1,
150  const TSparseVector<VT> &v2, int from = 0, int to = -1);
151  // as iprod, but assumes index lists of v1 and v2 are sorted in
152  // ascending order
153 
154  friend bool overlap_sorted (const TSparseVector<VT> &v1,
155  const TSparseVector<VT> &v2, int from = 0, int to = -1);
156  // returns TRUE if sorted vectors v1 and v2 share common nonzero entries
157  // in the (logical) range between 'from' and 'to'
158 
159  friend class TSparseMatrix<VT>;
160 };
161 
162 
163 // ==========================================================================
164 // inline functions
165 
166 template<class VT>
168 {
169  base = new spvecbase<VT>;
170  data = base->data;
171  index = base->index;
172  base->nref = 1;
173  base->bufsize = base->psize = base->lsize = 0;
174 }
175 
176 template<class VT>
177 inline TSparseVector<VT>::TSparseVector (int ldim)
178 {
179  base = new spvecbase<VT>;
180  data = base->data;
181  index = base->index;
182  base->nref = 1;
183  base->bufsize = base->psize = 0;
184  base->lsize = ldim;
185 }
186 
187 template<class VT>
188 inline TSparseVector<VT>::TSparseVector (int ldim, int pdim)
189 {
190  base = new spvecbase<VT>;
191  data = base->data;
192  index = base->index;
193  base->nref = 1;
194  base->bufsize = 0;
195  New (ldim, pdim);
196 }
197 
198 template<class VT>
200 {
201  if (--base->nref == 0) { // destroy base only if this was the last link
202  if (base->bufsize) {
203  delete []base->data;
204  delete []base->index;
205  }
206  delete base;
207  }
208 }
209 
210 template<class VT>
211 inline int TSparseVector<VT>::PIndex (int i) const
212 {
213  for (int j = 0; j < base->psize; j++)
214  if (index[j] == i) return j;
215  return -1;
216 }
217 
218 #ifndef MATH_DEBUG
219 template<class VT>
220 inline VT TSparseVector<VT>::operator& (const TVector<VT> &vec) const
221 {
222  VT sum = (VT)0;
223  int i, ps = base->psize;
224 
225  for (i = 0; i < ps; i++)
226  sum += data[i] * vec[index[i]];
227  return sum;
228 }
229 #endif // !MATH_DEBUG
230 
231 // ==========================================================================
232 // friend prototypes
233 
234 #ifdef NEED_FRIEND_PT
235 
236 template<class VT>
237 bool overlap_sorted (const TSparseVector<VT> &v1, const TSparseVector<VT> &v2,
238  int from = 0, int to = -1);
239 
240 template<class VT>
241 VT iprod (const TSparseVector<VT> &v1, const TSparseVector<VT> &v2,
242  int from = 0, int to = -1);
243 
244 template<class VT>
245 VT iprod_sorted (const TSparseVector<VT> &v1, const TSparseVector<VT> &v2,
246  int from = 0, int to = -1);
247 
248 template<class VT>
249 TVector<VT> ToVector (const TSparseVector<VT> &vec);
250 
251 template<class VT>
252 ostream &operator<< (ostream &os, const TSparseVector<VT> &vec);
253 
254 #endif
255 
256 // ==========================================================================
257 // typedefs for specific instances of `TSparseVector'
258 
259 typedef TSparseVector<double> RSparseVector; // 'real'
260 typedef TSparseVector<float> FSparseVector; // 'float'
261 typedef TSparseVector<complex> CSparseVector; // 'complex'
262 typedef TSparseVector<int> ISparseVector; // 'integer'
263 typedef TSparseVector<double> SparseVector; // for backward compatibility
264 
265 #endif // !__SPVECTOR_H
Templated vector class.
Definition: vector.h:39
Definition: spvector.h:37
Definition: spvector.h:25
Definition: spmatrix.h:26