tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
vector.h
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2010 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #ifndef TENSOR_VECTOR_H
21 #define TENSOR_VECTOR_H
22 
23 #include <tensor/refcount.h>
24 
25 namespace tensor {
26 
28 // VECTOR CLASS
29 //
30 
31 typedef long index;
32 
33 template<typename elt>
34 class Vector {
35  public:
36  typedef tensor::index index;
37  typedef elt elt_t;
38  typedef elt_t *iterator;
39  typedef const elt_t *const_iterator;
40 
41  Vector() : data_() {}
42 
43  explicit Vector(index size) : data_(size) {}
44 
45  /* Copy constructor and copy operator */
46  Vector(const Vector<elt_t> &v) : data_(v.data_) {}
47  Vector &operator=(const Vector<elt_t> &v) { data_ = v.data_; return *this; }
48 
49  /* Create a vector that references data we do not own (own=false in the
50  RefPointer constructor. */
51  Vector(index size, elt_t *data) : data_(data, size, false) {}
52 
53  index size() const {
54  return data_.size();
55  }
56  void resize(index new_size) {
57  data_.reallocate(new_size);
58  }
59 
60  const elt_t &operator[](index pos) const {
61  return *(data_.begin_const() + pos);
62  }
63  elt_t &at(index pos) {
64  return *(data_.begin() + pos);
65  }
66 
67  iterator begin() { return data_.begin(); }
68  const_iterator begin() const { return data_.begin_const(); }
69  const_iterator begin_const() const { return data_.begin_const(); }
70  const_iterator end_const() const { return data_.end_const(); }
71  const_iterator end() const { return data_.end_const(); }
72  iterator end() { return data_.end(); }
73 
74  // Only for testing purposes
75  int ref_count() const { return data_.ref_count(); }
76 
77  private:
78  RefPointer<elt_t> data_;
79 };
80 
81 template<typename t1, typename t2>
82 bool operator==(const Vector<t1> &v1, const Vector<t2> &v2) {
83  return (v1.size() == v2.size()) && std::equal(v1.begin(), v1.end(), v2.begin());
84 }
85 
86 }; // namespace
87 
88 #endif // !TENSOR_VECTOR_H