📄 afxtemplex.h
字号:
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - -
// - File: afxtemplex.h. -
// - -
// - Contents: Interface of class CArrayEx<TYPE, ARG_TYPE> and CSingletonArrayEx<TYPE, ARG_TYPE>. -
// - -
// - Purpose: - -
// - -
// - Remarks: - -
// - -
// - Originator: Michael Mogensen, MM-IT Consult 2003. -
// - -
// - Compiler: MS Visual C++ ver6.0. -
// - -
// - Period: 00.00.00 - 00.00.00. -
// - -
// - Version: 1.00. -
// - -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Miscellaneous. -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
#ifndef AFXTEMPLEX_H
#define AFXTEMPLEX_H
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - Header(s). -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
#include <afxtempl.h>
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - CArrayEx<TYPE, ARG_TYPE>. -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
template<class TYPE, class ARG_TYPE>
class CArrayEx : public CArray<TYPE, ARG_TYPE>
{
protected:
// 0. Data. (alphabetical).
bool m_bInNeedOfSort;
public:
// 0. Data. (alphabetical).
enum EDirection { eDir_Unknown, eDir_First, eDir_Prior2This, eDir_Next2This, eDir_Last };
// 1. Object. (alphabetical).
inline CArrayEx() :
m_bInNeedOfSort(false),
CArray<TYPE, ARG_TYPE>()
// Create.
{ SetSize(0); RemoveAll(); }
virtual ~CArrayEx()
// Destroy.
{ RemoveAll(); }
// 4. Slaves. (alphabetical).
int Add(ARG_TYPE Obj)
{
m_bInNeedOfSort = true;
return(CArray<TYPE, ARG_TYPE>::Add(Obj));
}
inline TYPE GetAt(int iId) const
// GetAt(...) in base dos not work in my MS VS 6.0!!!
{ return((*this)[iId]); }
inline const int GetLowerBound() const
{ return(IsEmpty() ? -1 : 0); }
inline ARG_TYPE GetNeighbourObj(const ARG_TYPE *pObj_Lookup, const enum EDirection iDir) const
// Return (copy of) first, prior to this, next to this or last obj. if possible. On error a default obj. are returned.
{
if(!IsEmpty())
{
switch(iDir)
{
case eDir_First:
return GetAt(GetLowerBound());
case eDir_Prior2This:
{
if(pObj_Lookup)
{
ARG_TYPE *pObj = NULL;
for(int iId = GetUpperBound(); iId >= GetLowerBound(); iId--)
if((pObj = &GetAt(iId)) && *pObj < *pObj_Lookup)
return *pObj;
}
break;
}
case eDir_Next2This:
{
if(pObj_Lookup)
{
ARG_TYPE *pObj = NULL;
for(int iId = GetLowerBound(); iId <= GetUpperBound(); iId++)
if((pObj = &GetAt(iId)) && *pObj > *pObj_Lookup)
return *pObj;
}
break;
}
case eDir_Last:
return GetAt(GetUpperBound());
}
}
// Ooops...
return ARG_TYPE();
}
inline const int GetObjPos(const ARG_TYPE &Obj) const
// Return pos. of this obj. in array and -1 on not present.
{
if(!IsEmpty())
{
for(int iId = GetLowerBound(); iId <= GetUpperBound(); iId++)
if(GetAt(iId) == Obj)
return iId;
}
return -1;
}
inline const bool HasObj(const ARG_TYPE &Obj) const
// Return T if this obj is contained in the array and F if not.
{ return(GetObjPos(Obj) > -1); }
inline const bool IsEmpty() const
// Return T if the array is empty and F if not.
{ return(GetUpperBound() == -1); }
inline const void Sort()
// Sort ascending.
{
if(IsEmpty() || !m_bInNeedOfSort)
return;
int iId_Best = 0;
ARG_TYPE Obj,
Obj_Best;
for(int iId_A = GetLowerBound(); iId_A <= GetUpperBound(); iId_A++)
{
Obj_Best = GetAt(iId_A);
iId_Best = iId_A;
for(int iId_B = iId_A + 1; iId_B <= GetUpperBound(); iId_B++)
{
Obj = GetAt(iId_B);
if(Obj < Obj_Best)
{
Obj_Best = Obj;
iId_Best = iId_B;
}
}
Swop(iId_A, iId_Best);
}
m_bInNeedOfSort = false;
}
inline const void Swop(const int iId_X, const int iId_Y)
// Change obj. at pos. X with obj. at pos. Y.
{
ARG_TYPE Obj_X(GetAt(iId_X));
SetAt(iId_X, GetAt(iId_Y));
SetAt(iId_Y, Obj_X);
}
};
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - CSingletonArrayEx<TYPE, ARG_TYPE>. -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
template<class TYPE, class ARG_TYPE>
class CSingletonArrayEx : public CArrayEx<TYPE, ARG_TYPE>
{
public:
// 1. Object. (alphabetical).
inline CSingletonArrayEx() :
CArrayEx<TYPE, ARG_TYPE>()
{}
virtual ~CSingletonArrayEx()
{ RemoveAll(); }
// 4. Slaves. (alphabetical).
inline int AddSingletonObj(const ARG_TYPE Obj)
// Add unique. Return pos. of new obj. or -1 if not present.
{
if(HasObj(Obj))
return -1;
return(CArrayEx<TYPE, ARG_TYPE>::Add(Obj));
}
inline const bool RemoveSingletonObj(const ARG_TYPE &Obj)
// Remove this obj. if present. Return T on succes and F if not. Note: No need of
// Sort(...) after this.
{
const int iPos = GetObjPos(Obj);
if(iPos == -1)
return false;
CArray<TYPE, ARG_TYPE>::RemoveAt(iPos);
return true;
}
};
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// - -
// ------------------------------------------------------------------------------------------------------------------------------------------------------
// 0. Data. (alphabetical).
// 1. Object. (alphabetical).
// 2. Event's. (alphabetical).
// 3.0. Menu choice. (menu order).
// 3.1. Menu choice, enablers. (menu order).
// 4. Slaves. (alphabetical).
// 5. Other. (alphabetical).
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -