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 > how to structure a class that may hold two kind of values
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
how to structure a class that may hold two kind of values

Réponse
 
LinkBack Outils de la discussion
Vieux 07/02/2008, 16h33   #1
fabricio.olivetti@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut how to structure a class that may hold two kind of values

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?

Regards,
Fabricio
  Réponse avec citation
Vieux 07/02/2008, 18h03   #2
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to structure a class that may hold two kind of values

fabricio.olivetti@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.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 07/02/2008, 18h19   #3
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: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.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask


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();...)
  Réponse avec citation
Vieux 07/02/2008, 18h26   #4
AnonMail2005@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 Feb 7, 11:33am, "fabricio.olive...@gmail.com"
<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?
>
> Regards,
> Fabricio


Since you haven't specified how large the dataset can be I would just
store doubles. Worry about space optimization later if it indeed
matters.

HTH
  Réponse avec citation
Vieux 07/02/2008, 18h32   #5
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to structure a class that may hold two kind of values

fabricio.olivetti@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.
>>
>> V
>> --
>> Please remove capital 'A's when replying by e-mail
>> I do not respond to top-posted replies, please don't ask

>
> 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!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 07/02/2008, 20h11   #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
Vieux 07/02/2008, 22h24   #7
Victor Bazarov
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to structure a class that may hold two kind of values

fabricio.olivetti@gmail.com wrote:
> [..]
> 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!


I am guessing you don't mind this tennis match, do you?

I take it that your data class in some cases has to store so much data,
and the data themselves are so imprecise, that storing a 'char' value
is OK and you'd like to do it because you want to save space. Well, it
smells like premature optimization, but if you want it that way, fine.

Now, it does seem that your data storage class is essentially dumb and
does not serve any other purpose except for storage and retrieval. Let
me level with you here. I don't think such class has enough reason to
exist. If your algorithm A (or B, or C) calls for some data to be
lugged around, let the class that handles the data also *store it*.

It makes no sense that the data are stored separately from where they
are processed. If your reading class knows how to read 'char' (or some
other type), fine. But let the processing class allocate the needed
buffer and pass it to the reader for populating with values from the
file.

If you let the processing object hold its own data, then you simply
declare different data types in your different processing classes.
Algorithm A would have 'double', so would Algorithm B, but Algorithm
C, for instance, would have 'char'. No need to extract this into
a separate class and torture yourself trying to squeeze two types
where even one doesn't feel comfortable.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  Réponse avec citation
Vieux 07/02/2008, 23h24   #8
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, 20:24, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> fabricio.olive...@gmail.com wrote:
> > [..]
> > 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!

>
> I am guessing you don't mind this tennis match, do you?
>
> I take it that your data class in some cases has to store so much data,
> and the data themselves are so imprecise, that storing a 'char' value
> is OK and you'd like to do it because you want to save space. Well, it
> smells like premature optimization, but if you want it that way, fine.
>
> Now, it does seem that your data storage class is essentially dumb and
> does not serve any other purpose except for storage and retrieval. Let
> me level with you here. I don't think such class has enough reason to
> exist. If your algorithm A (or B, or C) calls for some data to be
> lugged around, let the class that handles the data also *store it*.
>
> It makes no sense that the data are stored separately from where they
> are processed. If your reading class knows how to read 'char' (or some
> other type), fine. But let the processing class allocate the needed
> buffer and pass it to the reader for populating with values from the
> file.
>
> If you let the processing object hold its own data, then you simply
> declare different data types in your different processing classes.
> Algorithm A would have 'double', so would Algorithm B, but Algorithm
> C, for instance, would have 'char'. No need to extract this into
> a separate class and torture yourself trying to squeeze two types
> where even one doesn't feel comfortable.
>


I don't mind at all

You misunderstood some points of what my program will do.

First, imagine that the dataset is a large matrix of arbitrary values.
Some datasets hold small integer values on the range of [0; 5] (it's
not that storing a char is ok, it's just what I need), others may hold
double values on the range of [-10.0; 10.0]. There is some special
datasets that is extremely huge, and using double prevents these from
loading into memory (HUUUUUUUUUUGE). When using "char" I can load
those datasets, tho.

This data class must be shared among all the algorithms, there's no
sense in loading the dataset each time I run a different algorithm
(they all perform the same task, i.e., cluster the data). I must be
able to compare the performance among them, so it's most likely that
I'll run two or more algorithms on the same dataset.

The rational on operation is something like this: load data, run each
algorithm one at a time, compare results.
  Réponse avec citation
Vieux 08/02/2008, 01h10   #9
Kira Yamato
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to structure a class that may hold two kind of values

On 2008-02-07 11:33:07 -0500, "fabricio.olivetti@gmail.com"
<fabricio.olivetti@gmail.com> said:

> 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?
>
> Regards,
> Fabricio


// Try Boost::Variant. <http://www.boost.org/doc/html/variant.html>

#include <vector>
#include <iostream>
#include "boost/variant.hpp"

int main()
{
using namespace std;
using namespace boost;

// This allows a vector of mixed values, char or double.
vector<variant<char, double> > a;

a.push_back('A');
a.push_back(3.5);

cout << "a[0]=" << get<char>(a[0]) << endl;
cout << "a[1]=" << get<double>(a[1]) << endl;

return 0;
}

--

// kira

  Réponse avec citation
Vieux 08/02/2008, 05h10   #10
Michael Downton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to structure a class that may hold two kind of values

On 2008-02-08 02:33:07 +1000, "fabricio.olivetti@gmail.com"
<fabricio.olivetti@gmail.com> said:

> 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?
>
> Regards,
> Fabricio


First don't strain a class with having to play two roles (even if
similar). Make an interface, make two seperate classes one for
optimized storage, one for plain. If you don't want virtual functions
make the interface implicit and pass the class as a template argument
to the algorithms. (toss up between general, and specific).

Though if the data sets are that hugh, most of your data will be left
in swap anyway. You'd be better off working the algorithms to make
intellegent use of smaller buffers, and sorting the data into regions
to increase locality. That way (if the algorithms are intellegent)
misses on your active set of data is rarer. I might even go so far as
to setup a list of tasks that each algorithm performs. then if a cache
miss does happen (note you'll need to keep track of what is and isn't
loaded yourself), that specific task could be paused. Of course that
last depends on your algorithms.

cheers
Michael

  Réponse avec citation
Vieux 08/02/2008, 05h35   #11
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to structure a class that may hold two kind of values

fabricio.olivetti@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?


The most elegant and probably also most efficient way is to not solve the
problem. Just store doubles as doubles instead of converting back and forth
on the fly.

As for how you _could_ go about the problem of representing different data
transparently, you might have a look at this:


#include <vector>
#include <cassert>
#include <iostream>
#include <memory>

class foo {
public:

typedef std::size_t size_type;

private:

struct base {

virtual
void push_back ( double ) = 0;

virtual
double get ( size_type ) const = 0;

virtual
base * clone ( void ) const = 0;

virtual
~base ( void ) {}

};

template < typename T >
struct node : public base {

std::vector< T > the_data;

void push_back ( double d ) {
the_data.push_back( d );
}

double get ( size_type n ) const {
assert( n < the_data.size() );
return ( the_data[n] );
}

base * clone ( void ) const {
return new node ( *this );
}

};

base * node_ptr;

public:

foo ( bool use_double = true )
: node_ptr ()
{
std::auto_ptr< base > dummy
( use_double ? new node<double> () : new node<char> () );
// put your filling routine here:
for ( unsigned i = 0; i < 20; ++i ) {
dummy->push_back( 100 );
}
node_ptr = dummy.release();
}

double getData ( size_type n ) const {
return ( node_ptr->get( n ) );
}

foo ( foo const & other )
: node_ptr ( other.node_ptr->clone() )
{}

~foo ( void ) {
delete ( node_ptr );
}

};

int main ( void ) {
foo f;
std::cout << f.getData( 5 ) << '\n';
}


Best

Kai-Uwe Bux
  Réponse avec citation
Vieux 08/02/2008, 11h41   #12
Gerhard Fiedler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to structure a class that may hold two kind of values

On 2008-02-07 21:24:35, fabricio.olivetti@gmail.com wrote:

> First, imagine that the dataset is a large matrix of arbitrary values.
> Some datasets hold small integer values on the range of [0; 5] (it's not
> that storing a char is ok, it's just what I need), others may hold
> double values on the range of [-10.0; 10.0]. There is some special
> datasets that is extremely huge, and using double prevents these from
> loading into memory (HUUUUUUUUUUGE). When using "char" I can load those
> datasets, tho.
>
> This data class must be shared among all the algorithms, there's no
> sense in loading the dataset each time I run a different algorithm (they
> all perform the same task, i.e., cluster the data). I must be able to
> compare the performance among them, so it's most likely that I'll run
> two or more algorithms on the same dataset.
>
> The rational on operation is something like this: load data, run each
> algorithm one at a time, compare results.


Without getting into the details, I guess you can store them in your
optimized vectors, but return a union from getData() (or a struct of a type
identifier plus that union, if that's necessary).

Gerhard
  Réponse avec citation
Vieux 08/02/2008, 12h05   #13
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 8 fev, 03:35, Kai-Uwe Bux <jkherci...@gmx.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?

>
> The most elegant and probably also most efficient way is to not solve the
> problem. Just store doubles as doubles instead of converting back and forth
> on the fly.
>
> As for how you _could_ go about the problem of representing different data
> transparently, you might have a look at this:
>
> #include <vector>
> #include <cassert>
> #include <iostream>
> #include <memory>
>
> class foo {
> public:
>
> typedef std::size_t size_type;
>
> private:
>
> struct base {
>
> virtual
> void push_back ( double ) = 0;
>
> virtual
> double get ( size_type ) const = 0;
>
> virtual
> base * clone ( void ) const = 0;
>
> virtual
> ~base ( void ) {}
>
> };
>
> template < typename T >
> struct node : public base {
>
> std::vector< T > the_data;
>
> void push_back ( double d ) {
> the_data.push_back( d );
> }
>
> double get ( size_type n ) const {
> assert( n < the_data.size() );
> return ( the_data[n] );
> }
>
> base * clone ( void ) const {
> return new node ( *this );
> }
>
> };
>
> base * node_ptr;
>
> public:
>
> foo ( bool use_double = true )
> : node_ptr ()
> {
> std::auto_ptr< base > dummy
> ( use_double ? new node<double> () : new node<char> () );
> // put your filling routine here:
> for ( unsigned i = 0; i < 20; ++i ) {
> dummy->push_back( 100 );
> }
> node_ptr = dummy.release();
> }
>
> double getData ( size_type n ) const {
> return ( node_ptr->get( n ) );
> }
>
> foo ( foo const & other )
> : node_ptr ( other.node_ptr->clone() )
> {}
>
> ~foo ( void ) {
> delete ( node_ptr );
> }
>
> };
>
> int main ( void ) {
> foo f;
> std::cout << f.getData( 5 ) << '\n';
>
> }
>


Thanks! That's the way I'll go!
Thank you all!
  Réponse avec citation
Vieux 08/02/2008, 12h49   #14
Daniel T.
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to structure a class that may hold two kind of values

"fabricio.olivetti@gmail.com" <fabricio.olivetti@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?


If all the elements of the dataset can be the same type, (once you
determine which type it can be) then:

class foo {
public:
virtual ~foo() { }
virtual double getData(unsigned i) = 0;
};

class CharFoo {
vector< char > data;
public:
virtual double getData(unsigned i);
};

class DoubleFoo {
vector< char > data;
public:
virtual double getData(unsigned i);
};

If all of the elements can't be the same type (for example if you can
only represent some of the doubles as chars in a particular dataset,)
then store them all as doubles because the amount of memory you would
need to keep track of which ones were chars and which were doubles would
overwhelm the memory you would be saving by compressing the storage to
begin with.

Also, if your datasets are so big, maybe you should use deque instead of
vector.
  Réponse avec citation
Vieux 17/02/2008, 22h33   #15
Jerry Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: how to structure a class that may hold two kind of values

In article <6c3e36da-1b5f-45bd-9624-
c91c49aaa79b@p69g2000hsa.googlegroups.com>, fabricio.olivetti@gmail.com
says...

[ ... ]

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


If you want to use the method you originally outlined, I'd carry it out
via inheritance:

class base_container {
public:
virtual double getData(usigned i) = 0;
};

class char_container {
vector<char> data;
public:
double getData(unsigned i) { return data[i]; }
};

class double_container {
vector<double> data;
public:
double getData(unsigned i) { return data[i]; }
};

base_container *data;

if (file_contains_chars) {
data = new char_container;
read_chars(file, data);
}
else {
data = new double_container;
read_doubles(file, data);
}

switch (cluster_method) {
case KM: k_means_cluster(data); break;
case SOM: self_ordered_map(data); break;
};

--
Later,
Jerry.

The universe is a figment of its own imagination.
  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 06h21.


É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,38005 seconds with 23 queries