📄 utlist.h
字号:
//Remember to clear previous and the next pointers in the link
if (*ppLink)
{
(*ppLink)->m_pNextLink = NULL;
(*ppLink)->m_pPrevLink = NULL;
}
return bReturn;
} //End UTStaticList::RemoveLast
//---------------------------------------------------------------------------
// @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 UTStaticList< T >::FirstElement
(
UTLink <T> ** ppLink // @parm [out] location to put first element if available
)
{
BOOL bReturn = FALSE;
//If the first elment exists then return TRUE else FALSE
if (m_pFirstLink)
{
*ppLink = m_pFirstLink;
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 UTStaticList< T >::LastElement
(
UTLink<T> ** ppLink // @parm [out] location to put last element if available
)
{
BOOL bReturn = FALSE;
if (m_pLastLink)
{
*ppLink = m_pLastLink;
bReturn = TRUE;
}
return bReturn;
}
//---------------------------------------------------------------------------
// @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 UTStaticList< T >::IsEmpty
(
void
) const
{
return (m_ulCount == 0);
} //End UTStaticList::IsEmpty
//---------------------------------------------------------------------------
// @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 UTStaticList< T >::GetCount
(
void
) const
{
return (m_ulCount);
} //End UTStaticList::GetCount
//---------------------------------------------------------------------------
// @mfunc Constructors
//
// @tcarg class | T | data type to iterate over
//
// @syntax UTStaticListIterator< T >::UTStaticListIterator(UTStaticList< T >&)
//
// @rdesc None.
//---------------------------------------------------------------------------
template <class T> UTStaticListIterator< T >::UTStaticListIterator
(
UTStaticList< T >& StaticList // @parm [in] list reference for list to be iterated upon
)
: m_StaticList(StaticList)
{
Init();
}
//---------------------------------------------------------------------------
// @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> UTStaticListIterator< T >::~UTStaticListIterator
(
void
)
{
// do nothing
}
//---------------------------------------------------------------------------
// @mfunc This is the assignment operator.
//
// @tcarg class | T | data type to iterate over
//
// @syntax UTStaticListIterator< T >& UTStaticListIterator< T >
// ::operator =(const UTListIterator< T >&)
//
// @rdesc Returns a reference to the newly assigned object
//---------------------------------------------------------------------------
template <class T> UTStaticListIterator< T >& UTStaticListIterator< T >::operator =
(
const UTStaticListIterator< 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_StaticList = AssignListItr.m_StaticList;
m_pPreviousLink = AssignListItr.m_pPreviousLink;
m_pCurrentLink = AssignListItr.m_pCurrentLink;
}
return *this;
} //End UTStaticListIterator
//---------------------------------------------------------------------------
// @tcarg class | T | data type to iterate over
//
// @syntax UTStaticListIterator< T >& UTStaticListIterator< T >::operator =(T)
//
// @rdesc None.
//---------------------------------------------------------------------------
template <class T> void UTStaticListIterator< T >::operator =(
T newValue // @parm [in] value to place in link at current position
)
{
//CAUTION : -- Don't use this THIS WILL CRASH
UTLink <short> * pLink = 0;
pLink->m_Value;
}
//---------------------------------------------------------------------------
// @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 UTStaticListIterator< T >::Init
(
void
)
{
m_pPreviousLink = NULL;
m_pCurrentLink = m_StaticList.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 UTStaticListIterator< T >::operator ()
(
void
) const
{
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 UTStaticListIterator< 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 UTStaticListIterator< 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;
}
return (m_pCurrentLink != NULL);
}
//---------------------------------------------------------------------------
// @mfunc This operator decrements the iterator to the prev element in the
// list and returns an indication of whether or not the front 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
//---------------------------------------------------------------------------
template <class T> BOOL UTStaticListIterator< T >::operator --
(
int dummy // @parm [in] dummy so that operator is on right hand side
)
{
m_pCurrentLink = m_pPreviousLink;
if (m_pCurrentLink)
{
m_pPreviousLink = m_pCurrentLink->m_pPrevLink;
}
return (m_pCurrentLink != NULL);
}
//---------------------------------------------------------------------------
// @mfunc RemoveCurrent deletes the element at the position of the
// iterator returning the deleted value through a CALLER
// allocated parameter. <nl>
// It then fixes the current position to the following: <nl><nl>
// If we... The we... <nl>
// 1) removed any link but 1st one old previous position->next <nl>
// 2) removed first link(links remain) new first position <nl>
// 3) removed only link NULL (no current position) <nl>
//
// @tcarg class | T | data type to iterate over
//
// @rdesc Returns a BOOL
// @flag TRUE | if current element exists and was removed
// @flag FALSE | otherwise
//---------------------------------------------------------------------------
template <class T> BOOL UTStaticListIterator< T >::RemoveCurrent
(
UTLink <T> ** ppLink // @parm [out] location to put current element if available
)
{
BOOL bReturn = FALSE;
if (m_pCurrentLink) // remove ONLY if iterator is still in valid position
{
if (m_pPreviousLink == NULL) // removing first element
{
if (m_pCurrentLink->m_pNextLink == NULL) // ONLY element
{
m_StaticList.m_pFirstLink = m_StaticList.m_pLastLink = NULL;
}
else // first, but not last element
{
m_StaticList.m_pFirstLink = m_pCurrentLink->m_pNextLink;
m_pCurrentLink->m_pNextLink->m_pPrevLink = NULL;
}
}
else // not removing first element
{
if (m_pCurrentLink->m_pNextLink == NULL) // last element
{
m_StaticList.m_pLastLink = m_pPreviousLink;
m_pPreviousLink->m_pNextLink = NULL;
}
else // neither first nor last element
{
m_pPreviousLink->m_pNextLink = m_pCurrentLink->m_pNextLink;
m_pCurrentLink->m_pNextLink->m_pPrevLink = m_pPreviousLink;
}
}
*ppLink = m_pCurrentLink;
(*ppLink)->m_pNextLink = 0;
(*ppLink)->m_pPrevLink = 0;
bReturn = TRUE;
m_StaticList.m_ulCount--;
// now fix up the current iterator pointer
if (m_pPreviousLink)
{
m_pCurrentLink = m_pPreviousLink->m_pNextLink;
}
else if (m_StaticList.m_pFirstLink)
{
m_pCurrentLink = m_StaticList.m_pFirstLink;
}
else
{
m_pCurrentLink = NULL;
}
}
return bReturn;
} //End RemoveCurrent
//---------------------------------------------------------------------------
// @mfunc This method resets the iterator at the value passed
// in as an argument if it exists in the list and returns
// TRUE in that case indicating that it did change position.
// If the value does not exist, the position remains
// unchanged, and FALSE is returned to indicate this.
//
// @tcarg class | T | data type to iterate over
//
// @rdesc Returns a BOOL
// @flag TRUE | if posistion has changed
// @flag FALSE | otherwise
//---------------------------------------------------------------------------
template <class T> BOOL UTStaticListIterator< T >::SetPosAt
(
const T& value //@parm [in] if this value is in list, set up iterator at its location
)
{
BOOL bReturn = FALSE;
UTLink< T > * pLink = m_StaticList.m_pFirstLink;
while (pLink && pLink->m_Value != value)
pLink = pLink->m_pNextLink;
if (pLink) // found value in list
{
m_pCurrentLink = pLink;
m_pPreviousLink = pLink->m_pPrevLink;
bReturn = TRUE;
}
return bReturn;
} //End SetPosAt
#endif // __UTLIST_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -