Jump to content
  • Advertisement

Archived

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

robert_s

Large objects - fast equality test

This topic is 5154 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

Hi. I am just wondering how to quickly compare two objects if they are equal or not. eg. if( object1 == object2 ) return true; I know I can add == comparison operator in my class: eg. class Object { int m_id; public: bool operator== (const Object & obj) { if (m_id == obj.m_id) { return TRUE; } } but then what do I do if I have a huge object with many members that store list of pointers and other objects or list of vertices storing 3000 vertices? I dont thik it would be wise to comapre each member (eg. vertex list vertex-by-vertex)in the Object class to determine if the two objects Obj1 and Obj2 are equal. It would take ages!! Is there any clever way of doing it. My problem is that I have a global list of Objects stored in vector container. vector<objects*>vlist; I select an object randomly from the list of objects and I pass that object to some other function. eg.
// 'loc_obj' is pointing at one of those objects in 'vlist' but I dont know at this point which object this is. I mean I dont know which index in the list it is.

bool ObjFound( Object *loc_obj)
{
   for( loop through vlist of objects to the end)
   {
      // see if loc_obj is one of those in the list

      if( vlist[i] == loc_obj )return true;
   }
}
I know the code is meaningless but its just a quick problem illustration How can I compare those objects fast? [edited by - robert_s on June 10, 2004 12:22:12 PM]

Share this post


Link to post
Share on other sites
Advertisement
quote:
but then what do I do if I have a huge object with many members that store list of pointers and other objects or list of vertices storing 3000 vertices?
I dont thik it would be wise to comapre each member (eg. vertex list vertex-by-vertex)in the Object class to determine if the two objects Obj1 and Obj2 are equal. It would take ages!!
Is there any clever way of doing it.

Don''t use the raw vertex data as a way of comparing your objects. Use some other form of ID system that only compares maybe a few members.

And if you sort your object list, you can use something besides a linear search, like a binary search.

Share this post


Link to post
Share on other sites
have a model ID in the model class (an int for example)
make sure this modelID is unique.

then comparing 2 models is just a single int comparision.


class Model
{
private:
static int s_modelID = 0;
int modelID;

Model()
{
s_modelID++;
modelID == s_modelID;
}

Model& operator==(Model& m)
{
return (m.modelID == this.modelID);
}
};


now there is plenty of things to keep into consideration, like threadsafety (will you create model instances from more then one thread? How does it behave under a copy etc, but this is the basic gist of it..

(pseudo code, not compiled)

Share this post


Link to post
Share on other sites
If you want to see if they''re the *EXACT* same object, you could use pointer comparisons.

If you just want to see if they''re "equivilent" (for something like eliminating duplicated objects), your best bet would be to
overload == to compare just certain data members.

By default, == compares the objects bit for bit, which is fine for objects that don''t contain pointers. An array of vertices would always fail the comparison, though, since every object would have a different pointer.

If you''re using this for comparing geometry, I''m going to go out on a limb here and say that you''re doing something wrong already. You should never have any need for 2 instances of the exact same geometry loaded into memory, unless you''re planning on altering one of them.

Share this post


Link to post
Share on other sites
Thank you.
Etnu. No, I dont have same object instances loaded in memory. I have unique objects loaded into a container and I manipulate each object using a pointer to each object. I guess comparing address is the easiest option as the objects are constants.
Thanks all.

Share this post


Link to post
Share on other sites

bool cBla::operator== (cBla *blub)
{
for(unsigned int i=sizeof(cBla), unsigned char *a = (unsigned char*)this, unsigned char* b = (unsigned char*)blub;i>0;--i,++a,++b)
if(*a != *b) return false;
return true;
};

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!