C++로 자료구조 구현하기

C++ Code

template <class T>
class ASC {
public:
	bool operator() (const T& t1, const T& t2) const  { return t1 < t2; }
};

template <class T>
class EQ { // EE -> EQ!
public:
	bool operator() (const T& t1, const T& t2) const { return t1 == t2; }
};


template <class T, class COMP = ASC<T>, class COMP2 = ASC<int>, class EE = EQ<T> >
class WrapForInfinity
{
	enum Op{ MIF = 0, GR = 1, IF = 2 };
	COMP comp;
	COMP2 comp2;
	EE ee;
	Op op; // Option
private:
	explicit WrapForInfinity(const T& val, const Op& op) : val(val), op(op) { }
public:
	explicit WrapForInfinity(const T& val = T()) : val(val), op(GR) { }
public:
	T val;
	bool operator>(const WrapForInfinity<T,COMP,COMP2,EE>& wfi)const
	{
		return wfi < (*this);
	}
	bool operator<(const WrapForInfinity<T,COMP,COMP2,EE>& wfi)const
	{
		if (GR == this->op&& GR == wfi.op)
		{
			return comp(this->val, wfi.val);
		}
		else
		{
			return comp2( this->op, wfi.op ); /// chk...
		}
	}
	bool operator==(const WrapForInfinity<T,COMP,COMP2,EE>& wfi)const
	{
		if( wfi.op == IF && this->op == IF ) { return true; }
		if( wfi.op == MIF && this->op == MIF ) { return true; }
		return (ee(wfi.val, this->val) && wfi.op == this->op);
	}
	bool operator!=( const WrapForInfinity<T,COMP,COMP2,EE>& wfi)const
	{
		return !( *this == wfi );
	}
	bool operator<=(const WrapForInfinity<T,COMP,COMP2,EE>& wfi)const
	{
		return *this < wfi ||
			*this == wfi;
	}
	bool operator>=(const WrapForInfinity <T,COMP, COMP2, EE>& wfi)const
	{
		return wfi <= *this;
	}
	WrapForInfinity<T> operator+(const WrapForInfinity<T>& other)const
	{
		if (other.op == this->op) {
			return WrapForInfinity<T>(this->val + other.val, this->op);
		}
		else if (this->op == MIF && other.op == GR) {
			return WrapForInfinity<T>::GetMinusInfinity();
		}
		else if (this->op == IF && other.op == GR) {
			return WrapForInfinity<T>::GetInfinity();
		}
		else if (this->op == GR && other.op == IF) {
			return WrapForInfinity<T>::GetInfinity();
		}
		else if (this->op == GR && other.op == MIF) {
			return WrapForInfinity<T>::GetMinusInfinity();
		}
		else if (this->op == MIF && other.op == MIF) {
			return WrapForInfinity<T>::GetMinusInfinity();
		}
		else if (this->op == IF && other.op == IF) {
			return WrapForInfinity<T>::GetInfinity();
		}
		else
		{
			throw string(" + Error in wrapforinfiinity");
		}
	}
	WrapForInfinity<T> operator-(const WrapForInfinity<T>& other)const
	{
		if (other.op == this->op && this->op == GR) {
			return WrapForInfinity<T>(this->val - other.val, this->op);
		}
		else if (this->op == MIF && other.op == GR) {
			return WrapForInfinity<T>::GetMinusInfinity();
		}
		else if (this->op == GR && other.op == MIF) {
			return WrapForInfinity<T>::GetInfinity();
		}
		else if (this->op == IF && other.op == GR) {
			return WrapForInfinity<T>::GetInfinity();
		}
		else if (this->op == GR && other.op == IF) {
			return WrapForInfinity<T>::GetMinusInfinity();
		}
		else if (this->op == MIF && other.op == IF) {
			return WrapForInfinity<T>::GetMinusInfinity();
		}
		else if (this->op == IF && other.op == MIF) {
			return WrapForInfinity<T>::GetInfinity();
		}
		else
		{
			throw string(" - Error in wrapforinfiinity");
		}
	}
	string toString()const
	{
		if (this->op == MIF) return "minus infinity";
		else if (this->op == IF) return "plus infinity";
		else return wiz::toStr(this->val);
	}

	friend
	ostream& operator<<(ostream& stream, const WrapForInfinity<T,COMP,COMP2,EE>& wfi)
	{
		if( wfi.op == MIF ) stream << "minus infinity";
		else if(wfi.op == IF ) stream << "plus infinity";
		else stream << wfi.val;

		return stream;
	}
	static WrapForInfinity<T, COMP, COMP2,EE > GetInfinity()
	{
		return WrapForInfinity<T, COMP, COMP2, EE >(T(), IF);
	}
	static WrapForInfinity<T, COMP, COMP2, EE > GetMinusInfinity()
	{
		return WrapForInfinity<T, COMP, COMP2, EE >(T(), MIF);
	}
};

댓글

댓글 본문
작성자
비밀번호
버전 관리
vztpv
현재 버전
선택 버전
graphittie 자세히 보기