1 /*
2   Copyright 2009 Andreas Biegert
3 
4   This file is part of the CS-BLAST package.
5 
6   The CS-BLAST package is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10 
11   The CS-BLAST package is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15 
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef CS_VECTOR_H_
21 #define CS_VECTOR_H_
22 
23 template <class T>
24 class Vector {
25  public:
26   typedef T value_type;
27 
28   Vector();
29   explicit Vector(size_t n);
30   Vector(size_t n, const T &a);
31   Vector(size_t n, const T *a);
32   Vector(const Vector &rhs);
33   ~Vector();
34 
35   Vector & operator=(const Vector &rhs);
36 
37   inline T & operator[](const size_t i);
38   inline const T & operator[](const size_t i) const;
39   inline size_t size() const;
40   void Resize(size_t newn);
41   void Assign(size_t newn, const T &a);
42   inline void Assign(const T &a);
43 
44  private:
45   size_t nn;
46   T *v;
47 };
48 
49 // Vector definitions
50 template <class T>
Vector()51 Vector<T>::Vector() : nn(0), v(NULL) {}
52 
53 template <class T>
Vector(size_t n)54 Vector<T>::Vector(size_t n) : nn(n), v(n>0 ? new T[n] : NULL) {}
55 
56 template <class T>
Vector(size_t n,const T & a)57 Vector<T>::Vector(size_t n, const T& a) : nn(n), v(n>0 ? new T[n] : NULL) {
58   for(size_t i=0; i<n; i++) v[i] = a;
59 }
60 
61 template <class T>
Vector(size_t n,const T * a)62 Vector<T>::Vector(size_t n, const T *a) : nn(n), v(n>0 ? new T[n] : NULL) {
63   for(size_t i=0; i<n; i++) v[i] = *a++;
64 }
65 
66 template <class T>
Vector(const Vector<T> & rhs)67 Vector<T>::Vector(const Vector<T> &rhs) : nn(rhs.nn), v(nn>0 ? new T[nn] : NULL) {
68   for(size_t i=0; i<nn; i++) v[i] = rhs[i];
69 }
70 
71 template <class T>
72 Vector<T> & Vector<T>::operator=(const Vector<T> &rhs) {
73   if (this != &rhs) {
74     if (nn != rhs.nn) {
75       if (v != NULL) delete [] (v);
76       nn=rhs.nn;
77       v= nn>0 ? new T[nn] : NULL;
78     }
79     for (size_t i=0; i<nn; i++)
80       v[i]=rhs[i];
81   }
82   return *this;
83 }
84 
85 template <class T>
86 inline T & Vector<T>::operator[](const size_t i) {
87 #ifdef CHECKBOUNDS
88   if (i<0 || i>=nn) {
89     throw Exception("Vector subscript out of bounds");
90   }
91 #endif
92   return v[i];
93 }
94 
95 template <class T>
96 inline const T & Vector<T>::operator[](const size_t i) const {
97 #ifdef CHECKBOUNDS
98   if (i<0 || i>=nn) {
99     throw Exception("Vector subscript out of bounds");
100   }
101 #endif
102   return v[i];
103 }
104 
105 template <class T>
size()106 inline size_t Vector<T>::size() const {
107   return nn;
108 }
109 
110 template <class T>
Resize(size_t newn)111 void Vector<T>::Resize(size_t newn) {
112   if (newn != nn) {
113     if (v != NULL) delete[] (v);
114     nn = newn;
115     v = nn > 0 ? new T[nn] : NULL;
116   }
117 }
118 
119 template <class T>
Assign(size_t newn,const T & a)120 void Vector<T>::Assign(size_t newn, const T& a) {
121   if (newn != nn) {
122     if (v != NULL) delete[] (v);
123     nn = newn;
124     v = nn > 0 ? new T[nn] : NULL;
125   }
126   for (size_t i=0;i<nn;i++) v[i] = a;
127 }
128 
129 template <class T>
Assign(const T & a)130 inline void Vector<T>::Assign(const T& a) {
131   Assign(nn, a);
132 }
133 
134 template <class T>
~Vector()135 Vector<T>::~Vector() {
136   if (v != NULL) delete[] (v);
137 }
138 
139 // Assigns given constant value or default to all entries in vector
140 template<class T>
141 inline void Assign(Vector<T>& v, T val = T()) {
142   for (size_t i = 0; i < v.size(); ++i) v[i] = val;
143 }
144 
145 #endif  // CS_VECTOR_H_
146