wrapper around data structure?

A

aaragon

Hi everyone, I am trying to create a class but I would like to be able
to chose between several data structures for one of its members. The
basic idea is

template
<
class ClassA
{
Sequence* storage_;

public:

// parameter constructor
ClassA(size_t l) {storage_ = new Sequence(l); }

// size
size_t size() { return storage_->size(); }

bool element(size_t site_) {

// since in the bitset the bits are stored from right to left, the
// actual complement of location site_ is located at a distance
// size() - 1 - site_ from the left
return (*storage_)[storage_->size() - 1 - site_];
}

void print() { cout<<*storage_; }
};

This works fine for the boost dynamic bitset that I've been working
with. However, if I try to use a std::vector instead of the
dynamic_bitset<> then the function element is no longer valid. Does
anyone know how can I fix this? I was thinking maybe in implementing a
wrapper along the data structure with but I need some help on this.
Thank you,

aa
 
S

Salt_Peter

aaragon said:
Hi everyone, I am trying to create a class but I would like to be able
to chose between several data structures for one of its members. The
basic idea is

template
<
class ClassA
{
Sequence* storage_;

public:

// parameter constructor
ClassA(size_t l) {storage_ = new Sequence(l); }

// size
size_t size() { return storage_->size(); }

bool element(size_t site_) {

// since in the bitset the bits are stored from right to left, the
// actual complement of location site_ is located at a distance
// size() - 1 - site_ from the left
return (*storage_)[storage_->size() - 1 - site_];
}

void print() { cout<<*storage_; }
};

This works fine for the boost dynamic bitset that I've been working
with. However, if I try to use a std::vector instead of the
dynamic_bitset<> then the function element is no longer valid. Does
anyone know how can I fix this? I was thinking maybe in implementing a
wrapper along the data structure with but I need some help on this.
Thank you,

aa

I'm not sure i get the question, you do mean replace the
boost::dynamic_bitset<> with a std::vector<bool>, right? If so,
specialize both element and print:

#include <iostream>
#include <ostream>
#include <vector>
#include <boost/dynamic_bitset.hpp>
#include <boost/shared_ptr.hpp>
#include <iterator>

template< class Sequence >
class ClassA
{
boost::shared_ptr<Sequence> storage_;

public:
// parameter constructor
ClassA(size_t l)
: storage_( boost::shared_ptr<Sequence>( new Sequence(l) ) )
{ }

// size
size_t size() { return storage_->size(); }

bool element(size_t site_) const;

void print() const;
};

template< >
bool ClassA< std::vector<bool> >::element(size_t site_) const
{
return (*storage_)[site_];
}

template< >
void ClassA< std::vector<bool> >::print() const
{
typedef std::vector<bool>::iterator VIter;
std::copy( (*storage_).begin(),
(*storage_).end(),
std::eek:stream_iterator<bool>( std::cout ) );
std::cout << std::endl;
}

template< >
bool ClassA< boost::dynamic_bitset<> >::element(size_t site_) const
{
return (*storage_)[storage_->size() - 1 - site_];
}

template< >
void ClassA< boost::dynamic_bitset<> >::print() const
{
std::cout<< *storage_ << std::endl;
}

int main()
{
ClassA< std::vector<bool> > av(8);
std::cout << av.element(0) << std::endl;
av.print();
ClassA< boost::dynamic_bitset<> > abs(8);
std::cout << abs.element(0) << std::endl;
abs.print();
return 0;
}
 
M

mlimber

Salt_Peter said:
I'm not sure i get the question, you do mean replace the
boost::dynamic_bitset<> with a std::vector<bool>, right? If so,
specialize both element and print:

The reason for this is that vector<bool> is an explicit (some would
say, botched) specialization of vector.

Cheers! --M
 
S

Salt_Peter

mlimber said:
The reason for this is that vector<bool> is an explicit (some would
say, botched) specialization of vector.

Cheers! --M

Indeed, the level of botchness also depends on the particular
implementation.
Its relatively slow because of the bit operations (thats expected) and
if i recall correctly operators return an internal type instead of
bools with a conversion operator bool() const to convert with - strange
to say the least. I realize now that i completely ignored that in my
code. And so on.

Peter
 
A

aaragon

Salt_Peter said:
Indeed, the level of botchness also depends on the particular
implementation.
Its relatively slow because of the bit operations (thats expected) and
if i recall correctly operators return an internal type instead of
bools with a conversion operator bool() const to convert with - strange
to say the least. I realize now that i completely ignored that in my
code. And so on.

Peter

Hi and thanks for replying. What you suggest may work indeed.
However, imagine that the class have MANY functions. Those that I gave
were just to show my point. Then, I will have to do SO MANY template
specializations and that may not be the best way to do this. Any other
ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top