Archived

This topic is now archived and is closed to further replies.

kelaklub

A better way??

Recommended Posts

I wrote the following function as way to eliminate duplicate points that I stored inside an STL vector. Would anyone please be able to give me some ideas on ways that I could optimize this? Thank you.
template <typename tType>
void fn_eliminateDuplicatePoints(vector<cl_3dPoint<tType> >& vertices);

template <typename tType>
void fn_eliminateDuplicatePoints(vector<cl_3dPoint<tType> >& vertices)
{
    int i = 0;
    int j = 0;
    bool* duplicateFound = NULL;

	// GET THE TOTAL NUMBER OF POINTS

    int number_of_points = vertices.size();

    duplicateFound = new bool[number_of_points];

    // INITIALIZE OUR ARRAY OF FLAGS

    for( i = 0 ; i < number_of_points ; i = i + 1 )
    {
        duplicateFound[i] = false;
    }
    // TAG THE INDEXES OF THE ARRAY WHEREVER DUPLICATE POINTS OCCUR BY SETTING OUR BOOLEAN FLAG

    for( i = 0 ; i < number_of_points ; i = i + 1 )
    {
        for( j = i + 1 ; j <= number_of_points - 1 ; j = j + 1  )
        {
            if( ( vertices[i] == vertices[j] ) && ( duplicateFound[j] == false  ) )
            {
                duplicateFound[j] = true;
            }
        }
    }

	// GO THRU THE VECTOR CONTAINER ELIMINATING ALL DUPLICATE POINTS

    for( i = number_of_points - 1 ; i >= 0 ; i = i - 1 )
    {
        if( duplicateFound[i] == true )
        {
			vertices.erase(&vertices[i]);
        }
    }
    delete [] duplicateFound;  // DEALLOCATING OUR DYNAMICALLY ALLOCATED ARRAY

    duplicateFound = NULL;
}
[edited by - kelaklub on January 17, 2004 9:42:11 PM] [edited by - kelaklub on January 17, 2004 9:43:52 PM]

Share this post


Link to post
Share on other sites
Is the order of your points important? If it isn''t, it might just be easier to std::sort() the vector then use std::unique() to get rid of duplicate elements.

Share this post


Link to post
Share on other sites