⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vector.inl

📁 图像分割算法
💻 INL
📖 第 1 页 / 共 2 页
字号:
	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>::Eq(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>::Ne(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;
}

















// Add vector to another vector
template< class T >
Vector<T> Vector<T>::Add(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<T> temp(m1.length);
	for(int i=0;i<temp.length;i++)
	{
		temp.data[i] = m1.data[i] + m2.data[i];
	}
	
	return temp;
}


template< class T >
Vector<T> Vector<T>::Subtract(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<T> temp(m1.length);
	for(int i=0;i<temp.length;i++)
	{
		temp.data[i] = m1.data[i] - m2.data[i];
	}
	
	return temp;
}

template< class T >
Vector<T> Vector<T>::Multiply(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<T> temp(m1.length);
	for(int i=0;i<temp.length;i++)
	{
		temp.data[i] = m1.data[i] * m2.data[i];
	}
	
	return temp;
}

template< class T >
Vector<T> Vector<T>::Divide(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<T> temp(m1.length);
	for(int i=0;i<temp.length;i++)
	{
		//catch division exception instead...
		if(m2.data[i] != 0)
		{
			temp.data[i] = m1.data[i] / m2.data[i];
		}
		else
		{
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
			Utility::RunTimeError("Divide by zero in vector division!");
		}
	}
	
	return temp;
}


// Add value to another vector
template< class T >
Vector<T> Vector<T>::Add(Vector<T>& m1, T v2)
{
	Vector<T> temp(m1.length);
	for(int i=0;i<temp.length;i++)
	{
		temp.data[i] = m1.data[i] + v2;
	}
	return temp;
}

template< class T >
Vector<T> Vector<T>::Subtract(Vector<T>& m1, T v2)
{
	Vector<T> temp(m1.length);
	for(int i=0;i<temp.length;i++)
	{
		temp.data[i] = m1.data[i] - v2;
	}
	return temp;
}

template< class T >
Vector<T> Vector<T>::Subtract(T v2, Vector<T>& m1)
{
	Vector<T> temp(m1.length);
	for(int i=0;i<temp.length;i++)
	{
		temp.data[i] = v2 - m1.data[i];
	}
	return temp;
}


template< class T >
Vector<T> Vector<T>::Multiply(Vector<T>& m1, T v2)
{
	Vector<T> temp(m1.length);
	for(int i=0;i<temp.length;i++)
	{
		temp.data[i] = m1.data[i] * v2;
	}
	return temp;
}

template< class T >
Vector<T> Vector<T>::Divide(Vector<T>& m1, T v2)
{
	if(v2 == 0)
	{
		cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
		Utility::RunTimeError("Divide by zero in vector by value division!");
	}

	Vector<T> temp(m1.length);
	for(int i=0;i<temp.length;i++)
	{
		temp.data[i] = m1.data[i] / v2;
	}
	return temp;
}

template< class T >
Vector<T> Vector<T>::Divide(T v2, Vector<T>& m1)
{
	Vector<T> temp(m1.length);
	for(int i=0;i<temp.length;i++)
	{
		if(m1.data[i] != 0)
		{
			temp.data[i] = v2 / m1.data[i];
		}
		else
		{
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
			Utility::RunTimeError("Divide by zero in value by vector division!");
		}
	}
	return temp;
}



// Add vector to "this" vector
template< class T >
Vector<T>& Vector<T>::Add(Vector<T>& m)
{
	if(length != m.length)
	{
		cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
		Utility::RunTimeError("Vector lengths are not the same!");
	}

	for(int i=0;i<this->length;i++)
	{
		this->data[i] += m.data[i];
	}
	
	return *this;
}

template< class T >
Vector<T>& Vector<T>::Subtract(Vector<T>& m)
{
	if(length != m.length)
	{
		cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
		Utility::RunTimeError("Vector lengths are not the same!");
	}

	for(int i=0;i<this->length;i++)
	{
		this->data[i] -= m.data[i];
	}
	
	return *this;
}

template< class T >
Vector<T>& Vector<T>::Multiply(Vector<T>& m)
{
	if(length != m.length)
	{
		cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
		Utility::RunTimeError("Vector lengths are not the same!");
	}

	for(int i=0;i<this->length;i++)
	{
		this->data[i] *= m.data[i];
	}
	
	return *this;
}

template< class T >
Vector<T>& Vector<T>::Divide(Vector<T>& m)
{
	if(length != m.length)
	{
		cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
		Utility::RunTimeError("Vector lengths are not the same!");
	}

	for(int i=0;i<this->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<T>& Vector<T>::Add(T v)
{
	for(int i=0;i<this->length;i++)
	{
		this->data[i] += v;
	}
	return *this;
}

template< class T >
Vector<T>& Vector<T>::Subtract(T v)
{
	for(int i=0;i<this->length;i++)
	{
		this->data[i] -= v;
	}
	return *this;
}

template< class T >
Vector<T>& Vector<T>::Multiply(T v)
{
	for(int i=0;i<this->length;i++)
	{
		this->data[i] *= v;
	}
	return *this;
}

template< class T >
Vector<T>& Vector<T>::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<this->length;i++)
	{
		this->data[i] /= v;
	}
	return *this;
}




//OPERATORS

template< class T >
Vector<T>& Vector<T>::operator= (Vector<T>& m)
{
	
	if(&m != this)
	{
		if(m.memoryManaged == true && memoryManaged == true)
		{
			length = m.length;
			data = m.data;
			delete clean;
			clean = new (std::nothrow) Cleaner<T>(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<T>(data);
			Utility::CheckPointer(clean);
			for(int j=0;j<length;j++)
			{
				data[j] = m.data[j];
			}
			//Utility::Warning("Using assignment from an unmanaged vector!\nValues are copied to the new vector.");
		}
		else if(memoryManaged == false)
		{
			if(length == m.length)
			{
				for(int j=0;j<length;j++)
				{
					data[j] = m.data[j];
				}
				//Utility::Warning("Using assignment to an unmanaged vector!\nValues are copied.");
			}
			else
			{
				cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
				Utility::RunTimeError("Assignment to an unmanaged vector. Lengths of the vectors must match!");
			}
		}
		else
		{
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
			Utility::RunTimeError("Something is wrong. We shouldn't arrive to this branch!");
		}
	}
	
	return *this;
}


//template< class T >
//Vector<T>& Vector<T>::operator= (Array<T>& 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<length;j++)
//			{
//				data[j] = m.data[j];
//			}
//			//Utility::Warning("Using assignment to an unmanaged vector!\nValues are copied.");
//		}
//		else
//		{
//			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
//			Utility::RunTimeError("Assignment to an unmanaged vector. Number of elements in vector and array must match!");
//		}
//	}
//
//	return *this;
//}

template< class T >
Vector<T>& Vector<T>::operator= (Matrix<T>& 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<length;j++)
			{
				data[j] = m.data[j];
			}
			//Utility::Warning("Using assignment to an unmanaged vector!\nValues are copied.");
		}
		else
		{
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
			Utility::RunTimeError("Assignment to an unmanaged vector. Number of elements in vector and matrix must match!");
		}
	}

	return *this;
}



template< class T >
Vector<T>& Vector<T>::operator= (string str)
{
	Vector<T> temp(str);
	*this = temp;
	return *this;
}



//Unary operators
template< class T >
inline Vector<T> Vector<T>::operator+ ()
{
	return *this;
}


template< class T >
inline Vector<T> Vector<T>::operator- ()
{
	Vector<T> temp(length);
	for(int i=0;i<length;i++)
	{
		temp.data[i] = - data[i];
	}
	return temp;
}


template< class T >
inline Vector<int> Vector<T>::operator! ()
{
	Vector<int> temp(yDim, xDim);
	for(int i=0;i<length;i++)
	{
		if(data[i] == 0)
		{
			temp.Data()[i] = 1;
		}
		else
		{
			temp.Data()[i] = 0;
		}
	}
	return temp;
}




template< class T >
inline T& Vector<T>::operator() (const int i)
{

#ifdef CIMPL_BOUNDS_CHECK
	if(i<0 || i>=length)
	{
		cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
		Utility::RunTimeError("Index outside bounds!");
	}
#endif

	return data[i];
}


template< class T >
inline T& Vector<T>::operator[] (const int i)
{
	return data[i];
}





template< class T >
inline Vector<T> Vector<T>::operator() (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 >
inline Vector<T> Vector<T>::operator() (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->operator()(start, end);


}




template< class T >
inline Vector<T> Vector<T>::operator() (Vector<int>& ind)
{
	Vector<T> temp(ind.Length());
	for(int i=0; i<ind.Length(); i++)
	{
		if(ind.ElemNC(i) >= length)
		{
			cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
			Utility::RunTimeError("Index outside bounds!");
		}
		temp.data[i] = data[ind.ElemNC(i)];
	}
	return temp;
}





/// \brief Vector element access. Bounds are checked.
template< class T >
inline T& Vector<T>::Elem(const int i)
{
	if(i<0 || i>=length)
	{
		cerr << "Line: " << __LINE__ << " File: " << __FILE__ << endl;
		Utility::RunTimeError("Index outside bounds!");
	}
	return data[i];
}

/// \brief Vector element access. Bounds are not checked.
template< class T >
inline T& Vector<T>::ElemNC(const int i)
{
	return data[i];
}







// Add inline
template< class T >
Vector<T>& Vector<T>::operator+= (Vector<T>& m)
{
	return this->Add(m);
}

template< class T >
Vector<T>& Vector<T>::operator-= (Vector<T>& m)
{
	return this->Subtract(m);
}

template< class T >
Vector<T>& Vector<T>::operator*= (Vector<T>& m)
{
	return this->Multiply(m);
}

template< class T >
Vector<T>& Vector<T>::operator/= (Vector<T>& m)
{
	return this->Divide(m);
}


template< class T >
Vector<T>& Vector<T>::operator+= (T v)
{
	return this->Add(v);
}

template< class T >
Vector<T>& Vector<T>::operator-= (T v)
{
	return this->Subtract(v);
}

template< class T >
Vector<T>& Vector<T>::operator*= (T v)
{
	return this->Multiply(v);
}

template< class T >
Vector<T>& Vector<T>::operator/= (T v)
{
	return this->Divide(v);
}





// Converting constructors
template< class T >
Vector<T>::Vector(Matrix<T> &m) 
{
	length = m.length;
	
	data = m.data;
	
	clean = new (std::nothrow) Cleaner<T>(data);
	Utility::CheckPointer(clean);
	
	memoryManaged = true;
	
}




//template< class T >
//Vector<T>::Vector(Array<T> &m) 
//{
//	length = m.length;
//	
//	data = m.data;
//	
//	clean = new (std::nothrow) Cleaner<T>(data);
//	Utility::CheckPointer(clean);
//	
//	memoryManaged = true;
//
//}



























⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -