📄 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<T>::Vector()
: length(0)
{
data = 0;
clean = 0;
memoryManaged = true;
}
template< class T >
Vector<T>::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<T>(data);
Utility::CheckPointer(clean);
memoryManaged = true;
}
template< class T >
Vector<T>::Vector(string str)
{
if(str.substr(0,1) == "[")
{
if(str.substr(str.size()-1,1) == "]")
{
str = str.substr(1,str.size()-2);
vector<string> 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<T>(data);
Utility::CheckPointer(clean);
memoryManaged = true;
vector<string>::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<string> 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<float>::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<T>(data);
Utility::CheckPointer(clean);
memoryManaged = true;
for(int i=0;i<length;i++)
{
data[i] = (T)(start+i*inc);
}
}
}
template< class T >
Vector<T>::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<T>(data);
Utility::CheckPointer(clean);
memoryManaged = true;
for(int i=0;i<length;i++)
{
data[i] = init;
}
}
template< class T >
Vector<T>::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<T>::Vector(Vector<T> &m)
{
length = m.length;
if(m.memoryManaged == true)
{
data = m.data;
clean = new (std::nothrow) Cleaner<T>(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<T>::~Vector(void)
{
if(clean != 0 && memoryManaged == true) // probably redundant
{
delete clean;
}
}
template< class T >
void Vector<T>::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<T>::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<T>::DataPtr() const
{
return data;
}
/// Useful for passing data to third party libraries.
template< class T >
inline T* Vector<T>::Data()
{
return data;
}
template< class T >
Vector<T> Vector<T>::Clone() const
{
Vector<T> temp(length);
for(int j=0;j<temp.length;j++)
{
temp.data[j] = data[j];
}
return temp;
}
// returns an unmanaged vector
//template< class T >
//Vector<T> Vector<T>::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<T> temp(&(data[start]), end-start+1);
// return temp;
//}
template< class T >
Vector<T> Vector<T>::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<T> temp(end-start+1);
//memcopy here...
for(int i=start; i<=end; i++)
{
temp[i-start] = data[i];
}
return temp;
}
template< class T >
Vector<T> Vector<T>::Slice(string str)
{
int start, end;
vector<string> 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<T>::IsMemoryManaged() const
{
return memoryManaged;
}
template< class T >
inline const int Vector<T>::Length() const
{
return length;
}
template< class T >
inline const int Vector<T>::Numel() const
{
return length;
}
template< class T >
inline void Vector<T>::Init(const T init)
{
for(int i=0;i<length;i++)
{
data[i] = init;
}
}
template< class T >
inline Vector<T>& Vector<T>::Rand(const double max)
{
if(!RandomGen::Initialized())
{
RandomGen::Initialize();
}
for(int i=0;i<length;i++)
{
data[i] = (T)(rand()/(double)RAND_MAX*max);
}
return *this;
}
template< class T >
Vector<T> Vector<T>::Rand(const int l, const double max)
{
Vector<T> m(l);
if(!RandomGen::Initialized())
{
RandomGen::Initialize();
}
for(int i=0;i<l;i++)
{
m.data[i] = (T)(rand()/(double)RAND_MAX*max);
}
return m;
}
template< class T >
void Vector<T>::ReadFromVector(const Vector<T>& 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<m.length;i++)
{
data[i+index] = m.data[i];
}
}
template< class T >
Vector<T> Vector<T>::Cat(Vector<T>& m1, Vector<T>& m2)
{
Vector<T> temp(m1.Length()+m2.Length());
for(int i=0;i<m1.Length();i++)
{
temp.data[i] = m1.data[i];
}
for(int i=m1.Length();i<temp.Length();i++)
{
temp.data[i] = m2.data[i-m1.Length()];
}
return temp;
}
template< class T >
Vector<T> Vector<T>::Ones(int side)
{
Vector<T> temp(side,1);
return temp;
}
template< class T >
Vector<T> Vector<T>::Zeros(int side)
{
Vector<T> temp(side,0);
return temp;
}
template< class T >
T Vector<T>::Inner(Vector<T>& m1, Vector<T>& 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<m1.length; i++)
{
inner += m1.data[i]*m2.data[i];
}
return inner;
}
// Boolean Operations...
template< class T >
Vector<int> Vector<T>::And(Vector<T>& m1, Vector<T>& m2)
{
if(m1.length != m2.length)
{
cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
Utility::RunTimeError("Vector lengths are not the same!");
}
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] != 0 && m2.data[i] != 0) ? 1 : 0;
}
return temp;
}
template< class T >
Vector<int> Vector<T>::Or(Vector<T>& m1, Vector<T>& m2)
{
if(m1.length != m2.length)
{
cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
Utility::RunTimeError("Vector lengths are not the same!");
}
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] == 0 && m2.data[i] == 0) ? 0 : 1;
}
return temp;
}
template< class T >
Vector<int> Vector<T>::Lt(Vector<T>& m1, Vector<T>& m2)
{
if(m1.length != m2.length)
{
cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
Utility::RunTimeError("Vector lengths are not the same!");
}
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] < m2.data[i]) ? 1 : 0;
}
return temp;
}
template< class T >
Vector<int> Vector<T>::Gt(Vector<T>& m1, Vector<T>& m2)
{
if(m1.length != m2.length)
{
cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
Utility::RunTimeError("Vector lengths are not the same!");
}
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] > m2.data[i]) ? 1 : 0;
}
return temp;
}
template< class T >
Vector<int> Vector<T>::Le(Vector<T>& m1, Vector<T>& m2)
{
if(m1.length != m2.length)
{
cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
Utility::RunTimeError("Vector lengths are not the same!");
}
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] <= m2.data[i]) ? 1 : 0;
}
return temp;
}
template< class T >
Vector<int> Vector<T>::Ge(Vector<T>& m1, Vector<T>& m2)
{
if(m1.length != m2.length)
{
cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
Utility::RunTimeError("Vector lengths are not the same!");
}
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] >= m2.data[i]) ? 1 : 0;
}
return temp;
}
template< class T >
Vector<int> Vector<T>::Eq(Vector<T>& m1, Vector<T>& m2)
{
if(m1.length != m2.length)
{
cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
Utility::RunTimeError("Vector lengths are not the same!");
}
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] == m2.data[i]) ? 1 : 0;
}
return temp;
}
template< class T >
Vector<int> Vector<T>::Ne(Vector<T>& m1, Vector<T>& m2)
{
if(m1.length != m2.length)
{
cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
Utility::RunTimeError("Vector lengths are not the same!");
}
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] != m2.data[i]) ? 1 : 0;
}
return temp;
}
// Boolean Operations with value types...
template< class T >
Vector<int> Vector<T>::And(Vector<T>& m1, T v)
{
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] != 0 && v != 0) ? 1 : 0;
}
return temp;
}
template< class T >
Vector<int> Vector<T>::Or(Vector<T>& m1, T v)
{
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] == 0 && v == 0) ? 0 : 1;
}
return temp;
}
template< class T >
Vector<int> Vector<T>::Lt(Vector<T>& m1, T v)
{
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] < v) ? 1 : 0;
}
return temp;
}
template< class T >
Vector<int> Vector<T>::Gt(Vector<T>& m1, T v)
{
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] > v) ? 1 : 0;
}
return temp;
}
template< class T >
Vector<int> Vector<T>::Le(Vector<T>& m1, T v)
{
Vector<int> temp(m1.length);
for(int i=0;i<m1.length;i++)
{
temp[i] = (m1.data[i] <= v) ? 1 : 0;
}
return temp;
}
template< class T >
Vector<int> Vector<T>::Ge(Vector<T>& m1, T v)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -