Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

An Addition to Smart List classes

3.79/5 (18 votes)
22 Jul 2008CPOL 1   232  
This article presents the code of Simon Hughes’ SmartList with some new functions

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

C++
BOOL FindAndRemoveElement (const ARG_TYPE searchValue);

BOOL FindAndReplaceElement (const ARG_TYPE searchValue, const ARG_TYPE 
    newValue);

BOOL operator== (const CMyList &temp)

// Find and Remove a TYPE object of the list, searching by ARG_TYPE
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)    // When found, remove element 
    {   
        RemoveAt(pos); 
        return TRUE;
    } 
    else 
        return FALSE; 
} 

// Find and Replace a TYPE object of the list, searching by ARG_TYPE
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)    // When found, replace old element with new one 
    {    
        SetAt (pos, newValue); 
        return TRUE;
    } 
    else 
        return FALSE; 
}

// Equality operator for the whole list (CMyList1 == CMyList2)
template <class TYPE, class ARG_TYPE> 
BOOL CMyList<TYPE, ARG_TYPE>::operator== (const CMyList &temp) 
{   
    ASSERT_VALID (this);
    ASSERT_VALID (&temp);

    int nMatches = 0; // To have the number of matches

    if(this == &temp) // Check for self assignment
        return TRUE;

    // If one list has different number of elements, can't be equal
    if (GetCount () != temp.GetCount ())
        return FALSE;

    POSITION posThis = GetHeadPosition ();
    POSITION posOther = temp.GetHeadPosition ();
    while ((posThis)&&(posOther))
    {    // This is to look for in the same place in both lists
        TYPE thisTYPE = (TYPE)GetNext(posThis);
        TYPE otherTYPE = (TYPE)temp.GetNext(posOther);

        //This presupposes that TYPE object has implemented the operator==
        if (thisTYPE == otherTYPE)
            nMatches++;
        else
        break;
    }
    // If all the objects in the list were equal… the lists are equal
    if (nMatches == GetCount ()) 
        return TRUE;
    return
        FALSE; 
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)