Game Development Community

Sort on Insert

by Demolishun · in Torque Game Engine · 02/10/2007 (7:12 pm) · 1 replies

I have been developing (I think it works good now) a sort on insert function for my vector based class I am building in TGE. Here it goes:
void VirtualCellList::add(VirtualCell *object)
{
   U32 index;   
   S32 result;
   S32 limit;
   U32 min;
   U32 max;
   VirtualCell *a;
   
   index = 0;
   limit = 64; 
   if(size())
   {
      a = (VirtualCell *)mArray[index];
   
      // if in front
      if(comparePos((void*) &object,(void*) &(mArray[0])) < 0)
      {
         insert(begin(),object);
         //Con::printf("begin() Insert: %d,%d : %d,%d", (S32)object->mPos.x, (S32)object->mPos.y,
                                                   //(S32)a->mPos.x, (S32)a->mPos.y);
         return;
      }
      // if in back
      if(comparePos((void*) &object,(void*) &(mArray[size()-1])) > 0)
      {
         insert(end(),object);
         //Con::printf("end() Insert: %d,%d : %d,%d", (S32)object->mPos.x, (S32)object->mPos.y,
                                                   //(S32)a->mPos.x, (S32)a->mPos.y);
         return;
      }
      //index = 0;
      min = 0;
      max = size(); // size() - 1 introduces errors in large lists
      index = (max - min)/2 + min;     
      limit = 64;  // should be very hard to reach    
      while((index != max && index != min) && limit)
      {         
         result = comparePos((void*) &object,(void*) &(mArray[index]));
         //Con::printf("step:%d, index:%d, min:%d, max:%d, result:%d",20 - limit,index,min,max,result);
         if(result < 0)
         {
            max = index;
         }
         else if(result > 0)
         {
            min = index;
         }
         else
         {
            Con::printf("VirtualCellList::add - Cell exists, aborting add: %d, %d",(S32)object->mPos.x, (S32)object->mPos.y);
            return;
         }         
         
         //index = (max - min)/2 + min; 
         // round off loses up to 1 cell in the span of remaining cells.
         
         // below equation avoids roundoff error span loss
         index = max - (max - min)/2;
      
         limit--;
      }         
   }
      
   //Con::printf("VirtualCellList::add %d,%d",(S32)object->mPos.x,(S32)object->mPos.y);
   //if(size())
   //   a = (VirtualCell *)mArray[index];
   insert(begin()+index,object);
   if(!limit)
   {
      a = (VirtualCell *)mArray[index];
      Con::printf("Limit Reached!: %d,%d : %d,%d", (S32)object->mPos.x, (S32)object->mPos.y,
                                                   (S32)a->mPos.x, (S32)a->mPos.y);
   }   
   //Con::printf("Insert: %d,%d : %d,%d", (S32)object->mPos.x, (S32)object->mPos.y,
                                        //(S32)a->mPos.x, (S32)a->mPos.y);
                                        
   //Con::printf("%d,%d : %d,%d - min: %d, max: %d, index: %d",
   //            (S32)object->mPos.x, (S32)object->mPos.y,
   //            (S32)a->mPos.x, (S32)a->mPos.y, min, max, index);
}

I also built a method for sorting my objects that uses the dQsort function in TGE. This works really well, but I wanted to be able to sort my objects as I insert them. That is why I built the function above. From what I can tell it works now. I did a test where I compare every element to the next element using the compare function I built to use with the dQsort based function. I believe that is pretty good test, and I have tested that functionality with more approximately 3000 objects so far and the I fixed a couple of problems but it seems work well now. Yes, I am leading up to a question.

About the author

I love programming, I love programming things that go click, whirr, boom. For organized T3D Links visit: http://demolishun.com/?page_id=67


#1
02/10/2007 (7:13 pm)
1. Is there another built in function/class/etc that already does this?
2. Is it going to be faster to "sort on insert" vs copying a non-ordered list and running the sort function based on the dQsort function?

Anyway, feel free to use, play, change the code above for your own purposes. I am not going to clean it up right now so you will have to remove the commented code (not needed) and make it fit your app if you are going to use it. You are also going to have to build your own compare function "comparePos" as mine is geared toward my objects I have built and not for standard TGE objects.

Thanks,
Frank