3D Vector vs. 1D Vector with index algorithm

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

3D Vector vs. 1D Vector with index algorithm

Post 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. :mrgreen:
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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. 8-)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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.
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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. :)
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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.
User avatar
LuciDreamTheater
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Jan 20, 2009 2:18 am
Location: Southern CA
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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.
User avatar
PixelP
Chaos Rift Regular
Chaos Rift Regular
Posts: 153
Joined: Tue Oct 07, 2008 12:23 pm
Programming Language of Choice: c/c++
Location: sweden
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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?
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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)
User avatar
PixelP
Chaos Rift Regular
Chaos Rift Regular
Posts: 153
Joined: Tue Oct 07, 2008 12:23 pm
Programming Language of Choice: c/c++
Location: sweden
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post by PixelP »

GyroVorbis wrote:No? The limit to a vector size is your RAM.
as i thought.
User avatar
Kros
Chaos Rift Regular
Chaos Rift Regular
Posts: 136
Joined: Tue Feb 24, 2009 4:01 pm
Current Project: N/A
Favorite Gaming Platforms: PC, Playstation/2/3
Programming Language of Choice: C++
Location: Oregon,USA
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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.
Isaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
YouTube Channel
User avatar
LuciDreamTheater
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Jan 20, 2009 2:18 am
Location: Southern CA
Contact:

Re: 3D Vector vs. 1D Vector with index algorithm

Post 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.
Post Reply