361 lines
12 KiB
C++
361 lines
12 KiB
C++
|
|
|
|
#ifndef __ZCPPMAIN_ZMAINAVL_H__
|
|
#define __ZCPPMAIN_ZMAINAVL_H__
|
|
|
|
|
|
#include "ZCppMain/ZMainHead.H"
|
|
|
|
|
|
namespace ZNsMain
|
|
{
|
|
|
|
namespace ZNsEnum
|
|
{
|
|
|
|
enum ZEAVL_INSERT
|
|
{
|
|
ZEAVL_INSERT_RIGHT = +1,
|
|
ZEAVL_INSERT_OK = 0, /* 삽입 후의 평형계수가 0 이다. */
|
|
ZEAVL_INSERT_LEFT = -1,
|
|
ZEAVL_INSERT_NONE = -2 /* 삽입 안 됨 */
|
|
};/*
|
|
enum ZEAVL_INSERT*/
|
|
|
|
}/*
|
|
namespace ZNsEnum*/
|
|
|
|
|
|
namespace ZNsInterface
|
|
{
|
|
|
|
template<typename TypeArg> class ZtCAVL_BASE
|
|
{
|
|
protected:
|
|
|
|
template<typename TNode> void OnEqual(TypeArg AR_Data, TNode* AP_Node)
|
|
{
|
|
}/*
|
|
template<typename TNode> void OnEqual(TypeArg AR_Data, TNode* AP_Node) */
|
|
|
|
template<typename TKey, typename TNode> void OnEqualKey(TKey AR_Key, TNode* AP_Node)
|
|
{
|
|
}/*
|
|
template<typename TKey, typename TNode> void OnEqualKey(TKey AR_Key, TNode* AP_Node) */
|
|
|
|
protected:
|
|
};/*
|
|
template<typename TypeArg> class ZtCAVL_BASE*/
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////////////////
|
|
|
|
■ 아래 ZtCAVL_NodeBase<> 의 각 멤버 함수는 ZtCAVL_BASE<> 의 멤버 함수보다
|
|
인수가 하나 줄어든 것을 볼 수 있다. ZtCAVL_BASE<> 에서 같은 이름의 함수를
|
|
자연스럽게 호출할 수 있게 한 것이다. 따라서 ZtCAVL_BASE<> 의 멤버 함수에
|
|
서, AP_Node->OnEqual() 등으로 ZtCAVL_NodeBase<> 의 멤버 함수를 호출해 주
|
|
지 않으면, ZtCAVL_NodeBase<> 의 멤버는 어디에서도 호출되지 않는다. 왜 최초
|
|
에 이런 식으로 설계했을까. 한참 생각했는데, 아마 ZtCAVL_Multi_BASE<> 와
|
|
ZtCAVL_Multi_NodeBase<> 를 사용할려고 그랬을 것이다.
|
|
|
|
-- 2012-09-03 19:20:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////*/
|
|
|
|
template<typename TNodeArg> class ZtCAVL_NodeBase
|
|
{
|
|
protected:
|
|
|
|
void OnEqual(TNodeArg AR_Data)
|
|
{
|
|
}/*
|
|
void OnEqual(TNodeArg AR_Data)*/
|
|
|
|
template<typename TKey> void OnEqualKey(TKey AR_Key)
|
|
{
|
|
}/*
|
|
template<typename TKey> void OnEqualKey(TKey AR_Key)*/
|
|
|
|
protected:
|
|
};/*
|
|
template<typename TNodeArg> class ZtCAVL_NodeBase*/
|
|
|
|
|
|
template<typename TypeArg> class ZtCAVL_Multi_BASE
|
|
{
|
|
protected:
|
|
|
|
template<typename TNode> void OnEqual(TypeArg AR_Data, TNode* AP_Node)
|
|
{
|
|
AP_Node->OnEqual(AR_Data);
|
|
}/*
|
|
template<typename TNode> void OnEqual(TypeArg AR_Data, TNode* AP_Node) */
|
|
|
|
template<typename TKey, typename TNode> void OnEqualKey(TKey AR_Key, TNode* AP_Node)
|
|
{
|
|
AP_Node->OnEqualKey(AR_Key);
|
|
}/*
|
|
template<typename TKey, typename TNode> void OnEqualKey(TKey AR_Key, TNode* AP_Node) */
|
|
|
|
protected:
|
|
};/*
|
|
template<typename TypeArg> class ZtCAVL_Multi_BASE*/
|
|
|
|
|
|
/*//////////////////////////////////////////////////////////////
|
|
|
|
■ ZtCAVL_Multi_NodeBase template 로 multi set/map 을 구성하는 경우,
|
|
ZtCAVL_Multi_NodeBase::TypeEqual 자료형에 접근하는 예.
|
|
|
|
class CTypeMy
|
|
{
|
|
public:
|
|
typedef CLargeAVL<some param> CLargeMap;
|
|
typedef typename CLargeMap::ZCNode::TypeEqual TypeEqual;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////*/
|
|
|
|
template< typename TNodeArg, typename TEqualList
|
|
>
|
|
class ZtCAVL_Multi_NodeBase ////////////////////
|
|
{
|
|
public :
|
|
typedef TEqualList TypeEqual;
|
|
protected:
|
|
TEqualList mo_EqualList;
|
|
public :
|
|
|
|
void OnEqual(TNodeArg AR_Data)
|
|
{
|
|
mo_EqualList.push_back(AR_Data);
|
|
}/*
|
|
void OnEqual(TNodeArg AR_Data)*/
|
|
|
|
template<typename TKey> void OnEqualKey(TKey AR_Key)
|
|
{
|
|
mo_EqualList.push_back(AR_Key);
|
|
}/*
|
|
template<typename TKey> void OnEqualKey(TKey AR_Key) */
|
|
|
|
const TEqualList& GetCEqualList() const
|
|
{
|
|
return mo_EqualList;
|
|
}/*
|
|
const TEqualList& GetCEqualList() const*/
|
|
|
|
public:
|
|
};/*
|
|
template< typename TNodeArg, typename TEqualList
|
|
>
|
|
class ZtCAVL_Multi_NodeBase //////////////////*/
|
|
|
|
|
|
|
|
/*/////////////////////////////////////////////////////////////////////
|
|
|
|
■ AVL 트리의 데이타 타입이 포인터인 경우, 포인터를 정수처럼 다루어서
|
|
비교 연산자를 정의하는 클래스 템플릿이다.
|
|
|
|
/////////////////////////////////////////////////////////////////////*/
|
|
|
|
template<typename TData> class ZtCAVL_PtrLongData
|
|
{
|
|
public :
|
|
typedef TData* TypeData;
|
|
protected:
|
|
TData* mp_CData;
|
|
public :
|
|
|
|
ZtCAVL_PtrLongData()
|
|
{
|
|
mp_CData=0;
|
|
}/*
|
|
ZtCAVL_PtrLongData()*/
|
|
|
|
TData* GetDataPtr() const
|
|
{
|
|
return mp_CData;
|
|
}/*
|
|
TData* GetDataPtr() const*/
|
|
|
|
void SetDataPtr(TData* AP_CData)
|
|
{
|
|
mp_CData=AP_CData;
|
|
}/*
|
|
void SetDataPtr(TData* AP_CData)*/
|
|
|
|
operator ZNsMain::ZTypIntPtr() const
|
|
{
|
|
return mp_CData;
|
|
}/*
|
|
operator ZNsMain::ZTypIntPtr() const*/
|
|
|
|
|
|
ZtCAVL_PtrLongData& operator=(const ZtCAVL_PtrLongData& rhs)
|
|
{
|
|
mp_CData=rhs.mp_CData; return *this;
|
|
}/*
|
|
ZtCAVL_PtrLongData& operator=(const ZtCAVL_PtrLongData& rhs)*/
|
|
|
|
ZtCAVL_PtrLongData& operator=(const TData* AP_CData)
|
|
{
|
|
mp_CData=const_cast<TData*>(AP_CData); return *this;
|
|
}/*
|
|
ZtCAVL_PtrLongData& operator=(const TData* AP_CData)*/
|
|
|
|
bool operator==(const ZtCAVL_PtrLongData& rhs) const{return mp_CData==rhs.mp_CData;}
|
|
bool operator> (const ZtCAVL_PtrLongData& rhs) const{return (ZNsMain::ZTypIntPtr)mp_CData > (ZNsMain::ZTypIntPtr)rhs.mp_CData;}
|
|
bool operator< (const ZtCAVL_PtrLongData& rhs) const{return (ZNsMain::ZTypIntPtr)mp_CData < (ZNsMain::ZTypIntPtr)rhs.mp_CData;}
|
|
|
|
bool operator==(const TData* AP_CData) const{return mp_CData==AP_CData;}
|
|
bool operator> (const TData* AP_CData) const{return (ZNsMain::ZTypIntPtr)mp_CData > (ZNsMain::ZTypIntPtr)AP_CData;}
|
|
bool operator< (const TData* AP_CData) const{return (ZNsMain::ZTypIntPtr)mp_CData < (ZNsMain::ZTypIntPtr)AP_CData;}
|
|
|
|
protected:
|
|
};/*
|
|
template<typename TData> class ZtCAVL_PtrLongData*/
|
|
|
|
|
|
/* int, long 같이 비교연산이 가능한 primitive 타입이나 object 를
|
|
key 로 가지고 있는 경우에, 해당 AVL 트리의 노드 데이타로 바로
|
|
쓸 수 있도록 wrap 한 클래스 템플릿 */
|
|
|
|
template< typename TKeyData ,
|
|
typename TMainData,
|
|
typename TKeyArg =const TKeyData &,
|
|
typename TMainArg=const TMainData&
|
|
>
|
|
class ZtCAVL_KeyData //////////////////////////
|
|
{
|
|
public :
|
|
typedef TKeyData TypeKey ;
|
|
typedef TMainData TypeData;
|
|
protected:
|
|
TKeyData mo_CKeyData ; mutable
|
|
TMainData mo_CMainData;
|
|
public :
|
|
|
|
ZtCAVL_KeyData(){}
|
|
|
|
ZtCAVL_KeyData(TKeyArg AO_TKeyArg)
|
|
{
|
|
mo_CKeyData=AO_TKeyArg;
|
|
}/*
|
|
ZtCAVL_KeyData(TKeyArg AO_TKeyArg)*/
|
|
|
|
ZtCAVL_KeyData(TKeyArg AO_TKeyArg, TMainArg AO_TMainArg)
|
|
{
|
|
mo_CKeyData =AO_TKeyArg ;
|
|
mo_CMainData=AO_TMainArg;
|
|
}/*
|
|
ZtCAVL_KeyData(TKeyArg AO_TKeyArg, TMainArg AO_TMainArg)*/
|
|
|
|
TKeyArg GetCKeyData () const{return mo_CKeyData ;}
|
|
TMainArg GetCMainData () const{return mo_CMainData;}
|
|
TMainData& GetCMainDataRef() const{return mo_CMainData;}
|
|
|
|
void SetCMainData(TMainArg AO_TRealArg) const
|
|
{
|
|
mo_CMainData=AO_TRealArg;
|
|
}/*
|
|
void SetCMainData(TMainArg AO_TRealArg) const*/
|
|
|
|
void SetKeyData(TKeyArg AO_TKeylArg)
|
|
{
|
|
mo_CKeyData=AO_TKeylArg;
|
|
}/*
|
|
void SetKeyData(TKeyArg AO_TKeylArg)*/
|
|
|
|
bool operator==(const ZtCAVL_KeyData& rhs) const{return mo_CKeyData==rhs.mo_CKeyData;}
|
|
bool operator> (const ZtCAVL_KeyData& rhs) const{return mo_CKeyData> rhs.mo_CKeyData;}
|
|
bool operator< (const ZtCAVL_KeyData& rhs) const{return mo_CKeyData< rhs.mo_CKeyData;}
|
|
|
|
bool operator==(TKeyArg AO_TPrimitive) const{return mo_CKeyData==AO_TPrimitive;}
|
|
bool operator> (TKeyArg AO_TPrimitive) const{return mo_CKeyData> AO_TPrimitive;}
|
|
bool operator< (TKeyArg AO_TPrimitive) const{return mo_CKeyData< AO_TPrimitive;}
|
|
|
|
ZtCAVL_KeyData& operator=(const ZtCAVL_KeyData& rhs)
|
|
{
|
|
if(this==&rhs) return *this;
|
|
|
|
mo_CKeyData =rhs.mo_CKeyData ;
|
|
mo_CMainData=rhs.mo_CMainData; return *this;
|
|
}/*
|
|
ZtCAVL_KeyData& operator=(const ZtCAVL_KeyData& rhs)*/
|
|
|
|
ZtCAVL_KeyData& operator=(TKeyArg AO_TPrimitive)
|
|
{
|
|
mo_CKeyData=AO_TPrimitive; return *this;
|
|
}/*
|
|
ZtCAVL_KeyData& operator=(TKeyArg AO_TPrimitive)*/
|
|
|
|
public:
|
|
};/*
|
|
template< typename TKeyData ,
|
|
typename TMainData,
|
|
typename TKeyArg =const TKeyData &,
|
|
typename TMainArg=const TMainData&
|
|
>
|
|
class ZtCAVL_KeyData ////////////////////////*/
|
|
|
|
}/*
|
|
namespace ZNsInterface*/
|
|
|
|
|
|
/*/////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
■ mulit set 으로 활용하는 예제코드
|
|
|
|
ZNsMain::CLargeAVL ///////////////////////////////////
|
|
<
|
|
int,
|
|
int,
|
|
ZNsMain::ZNsInterface::ZtCAVL_Multi_BASE<int>,
|
|
ZNsMain::ZNsInterface::ZtCAVL_Multi_NodeBase<int, ZNsMain::ZtCDoubleList<int> >
|
|
>
|
|
VO_CLargeAvl_Multi; //////////////////////////////////
|
|
|
|
VO_CLargeAvl_Multi.AddData(1);
|
|
VO_CLargeAvl_Multi.AddData(2);
|
|
VO_CLargeAvl_Multi.AddData(2);
|
|
VO_CLargeAvl_Multi.AddData(2);
|
|
VO_CLargeAvl_Multi.AddData(2);
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////*/
|
|
|
|
|
|
namespace NsType
|
|
{
|
|
|
|
template< typename TStringData, //= ZNsMain::ZtCStringBase<char> ,
|
|
typename TEnvVar , //= ZNsMain::ZNsCGI::ZNsBase::ZtCEnvVar<TStringData>,
|
|
typename TEnvVarSet //= ZNsMain::ZtCObjAVL<TEnvVar>
|
|
>
|
|
class ZtCTypeEnvVarSet /*############################################################*/
|
|
{
|
|
public:
|
|
typedef TStringData ZCStringData;
|
|
typedef TEnvVar ZCEnvVar ;
|
|
typedef TEnvVarSet ZCEnvVarSet ;
|
|
typedef typename ZCEnvVarSet ::ZCNode ZCEnvVarNode;
|
|
typedef typename ZCStringData::ZCMainChars ZCMainChars ;
|
|
typedef typename ZCStringData::TypeChar TypeChar ;
|
|
typedef typename ZCStringData::TypeLength TypeLength ;
|
|
public:
|
|
};/*
|
|
template< typename TStringData, //= ZNsMain::ZtCStringBase<char> ,
|
|
typename TEnvVar , //= ZNsMain::ZNsCGI::ZNsBase::ZtCEnvVar<TStringData>,
|
|
typename TEnvVarSet //= NsCPP::ZtCObjAVL<TEnvVar>
|
|
>
|
|
class ZtCTypeEnvVarSet /*############################################################*/
|
|
|
|
}/*
|
|
namespace NsType*/
|
|
|
|
}/*
|
|
namespace ZNsMain */
|
|
|
|
|
|
#endif // __ZCPPMAIN_ZMAINAVL_H__
|