Click here to Skip to main content
Licence CPOL
First Posted 2 Apr 2007
Views 20,150
Downloads 69
Bookmarked 16 times

An Addition to Smart List classes

By | 22 Jul 2008 | Article
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)

About the Author

Nelek

Engineer

Germany Germany

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralA minor improvement PinmemberSteve Mayfield16:12 9 Apr '07  
GeneralRe: A minor improvement PinmemberNelek1:09 10 Apr '07  
GeneralLooks useful PinmvpHans Dietrich15:47 2 Apr '07  
GeneralRe: Looks useful PinmemberGoran Mitrovic0:48 3 Apr '07  
GeneralRe: Looks useful PinmvpStephen Hewitt14:06 10 Apr '07  
GeneralRe: Looks useful [modified] PinmemberNelek19:53 10 Apr '07  
GeneralRe: Looks useful [modified] PinmemberNelek5:22 3 Apr '07  

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.

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