KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
m_vec_view.h
Go to the documentation of this file.
1 /* $Id: m_vec_view.h 17810 2014-10-22 05:21:13Z ksimek $ */
2 /* =========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== */
19 
20 #ifndef KJB_M_CPP_VEC_VIEW
21 #define KJB_M_CPP_VEC_VIEW
22 
23 #include <l_cpp/l_exception.h>
24 #include <l_cpp/l_index.h>
25 
26 namespace kjb
27 {
28 
55 template <class Vector_type>
57 {
58 public:
60  typedef typename Vector_type::value_type Value_type;
61  typedef int Size_type;
62 
64  Vector_type& vec,
65  const Index_range& indices) :
66  indices_(indices),
67  vec_(vec)
68  {
69  }
70 
71  Generic_vector_view(Vector_type& vec) :
72  indices_(true), // "all" type range
73  vec_(vec)
74  {
75  }
76 
78  indices_(src.indices_), // "all" type range
79  vec_(src.vec_)
80  {
81  }
82 
83  // ----------------------
84  // Vector-LIKE INTERFACE:
85  // ----------------------
86 
87  bool operator==(const Self& other)
88  {
89  return equality(other);
90  }
91 
92  bool operator==(const Vector_type& other)
93  {
94  return equality(other);
95  }
96 
97  bool operator!=(const Self& other)
98  {
99  return !equality(other);
100  }
101 
102  bool operator!=(const Vector_type& other)
103  {
104  return !equality(other);
105  }
106 
111  {
112  int idx = indices_[i];
113  return vec_.at(idx);
114  }
115 
119  Value_type operator[](int i) const
120  {
121  int idx = indices_[i];
122  return vec_.at(idx);
123  }
124 
125  Size_type size() const
126  {
127  // "all"-type ranges can't know their size, so we provide it
128  if(indices_.all()) return vec_.size();
129  return indices_.size();
130  }
131 
132  Self& operator=(const Vector_type& vec)
133  {
134  if(&vec_ == &vec)
135  {
136  // avoid modifying what we're reading from
137  // copy first, then assign
138  return assign(Vector_type(vec));
139  }
140 
141  return assign(vec);
142  }
143 
144  Self& operator=(const Self& vec)
145  {
146  // do they refer to the same vector?
147  if(&vec_ == &vec.vec_)
148  {
149  // avoid modifying what we're reading from
150  // copy first, then assign
151  return assign(Vector_type(vec));
152  }
153  return assign(vec);
154  }
155 
160  {
161  const int n = size();
162 
163  for(int i = 0; i < n; i++)
164  {
165  operator[](i) = s;
166  }
167 
168  return *this;
169  }
170 
175  Self& operator=(const std::vector<double>& v)
176  {
177  return assign(v);
178  }
179 
184  Self& operator=(const std::vector<float>& v)
185  {
186  return assign(v);
187  }
188 
190  {
191  const int N = size();
192  for(int i = 0; i < N ; i++)
193  operator[](i) *= s;
194 
195  return *this;
196  }
197 
198  /*
199  * This doesn't make sense, since the output dimension could be different from input dimension
200  Self& operator*=(const Matrix_view& s)
201  {
202  }
203  */
204 
205  double dot(const Self& s)
206  {
207  // do they refer to the same vector?
208  if(&vec_ == &s.vec_)
209  {
210  // avoid modifying what we're reading from
211  // copy first, then assign
212  return dot_with_(Vector_type(s));
213  }
214  return dot_with_(s);
215  }
216 
217  double dot(const Vector_type& s)
218  {
219  // do they refer to the same vector?
220  if(&vec_ == &s)
221  {
222  // avoid modifying what we're reading from
223  // copy first, then assign
224  return dot_with_(Vector_type(s));
225  }
226  return dot_with_(s);
227  }
228 
230  {
231  const int N = size();
232  for(int i = 0; i < N ; i++)
233  operator[](i) /= s;
234 
235  return *this;
236  }
237 
238  Self& operator+=(const Self& s)
239  {
240  // do they refer to the same vector?
241  if(&vec_ == &s.vec_)
242  {
243  // avoid modifying what we're reading from
244  // copy first, then assign
245  return plus_equals(Vector_type(s));
246  }
247  return plus_equals(s);
248  }
249 
250  Self& operator+=(const Vector_type& s)
251  {
252  // do they refer to the same vector?
253  if(&vec_ == &s)
254  {
255  // avoid modifying what we're reading from
256  // copy first, then assign
257  return plus_equals(Vector_type(s));
258  }
259 
260  return plus_equals(s);
261  }
262 
263  Self& operator-=(const Self& s)
264  {
265  // do they refer to the same vector?
266  if(&vec_ == &s.vec_)
267  {
268  // avoid modifying what we're reading from
269  // copy first, then assign
270  return minus_equals(Vector_type(s));
271  }
272  return minus_equals(s);
273  }
274 
275  Self& operator-=(const Vector_type& s)
276  {
277  return minus_equals(s);
278  }
279 protected:
284  template <class Generic_vector>
285  bool equality(const Generic_vector& mat)
286  {
287  const int N = size();
288  if(mat.size () != N)
290 
291  for(int i = 0; i < N; i++)
292  {
293  if(operator[](i) != mat[i])
294  {
295  return false;
296  }
297  }
298 
299  return true;
300  }
301 
306  template <class Generic_vector>
307  Self& assign(const Generic_vector& vec)
308  {
309  const int N = size();
310  if(vec.size() != N)
311  {
313  }
314 
315  for(int i = 0; i < N; i++)
316  {
317  operator[](i) = vec[i];
318  }
319 
320  return *this;
321  }
322 
327  template <class Generic_vector>
328  Self& plus_equals(const Generic_vector& vec)
329  {
330  const int N = size();
331  if(vec.size() != N)
332  {
334  }
335 
336  for(int i = 0; i < N; i++)
337  {
338  operator[](i) += vec[i];
339  }
340 
341  return *this;
342 
343  }
344 
349  template <class Generic_vector>
350  double dot_with_(const Generic_vector& vec)
351  {
352  const int N = size();
353  if(vec.size() != N)
354  {
356  }
357 
358  double result = 0;
359  for(int i = 0; i < N; i++)
360  {
361  result += operator[](i) * vec[i];
362  }
363 
364  return result;
365  }
366 
371  template <class Generic_vector>
372  Self& minus_equals(const Generic_vector& vec)
373  {
374  const int N = size();
375  if(vec.size() != N)
376  {
378  }
379 
380  for(int i = 0; i < N; i++)
381  {
382  operator[](i) -= vec[i];
383  }
384 
385  return *this;
386 
387  }
388 protected:
390  Vector_type& vec_;
391 
392 };
393 
394 // "template typedef"
395 // see: http://www.gotw.ca/gotw/079.htm
396 template <class Vector_type>
398 {
400 };
401 
404 } // namespace kjb
405 
406 #endif
Self & operator-=(const Vector_type &s)
Definition: m_vec_view.h:275
Self & operator=(const Vector_type &vec)
Definition: m_vec_view.h:132
Object thrown when an argument is of the wrong size or dimensions.
Definition: l_exception.h:426
Generic_vector_view(Vector_type &vec, const Index_range &indices)
Definition: m_vec_view.h:63
Self & operator=(const std::vector< double > &v)
Definition: m_vec_view.h:175
double dot(const Vector_type &s)
Definition: m_vec_view.h:217
bool equality(const Generic_vector &mat)
Definition: m_vec_view.h:285
Size_type size() const
Definition: m_vec_view.h:125
size_t size() const
Definition: l_index.h:273
Self & plus_equals(const Generic_vector &vec)
Definition: m_vec_view.h:328
bool operator!=(const Vector_type &other)
Definition: m_vec_view.h:102
bool operator!=(const Self &other)
Definition: m_vec_view.h:97
#define KJB_THROW(ex)
Definition: l_exception.h:46
bool all() const
Definition: l_index.h:290
Generic_vector_view(Vector_type &vec)
Definition: m_vec_view.h:71
Self & minus_equals(const Generic_vector &vec)
Definition: m_vec_view.h:372
Generic_vector_view Self
Definition: m_vec_view.h:59
double dot_with_(const Generic_vector &vec)
Definition: m_vec_view.h:350
Self & operator=(const Value_type &s)
Definition: m_vec_view.h:159
Self & operator/=(Value_type s)
Definition: m_vec_view.h:229
Generic_vector_view(const Generic_vector_view &src)
Definition: m_vec_view.h:77
const Generic_vector_view< const Vector_type > Type
Definition: m_vec_view.h:399
double dot(const Self &s)
Definition: m_vec_view.h:205
bool operator==(const Vector_type &other)
Definition: m_vec_view.h:92
Self & operator+=(const Self &s)
Definition: m_vec_view.h:238
Self & operator=(const std::vector< float > &v)
Definition: m_vec_view.h:184
Self & operator*=(Value_type s)
Definition: m_vec_view.h:189
Definition: l_index.h:65
Value_type operator[](int i) const
Definition: m_vec_view.h:119
Self & operator-=(const Self &s)
Definition: m_vec_view.h:263
Value_type & operator[](int i)
Definition: m_vec_view.h:110
Definition: m_vec_view.h:56
Self & operator+=(const Vector_type &s)
Definition: m_vec_view.h:250
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
bool operator==(const Self &other)
Definition: m_vec_view.h:87
Index_range indices_
Definition: m_vec_view.h:389
Support for error handling exception classes in libKJB.
Self & operator=(const Self &vec)
Definition: m_vec_view.h:144
Definition: m_vec_view.h:397
Vector_type::value_type Value_type
Definition: m_vec_view.h:60
Vector_type & vec_
Definition: m_vec_view.h:390
int Size_type
Definition: m_vec_view.h:61
Self & assign(const Generic_vector &vec)
Definition: m_vec_view.h:307