Page 1 of 2
3D Vector vs. 1D Vector with index algorithm
Posted: Tue Feb 24, 2009 4:13 pm
by dandymcgee
Okay so I'm working a project that involves loading tile-based maps, editing them, drawing them, and saving them (a.k.a. "Level Editor"). These maps will each have a certain number of layers ( Let's assume 2 for ease ).
So far I've come up with 3 different ways that I could go about storing the maps in memory:
- A 3-Dimensional vector
A 1-Dimensional vector with an algorithm that finds the appropriate index for any particular tile
A 1-Dimensional vector storing a Layer class object for each layer, each of which contains a 1D (with index algorithm) vector storing tile data.
A 3-Dimensional vector
Code: Select all
//declaration
vector<vector<vector<Tile*>>> mapData;
//accessing contents
mapData[layer][x][y]->getTileType();
A 1-Dimensional vector with an index algorithm
Code: Select all
//declaration
vector<Tile*> mapData;
//accessing contents
int tempTileNumber = GetTileNumber(layer, x, y);
mapData[tempTileNumber]->getTileType();
//Index finding function
int GetTileNumber(int layer, int x, int y)
{
int tempa = x / tileWidth;
int tempb = y / tileHeight;
//Note: first layer is 0
int tilenum = layer * tilesPerLayer + tempa + tempb * tilesPerRow;
return tilenum;
}
A 1-Dimensional vector storing Layer class objects
Level.cpp
Code: Select all
//declaration
vector<Layer*> layerData;
//accessing contents
mapData[layer]->layerGetTileType(x, y);
Layer.cpp
Code: Select all
//declaration
vector<Tile*> tileData;
//Index finding function
int GetTileNumber(int layer, int x, int y)
{
int tempa = x / tileWidth;
int tempb = y / tileHeight;
//Note: first layer is 0
int tilenum = layer * tilesPerLayer + tempa + tempb * tilesPerRow;
return tilenum;
}
//returns tile type
int layerGetTileType(int x, int y)
{
int tempTileNumber = GetTileNumber( x, y);
return tileData[tempTileNumber]->getTileType();
}
Sorry for any errors in my pseudo-code I made it up right in the text box as I was posting.
Any opinions regarding ease of use later in the project, performance hits, etc. are much appreciated.
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Tue Feb 24, 2009 4:43 pm
by wtetzner
I like the 1-Dimensional vector storing Layers. It is easily resizable, easy to use, and I don't really see any performance problems it would have that the other ways wouldn't have, since you're just storing pointers in the vector.
-Walter
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Wed Feb 25, 2009 10:45 am
by dandymcgee
wtetzner wrote:I like the 1-Dimensional vector storing Layers. It is easily resizable, easy to use, and I don't really see any performance problems it would have that the other ways wouldn't have, since you're just storing pointers in the vector.
-Walter
Thanks for your opinion, I was actually leaning towards that one myself. I think the best part about using this method would be the extreme ease of adding and deleting specific layers while working.
It can always be changed later if I discover a better solution.
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Wed Feb 25, 2009 11:17 am
by Falco Girgis
I disagree. You are going to have a large performance drop with the 1D approach (so large that you won't even notice it, probably. But it's still way less efficient.
). Think about it, every time that you are looping through your vectors to render, or even fetch an index from an array, you are doing a calculation rather than just indexing straight up.
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Wed Feb 25, 2009 12:27 pm
by wtetzner
GyroVorbis wrote:I disagree. You are going to have a large performance drop with the 1D approach (so large that you won't even notice it, probably. But it's still way less efficient.
). Think about it, every time that you are looping through your vectors to render, or even fetch an index from an array, you are doing a calculation rather than just indexing straight up.
You have that problem with the 1D Vector using the indexing algorithim, but not with the 1D Vector that stores pointers to Layer objects. You are still indexing each layer straight up, and assuming the Layer objects use fast indexing as well, it should be just as fast as the 3D array.
Also, does anyone know how the compiler does multi-dimensional array indexing anyway? I mean, they're still stored in linear memory, so it's probably using and indexing algorithm anyway.
Edit: I found this on how the compiler does indexing. So it has to do a calculation anyway.
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Wed Feb 25, 2009 1:20 pm
by avansc
as far as i know every modernday compiles converts [y][x] int depth*y+x or what ever the form is, and also for [][][][][][].... etc.
i dont know which is faster, but imagine they would be similar. did this in data structures and OS classes so its been a while.
hope that helps.
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Wed Feb 25, 2009 5:14 pm
by dandymcgee
Well if multi-dimensional arrays use an algorithm to find my data anyways, then it would probably be best to use them. I mean if you think about it the chance that I'm going to write a faster or more efficient indexing algorithm than the one that C++ uses for vectors with my current knowledge is pretty damn slim.
Thanks for your input everyone, and that link wtetzner.
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Wed Feb 25, 2009 8:42 pm
by wtetzner
dandymcgee wrote:Well if multi-dimensional arrays use an algorithm to find my data anyways, then it would probably be best to use them. I mean if you think about it the chance that I'm going to write a faster or more efficient indexing algorithm than the one that C++ uses for vectors with my current knowledge is pretty damn slim.
Wait, you missed the point I was getting at
. I was basically saying that since multi-dimensional arrays use an indexing algorithm, there's really no speed benefit to using them over the other methods. So I still think the 1D vector storing pointers to Layer objects is the best way to go. As far as I know, the internal data structure in a vector is an array, so you still have constant time access, and you might even save a multiplication (depending on how the Layer objects are indexed).
Either way I think the 1D vector of Layers will be fast enough, and I really think it would keep your code more flexible and easier to work with.
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Fri Feb 27, 2009 1:23 am
by Falco Girgis
He's right, my previous post was pretty stupid. Not sure what I was thinking. Too much wine?
So anyway, yeah. Vectors are implemented as arrays (complete with realloc for resizing). Accessing them is just as fast as accessing a standard array. It's resizing that is considerably slower.
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Sun Mar 01, 2009 3:18 am
by LuciDreamTheater
There's a bigger problem with vectors that nobody mentioned: they have a small maximum size. If you want to have large maps with more than one layer, I don't think vectors will work for you.
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Sun Mar 01, 2009 8:13 am
by PixelP
anothrguitarist wrote:There's a bigger problem with vectors that nobody mentioned: they have a small maximum size. If you want to have large maps with more than one layer, I don't think vectors will work for you.
hmm... is that so?
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Sun Mar 01, 2009 2:34 pm
by Falco Girgis
No? The limit to a vector size is your RAM.
You can even edit the default allowance for a vector size. anothrguitarist, you sure that you aren't just talking about the default vector size on Dreamcast being pretty small? (I know that you're active in the DC scene, like me)
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Sun Mar 01, 2009 2:41 pm
by PixelP
GyroVorbis wrote:No? The limit to a vector size is your RAM.
as i thought.
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Sun Mar 01, 2009 5:03 pm
by Kros
PixelP wrote:GyroVorbis wrote:No? The limit to a vector size is your RAM.
as i thought.
i c wut u did thar.
Re: 3D Vector vs. 1D Vector with index algorithm
Posted: Sun Mar 01, 2009 6:16 pm
by LuciDreamTheater
GyroVorbis wrote: anothrguitarist, you sure that you aren't just talking about the default vector size on Dreamcast being pretty small? (I know that you're active in the DC scene, like me)
I was making a mistake.
Must've been the wine.
EDIT: By the way, I oughtta tell you the story of why I started hanging out around here, but alas, I don't have the time.