Click here to Skip to main content
Click here to Skip to main content

An Addition to Smart List classes

, 22 Jul 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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

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)

Share

About the Author

Nelek
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 have worked a time with VC++ 6 but I now am more orientated to industry and roboter programming.
 
At the moment learning C# in my spare time (which is not so much)

Comments and Discussions

 
GeneralMy vote of 5 PinmvpMichael Haephrati7-Jun-13 10:00 
GeneralRe: My vote of 5 PinmemberNelek8-Jun-13 5:14 
GeneralMy vote of 5 PinmvpMichael Haephrati15-Mar-13 3:44 
Great work, Nelek!
GeneralRe: My vote of 5 PinmemberNelek15-Mar-13 6:33 
GeneralA minor improvement PinmemberSteve Mayfield9-Apr-07 17:12 
GeneralRe: A minor improvement PinmemberNelek10-Apr-07 2:09 
GeneralLooks useful PinmvpHans Dietrich2-Apr-07 16:47 
GeneralRe: Looks useful PinmemberGoran Mitrovic3-Apr-07 1:48 
GeneralRe: Looks useful PinmvpStephen Hewitt10-Apr-07 15:06 
GeneralRe: Looks useful [modified] PinmemberNelek10-Apr-07 20:53 
GeneralRe: Looks useful [modified] PinmemberNelek3-Apr-07 6:22 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411022.1 | Last Updated 22 Jul 2008
Article Copyright 2007 by Nelek
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid