Jump to content
  • Advertisement

Archived

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

kelaklub

A better way??

This topic is 5329 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!