Hey everybody, I've decided to give DGDK another try now that it's been officially updated to support VS2010. I've recently been trying to make a "find free object()"(from the Matrix1Utils) type of function/class utilizing std::list, but I've hit a roadblock.
Here's my function:
unsigned int generateObj(bool isLinear)
{
//need to figure out why function won't fix holes like 1,2,3,4,6,7,8,9,...
unsigned int tmp;
std::list<unsigned int>::iterator test;
test=obj.end();
obj.sort();
if(isLinear)
{
if(obj.empty())
{
obj.push_back(1);
return 1;
}
for(objIT=obj.begin();objIT!=obj.end();objIT++)
{
tmp=*objIT;
(objIT)++;
if(objIT!=obj.end())
{
if((*objIT-tmp)>1)
{
objIT--;
obj.insert(objIT,tmp+1);
return (tmp+1);
break;
}
if((*objIT)==obj.back())
{
obj.push_back((*objIT)+1);
return ((*objIT)+1);
break;
}
}
else
{
obj.push_back(tmp+1);
return (tmp+1);
}
}
}
return 0;
}
This works fine in the fact that it generates a linear list of numbers at first. What I also want it to do is fill any "holes" in the list made by the list members being deleted. It's having some problems doing that, such as I'll call this function 11 times at first, then delete the elements 2,5,10. Somehow, the variable tmp never equals 4, it always skips to 6 which prevents the hole from getting fixed.
Here's my whole main.cpp
#include <iostream>
#include <fstream>
#include <list>
#include <string>
//#include <stdlib.h>
#include <algorithm>
#include <boost\lexical_cast.hpp>
class cResource
{
public:
cResource()
{
objIT=obj.begin();
}
unsigned int generateObj(bool isLinear)
{
//need to figure out why function won't fix holes like 1,2,3,4,6,7,8,9,...
unsigned int tmp;
std::list<unsigned int>::iterator test;
test=obj.end();
obj.sort();
if(isLinear)
{
if(obj.empty())
{
obj.push_back(1);
return 1;
}
for(objIT=obj.begin();objIT!=obj.end();objIT++)
{
tmp=*objIT;
(objIT)++;
if(objIT!=obj.end())
{
if((*objIT-tmp)>1)
{
objIT--;
obj.insert(objIT,tmp+1);
return (tmp+1);
break;
}
if((*objIT)==obj.back())
{
obj.push_back((*objIT)+1);
return ((*objIT)+1);
break;
}
}
else
{
obj.push_back(tmp+1);
return (tmp+1);
}
}
}
return 0;
}
void deleteObj(int iID)
{
obj.remove(iID);
}
public:
std::list<unsigned int> obj;
std::list<unsigned int>::iterator objIT;
}resource;
unsigned int makeObject(int meshID, int imgID)
{
return resource.generateObj(true);
}
int main()
{
for(int x=0; x<11; x++)
{
std::cout<<makeObject(0,0)<<" ";
}
resource.deleteObj(2);
resource.deleteObj(7);
resource.deleteObj(10);
std::cout<<std::endl;
for(resource.objIT=resource.obj.begin();resource.objIT!=resource.obj.end();resource.objIT++)
{
std::cout<<*resource.objIT<<" ";
}
std::cout<<std::endl<<resource.generateObj(true);
std::cout<<std::endl<<resource.generateObj(true);
std::cout<<std::endl<<resource.generateObj(true);
std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
return 0;
}
Any help would be greatly appreciated.