Introduction
This article presents the code of Simon Hughes' SmartList with some new functions that I have added. This code is (as it was) completely free and can be used however you want, but please leave our (Simon's and mine) e-mail addresses in the code to receive possible bug-reports.
This article presents a number of list classes that encapsulate the MFC list classes with some new features. This code (with the new functions) is in some of my projects and has been fully tested. But, if anyone out there finds out any bugs or improvements please send them to me, I will correct them as soon as possible.
The New Functions
BOOL FindAndRemoveElement (const ARG_TYPE searchValue);
BOOL FindAndReplaceElement (const ARG_TYPE searchValue, const ARG_TYPE
newValue);
BOOL operator== (const CMyList &temp)
template <class TYPE, class ARG_TYPE>
BOOL CMyList<TYPE, ARG_TYPE>::FindAndRemoveElement(const ARG_TYPE
searchValue)
{
ASSERT_VALID (this);
POSITION pos = Find (searchValue);
if (pos != NULL) {
RemoveAt(pos);
return TRUE;
}
else
return FALSE;
}
template <class TYPE, class ARG_TYPE>
BOOL CMyList<TYPE, ARG_TYPE>::FindAndReplaceElement(const ARG_TYPE
searchValue, const ARG_TYPE newValue)
{
ASSERT_VALID (this);
POSITION pos = Find (searchValue);
if (pos != NULL) {
SetAt (pos, newValue);
return TRUE;
}
else
return FALSE;
}
template <class TYPE, class ARG_TYPE>
BOOL CMyList<TYPE, ARG_TYPE>::operator== (const CMyList &temp)
{
ASSERT_VALID (this);
ASSERT_VALID (&temp);
int nMatches = 0;
if(this == &temp) return TRUE;
if (GetCount () != temp.GetCount ())
return FALSE;
POSITION posThis = GetHeadPosition ();
POSITION posOther = temp.GetHeadPosition ();
while ((posThis)&&(posOther))
{ TYPE thisTYPE = (TYPE)GetNext(posThis);
TYPE otherTYPE = (TYPE)temp.GetNext(posOther);
if (thisTYPE == otherTYPE)
nMatches++;
else
break;
}
if (nMatches == GetCount ())
return TRUE;
return
FALSE;
}