Vector.h

Go to the documentation of this file.
00001 //Copyright (c) 2004-2005, Baris Sumengen
00002 //All rights reserved.
00003 //
00004 // CIMPL Matrix Performance Library
00005 //
00006 //Redistribution and use in source and binary
00007 //forms, with or without modification, are
00008 //permitted provided that the following
00009 //conditions are met:
00010 //
00011 //    * No commercial use is allowed. 
00012 //    This software can only be used
00013 //    for non-commercial purposes. This 
00014 //    distribution is mainly intended for
00015 //    academic research and teaching.
00016 //    * Redistributions of source code must
00017 //    retain the above copyright notice, this
00018 //    list of conditions and the following
00019 //    disclaimer.
00020 //    * Redistributions of binary form must
00021 //    mention the above copyright notice, this
00022 //    list of conditions and the following
00023 //    disclaimer in a clearly visible part 
00024 //    in associated product manual, 
00025 //    readme, and web site of the redistributed 
00026 //    software.
00027 //    * Redistributions in binary form must
00028 //    reproduce the above copyright notice,
00029 //    this list of conditions and the
00030 //    following disclaimer in the
00031 //    documentation and/or other materials
00032 //    provided with the distribution.
00033 //    * The name of Baris Sumengen may not be
00034 //    used to endorse or promote products
00035 //    derived from this software without
00036 //    specific prior written permission.
00037 //
00038 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
00039 //HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
00040 //EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
00041 //NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00042 //MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00043 //PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00044 //CONTRIBUTORS BE LIABLE FOR ANY
00045 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00046 //EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00047 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00048 //OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00049 //DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00050 //HOWEVER CAUSED AND ON ANY THEORY OF
00051 //LIABILITY, WHETHER IN CONTRACT, STRICT
00052 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00053 //OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00054 //OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00055 //POSSIBILITY OF SUCH DAMAGE.
00056 
00057 
00058 
00059 #pragma once
00060 #ifndef VECTOR_H
00061 #define VECTOR_H
00062 
00063 #include <iostream>
00064 
00065 using std::cout;
00066 using std::cerr;
00067 using std::endl;
00068 using std::ostream;
00069 using std::right;
00070 using std::fixed;
00071 
00072 #include <iomanip>
00073 
00074 using std::setw;
00075 
00076 #include <math.h>
00077 
00078 #include <typeinfo>
00079 
00080 #include "./Cleaner.h"
00081 #include "./Utility.h"
00082 #include "./Array.h"
00083 #include "./Matrix.h"
00084 #include "./RandomGen.h"
00085 
00086 
00087 
00088 
00089 // forward declaration
00090 template< class T > class Array;
00091 template< class T > class Matrix;
00092 template< class T > class Vector;
00093 template< class T, int l > class List;
00094 template< class T > class VarList;
00095 
00097 template< class T >
00098 class Vector
00099 {
00100     friend class Array<T>;
00101     friend class Matrix<T>;
00102 
00103 
00104 protected:
00105     T *data;
00106     int length;
00107     bool memoryManaged;
00108     Cleaner<T> *clean;   // Reference counting Garbage collector.
00109 
00110 
00111 public:
00112     Vector(void);
00113     explicit Vector(int l);
00114     Vector(int l, T init);
00115     Vector(T* _data, int l); // creates unmanaged memory
00116     Vector(Vector<T> &v);
00117     
00118     ~Vector(void);
00119     void Set(T* _data, const int l); // creates unmanaged memory
00120 
00121     void Clean();
00122 
00123     const T* DataPtr() const;
00124     T* Data();
00125 
00126     Vector<T> Clone() const;
00127     Vector<T> Slice(int start, int end);
00128 
00129     const bool IsMemoryManaged() const;
00130 
00131     const int Length() const;
00132     void Init(const T init);
00133     Vector<T>& Rand();
00134     Vector<T>& Rand(const double max);
00135     static Vector<T> Rand(const int l);
00136     static Vector<T> Rand(const int l, const double max);
00137 
00138     static T Inner(Vector<T>& m1, Vector<T>& m2);
00139 
00140     // Boolean Operations...
00141     static Vector<int> And(Vector<T>& m1, Vector<T>& m2);
00142     static Vector<int> Or(Vector<T>& m1, Vector<T>& m2);
00143     static Vector<int> Lt(Vector<T>& m1, Vector<T>& m2);
00144     static Vector<int> Gt(Vector<T>& m1, Vector<T>& m2);
00145     static Vector<int> Le(Vector<T>& m1, Vector<T>& m2);
00146     static Vector<int> Ge(Vector<T>& m1, Vector<T>& m2);
00147     static Vector<int> Eq(Vector<T>& m1, Vector<T>& m2);
00148     static Vector<int> Ne(Vector<T>& m1, Vector<T>& m2);
00149 
00150     // Boolean Operations with value type...
00151     static Vector<int> And(Vector<T>& m, T v);
00152     static Vector<int> Or(Vector<T>& m, T v);
00153     static Vector<int> Lt(Vector<T>& m, T v);
00154     static Vector<int> Gt(Vector<T>& m, T v);
00155     static Vector<int> Le(Vector<T>& m, T v);
00156     static Vector<int> Ge(Vector<T>& m, T v);
00157     static Vector<int> Eq(Vector<T>& m, T v);
00158     static Vector<int> Ne(Vector<T>& m, T v);
00159 
00160 
00161 
00162 // Add vector to another vector
00163     static Vector<T> Add(Vector<T>& m1, Vector<T>& m2);
00164     static Vector<T> Subtract(Vector<T>& m1, Vector<T>& m2);
00165     static Vector<T> Multiply(Vector<T>& m1, Vector<T>& m2);
00166     static Vector<T> Divide(Vector<T>& m1, Vector<T>& m2);
00167 
00168 // Add value to another vector
00169     static Vector<T> Add(Vector<T>& m1, T v2);
00170     static Vector<T> Subtract(Vector<T>& m1, T v2);
00171     static Vector<T> Subtract(T v2, Vector<T>& m1);
00172     static Vector<T> Multiply(Vector<T>& m1, T v2);
00173     static Vector<T> Divide(Vector<T>& m1, T v2);
00174     static Vector<T> Divide(T v2, Vector<T>& m1);
00175 
00176 
00177 // Add vector to "this" vector
00178     Vector<T>& Add(Vector<T>& m);
00179     Vector<T>& Subtract(Vector<T>& m);
00180     Vector<T>& Multiply(Vector<T>& m);
00181     Vector<T>& Divide(Vector<T>& m);
00182 
00183 // Add value to "this" vector
00184     Vector<T>& Add(T v);
00185     Vector<T>& Subtract(T v);
00186     Vector<T>& Multiply(T v);
00187     Vector<T>& Divide(T v);
00188 
00189 
00190 //OPERATORS
00191     Vector<T>& operator= (Vector<T>& m);
00192     Vector<T>& operator= (Matrix<T>& m);
00193     Vector<T>& operator= (Array<T>& m);
00194 
00195     Vector<T> operator+ ();
00196     Vector<T> operator- ();
00197     Vector<int> operator! ();
00198 
00199     friend ostream& operator<< (ostream& output, const Vector<T>& v)
00200     {
00201         int lm = v.Length();
00202         int rowNoWidth = (int)log10((double)(lm-1))+2;
00203         int maxLength  = 1;
00204         for(int i=0; i<v.Length(); i++)
00205         {
00206             ostringstream oS;
00207             oS << v.data[i];
00208             int l = (int)oS.str().length();
00209             if(l>maxLength)
00210             {
00211                 maxLength = l;
00212             }
00213         }
00214 
00215         output << typeid(v).name() << " of size " << v.length << endl;
00216         output << "----------------------" << endl;
00217         for(int i=0;i<v.length;i++)
00218         {
00219             output << "ROW" << setw(rowNoWidth) << i+1 << "|" << right << setw(maxLength+3) << v.data[i] << " |" << endl;
00220         }
00221         output << "----------------------" << endl;
00222         output << endl;
00223         return output;
00224     }
00225     
00226 
00227     T& operator() (const int i);
00228     T& operator[] (const int i); // no bounds check
00229         
00230     // slice x slice: returns a vector.
00231     Vector<T> operator() (int start, int end);
00232     
00233     T& Elem(const int i);
00234     T& ElemNC(const int i); // No bounds check
00235     
00236     
00237     // Vector to Vector inner product
00238     friend T operator& (Vector<T>& m1, Vector<T>& m2)
00239     {
00240         return Vector<T>::Inner(m1, m2);
00241     }
00242     
00243     
00244     // Boolean operations
00245     friend Vector<int> operator&& (Vector<T>& m1, Vector<T>& m2)
00246     {
00247         return Vector<T>::And(m1, m2);
00248     }
00249 
00250     friend Vector<int> operator|| (Vector<T>& m1, Vector<T>& m2)
00251     {
00252         return Vector<T>::Or(m1, m2);
00253     }
00254 
00255     friend Vector<int> operator< (Vector<T>& m1, Vector<T>& m2)
00256     {
00257         return Vector<T>::Lt(m1, m2);
00258     }
00259 
00260     friend Vector<int> operator> (Vector<T>& m1, Vector<T>& m2)
00261     {
00262         return Vector<T>::Gt(m1, m2);
00263     }
00264 
00265     friend Vector<int> operator<= (Vector<T>& m1, Vector<T>& m2)
00266     {
00267         return Vector<T>::Le(m1, m2);
00268     }
00269 
00270     friend Vector<int> operator>= (Vector<T>& m1, Vector<T>& m2)
00271     {
00272         return Vector<T>::Ge(m1, m2);
00273     }
00274 
00275     friend Vector<int> operator== (Vector<T>& m1, Vector<T>& m2)
00276     {
00277         return Vector<T>::Eq(m1, m2);
00278     }
00279 
00280     friend Vector<int> operator!= (Vector<T>& m1, Vector<T>& m2)
00281     {
00282         return Vector<T>::Ne(m1, m2);
00283     }
00284 
00285 
00286     // Boolean operations with value type
00287     friend Vector<int> operator&& (Vector<T>& m, T v)
00288     {
00289         return Vector<T>::And(m, v);
00290     }
00291     friend Vector<int> operator&& (T v, Vector<T>& m)
00292     {
00293         return Vector<T>::And(m, v);
00294     }
00295 
00296     friend Vector<int> operator|| (Vector<T>& m, T v)
00297     {
00298         return Vector<T>::Or(m, v);
00299     }
00300     friend Vector<int> operator|| (T v, Vector<T>& m)
00301     {
00302         return Vector<T>::Or(m, v);
00303     }
00304 
00305     friend Vector<int> operator< (Vector<T>& m, T v)
00306     {
00307         return Vector<T>::Lt(m, v);
00308     }
00309     friend Vector<int> operator< (T v, Vector<T>& m)
00310     {
00311         return Vector<T>::Gt(m, v);
00312     }
00313 
00314     friend Vector<int> operator> (Vector<T>& m, T v)
00315     {
00316         return Vector<T>::Gt(m, v);
00317     }
00318     friend Vector<int> operator> (T v, Vector<T>& m)
00319     {
00320         return Vector<T>::Lt(m, v);
00321     }
00322 
00323     friend Vector<int> operator<= (Vector<T>& m, T v)
00324     {
00325         return Vector<T>::Le(m, v);
00326     }
00327     friend Vector<int> operator<= (T v, Vector<T>& m)
00328     {
00329         return Vector<T>::Ge(m, v);
00330     }
00331 
00332     friend Vector<int> operator>= (Vector<T>& m, T v)
00333     {
00334         return Vector<T>::Ge(m, v);
00335     }
00336     friend Vector<int> operator>= (T v, Vector<T>& m)
00337     {
00338         return Vector<T>::Le(m, v);
00339     }
00340 
00341     friend Vector<int> operator== (Vector<T>& m, T v)
00342     {
00343         return Vector<T>::Eq(m, v);
00344     }
00345     friend Vector<int> operator== (T v, Vector<T>& m)
00346     {
00347         return Vector<T>::Eq(m, v);
00348     }
00349 
00350     friend Vector<int> operator!= (Vector<T>& m, T v)
00351     {
00352         return Vector<T>::Ne(m, v);
00353     }
00354     friend Vector<int> operator!= (T v, Vector<T>& m)
00355     {
00356         return Vector<T>::Ne(m, v);
00357     }
00358 
00359 
00360 
00361 // Add Vector to another Vector
00362     friend Vector<T> operator+ (Vector<T>& m1, Vector<T>& m2)
00363     {
00364         return Vector<T>::Add(m1, m2);
00365     }
00366 
00367     friend Vector<T> operator- (Vector<T>& m1, Vector<T>& m2)
00368     {
00369         return Vector<T>::Subtract(m1, m2);
00370     }
00371 
00372     friend Vector<T> operator* (Vector<T>& m1, Vector<T>& m2)
00373     {
00374         return Vector<T>::Multiply(m1, m2);
00375     }
00376 
00377     friend Vector<T> operator/ (Vector<T>& m1, Vector<T>& m2)
00378     {
00379         return Vector<T>::Divide(m1, m2);
00380     }
00381 
00382 
00383 // Add value to another matrix
00384     friend Vector<T> operator+ (Vector<T>& m, T v)
00385     {
00386         return Vector<T>::Add(m, v);
00387     }
00388 
00389     friend Vector<T> operator+ (T v, Vector<T>& m)
00390     {
00391         return Vector<T>::Add(m, v);
00392     }
00393 
00394     friend Vector<T> operator- (Vector<T>& m, T v)
00395     {
00396         return Vector<T>::Subtract(m, v);
00397     }
00398 
00399     friend Vector<T> operator- (T v, Vector<T>& m)
00400     {
00401         return Vector<T>::Subtract(v, m);
00402     }
00403 
00404     friend Vector<T> operator* (Vector<T>& m, T v)
00405     {
00406         return Vector<T>::Multiply(m, v);
00407     }
00408 
00409     friend Vector<T> operator* (T v, Vector<T>& m)
00410     {
00411         return Vector<T>::Multiply(m, v);
00412     }
00413 
00414     friend Vector<T> operator/ (Vector<T>& m, T v)
00415     {
00416         return Vector<T>::Divide(m, v);
00417     }
00418 
00419     friend Vector<T> operator/ (T v, Vector<T>& m)
00420     {
00421         return Vector<T>::Divide(v, m);
00422     }
00423 
00424 
00425 
00426 
00427 // Add inline
00428     Vector<T>& operator+= (Vector<T>& m);
00429     Vector<T>& operator-= (Vector<T>& m);
00430     Vector<T>& operator*= (Vector<T>& m);
00431     Vector<T>& operator/= (Vector<T>& m);
00432 
00433     Vector<T>& operator+= (T v);
00434     Vector<T>& operator-= (T v);
00435     Vector<T>& operator*= (T v);
00436     Vector<T>& operator/= (T v);
00437 
00438     
00439 // TYPE CONVERSIONS
00440     Vector(Array<T> &m);
00441     Vector(Matrix<T> &m);
00442 
00443 };
00444 
00445 
00446 
00447 
00448 #include "./Vector.inl"
00449 
00450 
00451 
00452 
00453 
00454 
00455 #endif
00456 
00457 
00458 
00459 
00460 
00461 
00462 

CIMPL 0.1 Code Reference. Copyright © 2004, Baris Sumengen. All rights reserved.