PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Sparse Matrix
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Sparse Matrix

Réponse
 
LinkBack Outils de la discussion
Vieux 24/12/2007, 03h09   #1
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Sparse Matrix

I'm trying to figure out the best way to go about doing this.
I have a "map" for a game, which I'd like to store in a matrix. Some
cells will be empty (NULL), and some will hold objects.
I need a matrix so that I can quickly find neighboring cells.
However, when create this map, I don't know what size it is going to
be, so it needs to be expandable. I also don't know which direction
it's going to grow in, so starting at [0][0] and expanding as
necessary won't work either, because I may later need to use [-1][0].
I don't really care if the indices are re-written if I try to access a
negative index. (ie, if I try to insert something into [-1][0], if it
increased all the indices by 1 so it didn't have a negative index,
that would be fine).
I just need something that's simple to implement, and preferably has
little overhead. I was contemplating using something like
std::vector<vector<myClass> > but that wouldn't fill the negative
index requirement, would it? Are there any other suggestions?
  Réponse avec citation
Vieux 24/12/2007, 03h51   #2
johanatan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sparse Matrix

On Dec 23, 10:09 pm, Mark <mnbaya...@gmail.com> wrote:
> I'm trying to figure out the best way to go about doing this.
> I have a "map" for a game, which I'd like to store in a matrix. Some
> cells will be empty (NULL), and some will hold objects.
> I need a matrix so that I can quickly find neighboring cells.
> However, when create this map, I don't know what size it is going to
> be, so it needs to be expandable. I also don't know which direction
> it's going to grow in, so starting at [0][0] and expanding as
> necessary won't work either, because I may later need to use [-1][0].
> I don't really care if the indices are re-written if I try to access a
> negative index. (ie, if I try to insert something into [-1][0], if it
> increased all the indices by 1 so it didn't have a negative index,
> that would be fine).
> I just need something that's simple to implement, and preferably has
> little overhead. I was contemplating using something like
> std::vector<vector<myClass> > but that wouldn't fill the negative
> index requirement, would it? Are there any other suggestions?


You could build your own dynamically sizing array (or derive of vector
and override) and let your base ptr point to the middle of the array
(then you could negative index up to numElementsOf(array)/2.

See:
http://msdn2.microsoft.com/en-us/lib...c4(VS.80).aspx

I'm sure if you dig around the net, you can find an automatically
increasing array (dynarray) or you could use CArray in MFC or as
mentioned previously std::vector (with derivation).

Hope that s.
  Réponse avec citation
Vieux 24/12/2007, 03h54   #3
johanatan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sparse Matrix

On Dec 23, 10:09 pm, Mark <mnbaya...@gmail.com> wrote:
> I'm trying to figure out the best way to go about doing this.
> I have a "map" for a game, which I'd like to store in a matrix. Some
> cells will be empty (NULL), and some will hold objects.
> I need a matrix so that I can quickly find neighboring cells.
> However, when create this map, I don't know what size it is going to
> be, so it needs to be expandable. I also don't know which direction
> it's going to grow in, so starting at [0][0] and expanding as
> necessary won't work either, because I may later need to use [-1][0].
> I don't really care if the indices are re-written if I try to access a
> negative index. (ie, if I try to insert something into [-1][0], if it
> increased all the indices by 1 so it didn't have a negative index,
> that would be fine).
> I just need something that's simple to implement, and preferably has
> little overhead. I was contemplating using something like
> std::vector<vector<myClass> > but that wouldn't fill the negative
> index requirement, would it? Are there any other suggestions?


Another option would be an std::map with the indices you choose
(including negative):

std::map< int, std::map< int, yourClass >>

it should perform fairly well as it is an highly optimized balanced
tree of some sort (red/black maybe??).

  Réponse avec citation
Vieux 24/12/2007, 04h15   #4
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sparse Matrix

On Dec 23, 7:51 pm, johanatan <johana...@gmail.com> wrote:
> On Dec 23, 10:09 pm, Mark <mnbaya...@gmail.com> wrote:
>
>
>
> > I'm trying to figure out the best way to go about doing this.
> > I have a "map" for a game, which I'd like to store in a matrix. Some
> > cells will be empty (NULL), and some will hold objects.
> > I need a matrix so that I can quickly find neighboring cells.
> > However, when create this map, I don't know what size it is going to
> > be, so it needs to be expandable. I also don't know which direction
> > it's going to grow in, so starting at [0][0] and expanding as
> > necessary won't work either, because I may later need to use [-1][0].
> > I don't really care if the indices are re-written if I try to access a
> > negative index. (ie, if I try to insert something into [-1][0], if it
> > increased all the indices by 1 so it didn't have a negative index,
> > that would be fine).
> > I just need something that's simple to implement, and preferably has
> > little overhead. I was contemplating using something like
> > std::vector<vector<myClass> > but that wouldn't fill the negative
> > index requirement, would it? Are there any other suggestions?

>
> You could build your own dynamically sizing array (or derive of vector
> and override) and let your base ptr point to the middle of the array
> (then you could negative index up to numElementsOf(array)/2.
>
> See:http://msdn2.microsoft.com/en-us/lib...c4(VS.80).aspx
>
> I'm sure if you dig around the net, you can find an automatically
> increasing array (dynarray) or you could use CArray in MFC or as
> mentioned previously std::vector (with derivation).
>
> Hope that s.


That link you pointed me to.. it's interesting. Never thought about
doing something like that before. Maybe I'll try it out, thanks!
  Réponse avec citation
Vieux 24/12/2007, 04h18   #5
Jim Langston
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sparse Matrix

Mark wrote:
> I'm trying to figure out the best way to go about doing this.
> I have a "map" for a game, which I'd like to store in a matrix. Some
> cells will be empty (NULL), and some will hold objects.
> I need a matrix so that I can quickly find neighboring cells.
> However, when create this map, I don't know what size it is going to
> be, so it needs to be expandable. I also don't know which direction
> it's going to grow in, so starting at [0][0] and expanding as
> necessary won't work either, because I may later need to use [-1][0].
> I don't really care if the indices are re-written if I try to access a
> negative index. (ie, if I try to insert something into [-1][0], if it
> increased all the indices by 1 so it didn't have a negative index,
> that would be fine).
> I just need something that's simple to implement, and preferably has
> little overhead. I was contemplating using something like
> std::vector<vector<myClass> > but that wouldn't fill the negative
> index requirement, would it? Are there any other suggestions?


std::vector<std::vector<cMyClass> > would work as long as you consider that
0,0 could actually be 1,2 or such. In which case I'd probably wrap the
std::vector in a class.

Something like this although I just threw this together and you should
probably check for the size in operator() and add if you want etc...

#include <string>
#include <iostream>
#include <vector>

class Cell
{
public:
int SomeData;
};

class GameMapClass
{
public:
typedef std::vector< std::vector< Cell > > MapDataType;
GameMapClass( int MinCol, int MinRow, int MaxCol, int MaxRow ):
MinCol_( MinCol ), MaxCol_( MaxCol ),
MinRow_( MinRow ),
MaxRow_( MaxRow )
{
std::vector<Cell> Row( MaxCol_ - MinCol_ + 1 );
for ( int i = MinRow; i < MaxRow + 1; ++i )
{
Data_.push_back( Row );
}
}

Cell& operator()( int Col, int Row )
{
return Data_[Row - MinRow_][Col - MinCol_];
}

void Dump()
{
for ( std::vector< std::vector<Cell> >::iterator Rit =
Data_.begin(); Rit != Data_.end(); ++Rit )
{
for ( std::vector<Cell>::iterator Cit = Rit->begin(); Cit !=
Rit->end(); ++Cit )
{
std::cout << Cit->SomeData << " ";
}
std::cout << "\n";
}
}
private:
MapDataType Data_;
int MinRow_;
int MaxRow_;
int MinCol_;
int MaxCol_;
};

int main()
{
int MapMinRow = -2;
int MapMinCol = -4;
int MapMaxRow = 10;
int MapMaxCol = 12;

GameMapClass GameMap( MapMinCol, MapMinRow, MapMaxCol, MapMaxRow );
for ( int Row = MapMinRow; Row <= MapMaxRow; ++Row )
for ( int Col = MapMinCol; Col <= MapMaxCol; ++Col )
{
GameMap( Col, Row ).SomeData = Col;
}
GameMap.Dump();
std::cout << "\n";
GameMap(0, 0).SomeData = 999;
GameMap.Dump();

}


--
Jim Langston
tazmaster@rocketmail.com


  Réponse avec citation
Vieux 24/12/2007, 04h18   #6
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sparse Matrix

On Dec 23, 7:54 pm, johanatan <johana...@gmail.com> wrote:
> On Dec 23, 10:09 pm, Mark <mnbaya...@gmail.com> wrote:
>
>
>
> > I'm trying to figure out the best way to go about doing this.
> > I have a "map" for a game, which I'd like to store in a matrix. Some
> > cells will be empty (NULL), and some will hold objects.
> > I need a matrix so that I can quickly find neighboring cells.
> > However, when create this map, I don't know what size it is going to
> > be, so it needs to be expandable. I also don't know which direction
> > it's going to grow in, so starting at [0][0] and expanding as
> > necessary won't work either, because I may later need to use [-1][0].
> > I don't really care if the indices are re-written if I try to access a
> > negative index. (ie, if I try to insert something into [-1][0], if it
> > increased all the indices by 1 so it didn't have a negative index,
> > that would be fine).
> > I just need something that's simple to implement, and preferably has
> > little overhead. I was contemplating using something like
> > std::vector<vector<myClass> > but that wouldn't fill the negative
> > index requirement, would it? Are there any other suggestions?

>
> Another option would be an std::map with the indices you choose
> (including negative):
>
> std::map< int, std::map< int, yourClass >>
>
> it should perform fairly well as it is an highly optimized balanced
> tree of some sort (red/black maybe??).


Thanks for the suggestion
  Réponse avec citation
Vieux 24/12/2007, 12h57   #7
apm35@student.open.ac.uk
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sparse Matrix

On 24 Dec, 03:09, Mark <mnbaya...@gmail.com> wrote:
> I'm trying to figure out the best way to go about doing this.
> I have a "map" for a game, which I'd like to store in a matrix. Some
> cells will be empty (NULL), and some will hold objects.
> I need a matrix so that I can quickly find neighboring cells.
> However, when create this map, I don't know what size it is going to
> be, so it needs to be expandable. I also don't know which direction
> it's going to grow in, so starting at [0][0] and expanding as
> necessary won't work either, because I may later need to use [-1][0].
> I don't really care if the indices are re-written if I try to access a
> negative index. (ie, if I try to insert something into [-1][0], if it
> increased all the indices by 1 so it didn't have a negative index,
> that would be fine).


maybe this will :

http://math.nist.gov/sparselib++/

It is for compute-intensive jobs but hopefully it will be of use.

Regards,

Andrew Marlow
  Réponse avec citation
Vieux 04/01/2008, 23h57   #8
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Sparse Matrix

On Dec 23 2007, 8:18 pm, "Jim Langston" <tazmas...@rocketmail.com>
wrote:
> Mark wrote:
> > I'm trying to figure out the best way to go about doing this.
> > I have a "map" for a game, which I'd like to store in a matrix. Some
> > cells will be empty (NULL), and some will hold objects.
> > I need a matrix so that I can quickly find neighboring cells.
> > However, when create this map, I don't know what size it is going to
> > be, so it needs to be expandable. I also don't know which direction
> > it's going to grow in, so starting at [0][0] and expanding as
> > necessary won't work either, because I may later need to use [-1][0].
> > I don't really care if the indices are re-written if I try to access a
> > negative index. (ie, if I try to insert something into [-1][0], if it
> > increased all the indices by 1 so it didn't have a negative index,
> > that would be fine).
> > I just need something that's simple to implement, and preferably has
> > little overhead. I was contemplating using something like
> > std::vector<vector<myClass> > but that wouldn't fill the negative
> > index requirement, would it? Are there any other suggestions?

>
> std::vector<std::vector<cMyClass> > would work as long as you consider that
> 0,0 could actually be 1,2 or such. In which case I'd probably wrap the
> std::vector in a class.
>
> Something like this although I just threw this together and you should
> probably check for the size in operator() and add if you want etc...
>
> #include <string>
> #include <iostream>
> #include <vector>
>
> class Cell
> {
> public:
> int SomeData;
>
> };
>
> class GameMapClass
> {
> public:
> typedef std::vector< std::vector< Cell > > MapDataType;
> GameMapClass( int MinCol, int MinRow, int MaxCol, int MaxRow ):
> MinCol_( MinCol ), MaxCol_( MaxCol ),
> MinRow_( MinRow ),
> MaxRow_( MaxRow )
> {
> std::vector<Cell> Row( MaxCol_ - MinCol_ + 1 );
> for ( int i = MinRow; i < MaxRow + 1; ++i )
> {
> Data_.push_back( Row );
> }
> }
>
> Cell& operator()( int Col, int Row )
> {
> return Data_[Row - MinRow_][Col - MinCol_];
> }
>
> void Dump()
> {
> for ( std::vector< std::vector<Cell> >::iterator Rit =
> Data_.begin(); Rit != Data_.end(); ++Rit )
> {
> for ( std::vector<Cell>::iterator Cit = Rit->begin(); Cit !=
> Rit->end(); ++Cit )
> {
> std::cout << Cit->SomeData << " ";
> }
> std::cout << "\n";
> }
> }
> private:
> MapDataType Data_;
> int MinRow_;
> int MaxRow_;
> int MinCol_;
> int MaxCol_;
>
> };
>
> int main()
> {
> int MapMinRow = -2;
> int MapMinCol = -4;
> int MapMaxRow = 10;
> int MapMaxCol = 12;
>
> GameMapClass GameMap( MapMinCol, MapMinRow, MapMaxCol, MapMaxRow );
> for ( int Row = MapMinRow; Row <= MapMaxRow; ++Row )
> for ( int Col = MapMinCol; Col <= MapMaxCol; ++Col )
> {
> GameMap( Col, Row ).SomeData = Col;
> }
> GameMap.Dump();
> std::cout << "\n";
> GameMap(0, 0).SomeData = 999;
> GameMap.Dump();
>
> }
>
> --
> Jim Langston
> tazmas...@rocketmail.com


Thank you for going through the trouble of writing all that out! I
think something like this would probably be the best approach for me.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h21.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,21791 seconds with 16 queries