Vector.h

Go to the documentation of this file.
00001 /************************************************************************/
00002 /*                                                                      */
00003 /*   V E C T O R   C O N T A I N E R                                    */
00004 /*                                                                      */
00005 /*   Roberto Lopez                                                      */
00006 /*   International Center for Numerical Methods in Engineering (CIMNE)  */
00007 /*   Technical University of Catalonia (UPC)                            */
00008 /*   Barcelona, Spain                                                   */
00009 /*   E-mail: rlopez@cimne.upc.edu                                       */
00010 /*                                                                      */
00011 /************************************************************************/
00012 
00013 #ifndef __VECTOR_H__
00014 #define __VECTOR_H__
00015 
00016 namespace Purple
00017 {
00018 
00019 /// This template class defines a vector for general purpose use.
00020 ///
00021 /// @see Matrix.
00022 
00023 template <class Type>
00024 class Vector
00025 {
00026 
00027 private:
00028 
00029    /// Size of vector.
00030 
00031    int size;
00032 
00033    /// Pointer to a Type.
00034 
00035    Type* vector;
00036 
00037 public:
00038 
00039    // CONSTRUCTORS
00040 
00041    Vector();
00042 
00043    Vector(int);
00044 
00045    Vector(int, const Type&);
00046 
00047    Vector(int, const Type*);
00048 
00049    Vector(const Vector&);
00050 
00051 
00052    // ASSINGMENT OPERATOR
00053 
00054    Vector& operator=(const Vector&);
00055 
00056 
00057    // REFERENCE OPERATORS
00058 
00059    inline Type& operator[](const int);
00060 
00061    inline const Type& operator[](const int) const;
00062 
00063 
00064    // METHODS
00065 
00066    inline int getSize();
00067 
00068    inline Type* begin();
00069    inline Type* end();
00070 
00071 
00072    // DESTRUCTOR
00073 
00074    ~Vector();
00075 };
00076 
00077 
00078 // CONSTRUCTORS
00079 
00080 
00081 /// Default constructor. It creates a vector of size zero.
00082 
00083 template <typename Type>
00084 Vector<Type>::Vector() : size(0), vector(0)
00085 {
00086 
00087 }
00088 
00089 
00090 /// Constructor. It creates a vector of size n, containing n copies of 
00091 /// the default value for Type.
00092 ///
00093 /// @param newSize: Size of Vector.
00094 
00095 template <typename Type>
00096 Vector<Type>::Vector(int newSize) : size(newSize), vector(new Type[newSize])
00097 {
00098 
00099 }
00100 
00101 
00102 /// Constructor. It creates a vector of size n, containing n copies of 
00103 /// the type value of Type. 
00104 ///
00105 /// @param newSize: Size of Vector.
00106 /// @param type: Value of Type.
00107 
00108 template <typename Type>
00109 Vector<Type>::Vector(int newSize, const Type& type) : size(newSize), vector(new Type[newSize])
00110 {
00111    for(int i = 0; i < newSize; i++)
00112    {
00113       vector[i] = type;
00114    }
00115 }
00116 
00117 
00118 /// Constructor. It creates a vector of size n, containing n copies of 
00119 /// the type value of Type. 
00120 ///
00121 /// @param newSize: Size of Vector.
00122 /// @param type: Value of Type.
00123 
00124 template <typename Type>
00125 Vector<Type>::Vector(int newSize, const Type* type) : size(newSize), vector(new Type[newSize])
00126 {
00127    for(int i = 0; i < newSize; i++)
00128    {
00129       vector[i] = *type++;
00130    }
00131 }
00132 
00133 
00134 /// Copy constructor. It creates a copy of an existing Vector. 
00135 ///
00136 /// @param oldVector: Vector to be copied.
00137 
00138 template <typename Type>
00139 Vector<Type>::Vector(const Vector<Type>& oldVector) : size(oldVector.size), vector(new Type[size])
00140 {
00141    for(int i = 0; i < size; i++)
00142    {
00143       vector[i] = oldVector[i];
00144    }
00145 }
00146 
00147 
00148 // ASSIGNMENT OPERATORS
00149 
00150 /// Assignment operator. It assigns to self a copy of an existing Vector.
00151 ///
00152 /// @param oldVector: Vector to be assigned.
00153 
00154 template <typename Type>
00155 Vector<Type>& Vector<Type>::operator=(const Vector<Type>& oldVector)
00156 {
00157    if (this != &oldVector)
00158    {
00159       if (size != oldVector.size)
00160       {
00161          if (vector != 0)
00162          {
00163             delete [] (vector);
00164          }
00165 
00166          size = oldVector.size;
00167 
00168          vector= new Type[size];
00169       }
00170 
00171       for (int i = 0; i < size; i++)
00172       {
00173          vector[i] = oldVector[i];
00174       }
00175    }
00176 
00177    return(*this);
00178 }
00179 
00180 
00181 // REFERENCE OPERATORS
00182 
00183 /// Reference operator. 
00184 
00185 template <typename Type>
00186 inline Type& Vector<Type>::operator[](const int i) 
00187 {
00188    return(vector[i]);
00189 }
00190 
00191 /// Reference operator. 
00192 
00193 template <typename Type>
00194 inline const Type& Vector<Type>::operator[](const int i) const 
00195 {
00196    return(vector[i]);
00197 }
00198 
00199 
00200 // METHODS
00201 
00202 // int getSize(void) method
00203 
00204 /// This method returns the number of elements in the vector. 
00205 
00206 template <typename Type>
00207 inline int Vector<Type>::getSize()
00208 {
00209    return(size);
00210 }
00211 
00212 
00213 // Type* begin(void) method
00214 
00215 /// This method returns a pointer to the first element in the container.
00216 
00217 template <typename Type>
00218 inline Type* Vector<Type>::begin()
00219 {
00220    return(vector);
00221 }
00222 
00223 
00224 // Type* end(void) method
00225 
00226 /// This method returns a pointer to the last element in the container. 
00227 
00228 template <typename Type>
00229 inline Type* Vector<Type>::end()
00230 {
00231    return(vector + size);
00232 }
00233 
00234 
00235 // DESTRUCTOR
00236 
00237 /// Destructor. 
00238 
00239 template <typename Type>
00240 Vector<Type>::~Vector()
00241 {
00242    if (vector != 0)
00243    {
00244       delete[] (vector);
00245    }
00246 }
00247 
00248 }
00249 
00250 #endif
00251 
00252 
00253 // Purple: An Open Source Numerical Optimization C++ Library.
00254 // Copyright (C) 2006 Roberto Lopez 
00255 //
00256 // This library is free software; you can redistribute it and/or
00257 // modify it under the terms of the GNU Lesser General Public
00258 // License as published by the Free Software Foundation; either
00259 // version 2.1 of the License, or any later version.
00260 //
00261 // This library is distributed in the hope that it will be useful,
00262 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00263 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00264 // Lesser General Public License for more details.
00265 
00266 // You should have received a copy of the GNU Lesser General Public
00267 // License along with this library; if not, write to the Free Software
00268 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

Generated on Wed Jun 21 13:10:38 2006 for Purple by  doxygen 1.4.7