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

📄 vector.h

📁 图像分割算法
💻 H
字号:
//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.



#pragma once
#ifndef VECTOR_H
#define VECTOR_H

#include <iostream>

using std::cout;
using std::cerr;
using std::endl;
using std::ostream;
using std::right;
using std::fixed;

#include <iomanip>

using std::setw;

#include <math.h>

#include <typeinfo>


#include "cimpl.h"


#include <sstream>

using std::ostringstream;

#include <limits>
using std::numeric_limits;

using namespace std;


namespace CIMPL
{


// forward declaration
//template< class T > class Array;
template< class T > class Matrix;
template< class T > class Vector;


/// \brief 1-D Vector class.
template< class T >
class Vector
{
	//friend class Array<T>;
	friend class Matrix<T>;


protected:
	T *data;
	int length;
	bool memoryManaged;
	Cleaner<T> *clean;   // Reference counting Garbage collector.


public:
	Vector(void);
	explicit Vector(int l);
	Vector(string str);
	Vector(int l, T init);
	Vector(T* _data, int l); // creates unmanaged memory
	Vector(Vector<T> &v);
	
	~Vector(void);
	void Set(T* _data, const int l); // creates unmanaged memory

	void Clean();

	const T* DataPtr() const;
	T* Data();

	Vector<T> Clone() const;
	Vector<T> Slice(int start, int end);
	Vector<T> Slice(string str);

	const bool IsMemoryManaged() const;

	const int Length() const;
	const int Numel() const;
	void Init(const T init);
	Vector<T>& Rand(const double max);
	static Vector<T> Rand(const int l, const double max);

	void ReadFromVector(const Vector<T>& m, const int index=0);


	static Vector<T> Cat(Vector<T>& m1, Vector<T>& m2);

	static Vector<T> Ones(int side);
	static Vector<T> Zeros(int side);


	static T Inner(Vector<T>& m1, Vector<T>& m2);

	// Boolean Operations...
	static Vector<int> And(Vector<T>& m1, Vector<T>& m2);
	static Vector<int> Or(Vector<T>& m1, Vector<T>& m2);
	static Vector<int> Lt(Vector<T>& m1, Vector<T>& m2);
	static Vector<int> Gt(Vector<T>& m1, Vector<T>& m2);
	static Vector<int> Le(Vector<T>& m1, Vector<T>& m2);
	static Vector<int> Ge(Vector<T>& m1, Vector<T>& m2);
	static Vector<int> Eq(Vector<T>& m1, Vector<T>& m2);
	static Vector<int> Ne(Vector<T>& m1, Vector<T>& m2);

	// Boolean Operations with value type...
	static Vector<int> And(Vector<T>& m, T v);
	static Vector<int> Or(Vector<T>& m, T v);
	static Vector<int> Lt(Vector<T>& m, T v);
	static Vector<int> Gt(Vector<T>& m, T v);
	static Vector<int> Le(Vector<T>& m, T v);
	static Vector<int> Ge(Vector<T>& m, T v);
	static Vector<int> Eq(Vector<T>& m, T v);
	static Vector<int> Ne(Vector<T>& m, T v);



// Add vector to another vector
	static Vector<T> Add(Vector<T>& m1, Vector<T>& m2);
	static Vector<T> Subtract(Vector<T>& m1, Vector<T>& m2);
	static Vector<T> Multiply(Vector<T>& m1, Vector<T>& m2);
	static Vector<T> Divide(Vector<T>& m1, Vector<T>& m2);

// Add value to another vector
	static Vector<T> Add(Vector<T>& m1, T v2);
	static Vector<T> Subtract(Vector<T>& m1, T v2);
	static Vector<T> Subtract(T v2, Vector<T>& m1);
	static Vector<T> Multiply(Vector<T>& m1, T v2);
	static Vector<T> Divide(Vector<T>& m1, T v2);
	static Vector<T> Divide(T v2, Vector<T>& m1);


// Add vector to "this" vector
	Vector<T>& Add(Vector<T>& m);
	Vector<T>& Subtract(Vector<T>& m);
	Vector<T>& Multiply(Vector<T>& m);
	Vector<T>& Divide(Vector<T>& m);

// Add value to "this" vector
	Vector<T>& Add(T v);
	Vector<T>& Subtract(T v);
	Vector<T>& Multiply(T v);
	Vector<T>& Divide(T v);


//OPERATORS
	Vector<T>& operator= (Vector<T>& m);
	Vector<T>& operator= (Matrix<T>& m);
	//Vector<T>& operator= (Array<T>& m);

	Vector<T>& operator= (string str);

	Vector<T> operator+ ();
	Vector<T> operator- ();
	Vector<int> operator! ();

	friend Matrix<T> operator, (Vector<T>& m1, Vector<T>& m2)
	{
		return Matrix<T>::Cat(2,m1, m2);
	}
	



	friend ostream& operator<< (ostream& output, const Vector<T>& v)
	{
		int lm = v.Length();
		int rowNoWidth = (int)log10((double)(lm-1))+2;
		int maxLength  = 1;
		for(int i=0; i<v.Length(); i++)
		{
			ostringstream oS;
			oS << v.data[i];
			int l = (int)oS.str().length();
			if(l>maxLength)
			{
				maxLength = l;
			}
		}

		output << typeid(v).name() << " of size " << v.length << endl;
		output << "----------------------" << endl;
		for(int i=0;i<v.length;i++)
		{
			output << "ROW" << setw(rowNoWidth) << i+1 << "|" << right << setw(maxLength+3) << v.data[i] << " |" << endl;
		}
		output << "----------------------" << endl;
		output << endl;
		return output;
	}
	

	T& operator() (const int i);
	T& operator[] (const int i); // no bounds check
		
	// slice x slice: returns a vector.
	Vector<T> operator() (int start, int end);
	Vector<T> operator() (string str);

	Vector<T> operator() (Vector<int>& ind);
	
	T& Elem(const int i);
	T& ElemNC(const int i); // No bounds check
	
	
	// Vector to Vector inner product
	friend T operator& (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::Inner(m1, m2);
	}
	
	
	// Boolean operations
	friend Vector<int> operator&& (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::And(m1, m2);
	}

	friend Vector<int> operator|| (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::Or(m1, m2);
	}

	friend Vector<int> operator< (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::Lt(m1, m2);
	}

	friend Vector<int> operator> (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::Gt(m1, m2);
	}

	friend Vector<int> operator<= (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::Le(m1, m2);
	}

	friend Vector<int> operator>= (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::Ge(m1, m2);
	}

	friend Vector<int> operator== (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::Eq(m1, m2);
	}

	friend Vector<int> operator!= (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::Ne(m1, m2);
	}


	// Boolean operations with value type
	friend Vector<int> operator&& (Vector<T>& m, T v)
	{
		return Vector<T>::And(m, v);
	}
	friend Vector<int> operator&& (T v, Vector<T>& m)
	{
		return Vector<T>::And(m, v);
	}

	friend Vector<int> operator|| (Vector<T>& m, T v)
	{
		return Vector<T>::Or(m, v);
	}
	friend Vector<int> operator|| (T v, Vector<T>& m)
	{
		return Vector<T>::Or(m, v);
	}

	friend Vector<int> operator< (Vector<T>& m, T v)
	{
		return Vector<T>::Lt(m, v);
	}
	friend Vector<int> operator< (T v, Vector<T>& m)
	{
		return Vector<T>::Gt(m, v);
	}

	friend Vector<int> operator> (Vector<T>& m, T v)
	{
		return Vector<T>::Gt(m, v);
	}
	friend Vector<int> operator> (T v, Vector<T>& m)
	{
		return Vector<T>::Lt(m, v);
	}

	friend Vector<int> operator<= (Vector<T>& m, T v)
	{
		return Vector<T>::Le(m, v);
	}
	friend Vector<int> operator<= (T v, Vector<T>& m)
	{
		return Vector<T>::Ge(m, v);
	}

	friend Vector<int> operator>= (Vector<T>& m, T v)
	{
		return Vector<T>::Ge(m, v);
	}
	friend Vector<int> operator>= (T v, Vector<T>& m)
	{
		return Vector<T>::Le(m, v);
	}

	friend Vector<int> operator== (Vector<T>& m, T v)
	{
		return Vector<T>::Eq(m, v);
	}
	friend Vector<int> operator== (T v, Vector<T>& m)
	{
		return Vector<T>::Eq(m, v);
	}

	friend Vector<int> operator!= (Vector<T>& m, T v)
	{
		return Vector<T>::Ne(m, v);
	}
	friend Vector<int> operator!= (T v, Vector<T>& m)
	{
		return Vector<T>::Ne(m, v);
	}



// Add Vector to another Vector
	friend Vector<T> operator+ (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::Add(m1, m2);
	}

	friend Vector<T> operator- (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::Subtract(m1, m2);
	}

	friend Vector<T> operator* (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::Multiply(m1, m2);
	}

	friend Vector<T> operator/ (Vector<T>& m1, Vector<T>& m2)
	{
		return Vector<T>::Divide(m1, m2);
	}


// Add value to another matrix
	friend Vector<T> operator+ (Vector<T>& m, T v)
	{
		return Vector<T>::Add(m, v);
	}

	friend Vector<T> operator+ (T v, Vector<T>& m)
	{
		return Vector<T>::Add(m, v);
	}

	friend Vector<T> operator- (Vector<T>& m, T v)
	{
		return Vector<T>::Subtract(m, v);
	}

	friend Vector<T> operator- (T v, Vector<T>& m)
	{
		return Vector<T>::Subtract(v, m);
	}

	friend Vector<T> operator* (Vector<T>& m, T v)
	{
		return Vector<T>::Multiply(m, v);
	}

	friend Vector<T> operator* (T v, Vector<T>& m)
	{
		return Vector<T>::Multiply(m, v);
	}

	friend Vector<T> operator/ (Vector<T>& m, T v)
	{
		return Vector<T>::Divide(m, v);
	}

	friend Vector<T> operator/ (T v, Vector<T>& m)
	{
		return Vector<T>::Divide(v, m);
	}




// Add inline
	Vector<T>& operator+= (Vector<T>& m);
	Vector<T>& operator-= (Vector<T>& m);
	Vector<T>& operator*= (Vector<T>& m);
	Vector<T>& operator/= (Vector<T>& m);

	Vector<T>& operator+= (T v);
	Vector<T>& operator-= (T v);
	Vector<T>& operator*= (T v);
	Vector<T>& operator/= (T v);

	
// TYPE CONVERSIONS
	//Vector(Array<T> &m);
	Vector(Matrix<T> &m);

};




#include "./Vector.inl"


}; //namespace



#endif







⌨️ 快捷键说明

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