📄 utlist.h
字号:
else // list is empty
{
m_pLastLink = new UTLink< T >(newValue, NULL, NULL);
if (m_pLastLink)
{
m_pFirstLink = m_pLastLink;
}
else
throw (EXCEPT_MEMORY);
}
m_ulCount++;
}
//---------------------------------------------------------------------------
// @mfunc This method inserts the new value at the end of the list.
//
// @tcarg class | T | data type to store in the list
//
// @rdesc None.
//---------------------------------------------------------------------------
template <class T> void UTList< T >::InsertLast(
const T& newValue // @parm [in] value to be inserted at end of list
)
{
UTLink< T > * pLink = m_pLastLink; // save ptr to link
if (pLink) // list is not empty
m_pLastLink = pLink->AddAfter(newValue);
else // list is empty
{
m_pLastLink = new UTLink< T >(newValue, NULL, NULL);
if (m_pLastLink)
{
m_pFirstLink = m_pLastLink;
}
else
throw (EXCEPT_MEMORY);
}
m_ulCount++;
}
//---------------------------------------------------------------------------
// @mfunc This method moves through the list and deletes each link and
// then resets the list count and pointers as they were set in
// the constructor.
//
// @tcarg class | T | data type to store in the list
//
// @rdesc None.
//---------------------------------------------------------------------------
template <class T> void UTList< T >::RemoveAll(
void
)
{
UTListIterator< T > itr((UTList< T >&) *this);
T tValue; // required for RemoveCurrent()
while (!itr)
itr.RemoveCurrent(&tValue); // this will reset itr to valid pos
}
//---------------------------------------------------------------------------
// @mfunc This method removes the first element from the list and fixes
// the pointer to the first element according to what remains
// of the list. The former first element is returned through a
// CALLER allocated variable.
//
// @tcarg class | T | data type to store in the list
//
// @rdesc Returns a BOOL
// @flag TRUE | if first element exists and is deleted
// @flag FALSE | otherwise
//---------------------------------------------------------------------------
template <class T> BOOL UTList< T >::RemoveFirst(
T* pType // @parm [out] location to put first element if available
)
{
BOOL bReturn = FALSE;
UTLink< T > * pLink = m_pFirstLink;
if (pLink)
{
// isolate the link that is about to be deleted
m_pFirstLink = m_pFirstLink->m_pNextLink;
if (m_pFirstLink)
m_pFirstLink->m_pPrevLink = NULL;
if (m_pLastLink == pLink)
m_pLastLink = NULL;
// prepare return value and delete node
*pType = pLink->m_Value;
bReturn = TRUE;
delete pLink;
m_ulCount--;
}
return bReturn;
}
//---------------------------------------------------------------------------
// @mfunc This method removes the last element from the list and fixes
// the pointer to the last element according to what remains
// of the list. The former last element is returned through a
// CALLER allocated variable.
//
// @tcarg class | T | data type to store in the list
//
// @rdesc Returns a BOOL
// @flag TRUE | if last element exists and is deleted
// @flag FALSE | otherwise
//
//---------------------------------------------------------------------------
template <class T> BOOL UTList< T >::RemoveLast(
T* pType // @parm [out] location to put first element if available
)
{
BOOL bReturn = FALSE;
UTLink< T > * pLink = m_pLastLink;
if (pLink)
{
// isolate the link that is about to be deleted
m_pLastLink = m_pLastLink->m_pPrevLink;
if (m_pLastLink)
m_pLastLink->m_pNextLink = NULL;
if (m_pFirstLink == pLink)
m_pFirstLink = NULL;
// prepare return value and delete node
*pType = pLink->m_Value;
bReturn = TRUE;
delete pLink;
m_ulCount--;
}
return bReturn;
}
//---------------------------------------------------------------------------
// @mfunc This method returns TRUE if there is a first element in the
// list and fills the out argument (CALLER allocated) with that
// first element value.
//
// @tcarg class | T | data type to store in the list
//
// @rdesc Returns a BOOL
// @flag TRUE | if first element exists
// @flag FALSE | otherwise
//
//---------------------------------------------------------------------------
template <class T> BOOL UTList< T >::FirstElement(
T* pType // @parm [out] location to put first element if available
) const
{
BOOL bReturn = FALSE;
if (m_pFirstLink)
{
*pType = m_pFirstLink->m_Value;
bReturn = TRUE;
}
return bReturn;
}
//---------------------------------------------------------------------------
// @mfunc This method returns TRUE if there is a last element in the
// list and fills the out argument (CALLER allocated) with that
// last element value.
//
// @tcarg class | T | data type to store in the list
//
// @rdesc Returns a BOOL
// @flag TRUE | if last element exists
// @flag FALSE | otherwise
//
//---------------------------------------------------------------------------
template <class T> BOOL UTList< T >::LastElement(
T* pType // @parm [out] location to put last element if available
) const
{
BOOL bReturn = FALSE;
if (m_pLastLink)
{
*pType = m_pLastLink->m_Value;
bReturn = TRUE;
}
return bReturn;
}
//---------------------------------------------------------------------------
// @mfunc This method checks the list for the given value and returns
// TRUE if that value is in the list.
//
// @tcarg class | T | data type to store in the list
//
// @rdesc Returns a BOOL
// @flag TRUE | if element exists in list
// @flag FALSE | otherwise
//
//---------------------------------------------------------------------------
template <class T> BOOL UTList< T >::Includes(
const T& value // @parm [in] does list include this value?
)
{
UTListIterator< T > itr((UTList< T >&) *this);
while (!itr && (itr() != value))
itr++;
return (!itr);
}
//---------------------------------------------------------------------------
// @mfunc This method returns TRUE if the count of elements in the list
// is ZERO.
//
// @tcarg class | T | data type to store in the list
//
// @rdesc Returns a BOOL
// @flag TRUE | if list is empty
// @flag FALSE | list is not empty
//---------------------------------------------------------------------------
template <class T> BOOL UTList< T >::IsEmpty(
void
) const
{
return (m_ulCount == 0);
}
//---------------------------------------------------------------------------
// @mfunc This method returns the member variable that indicates the
// count of elements. It is incremented whenever values are added
// and decremented whenever values are removed.
//
// @tcarg class | T | data type to store in the list
//
// @rdesc ULONG
//---------------------------------------------------------------------------
template <class T> ULONG UTList< T >::GetCount(
void
) const
{
return (m_ulCount);
}
//---------------------------------------------------------------------------
// @mfunc Constructors
//
// @tcarg class | T | data type to iterate over
//
// @syntax UTListIterator< T >::UTListIterator(UTList< T >&)
//
// @rdesc None.
//---------------------------------------------------------------------------
template <class T> UTListIterator< T >::UTListIterator(
UTList< T >& List // @parm [in] list reference for list to be iterated upon
)
: m_List(List)
{
Init();
}
//---------------------------------------------------------------------------
// @tcarg class | T | data type to iterate over
//
// @syntax UTListIterator< T >::UTListIterator(const UTListIterator< T >&)
//
// @rdesc None.
//---------------------------------------------------------------------------
template <class T> UTListIterator< T >::UTListIterator(
const UTListIterator< T >& CopyListItr // @parm [in] value to copy into this obj
)
: m_List(CopyListItr.m_List),
m_pPreviousLink(CopyListItr.m_pPreviousLink),
m_pCurrentLink(CopyListItr.m_pCurrentLink)
{
}
//---------------------------------------------------------------------------
// @mfunc This destruct does nothing because no memory needs to be deleted.
//
// @tcarg class | T | data type to iterate over
//
// @rdesc None.
//---------------------------------------------------------------------------
template <class T> UTListIterator< T >::~UTListIterator(
void
)
{
// do nothing
}
//---------------------------------------------------------------------------
// @mfunc This is the assignment operator.
//
// @tcarg class | T | data type to iterate over
//
// @syntax UTListIterator< T >& UTListIterator< T >::operator =(const UTListIterator< T >&)
//
// @rdesc Returns a reference to the newly assigned object
//---------------------------------------------------------------------------
template <class T> UTListIterator< T >& UTListIterator< T >::operator =(
const UTListIterator< T >& AssignListItr // @parm [in] value to copy into this obj
)
{
// make sure iterators don't already point to the same thing
if (m_pCurrentLink != AssignListItr.m_pCurrentLink)
{
m_List = AssignListItr.m_List;
m_pPreviousLink = AssignListItr.m_pPreviousLink;
m_pCurrentLink = AssignListItr.m_pCurrentLink;
}
return *this;
}
//---------------------------------------------------------------------------
// @tcarg class | T | data type to iterate over
//
// @syntax UTListIterator< T >& UTListIterator< T >::operator =(T)
//
// @rdesc None.
//---------------------------------------------------------------------------
template <class T> void UTListIterator< T >::operator =(
T newValue // @parm [in] value to place in link at current position
)
{
if (m_pCurrentLink)
m_pCurrentLink->m_Value = newValue;
}
//---------------------------------------------------------------------------
// @mfunc This method sets up the iterator by making its internal state
// point to the first element in the list.
//
// @tcarg class | T | data type to iterate over
//
// @rdesc Returns a BOOL
// @flag TRUE | if there is at least on element in the list which means
// there is something to iterate over
// @flag FALSE | if no elements are to be iterated upon
//---------------------------------------------------------------------------
template <class T> BOOL UTListIterator< T >::Init(
void
)
{
m_pPreviousLink = NULL;
m_pCurrentLink = m_List.m_pFirstLink;
return (m_pCurrentLink != NULL);
}
//---------------------------------------------------------------------------
// @mfunc This operator retrieves the element from the current position
// of the iterator.
//
// @tcarg class | T | data type to iterate over
//
// @rdesc T - current value where iterator is located
//---------------------------------------------------------------------------
template <class T> T UTListIterator< T >::operator ()(
void
) const
{
//UNDONE:
//assert(m_pCurrentLink != NULL);
if (m_pCurrentLink)
return (m_pCurrentLink->m_Value);
else // incorrect use of iterator, return "bogus" default value
{
T tBogusDefault = (T) 0;
return tBogusDefault;
}
}
//---------------------------------------------------------------------------
// @mfunc This operator determines if the current position is valid.
//
// @tcarg class | T | data type to iterate over
//
// @rdesc Returns a BOOL
// @flag TRUE | if iterator is in a valid position
// @flag FALSE | if iterator is NOT in a valid position
//---------------------------------------------------------------------------
template <class T> BOOL UTListIterator< T >::operator !(
void
) const
{
return (m_pCurrentLink != NULL);
}
//---------------------------------------------------------------------------
// @mfunc This operator increments the iterator to the next element in the
// list and returns an indication of whether or not the end of the
// list has been passed.
//
// @tcarg class | T | data type to iterate over
//
// @rdesc Returns a BOOL
// @flag TRUE | if new position is valid
// @flag FALSE | if new position is NOT valid (i.e. end of the list)
//---------------------------------------------------------------------------
template <class T> BOOL UTListIterator< T >::operator ++(
int dummy // @parm [in] dummy so that operator is on right hand side
)
{
if (m_pCurrentLink)
{
m_pPreviousLink = m_pCurrentLink;
m_pCurrentLink = m_pCurrentLink->m_pNextLink;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -