Data structure 2d/3d array of tiles C++ - c++

I'm programming a level editor for a new game. The problem is, I am not sure what structure to use for storing my data.
It is a tile-based map engine, using x- and y coordinates and an id for the tile at that position.
I've got multiple layers, the map is resizeable, so an array may cause me some troubles, that's why I chose a std::vector for that case.
To prevent a lot of overload I only add a tile, when somebody placed it, so the vector size is zero if there are no tiles and increases the more tiles are placed.
struct tile {
unsigned short tile_id;
unsigned short tile_x;
unsigned short tile_y;
};
And my vector:
std::vector<tile> tiles;
The thing is, before adding a new tile I need to check if there's already a tile at that x- and y-position.
// Returns true/false if there is a tile at given position or not
bool Layer::has_tile_at(unsigned short x, unsigned short y) {
unsigned int i;
for (i = 0; i < tiles.size(); i++) {
if (tiles[i].tile_x == x && tiles[i].tile_y == y)
return true;
}
return false;
}
My problem is, that for every placed tile, I must loop through the whole vector, which is fast at the beginning, but really gets a pain in the ass after some tiles have been placed.
Do you think my approach is okay so far, or is there something smarter and more performant?

Data structure to use should depend mostly on the use cases: if you're doing mostly (x,y) reads, then perhaps you need a matrix (be it via a vector of vectors, or just array of arrays).
If you need indexed access AND easy iteration over tiles, perhaps keep the data within two data structures. You should be able to easily implement a 2d map with pointers to tiles within vector - initially empty, lazily loaded upon (x,y) access (remember about data safety!).
Consider for example:
class Map
{
public:
void addTile(std::shared_ptr<Tile> tile)
{
m_tiles.push_back(tile); // should probably remove old elements at (x,y) from the vector
m_map[{tile->x, tile->y}] = tile; // overwrites whatever was stored in there!
}
std::shared_ptr<Tile> getTile(int x, int y)
{
return m_tilesMap[{x, y}]; // if no tile there, returns default ctor-ed shared_ptr: nullptr
}
private:
std::vector<std::shared_ptr<Tile>> m_tiles;
using Index2D = std::pair<int, int>;
std::map<Index2D, std::shared_ptr<Tile>> m_tilesMap;
};
(extended comment with a brief code example: data is kept on the heap, while both vector and map keep copies of it - perhaps could be improved for easier removal)

Why not use multiple vectors? You could essentially create a growable 2-D vector by having a vector of vectors, then overload the [] operator to first check if the vector size could contain that element (returning false if not), and if it can, check if that element isn't a constructed value (whatever your default "tile" may be). This would allow nearly O(1) lookup like in regular vectors. Otherwise you can use a formula for your max col/row distance and do a O(1) look-up that way with some 2-D to 1-D conversions such as in 2-D arrays.
This is what I'm thinking:
vector< vector<tile> > tiles;
bool::Layer has_tile_at(unsigned short x, unsigned short y) {
if (tiles.size() <= x) {
return false;
} else if (tiles[x].size() > y) {
if (tiles[x][y] != tile()) {
return true;
}
}
return false;
}
Edit:
As another user pointed out, you could also use pointers and check if tiles[x][y] == nullptr; instead!

Related

How to create an auxiliary data structure to keep track of heap indices in a minheap for the decrease_key operation in c++

I think this is probably a trivial problem to solve but I have been struggling with this for past few days.
I have the following vector: v = [7,3,16,4,2,1]. I was able to implement with some help from google simple minheap algorithm to get the smallest element in each iteration. After extraction of the minimum element, I need to decrease the values of some of the elements and then bubble them up.
The issue I am having is that I want find the elements whose value has to be reduced in the heap in constant time, then reduce that value and then bubble it up.
After the heapify operation, the heap_vector v_h looks like this: v_h = [1,2,7,4,3,16]. When I remove the min element 1, then the heap vector becomes, [2,3,7,4,16]. But before we do the swap and bubble up, say I want to change the values of 7 to 4, 16 to 4 and 4 to 3.5 . But I am not sure where they will be in the heap. The indices of values of the elements that have to be decreased will be given with respect to the original vector v. I figured out that I need to have an auxiliary data structure that can keep track of the heap indices in relation to the original order of the elements (the heap index vector should look like h_iv = [2,4,5,3,1,0] after all the elements have been inserted into the minheap. And whenever an element is deleted from the minheap, the heap_index should be -1. I created a vector to try to update the heap indices whenever there is a change but I am unable to do it.
I am pasting my work here and also at https://onlinegdb.com/SJR4LqQO4
Some of the work I had tried is commented out. I am unable to map the heap indices when there is a swap in the bubble up or bubble down operations. I will be very grateful to anyone who can lead me in a direction to solve my problem. Please also let me know if I have to rethink some of my logic.
The .hpp file
#ifndef minheap_hpp
#define minheap_hpp
#include <stdio.h>
// #include "helper.h"
#include <vector>
class minheap
{
public:
std::vector<int> vect;
std::vector<int> heap_index;
void bubble_down(int index);
void bubble_up(int index);
void Heapify();
public:
minheap(const std::vector<int>& input_vector);
minheap();
void insert(int value);
int get_min();
void delete_min();
void print_heap_vector();
};
#endif /* minheap_hpp */
The .cpp file
#include "minheap.hpp"
minheap::minheap(const std::vector<int>& input_vector) : vect(input_vector)
{
Heapify();
}
void minheap::Heapify()
{
int length = static_cast<int>(vect.size());
// auto start = 0;
// for (auto i = 0; i < vect.size(); i++){
// heap_index.push_back(start);
// start++;
// }
for(int i=length/2-1; i>=0; --i)
{
bubble_down(i);
}
}
void minheap::bubble_down(int index)
{
int length = static_cast<int>(vect.size());
int leftChildIndex = 2*index + 1;
int rightChildIndex = 2*index + 2;
if(leftChildIndex >= length){
return;
}
int minIndex = index;
if(vect[index] > vect[leftChildIndex])
{
minIndex = leftChildIndex;
}
if((rightChildIndex < length) && (vect[minIndex] > vect[rightChildIndex]))
{
minIndex = rightChildIndex;
}
if(minIndex != index)
{
std::swap(vect[index], vect[minIndex]);
// std::cout << "swap " << index << " - " << minIndex << "\n";
// auto a = heap_index[heap_index[index]];
// auto b = heap_index[heap_index[minIndex]];
// heap_index[a] = b;
// heap_index[b] = a;
// print_vector(heap_index);
bubble_down(minIndex);
}
}
void minheap::bubble_up(int index)
{
if(index == 0)
return;
int par_index = (index-1)/2;
if(vect[par_index] > vect[index])
{
std::swap(vect[index], vect[par_index]);
bubble_up(par_index);
}
}
void minheap::insert(int value)
{
int length = static_cast<int>(vect.size());
vect.push_back(value);
bubble_up(length);
}
int minheap::get_min()
{
return vect[0];
}
void minheap::delete_min()
{
int length = static_cast<int>(vect.size());
if(length == 0)
{
return;
}
vect[0] = vect[length-1];
vect.pop_back();
bubble_down(0);
}
void minheap::print_heap_vector(){
// print_vector(vect);
}
and the main file
#include <iostream>
#include <iostream>
#include "minheap.hpp"
int main(int argc, const char * argv[]) {
std::vector<int> vec {7, 3, 16, 4, 2, 1};
minheap mh(vec);
// mh.print_heap_vector();
for(int i=0; i<3; ++i)
{
auto a = mh.get_min();
mh.delete_min();
// mh.print_heap_vector();
std::cout << a << "\n";
}
// std::cout << "\n";
return 0;
}
"I want to change the values of 7 to 4, 16 to 4 and 4 to 3.5 . But I am not sure where they will be in the heap. The indices of values of the elements that have to be decreased will be given with respect to the original vector v. ... Please also let me know if I have to rethink some of my logic."
Rather than manipulate the values inside the heap, I would suggest keeping the values that need changing inside a vector (possibly v itself). The heap could be based on elements that are a struct (or class) that holds an index into the corresponding position in the vector with the values, rather than hold the (changing) value itself.
The struct (or class) would implement an operator< function that compares the values retrieved from the two vector locations for the respective index values. So, instead of storing the comparison value in the heap elements and comparing a < b, you would store index positions i and j and so on and compare v[i] < v[j] for the purpose of heap ordering.
In this way, the positions of the numerical values you need to update will never change from their original positions. The position information will never go stale (as I understand it from your description).
Of course, when you make changes to those stored values in the vector, that could easily invalidate any ordering that might have existed in the heap itself. As I understand your description, that much was necessarily true in any case. Therefore, depending on how you change the values, you might need to do a fresh make_heap to restore proper heap ordering. (That isn't clear, since it depends on whether your intended changes violate heap assumptions, but it would be a safe thing to assume unless there are strong assurances otherwise.)
I think the rest is pretty straight forward. You can still operate the heap as you intended before. For ease you might even give the struct (or class) a lookup function to return the current value at it's corresponding position in the vector, if you need that (rather than the index) as you pop out minimum values.
p.s. Here is a variation on the same idea.
In the original version above, one would likely need to also store a pointer to the location of the vector that held the vector of values, possibly as a shared static pointer of that struct (or class) so that all the members could dereference the pointer to that vector in combination with the index values to look up the particular member associated with that element.
If you prefer, instead of storing that shared vector pointer and an index in each member, each struct (or class) instance could more simply store a pointer (or iterator) directly to the corresponding value's location. If the values are integers, the heap element struct's member value could be int pointer. While each pointer might be larger than an index value, this does have the advantage that it eliminates any assumption about the data structure that holds the compared values and it is even simpler/faster to dereference vs. lookup with an index into the vector. (Both are constant time.)
One caution: In this alternate approach, the pointer values would be invalidated if you were to cause the vector's storage positions to change, e.g. by pushing in new values and expanding it in a way that forces it to reallocate it's space. I'm assuming you only need to change values, not expand the number of values after you've begun to use the heap. But if you did need to do that, that would be one reason to prefer index values, since they remain valid after expanding the vector (unlike pointers).
p.p.s. This technique is also valuable when the objects that you want to compare in the heap are large. Rather than have the heap perform many copy operations on large objects as it reorders the positions of the heap elements, by storing only pointers (or index values) the copying is much more efficient. In fact, this makes it possible to use heaps on objects that you might not want to copy at all.
Here is a quick idea of one version of the comparison function (with some class context now added).
class YourHeapElementClassName
{
public:
// constructor
explicit YourHeapElementClassName(theTypeOfYourComparableValueOrObject & val)
: m_valPointer(&val)
{
}
bool operator<(const YourHeapElementClassName & other) const
{
return *m_valPointer < *(other.m_valPointer);
}
...
private:
theTypeOfYourComparableValueOrObject * m_valPointer;
}; // YourHeapElementClassName
// and later instead of making a heap of int or double,
// you make a heap of YourHeapElementClassName objects
// that you initialize so each points to a value in v
// by using the constructor above with each v member.
// If you (probably) don't need to change the v values
// through these heap objects, the member value could be
// a pointer to a const value and the constructor could
// have a const reference argument for the original value.
If you had need to do this with different types of values or objects, the pointer approach could be implemented with a template that generalizes on the type of value or object and holds a pointer to that general type.

Storing objects in 2 dimensional sprite matrix in c++

I want to store a list of tiles (pointers to objects of class "Sprite") in a two dimensional vector.
Currently I'm storing all the sprites of my tilemap in a vector.
std::vector<Sprite*> _tiles;
Now I need to get all the neighbored tiles and I thought it be easier, to store my sprites in a 2d matrix (vector in vector) to do those caluclations.
But I can't figure out how to do that.
Header:
private:
std::vector<std::vector<Sprite*>> matrix;
C++ File:
vectorMatrix::vectorMatrix(int columns, int rows) { }
vectorMatrix::~vectorMatrix() { }
void vectorMatrix::addCellAt(int x, int y, Sprite* sprite) {
std::vector< std::vector<Sprite*> > matrix;
matrix[x][y].push_back(sprite);
}
But I get an error message if I use two index operators.
std::vector< std::vector<Sprite*> > matrix;
This is an empty std::vector of of std::vector, and as others have pointed out, it's no longer the data member matrix.
Since it's empty, it's not possible to do matrix[0] - get the first row, matrix[1] - get the second row... You can however allocate the size of the matrix in your constructor first -- e.g. This will give you a rows*columns matrix
vectorMatrix::vectorMatrix(int columns, int rows)
: matrix(rows, std::vector<Sprite*>(columns))
{
}
And then you can set the xth and yth element to the corresponding Sprite*, and of course x and y should be smaller than rows and columns
matrix[x][y] = sprite;
And don't declare matrix again inside the addCellAt, You can use the data members directly anywhere inside the class.
Why do you think it's a good idea to make your matrix a vector-of-vectors? True, this will "work" in the sense that vec_of_vec_matrix[i][j] will return the right thing, but it's somewhat cumbersome and inefficient.
Instead, consider basing your class on a single vector - of all data, as done in this answer (and probably in many libraries). Element access will be something like (for column-major data):
Sprite*& Matrix::operator()(size_t i, size_t j)
{
return mData[i * num_columns + j];
}
You could even arrange with map[x][y] to work, using a row proxy class.
Three problems:
You declare a local variable matrix inside the function, which shadows the member variable.
If the size of the vectors is not set to include the x and y indexes then you will go out of bounds.
matrix[x][y] is not itself a vector, it's an element that you can assign directly:
matrix[x][y] = sprite;
Remember to consider problem 2 before doing this.

In C++, How do I store values into a vector that is inside of a vector when the two vectors are of different types?

I am writing a program where I need to use the following data structure:
struct shape
{
std::vector<float> verts; // contains the x & y values for each vertex
char type; // the type of shape being stored.
float shapeCol[3]; // stores the color of the shape being stored.
float shapeSize; // stores the size of the shape if it is a line or point
};
In my main program I need a vector of type shape. How would I store values into the vector inside of the struct shapes using the the vector of struct shapes.
For instance, vector<shape> myshapes;
If I wanted to store a value into the first index of my verts vector, inside of my first index of my myshapes vector how would I do this?
in pseudo code it would look something like this, with i being the index:
myshapes[i].vector[i] = 4; // but I know this is incorrect
Would this be easier to implement using a STL list instead and if so what would that syntax look like?
Thanks for the help I am new to vectors so any advice would be appreciated.
vector supports the use of the [] operator. The syntax and semantics are very similar to using the [] operator with arrays. See: http://en.cppreference.com/w/cpp/container/vector/operator_at.
As with any struct member, you need to access it by name. myshapes[i].verts[j] = 4;.
The general advice given is to use std::vector as your default container of choice. Naturally if you have specific needs (like adding/removing items in the middle of the container) other containers may have better performance characteristics.
If your vector(s) start out empty, you'll have to add elements to them before you can index into them with operator[]. This is usually done with push_back (to add an existing shape object) or emplace_back (to construct a new shape object directly in the vector).
Given vector<shape> myshapes, you could add some shapes like this:
// add 10 shapes
for (size_t n = 0; n < 10; n++) {
shape s; // creates a new, blank shape object
// initialize the shape's data
s.type = ...;
s.shapeSize = ...;
// etc.
// add verts
s.verts.push_back(1.0f);
s.verts.push_back(2.0f);
s.verts.push_back(3.0f);
// etc.
// add the shape to the vector
myshapes.push_back(std::move(s));
}
(Since we're done with s on that last line, we can use std::move. This allows push_back to move the shape's data into the vector instead of copying it. Look up move semantics for more info.)
Once you have stuff in the vector, you can access elements by index like this:
myshapes[index of shape].verts[index of vertex within that shape]
Using [] with an invalid index or when the vector is empty invokes undefined behavior (don't do it or your program will crash/malfunction).

Is it worth to use vector in case of making a map

I have got a class that represents a 2D map with size 40x40.
I read some data from sensors and create this map with marking cells if my sensors found something and I set value of propablity of finding an obstacle. For example when I am find some obstacle in cell [52,22] I add to its value for example to 10 and add to surrounded cells value 5.
So each cell of this map should keep some little value(propably not bigger). So when a cell is marked three times by sensor, its value will be 30 and surronding cells will have 15.
And my question is, is it worth to use casual array or is it better to use vector even I do not sort this cells, dont remove them etc. I just set its value, and read it later?
Update:
Actually I have in my header file:
using cell = uint8_t;
class Grid {
private:
int xSize, ySize;
cell *cells;
public:
//some methods
}
In cpp :
using cell = uint8_t;
Grid::Grid(int xSize, int ySize) : xSize(xSize), ySize(ySize) {
cells = new cell[xSize * ySize];
for (int i = 0; i < xSize; i++) {
for (int j = 0; j < ySize; j++)
cells[x + y * xSize] = 0;
}
}
Grid::~Grid(void) {
delete cells;
}
inline cell* Grid::getCell(int x, int y) const{
return &cells[x + y * xSize];
}
Does it look fine?
I'd use std::array rather than std::vector.
For fixed size arrays you get the benefits of STL containers with the performance of 'naked' arrays.
http://en.cppreference.com/w/cpp/container/array
A static (C-style) array is possible in your case since the size in known at compile-time.
BUT. It may be interesting to have the data on the heap instead of the stack.
If the array is a global variable, it's ugly an bug-prone (avoid that when you can).
If the array is a local variable (let say, in your main() function), then a stack overflow may occur. Well, it's very unlikely for a 40*40 array of tiny things, but I'd prefer have my data on the heap, to keep things safe, clean, and future-proof.
So, IMHO you should definitely go for the vector, it's fast, clean and readable, and you don't have to worry about stack overflow, memory allocation, etc.
About your data. If you know your values are storable on a single byte, go for it !
An uint8_t (same as unsigned char) can store values from 0 to 255. If it's enough, use it.
using cell = uint8_t; // define a nice name for your data type
std::vector<cell> myMap;
size_t size = 40;
myMap.reserve(size*size);
side note: don't use new[]. Well, you can, but it has no advantages over a vector. You will probably only gain headaches handling memory manually.
Some advantages of using a std::vector is that it can be dynamically allocated (flexible size, can be resized during execution, etc) and can be passed/returned from a function. Since you have a fixed size 40x40 and you know you have one element int in every cell, I don't think it matters that much in your case and I would NOT suggest using a class object std::vector to process this simple task.
And here is a possible duplicate.

Accessing methods of objects held in multidimensional vectors of unique_ptr's

I have a 2 dimensional structure of objects initialized as thus:
std::vector<std::shared_ptr<tile> > appearance;
for (int x = 0; x < building_data.x_width; x++)
{
appearance.push_back
(std::shared_ptr<tile>(new tile[building_data.y_length]));
}
now, as far as I can figure out, the only way to access a member function of a tile in this is to use
appearance.at(x).get()[y].member_function()
which is confusing and cumbersome, and I feel like I'm missing something.
Previously, I had used tile** for the same structure, and the syntax of
tile[x][y] was nice but the raw pointers were a headache.
So, is there a better way access functions of an object held in an array, where the first element in the array is pointed to by a smart pointer held in a vector? Wordy but its the best I have.
You can use the -> operator to access members of the object managed by the shared_ptr. It's the same syntax you use with raw pointers.
However, you're going to run into problems with delete as mentioned in Dantez's answer.
Also, it looks like you're building some sort of board of tiles, perhaps for a game? Have you considered replacing the multidimensional array with with a 1D vector and some accessor functions?
// board_width and height should be integers
std::vector<Tile> board;
board.reserve(board_width * board_height);
for (unsigned y_axis = 0; y_axis < board_height; ++y_axis)
{
for (unsigned x_axis = 0; x_axis < board_width; ++x_axis)
{
board.push_back(Tile());
}
}
...
vec2 index_to_coords(unsigned index)
{
return vec2(index % board_width, index / board_width);
}
...
unsigned coords_to_index(const vec2& coords)
{
return (static_cast<unsigned>(coords.y) * board_width) + static_cast<unsigned>(coords.x);
}
First of all, shared_ptr is not designed to work with arrays. When there is no more references, it calls delete instead of delete[] which results in undefined behaviour if managed object is an array. You can read about it here.
As for accessing shared_ptr object, you can use operator* to dereference it.
Also, if you know vector's final size, you may want to reserve some space to avoid reallocation.
I agree with Fibbles, but have an alternate idea to provide. Fibble's approach is actually quite common even in C, because multidimensional structures (matrices) are just much easier that way.
However, if you do insist on the two dimensional concept, you can nest vectors. Consider:
typedef std::vector< tile > TileRow;
typedef std::vector< TileRow > Tiles;
At first, this may be a bit confusing, so to be clear that creates:
std::vector< std::vector< tile > > t;
However, with the typedef, thats
Tiles t;
Now, that's empty. To use it you'd need to push in some rows, and for each row push in some columns. You might not like that, so...you can use the assign function to set some rows. If, for example, you needed a matrix of 10 rows by 10 columns, you might
t.assign( 10, TileRow( 10, tile() ) );
This assume tile has a default constructor, pushing 10 rows of TileRow, each with 10 columns of default constructed tiles.
Now, t[ 1 ] returns a reference to the row 1. As such, t[ 1 ][ 1 ] is a reference to the tile at location 1,1, much like an array.
Yet, now you have no allocation/deallocation issues.
Something similar can be done with std::array, even better.
typedef std::array< tile, 10 > TileRow;
typedef std::array< TileRow, 10 > Tiles;
Tiles t;
At which point, t is ready with default initialized tiles.