Dark Bit Factory &  Gravity
		PROGRAMMING => General coding questions => Topic started by: Pixel_Outlaw on December 05, 2007
		
			
			- 
				Is it better to store objects in a linked list or constantly resize and adapt an array to hold objects?
What is the standard? I've always used lists but if you are working with the order the objects are updated I would think arrays would be faster. Would the overhead of dynamically reconstructing and resizing the array to hold varying numbers of objects overshadow the speed gained from using them?
			 
			
			- 
				That's exactly the thing I'm working on just now, my plan at the moment is to use linked lists to set up the data first then have some sort of object/world finalise function that creates arrays for the data and deletes the original lists as well as calculating the normals, bounding boxes and stuff like that. I have no idea if it'll really make any difference though so I'd be interested in hearing any views on this too.
Fryer.
			 
			
			- 
				One could say this comes down to the best structure to hold my balls for my Christmas Demo.
			
 
			
			- 
				is it in cpp? cause the vector class might be something to look at its a really freindly array allocator/resizer class.
it lets you not only allocate space to variable arrays but also class arrays.
so you can have something like.
#include <vector>
class 3dobject{
public:
blah..
blah..
private:
blah..
blah..
};
std::vector<3dobject *> vectorbob ;
vectorbob.push_back( new 3dobject() ) ;
vectorbob.push_back( new 3dobject() ) ;
vectorbob[0]->setX(blah) ;
vectorbob[0]->setY(blah) ;
Vectorbob[0]->render() ;
vectorbob[1]->setX(blah) ;
vectorbob[1]->setY(blah) ;
Vectorbob[1]->render() ;
etc..
then if you need more objects later on you just keep doing vectorbob.push_back( new 3dobject() ) ; and each time it will add a new object into the array.
dont know how fast it is but i use it a fair amount and it seems to do the job for me.
edit - and it goes much further than just the couple of commands ive pointed out if you goggle it you will see what i mean.
			 
			
			- 
				I almost only use linked lists. Its just great way of handling objects. If using BlitzMax you can easily convert between lists and arrays, so unless you have experiencing performance problems using lists, then I would advice to stick with them. Here is an example of a BlitzMax function that swap two objects within a list by utilizing the link-to-array and array-to-link conversion. This should give you an idea about how they can be utilized.
Function swapListLinks(list:TList Var, s:Int, d:Int)
	Local o:Object, ar:Object[]
	ar = ListToArray(list)
	o = ar[s]
	ar[s] = ar[d]
	ar[d] = o
	list = ListFromArray(ar)
End Function
The parameters are first the list, then the two positions of the objects in the lists to swap. Because the command word "Var" is used behind the list parameter in the function, it will use the actual variable/list within the function and make changes directly. So when the array is converted on the last line of the function, the newly arrange list is replacing the old one. I hope that made sense, otherwise feel free to ask me about it.
			 
			
			- 
				The data structure you hold your balls in is up to you.  Whatever makes the coding the simpler.  I use a vector for my scene presently.  But performance-wise, it's irrelevant what you use.
Jim