Afficher un message
Vieux 07/02/2008, 21h11   #6
fabricio.olivetti@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to structure a class that may hold two kind of values

On 7 fev, 16:32, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> fabricio.olive...@gmail.com wrote:
> > On 7 fev, 16:03, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> >> fabricio.olive...@gmail.com wrote:
> >>> I am designing a class to read a data file and provide access to
> >>> another class, but as this dataset may contain either double or int
> >>> values, and some of them may be very large I'd like to create a
> >>> class that can decide upon allocating a vector of "char" (for the
> >>> integral values may be enough) or a vector of double.
> >>> But I can't see how can I do that...using templates I still have to
> >>> determine, prior the class declaration, which type this class will
> >>> hold.

>
> >>> Of course I could declare something like this:

>
> >>> class foo{

>
> >>> private:
> >>> vector< char > cData;
> >>> vector< double > dData;
> >>> bool type;
> >>> public:
> >>> double getData(unsigned i);
> >>> };

>
> >>> and always return a double (casting the char when required) and
> >>> using just the required data type using a flag to determine which
> >>> type is used by the class.
> >>> How would be the most elegant and optimized way of doing that?

>
> >> Without knowing how 'getData' is supposed to be used there is no way
> >> to tell if it fits the requirements (assumed or specified). I can
> >> easily think of a scenario where I'd like to have to members

>
> >> char getChar(unsigned i) const;
> >> double getDouble(unsigned i) const;

>
> >> and have them throw an exception if the type doesn't matcht the
> >> stored type of the class.

>
>
> > Let's say getData just returns the element on the 'i'th position of
> > the vector. And also, let's assume that I want to avoid a check of
> > what member function to call (don't wanna do a: switch(type) case 0:
> > getChar(); case 1: getDouble();...)

>
> That's not really a design specificiation. It looks very much like
> an implementation detail. Now, the check you are talking about has
> to be done inside 'getData' anyway. So, it doesn't really matter
> who makes it since it has to be made.
>
> Now, perhaps you will think a bit what the importance of the dual
> storage is and what the use of 'getData' is, and then (you don't
> really have to tell us) you will have a clearer picture why you
> think you need the two vectors and one 'getData' function. Who
> stores the vectors? Who sets the 'type'? How are they changed?
> And, most importantly, why?
>
> And, if you can it, please don't quote signatures. Thanks!
>
>


Hmm let me explain my problem further.
I'm designin a toolbox that holds a collection of clustering
algorithms. So let's say I'm implementing a class for algorithm A,
another one for algorithm B and so on.
I want the data access to be transparent for them (just use something
like data(i,j) whenever they need a direct access of it.

The data container class will have a function to read it from a file,
to store it in the most effective way and to perform some common
required calculations required by the algorithms.

So basically I built a GUI that gives a user the option to load a data
from a file (and the data class will be constructed), choose one
algorithm and this algorithm will its things with the data.

But, unfotunatelly, I can come accross datasets that can store either
integer values or double values, additionally the data may be dense or
sparse (so I may end up with a sparse matrix representation) and, even
when sparse, the data may be so large that I MUST save memory storing
it with "char" type instead of double!

I guess declaring both types and using it internally with a variable
that holds what type it is must be the only way to deal with it!

thanks
  Réponse avec citation
 
Page generated in 0,08092 seconds with 9 queries