Click here to Skip to main content
15,880,608 members
Articles / Desktop Programming / MFC
Article

An Addition to Smart List classes

Rate me:
Please Sign up or sign in to vote.
3.79/5 (18 votes)
22 Jul 2008CPOL 38.2K   232   17   11
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)


Written By
Engineer
Germany Germany
I come from Spain. After making a lot of silly things during the studies, I wanted to correct me and to make something positive with my life, so I asked for (and got) an “Erasmus” scholarship to go to Germany. After obtaining a placement to make my Thesis in a Firm of Automation and Software development, I reached the double Degree in Electronics’ Engineering and Informatic. I worked a time with VC++ 6, many years with industry PLC and robot programming, done backbone IT for something related to car electronics and production, back to my roots in C++ and PLC Programming again and currently working as system architect for my division.

I contrinue trying to learn more C# in my spare time (which is even less than before, God bless my kids)

Comments and Discussions

 
GeneralMy vote of 5 Pin
Michael Haephrati7-Jun-13 9:00
professionalMichael Haephrati7-Jun-13 9:00 
GeneralRe: My vote of 5 Pin
Nelek8-Jun-13 4:14
protectorNelek8-Jun-13 4:14 
GeneralMy vote of 5 Pin
Michael Haephrati15-Mar-13 2:44
professionalMichael Haephrati15-Mar-13 2:44 
GeneralRe: My vote of 5 Pin
Nelek15-Mar-13 5:33
protectorNelek15-Mar-13 5:33 
GeneralA minor improvement Pin
Steve Mayfield9-Apr-07 16:12
Steve Mayfield9-Apr-07 16:12 
In the operator function "=="

...
        //This presupposes that TYPE object has implemented the operator==
        if (thisTYPE == otherTYPE)
            nMatches++;
        else
            break;
...


this will exit out of the matching loop as soon as a mismatch is found potentially saving a lot of unnecessary comparisons

Steve
GeneralRe: A minor improvement Pin
Nelek10-Apr-07 1:09
protectorNelek10-Apr-07 1:09 
GeneralLooks useful Pin
Hans Dietrich2-Apr-07 15:47
mentorHans Dietrich2-Apr-07 15:47 
GeneralRe: Looks useful Pin
Goran Mitrovic3-Apr-07 0:48
Goran Mitrovic3-Apr-07 0:48 
GeneralRe: Looks useful Pin
Stephen Hewitt10-Apr-07 14:06
Stephen Hewitt10-Apr-07 14:06 
GeneralRe: Looks useful [modified] Pin
Nelek10-Apr-07 19:53
protectorNelek10-Apr-07 19:53 
GeneralRe: Looks useful [modified] Pin
Nelek3-Apr-07 5:22
protectorNelek3-Apr-07 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.