By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,171 Members | 992 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,171 IT Pros & Developers. It's quick & easy.

wrapper around data structure?

P: n/a
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 Sequence = dynamic_bitset<>
>
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

Nov 3 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

aaragon wrote:
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 Sequence = dynamic_bitset<>
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<Sequencestorage_;

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::ostream_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;
}

Nov 3 '06 #2

P: n/a
Salt_Peter wrote:
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<boolis an explicit (some would
say, botched) specialization of vector.

Cheers! --M

Nov 3 '06 #3

P: n/a

mlimber wrote:
Salt_Peter wrote:
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<boolis 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

Nov 3 '06 #4

P: n/a

Salt_Peter wrote:
mlimber wrote:
Salt_Peter wrote:
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<boolis 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
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?

Nov 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.