www.pudn.com > Image_segment.rar > Vector.inl
//Copyright (c) 2004-2005, Baris Sumengen //All rights reserved. // // CIMPL Matrix Performance Library // //Redistribution and use in source and binary //forms, with or without modification, are //permitted provided that the following //conditions are met: // // * No commercial use is allowed. // This software can only be used // for non-commercial purposes. This // distribution is mainly intended for // academic research and teaching. // * Redistributions of source code must // retain the above copyright notice, this // list of conditions and the following // disclaimer. // * Redistributions of binary form must // mention the above copyright notice, this // list of conditions and the following // disclaimer in a clearly visible part // in associated product manual, // readme, and web site of the redistributed // software. // * Redistributions in binary form must // reproduce the above copyright notice, // this list of conditions and the // following disclaimer in the // documentation and/or other materials // provided with the distribution. // * The name of Baris Sumengen may not be // used to endorse or promote products // derived from this software without // specific prior written permission. // //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT //HOLDERS AND CONTRIBUTORS "AS IS" AND ANY //EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT //NOT LIMITED TO, THE IMPLIED WARRANTIES OF //MERCHANTABILITY AND FITNESS FOR A PARTICULAR //PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE //CONTRIBUTORS BE LIABLE FOR ANY //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, //EXEMPLARY, OR CONSEQUENTIAL DAMAGES //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT //OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, //DATA, OR PROFITS; OR BUSINESS INTERRUPTION) //HOWEVER CAUSED AND ON ANY THEORY OF //LIABILITY, WHETHER IN CONTRACT, STRICT //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR //OTHERWISE) ARISING IN ANY WAY OUT OF THE USE //OF THIS SOFTWARE, EVEN IF ADVISED OF THE //POSSIBILITY OF SUCH DAMAGE. template< class T > Vector::Vector() : length(0) { data = 0; clean = 0; memoryManaged = true; } template< class T > Vector ::Vector(const int l) { if(l < 1) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector length should be larger than 0!"); } length = l; data = new (std::nothrow) T[length]; Utility::CheckPointer(data); clean = new (std::nothrow) Cleaner (data); Utility::CheckPointer(clean); memoryManaged = true; } template< class T > Vector ::Vector(string str) { if(str.substr(0,1) == "[") { if(str.substr(str.size()-1,1) == "]") { str = str.substr(1,str.size()-2); vector elem_strs = Utility::Split(str); length = (int)elem_strs.size(); if(length <= 0) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector length should be larger than 0!"); } data = new (std::nothrow) T[length]; Utility::CheckPointer(data); clean = new (std::nothrow) Cleaner (data); Utility::CheckPointer(clean); memoryManaged = true; vector ::const_iterator constIterator; int i = 0; for(constIterator = elem_strs.begin(); constIterator != elem_strs.end(); constIterator++) { data[i] = (T)Utility::ToDouble((string)*constIterator); i++; } } else { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Incorrect initialization. String cannot be parsed!"); } } else { double start, inc, end; vector bounds = Utility::Split(str, ":"); if(bounds.size() == 3) { start = Utility::ToDouble(bounds[0]); inc = Utility::ToDouble(bounds[1]); end = Utility::ToDouble(bounds[2]); } else if(bounds.size() == 2) { start = Utility::ToDouble(bounds[0]); inc = 1.0; end = Utility::ToDouble(bounds[1]); } else { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Incorrect initialization. String cannot be parsed!"); } length = (int)(abs(end-start)/inc+numeric_limits ::epsilon())+1; if(length <= 0) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector length should be larger than 0!"); } data = new (std::nothrow) T[length]; Utility::CheckPointer(data); clean = new (std::nothrow) Cleaner (data); Utility::CheckPointer(clean); memoryManaged = true; for(int i=0;i Vector ::Vector(const int l, T init) { if(l < 1) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector length should be larger than 0!"); } length = l; data = new (std::nothrow) T[length]; Utility::CheckPointer(data); clean = new (std::nothrow) Cleaner (data); Utility::CheckPointer(clean); memoryManaged = true; for(int i=0;i Vector ::Vector(T* _data, const int l) { if(l < 1) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector length should be larger than 0!"); } length = l; data = _data; clean = 0; memoryManaged = false; } template< class T > Vector ::Vector(Vector &m) { length = m.length; if(m.memoryManaged == true) { data = m.data; clean = new (std::nothrow) Cleaner (data); Utility::CheckPointer(clean); memoryManaged = true; } else { //Utility::RunTimeError("Using copy constructor from an unmanaged vector is not allowed!\nUse Clone() instead."); data = m.data; clean = 0; memoryManaged = false; //Utility::Warning("Using copy constructor from an unmanaged vector!\nCreated a new unmanaged vector pointing to the same unmanaged memory."); } } template< class T > Vector ::~Vector(void) { if(clean != 0 && memoryManaged == true) // probably redundant { delete clean; } } template< class T > void Vector ::Set(T* _data, const int l) { if(l < 1) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector length should be larger than 0!"); } length = l; data = _data; delete clean; clean = 0; memoryManaged = false; } template< class T > void Vector ::Clean() { if(clean != 0 && memoryManaged == true) { delete clean; data = 0; clean = 0; } } /// Useful for passing data to third party libraries a read only. template< class T > inline const T* Vector ::DataPtr() const { return data; } /// Useful for passing data to third party libraries. template< class T > inline T* Vector ::Data() { return data; } template< class T > Vector Vector ::Clone() const { Vector temp(length); for(int j=0;j //Vector Vector ::Slice(int start, int end) //{ // if(start<0 || start>=length || end<0 || end>=length) // { // cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; // Utility::RunTimeError("Index outside bounds!"); // } // if(start > end) // { // cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; // Utility::RunTimeError("Second slice parameter cannot be less than the first parameter!"); // } // // // Vector temp(&(data[start]), end-start+1); // return temp; //} template< class T > Vector Vector ::Slice(int start, int end) { if(start<0 || start>=length || end<0 || end>=length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Index outside bounds!"); } if(start > end) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Second slice parameter cannot be less than the first parameter!"); } Vector temp(end-start+1); //memcopy here... for(int i=start; i<=end; i++) { temp[i-start] = data[i]; } return temp; } template< class T > Vector Vector ::Slice(string str) { int start, end; vector bounds = Utility::Split(str, ":"); if(str == ":") { start = 0; end = length-1; } else if(bounds.size() == 2) { start = Utility::ToInt(bounds[0]); end = Utility::ToInt(bounds[1]); } else { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Incorrect slice argument. String cannot be parsed!"); } return this->Slice(start, end); } template< class T > inline const bool Vector ::IsMemoryManaged() const { return memoryManaged; } template< class T > inline const int Vector ::Length() const { return length; } template< class T > inline const int Vector ::Numel() const { return length; } template< class T > inline void Vector ::Init(const T init) { for(int i=0;i inline Vector & Vector ::Rand(const double max) { if(!RandomGen::Initialized()) { RandomGen::Initialize(); } for(int i=0;i Vector Vector ::Rand(const int l, const double max) { Vector m(l); if(!RandomGen::Initialized()) { RandomGen::Initialize(); } for(int i=0;i void Vector ::ReadFromVector(const Vector & m, const int index) { if(length < m.length+index) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("You can't read from a larger Vector (from the copy start point) to a smaller Vector!"); } for(int i=0;i Vector Vector ::Cat(Vector & m1, Vector & m2) { Vector temp(m1.Length()+m2.Length()); for(int i=0;i Vector Vector ::Ones(int side) { Vector temp(side,1); return temp; } template< class T > Vector Vector ::Zeros(int side) { Vector temp(side,0); return temp; } template< class T > T Vector ::Inner(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } T inner = 0; for(int i=0; i Vector Vector ::And(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } Vector temp(m1.length); for(int i=0;i Vector Vector ::Or(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } Vector temp(m1.length); for(int i=0;i Vector Vector ::Lt(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } Vector temp(m1.length); for(int i=0;i Vector Vector ::Gt(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } Vector temp(m1.length); for(int i=0;i m2.data[i]) ? 1 : 0; } return temp; } template< class T > Vector Vector ::Le(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } Vector temp(m1.length); for(int i=0;i Vector Vector ::Ge(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } Vector temp(m1.length); for(int i=0;i = m2.data[i]) ? 1 : 0; } return temp; } template< class T > Vector Vector ::Eq(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } Vector temp(m1.length); for(int i=0;i Vector Vector ::Ne(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } Vector temp(m1.length); for(int i=0;i Vector Vector ::And(Vector & m1, T v) { Vector temp(m1.length); for(int i=0;i Vector Vector ::Or(Vector & m1, T v) { Vector temp(m1.length); for(int i=0;i Vector Vector ::Lt(Vector & m1, T v) { Vector temp(m1.length); for(int i=0;i Vector Vector ::Gt(Vector & m1, T v) { Vector temp(m1.length); for(int i=0;i v) ? 1 : 0; } return temp; } template< class T > Vector Vector ::Le(Vector & m1, T v) { Vector temp(m1.length); for(int i=0;i Vector Vector ::Ge(Vector & m1, T v) { Vector temp(m1.length); for(int i=0;i = v) ? 1 : 0; } return temp; } template< class T > Vector Vector ::Eq(Vector & m1, T v) { Vector temp(m1.length); for(int i=0;i Vector Vector ::Ne(Vector & m1, T v) { Vector temp(m1.length); for(int i=0;i Vector Vector ::Add(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } Vector temp(m1.length); for(int i=0;i Vector Vector ::Subtract(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } Vector temp(m1.length); for(int i=0;i Vector Vector ::Multiply(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } Vector temp(m1.length); for(int i=0;i Vector Vector ::Divide(Vector & m1, Vector & m2) { if(m1.length != m2.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } Vector temp(m1.length); for(int i=0;i Vector Vector ::Add(Vector & m1, T v2) { Vector temp(m1.length); for(int i=0;i Vector Vector ::Subtract(Vector & m1, T v2) { Vector temp(m1.length); for(int i=0;i Vector Vector ::Subtract(T v2, Vector & m1) { Vector temp(m1.length); for(int i=0;i Vector Vector ::Multiply(Vector & m1, T v2) { Vector temp(m1.length); for(int i=0;i Vector Vector ::Divide(Vector & m1, T v2) { if(v2 == 0) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Divide by zero in vector by value division!"); } Vector temp(m1.length); for(int i=0;i Vector Vector ::Divide(T v2, Vector & m1) { Vector temp(m1.length); for(int i=0;i Vector & Vector ::Add(Vector & m) { if(length != m.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } for(int i=0;i length;i++) { this->data[i] += m.data[i]; } return *this; } template< class T > Vector & Vector ::Subtract(Vector & m) { if(length != m.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } for(int i=0;i length;i++) { this->data[i] -= m.data[i]; } return *this; } template< class T > Vector & Vector ::Multiply(Vector & m) { if(length != m.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } for(int i=0;i length;i++) { this->data[i] *= m.data[i]; } return *this; } template< class T > Vector & Vector ::Divide(Vector & m) { if(length != m.length) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Vector lengths are not the same!"); } for(int i=0;i length;i++) { if(m.data[i] != 0) { this->data[i] /= m.data[i]; } else { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Divide by zero in vector division!"); } } return *this; } // Add value to "this" vector template< class T > Vector & Vector ::Add(T v) { for(int i=0;i length;i++) { this->data[i] += v; } return *this; } template< class T > Vector & Vector ::Subtract(T v) { for(int i=0;i length;i++) { this->data[i] -= v; } return *this; } template< class T > Vector & Vector ::Multiply(T v) { for(int i=0;i length;i++) { this->data[i] *= v; } return *this; } template< class T > Vector & Vector ::Divide(T v) { if(v == 0) { cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::RunTimeError("Divide by zero in vector by value division!"); } for(int i=0;i length;i++) { this->data[i] /= v; } return *this; } //OPERATORS template< class T > Vector & Vector ::operator= (Vector & m) { if(&m != this) { if(m.memoryManaged == true && memoryManaged == true) { length = m.length; data = m.data; delete clean; clean = new (std::nothrow) Cleaner (data); Utility::CheckPointer(clean); } else if(m.memoryManaged == false && memoryManaged == true) { length = m.length; data = new (std::nothrow) T[length]; Utility::CheckPointer(data); clean = new (std::nothrow) Cleaner (data); Utility::CheckPointer(clean); for(int j=0;j //Vector & Vector ::operator= (Array & m) //{ // if(memoryManaged == true) // { // length = m.length; // // data = m.data; // clean = new (std::nothrow) Cleaner(data); // Utility::CheckPointer(clean); // } // else // { // if(length == m.length) // { // for(int j=0;j Vector & Vector ::operator= (Matrix & m) { if(memoryManaged == true) { length = m.length; data = m.data; clean = new (std::nothrow) Cleaner(data); Utility::CheckPointer(clean); } else { if(length == m.length) { for(int j=0;j Vector & Vector ::operator= (string str) { Vector temp(str); *this = temp; return *this; } //Unary operators template< class T > inline Vector