Vector.inl

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 
00060 
00061 template< class T >
00062 Vector<T>::Vector()
00063     : length(0)
00064 {
00065     data = 0;
00066     clean = 0;
00067     memoryManaged = false;
00068 }
00069 
00070 
00071 template< class T >
00072 Vector<T>::Vector(const int l)
00073 {
00074     if(l < 1)
00075     {
00076         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00077         Utility::RunTimeError("Vector length should be larger than 0!");
00078     }
00079 
00080     length = l;
00081     data = new T[length];
00082     Utility::CheckPointer(data);
00083 
00084     clean = new Cleaner<T>(data);
00085     memoryManaged = true;
00086 
00087 }
00088 
00089 template< class T >
00090 Vector<T>::Vector(const int l, T init)
00091 {
00092     if(l < 1)
00093     {
00094         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00095         Utility::RunTimeError("Vector length should be larger than 0!");
00096     }
00097 
00098     length = l;
00099     data = new T[length];
00100     Utility::CheckPointer(data);
00101 
00102     clean = new Cleaner<T>(data);
00103     memoryManaged = true;
00104     for(int i=0;i<length;i++)
00105     {
00106         data[i] = init;
00107     }
00108 
00109 }
00110 
00111 
00112 template< class T >
00113 Vector<T>::Vector(T* _data, const int l)
00114 {
00115     if(l < 1)
00116     {
00117         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00118         Utility::RunTimeError("Vector length should be larger than 0!");
00119     }
00120 
00121     length = l;
00122     data = _data;
00123     clean = 0;
00124     memoryManaged = false;
00125 
00126 }
00127 
00128 
00129 
00130 template< class T >
00131 Vector<T>::Vector(Vector<T> &m) 
00132 {
00133     length = m.length;
00134 
00135     data = m.data;
00136 
00137     if(m.memoryManaged == true)
00138     {
00139         clean = new Cleaner<T>(data);
00140         memoryManaged = true;
00141     }
00142     else
00143     {
00144         Utility::RunTimeError("Using copy constructor from an unmanaged vector is not allowed!\nUse Clone() instead.");
00145     }
00146 
00147 }
00148 
00149 
00150 
00151 template< class T >
00152 Vector<T>::~Vector(void)
00153 {
00154 
00155     if(clean != 0 && memoryManaged == true)
00156     {
00157         delete clean;
00158     }
00159 }
00160 
00161 
00162 
00163 template< class T >
00164 void Vector<T>::Set(T* _data, const int l)
00165 {
00166     if(l < 1)
00167     {
00168         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00169         Utility::RunTimeError("Vector length should be larger than 0!");
00170     }
00171 
00172     length = l;
00173     data = _data;
00174     delete clean;
00175     clean = 0;
00176     memoryManaged = false;
00177 
00178 }
00179 
00180 
00181 template< class T >
00182 void Vector<T>::Clean()
00183 {
00184     if(clean != 0 && memoryManaged == true)
00185     {
00186         delete clean;
00187         data = 0;
00188         clean = 0;
00189     }
00190 }
00191 
00192 
00193 
00195 template< class T >
00196 inline const T* Vector<T>::DataPtr() const
00197 {
00198     return data;
00199 }
00200 
00202 template< class T >
00203 inline T* Vector<T>::Data()
00204 {
00205     return data;
00206 }
00207 
00208 
00209 
00210 template< class T >
00211 Vector<T> Vector<T>::Clone() const
00212 {
00213     Vector<T> temp(length);
00214     
00215     //memcopy here...
00216     for(int j=0;j<temp.length;j++)
00217     {
00218         temp.data[j] = data[j];
00219     }
00220 
00221     return temp;
00222 }
00223 
00224 
00225 template< class T >
00226 Vector<T> Vector<T>::Slice(int start, int end)
00227 {
00228     if(start<0 || start>=length || end<0 || end>=length)
00229     {
00230         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00231         Utility::RunTimeError("Index outside bounds!");
00232     }
00233     if(start > end)
00234     {
00235         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00236         Utility::RunTimeError("Second slice parameter cannot be less than the first parameter!");
00237     }
00238 
00239 
00240     Vector<T> temp(&(data[start]), end-start+1);
00241     return temp;
00242 }
00243 
00244 
00245 
00246 template< class T >
00247 inline const bool Vector<T>::IsMemoryManaged() const
00248 {
00249     return memoryManaged;
00250 }
00251 
00252 
00253 
00254 template< class T >
00255 inline const int Vector<T>::Length() const
00256 {
00257     return length;
00258 }
00259 
00260 
00261 
00262 
00263 template< class T >
00264 inline void Vector<T>::Init(const T init)
00265 {
00266     for(int i=0;i<length;i++)
00267     {
00268         data[i] = init;
00269     }
00270 }
00271 
00272 template< class T >
00273 inline Vector<T>& Vector<T>::Rand()
00274 {
00275     if(!RandomGen::Initialized())
00276     {
00277         RandomGen::Initialize();
00278     }
00279     for(int i=0;i<length;i++)
00280     {
00281         data[i] = (T)rand();
00282     }
00283     return *this;
00284 }
00285 
00286 
00287 template< class T >
00288 inline Vector<T>& Vector<T>::Rand(const double max)
00289 {
00290     if(!RandomGen::Initialized())
00291     {
00292         RandomGen::Initialize();
00293     }
00294     for(int i=0;i<length;i++)
00295     {
00296         data[i] = (T)(rand()/(double)RAND_MAX*max);
00297     }
00298     return *this;
00299 }
00300 
00301 
00302 template< class T >
00303 Vector<T> Vector<T>::Rand(const int l)
00304 {
00305     Vector<T> m(l);
00306     if(!RandomGen::Initialized())
00307     {
00308         RandomGen::Initialize();
00309     }
00310     for(int i=0;i<l;i++)
00311     {
00312         m.data[i] = (T)rand();
00313     }
00314     return m;
00315 }
00316 
00317 
00318 template< class T >
00319 Vector<T> Vector<T>::Rand(const int l, const double max)
00320 {
00321     Vector<T> m(l);
00322     if(!RandomGen::Initialized())
00323     {
00324         RandomGen::Initialize();
00325     }
00326     for(int i=0;i<l;i++)
00327     {
00328         m.data[i] = (T)(rand()/(double)RAND_MAX*max);
00329     }
00330     return m;
00331 }
00332 
00333 
00334 
00335 
00336 
00337 
00338 template< class T >
00339 T Vector<T>::Inner(Vector<T>& m1, Vector<T>& m2)
00340 {
00341     if(m1.length != m2.length)
00342     {
00343         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00344         Utility::RunTimeError("Vector lengths are not the same!");
00345     }
00346 
00347     T inner = 0;
00348     for(int i=0; i<m1.length; i++)
00349     {
00350         inner += m1.data[i]*m2.data[i];
00351     }
00352     
00353     return inner;
00354 }
00355 
00356 
00357 
00358 
00359 
00360 
00361 
00362 
00363 
00364 
00365 
00366 
00367 
00368 // Boolean Operations...
00369 template< class T >
00370 Vector<int> Vector<T>::And(Vector<T>& m1, Vector<T>& m2)
00371 {
00372     if(m1.length != m2.length)
00373     {
00374         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00375         Utility::RunTimeError("Vector lengths are not the same!");
00376     }
00377 
00378     Vector<int> temp(m1.length);
00379     for(int i=0;i<temp.length;i++)
00380     {
00381         temp[i] = (m1.data[i] != 0 && m2.data[i] != 0) ? 1 : 0;
00382     }
00383     
00384     return temp;
00385 }
00386 
00387 template< class T >
00388 Vector<int> Vector<T>::Or(Vector<T>& m1, Vector<T>& m2)
00389 {
00390     if(m1.length != m2.length)
00391     {
00392         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00393         Utility::RunTimeError("Vector lengths are not the same!");
00394     }
00395 
00396     Vector<int> temp(m1.length);
00397     for(int i=0;i<temp.length;i++)
00398     {
00399         temp[i] = (m1.data[i] == 0 && m2.data[i] == 0) ? 0 : 1;
00400     }
00401     
00402     return temp;
00403 }
00404 
00405 template< class T >
00406 Vector<int> Vector<T>::Lt(Vector<T>& m1, Vector<T>& m2)
00407 {
00408     if(m1.length != m2.length)
00409     {
00410         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00411         Utility::RunTimeError("Vector lengths are not the same!");
00412     }
00413 
00414     Vector<int> temp(m1.length);
00415     for(int i=0;i<temp.length;i++)
00416     {
00417         temp[i] = (m1.data[i] < m2.data[i]) ? 1 : 0;
00418     }
00419     
00420     return temp;
00421 }
00422 
00423 template< class T >
00424 Vector<int> Vector<T>::Gt(Vector<T>& m1, Vector<T>& m2)
00425 {
00426     if(m1.length != m2.length)
00427     {
00428         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00429         Utility::RunTimeError("Vector lengths are not the same!");
00430     }
00431 
00432     Vector<int> temp(m1.length);
00433     for(int i=0;i<temp.length;i++)
00434     {
00435         temp[i] = (m1.data[i] > m2.data[i]) ? 1 : 0;
00436     }
00437     
00438     return temp;
00439 }
00440 
00441 template< class T >
00442 Vector<int> Vector<T>::Le(Vector<T>& m1, Vector<T>& m2)
00443 {
00444     if(m1.length != m2.length)
00445     {
00446         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00447         Utility::RunTimeError("Vector lengths are not the same!");
00448     }
00449 
00450     Vector<int> temp(m1.length);
00451     for(int i=0;i<temp.length;i++)
00452     {
00453         temp[i] = (m1.data[i] <= m2.data[i]) ? 1 : 0;
00454     }
00455     
00456     return temp;
00457 }
00458 
00459 template< class T >
00460 Vector<int> Vector<T>::Ge(Vector<T>& m1, Vector<T>& m2)
00461 {
00462     if(m1.length != m2.length)
00463     {
00464         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00465         Utility::RunTimeError("Vector lengths are not the same!");
00466     }
00467 
00468     Vector<int> temp(m1.length);
00469     for(int i=0;i<temp.length;i++)
00470     {
00471         temp[i] = (m1.data[i] >= m2.data[i]) ? 1 : 0;
00472     }
00473     
00474     return temp;
00475 }
00476 
00477 template< class T >
00478 Vector<int> Vector<T>::Eq(Vector<T>& m1, Vector<T>& m2)
00479 {
00480     if(m1.length != m2.length)
00481     {
00482         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00483         Utility::RunTimeError("Vector lengths are not the same!");
00484     }
00485 
00486     Vector<int> temp(m1.length);
00487     for(int i=0;i<temp.length;i++)
00488     {
00489         temp[i] = (m1.data[i] == m2.data[i]) ? 1 : 0;
00490     }
00491     
00492     return temp;
00493 }
00494 
00495 template< class T >
00496 Vector<int> Vector<T>::Ne(Vector<T>& m1, Vector<T>& m2)
00497 {
00498     if(m1.length != m2.length)
00499     {
00500         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00501         Utility::RunTimeError("Vector lengths are not the same!");
00502     }
00503 
00504     Vector<int> temp(m1.length);
00505     for(int i=0;i<temp.length;i++)
00506     {
00507         temp[i] = (m1.data[i] != m2.data[i]) ? 1 : 0;
00508     }
00509     
00510     return temp;
00511 }
00512 
00513 
00514 
00515 
00516 
00517 // Boolean Operations with value types...
00518 template< class T >
00519 Vector<int> Vector<T>::And(Vector<T>& m1, T v)
00520 {
00521     Vector<int> temp(m1.length);
00522     for(int i=0;i<temp.length;i++)
00523     {
00524         temp[i] = (m1.data[i] != 0 && v != 0) ? 1 : 0;
00525     }
00526     
00527     return temp;
00528 }
00529 
00530 
00531 template< class T >
00532 Vector<int> Vector<T>::Or(Vector<T>& m1, T v)
00533 {
00534     Vector<int> temp(m1.length);
00535     for(int i=0;i<temp.length;i++)
00536     {
00537         temp[i] = (m1.data[i] == 0 && v == 0) ? 0 : 1;
00538     }
00539     
00540     return temp;
00541 }
00542 
00543 
00544 template< class T >
00545 Vector<int> Vector<T>::Lt(Vector<T>& m1, T v)
00546 {
00547     Vector<int> temp(m1.length);
00548     for(int i=0;i<temp.length;i++)
00549     {
00550         temp[i] = (m1.data[i] < v) ? 1 : 0;
00551     }
00552     
00553     return temp;
00554 }
00555 
00556 
00557 template< class T >
00558 Vector<int> Vector<T>::Gt(Vector<T>& m1, T v)
00559 {
00560     Vector<int> temp(m1.length);
00561     for(int i=0;i<temp.length;i++)
00562     {
00563         temp[i] = (m1.data[i] > v) ? 1 : 0;
00564     }
00565     
00566     return temp;
00567 }
00568 
00569 
00570 template< class T >
00571 Vector<int> Vector<T>::Le(Vector<T>& m1, T v)
00572 {
00573     Vector<int> temp(m1.length);
00574     for(int i=0;i<temp.length;i++)
00575     {
00576         temp[i] = (m1.data[i] <= v) ? 1 : 0;
00577     }
00578     
00579     return temp;
00580 }
00581 
00582 
00583 template< class T >
00584 Vector<int> Vector<T>::Ge(Vector<T>& m1, T v)
00585 {
00586     Vector<int> temp(m1.length);
00587     for(int i=0;i<temp.length;i++)
00588     {
00589         temp[i] = (m1.data[i] >= v) ? 1 : 0;
00590     }
00591     
00592     return temp;
00593 }
00594 
00595 
00596 template< class T >
00597 Vector<int> Vector<T>::Eq(Vector<T>& m1, T v)
00598 {
00599     Vector<int> temp(m1.length);
00600     for(int i=0;i<temp.length;i++)
00601     {
00602         temp[i] = (m1.data[i] == v) ? 1 : 0;
00603     }
00604     
00605     return temp;
00606 }
00607 
00608 
00609 template< class T >
00610 Vector<int> Vector<T>::Ne(Vector<T>& m1, T v)
00611 {
00612     Vector<int> temp(m1.length);
00613     for(int i=0;i<temp.length;i++)
00614     {
00615         temp[i] = (m1.data[i] != v) ? 1 : 0;
00616     }
00617     
00618     return temp;
00619 }
00620 
00621 
00622 
00623 
00624 
00625 
00626 
00627 
00628 
00629 
00630 
00631 
00632 
00633 
00634 
00635 
00636 
00637 // Add vector to another vector
00638 template< class T >
00639 Vector<T> Vector<T>::Add(Vector<T>& m1, Vector<T>& m2)
00640 {
00641     if(m1.length != m2.length)
00642     {
00643         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00644         Utility::RunTimeError("Vector lengths are not the same!");
00645     }
00646 
00647     Vector<T> temp(m1.length);
00648     for(int i=0;i<temp.length;i++)
00649     {
00650         temp.data[i] = m1.data[i] + m2.data[i];
00651     }
00652     
00653     return temp;
00654 }
00655 
00656 
00657 template< class T >
00658 Vector<T> Vector<T>::Subtract(Vector<T>& m1, Vector<T>& m2)
00659 {
00660     if(m1.length != m2.length)
00661     {
00662         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00663         Utility::RunTimeError("Vector lengths are not the same!");
00664     }
00665 
00666     Vector<T> temp(m1.length);
00667     for(int i=0;i<temp.length;i++)
00668     {
00669         temp.data[i] = m1.data[i] - m2.data[i];
00670     }
00671     
00672     return temp;
00673 }
00674 
00675 template< class T >
00676 Vector<T> Vector<T>::Multiply(Vector<T>& m1, Vector<T>& m2)
00677 {
00678     if(m1.length != m2.length)
00679     {
00680         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00681         Utility::RunTimeError("Vector lengths are not the same!");
00682     }
00683 
00684     Vector<T> temp(m1.length);
00685     for(int i=0;i<temp.length;i++)
00686     {
00687         temp.data[i] = m1.data[i] * m2.data[i];
00688     }
00689     
00690     return temp;
00691 }
00692 
00693 template< class T >
00694 Vector<T> Vector<T>::Divide(Vector<T>& m1, Vector<T>& m2)
00695 {
00696     if(m1.length != m2.length)
00697     {
00698         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00699         Utility::RunTimeError("Vector lengths are not the same!");
00700     }
00701 
00702     Vector<T> temp(m1.length);
00703     for(int i=0;i<temp.length;i++)
00704     {
00705         //catch division exception instead...
00706         if(m2.data[i] != 0)
00707         {
00708             temp.data[i] = m1.data[i] / m2.data[i];
00709         }
00710         else
00711         {
00712             cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00713             Utility::RunTimeError("Divide by zero in vector division!");
00714         }
00715     }
00716     
00717     return temp;
00718 }
00719 
00720 
00721 // Add value to another vector
00722 template< class T >
00723 Vector<T> Vector<T>::Add(Vector<T>& m1, T v2)
00724 {
00725     Vector<T> temp(m1.length);
00726     for(int i=0;i<temp.length;i++)
00727     {
00728         temp.data[i] = m1.data[i] + v2;
00729     }
00730     return temp;
00731 }
00732 
00733 template< class T >
00734 Vector<T> Vector<T>::Subtract(Vector<T>& m1, T v2)
00735 {
00736     Vector<T> temp(m1.length);
00737     for(int i=0;i<temp.length;i++)
00738     {
00739         temp.data[i] = m1.data[i] - v2;
00740     }
00741     return temp;
00742 }
00743 
00744 template< class T >
00745 Vector<T> Vector<T>::Subtract(T v2, Vector<T>& m1)
00746 {
00747     Vector<T> temp(m1.length);
00748     for(int i=0;i<temp.length;i++)
00749     {
00750         temp.data[i] = v2 - m1.data[i];
00751     }
00752     return temp;
00753 }
00754 
00755 
00756 template< class T >
00757 Vector<T> Vector<T>::Multiply(Vector<T>& m1, T v2)
00758 {
00759     Vector<T> temp(m1.length);
00760     for(int i=0;i<temp.length;i++)
00761     {
00762         temp.data[i] = m1.data[i] * v2;
00763     }
00764     return temp;
00765 }
00766 
00767 template< class T >
00768 Vector<T> Vector<T>::Divide(Vector<T>& m1, T v2)
00769 {
00770     if(v2 == 0)
00771     {
00772         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00773         Utility::RunTimeError("Divide by zero in vector by value division!");
00774     }
00775 
00776     Vector<T> temp(m1.length);
00777     for(int i=0;i<temp.length;i++)
00778     {
00779         temp.data[i] = m1.data[i] / v2;
00780     }
00781     return temp;
00782 }
00783 
00784 template< class T >
00785 Vector<T> Vector<T>::Divide(T v2, Vector<T>& m1)
00786 {
00787     Vector<T> temp(m1.length);
00788     for(int i=0;i<temp.length;i++)
00789     {
00790         if(m1.data[i] != 0)
00791         {
00792             temp.data[i] = v2 / m1.data[i];
00793         }
00794         else
00795         {
00796             cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00797             Utility::RunTimeError("Divide by zero in value by vector division!");
00798         }
00799     }
00800     return temp;
00801 }
00802 
00803 
00804 
00805 // Add vector to "this" vector
00806 template< class T >
00807 Vector<T>& Vector<T>::Add(Vector<T>& m)
00808 {
00809     if(length != m.length)
00810     {
00811         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00812         Utility::RunTimeError("Vector lengths are not the same!");
00813     }
00814 
00815     for(int i=0;i<this->length;i++)
00816     {
00817         this->data[i] += m.data[i];
00818     }
00819     
00820     return *this;
00821 }
00822 
00823 template< class T >
00824 Vector<T>& Vector<T>::Subtract(Vector<T>& m)
00825 {
00826     if(length != m.length)
00827     {
00828         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00829         Utility::RunTimeError("Vector lengths are not the same!");
00830     }
00831 
00832     for(int i=0;i<this->length;i++)
00833     {
00834         this->data[i] -= m.data[i];
00835     }
00836     
00837     return *this;
00838 }
00839 
00840 template< class T >
00841 Vector<T>& Vector<T>::Multiply(Vector<T>& m)
00842 {
00843     if(length != m.length)
00844     {
00845         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00846         Utility::RunTimeError("Vector lengths are not the same!");
00847     }
00848 
00849     for(int i=0;i<this->length;i++)
00850     {
00851         this->data[i] *= m.data[i];
00852     }
00853     
00854     return *this;
00855 }
00856 
00857 template< class T >
00858 Vector<T>& Vector<T>::Divide(Vector<T>& m)
00859 {
00860     if(length != m.length)
00861     {
00862         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00863         Utility::RunTimeError("Vector lengths are not the same!");
00864     }
00865 
00866     for(int i=0;i<this->length;i++)
00867     {
00868         if(m.data[i] != 0)
00869         {
00870             this->data[i] /= m.data[i];
00871         }
00872         else
00873         {
00874             cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00875             Utility::RunTimeError("Divide by zero in vector division!");
00876         }
00877     }
00878     
00879     return *this;
00880 }
00881 
00882 // Add value to "this" vector
00883 template< class T >
00884 Vector<T>& Vector<T>::Add(T v)
00885 {
00886     for(int i=0;i<this->length;i++)
00887     {
00888         this->data[i] += v;
00889     }
00890     return *this;
00891 }
00892 
00893 template< class T >
00894 Vector<T>& Vector<T>::Subtract(T v)
00895 {
00896     for(int i=0;i<this->length;i++)
00897     {
00898         this->data[i] -= v;
00899     }
00900     return *this;
00901 }
00902 
00903 template< class T >
00904 Vector<T>& Vector<T>::Multiply(T v)
00905 {
00906     for(int i=0;i<this->length;i++)
00907     {
00908         this->data[i] *= v;
00909     }
00910     return *this;
00911 }
00912 
00913 template< class T >
00914 Vector<T>& Vector<T>::Divide(T v)
00915 {
00916     if(v == 0)
00917     {
00918         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
00919         Utility::RunTimeError("Divide by zero in vector by value division!");
00920     }
00921     
00922     for(int i=0;i<this->length;i++)
00923     {
00924         this->data[i] /= v;
00925     }
00926     return *this;
00927 }
00928 
00929 
00930 
00931 
00932 //OPERATORS
00933 
00934 template< class T >
00935 Vector<T>& Vector<T>::operator= (Vector<T>& m)
00936 {
00937     length = m.length;
00938     
00939     data = m.data;
00940     
00941     
00942     if(m.memoryManaged == true)
00943     {
00944         delete clean;
00945         clean = new Cleaner<T>(data);
00946         memoryManaged = true;
00947     }
00948     else
00949     {
00950         Utility::RunTimeError("Assignment of an unmanaged vector to another vector is not allowed!\nUse Clone() instead.");
00951     }
00952     
00953     return *this;
00954 }
00955 
00956 
00957 template< class T >
00958 Vector<T>& Vector<T>::operator= (Array<T>& m)
00959 {
00960     length = m.length;
00961     
00962     data = m.data;
00963     clean = new Cleaner(data);
00964     
00965     memoryManaged = true;
00966 
00967     return *this;
00968 }
00969 
00970 template< class T >
00971 Vector<T>& Vector<T>::operator= (Matrix<T>& m)
00972 {
00973     length = m.length;
00974     
00975     data = m.data;
00976     clean = new Cleaner(data);
00977     
00978     memoryManaged = true;
00979 
00980     return *this;
00981 }
00982 
00983 
00984 
00985 //Unary operators
00986 template< class T >
00987 inline Vector<T> Vector<T>::operator+ ()
00988 {
00989     return *this;
00990 }
00991 
00992 
00993 template< class T >
00994 inline Vector<T> Vector<T>::operator- ()
00995 {
00996     Vector<T> temp(length);
00997     for(int i=0;i<length;i++)
00998     {
00999         temp.data[i] = - data[i];
01000     }
01001     return temp;
01002 }
01003 
01004 
01005 template< class T >
01006 inline Vector<int> Vector<T>::operator! ()
01007 {
01008     Vector<int> temp(yDim, xDim);
01009     for(int i=0;i<length;i++)
01010     {
01011         if(data[i] == 0)
01012         {
01013             temp.Data()[i] = 1;
01014         }
01015         else
01016         {
01017             temp.Data()[i] = 0;
01018         }
01019     }
01020     return temp;
01021 }
01022 
01023 
01024 
01025 
01026 template< class T >
01027 inline T& Vector<T>::operator() (const int i)
01028 {
01029     if(i<0 || i>=length)
01030     {
01031         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01032         Utility::RunTimeError("Index outside bounds!");
01033     }
01034     return data[i];
01035 }
01036 
01037 
01038 template< class T >
01039 inline T& Vector<T>::operator[] (const int i)
01040 {
01041     return data[i];
01042 }
01043 
01044 
01045 
01046 
01047     // slice x slice: returns a vector.
01048 template< class T >
01049 inline Vector<T> Vector<T>::operator() (int start, int end)
01050 {
01051 
01052     if(start<0 || start>=length || end<0 || end>=length)
01053     {
01054         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01055         Utility::RunTimeError("Index outside bounds!");
01056     }
01057     
01058     if(start > end)
01059     {
01060         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01061         Utility::RunTimeError("Second slice parameter cannot be less than the first parameter!");
01062     }
01063     
01064     Vector<T> temp(end-start+1);
01065     //memcopy here...
01066     for(int i=start; i<=end; i++)
01067     {
01068         temp[i-start] = data[i];
01069     }
01070 
01071     return temp;
01072 }
01073 
01074 
01075 
01076 
01077 
01078 
01079 
01080 
01081 
01083 template< class T >
01084 inline T& Vector<T>::Elem(const int i)
01085 {
01086     if(i<0 || i>=length)
01087     {
01088         cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
01089         Utility::RunTimeError("Index outside bounds!");
01090     }
01091     return data[i];
01092 }
01093 
01095 template< class T >
01096 inline T& Vector<T>::ElemNC(const int i)
01097 {
01098     return data[i];
01099 }
01100 
01101 
01102 
01103 
01104 
01105 
01106 
01107 // Add inline
01108 template< class T >
01109 Vector<T>& Vector<T>::operator+= (Vector<T>& m)
01110 {
01111     return this->Add(m);
01112 }
01113 
01114 template< class T >
01115 Vector<T>& Vector<T>::operator-= (Vector<T>& m)
01116 {
01117     return this->Subtract(m);
01118 }
01119 
01120 template< class T >
01121 Vector<T>& Vector<T>::operator*= (Vector<T>& m)
01122 {
01123     return this->Multiply(m);
01124 }
01125 
01126 template< class T >
01127 Vector<T>& Vector<T>::operator/= (Vector<T>& m)
01128 {
01129     return this->Divide(m);
01130 }
01131 
01132 
01133 template< class T >
01134 Vector<T>& Vector<T>::operator+= (T v)
01135 {
01136     return this->Add(v);
01137 }
01138 
01139 template< class T >
01140 Vector<T>& Vector<T>::operator-= (T v)
01141 {
01142     return this->Subtract(v);
01143 }
01144 
01145 template< class T >
01146 Vector<T>& Vector<T>::operator*= (T v)
01147 {
01148     return this->Multiply(v);
01149 }
01150 
01151 template< class T >
01152 Vector<T>& Vector<T>::operator/= (T v)
01153 {
01154     return this->Divide(v);
01155 }
01156 
01157 
01158 
01159 
01160 
01161 
01162 template< class T >
01163 Vector<T>::Vector(Matrix<T> &m) 
01164 {
01165     length = m.length;
01166     
01167     data = m.data;
01168     
01169     clean = new Cleaner<T>(data);
01170     
01171     memoryManaged = true;
01172     
01173 }
01174 
01175 
01176 
01177 
01178 template< class T >
01179 Vector<T>::Vector(Array<T> &m) 
01180 {
01181     length = m.length;
01182     
01183     data = m.data;
01184     
01185     clean = new Cleaner<T>(data);
01186     
01187     memoryManaged = true;
01188 
01189 }
01190 
01191 
01192 
01193 
01194 
01195 
01196 
01197 
01198 
01199 
01200 
01201 
01202 
01203 
01204 
01205 
01206 
01207 
01208 
01209 
01210 
01211 
01212 
01213 
01214 
01215 
01216 

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