How to get pointer and offset from vector<bool>::iterator (or reference)

B

Bo Peng

Dear list,

I am using std::vector<bool> (bit_vector) to store my bit sequence. To
access the same sequence from C (to expose to a python module), I need
to know the pointer and offset of vector::<bool>::iterator (or
reference). However, given a std::vector<bool> a, all a.begin(), a[0]
etc are instances of a proxy class so I can not do things like
&*a.begin(). Is there a safe way to get the information I need?

Many thanks in advance.
Bo
 
A

Alf P. Steinbach

* Bo Peng:
I am using std::vector<bool> (bit_vector) to store my bit sequence. To
access the same sequence from C (to expose to a python module), I need
to know the pointer and offset of vector::<bool>::iterator (or
reference). However, given a std::vector<bool> a, all a.begin(), a[0]
etc are instances of a proxy class so I can not do things like
&*a.begin(). Is there a safe way to get the information I need?

Something like

void python( int const bitValues[], int nValues );

void callPython( std::vector<bool> const& bits )
{
std::vector<int> const values( bits.begin(), bits.end() );
python( &values[0], safe_cast<int>( values.size() ) );
}

where "safe_cast" is your favorite safe cast from one integral type to
another, presumably aborting or throwing an exception if the value can't
be represented by the destination type.
 
V

Victor Bazarov

Bo said:
I am using std::vector<bool> (bit_vector) to store my bit sequence. To
access the same sequence from C (to expose to a python module), I need
to know the pointer and offset of vector::<bool>::iterator (or
reference). However, given a std::vector<bool> a, all a.begin(), a[0]
etc are instances of a proxy class so I can not do things like
&*a.begin(). Is there a safe way to get the information I need?

No. Bits have no separate address.

V
 
B

Bo Peng

Victor said:
Bo said:
I am using std::vector<bool> (bit_vector) to store my bit sequence. To
access the same sequence from C (to expose to a python module), I need
to know the pointer and offset of vector::<bool>::iterator (or
reference). However, given a std::vector<bool> a, all a.begin(), a[0]
etc are instances of a proxy class so I can not do things like
&*a.begin(). Is there a safe way to get the information I need?


No. Bits have no separate address.

Really? What I need is actually the starting point of the bits. I.e.,
&*vec.begin() if the vector is not vector<bool>. It should not be too
diffcult since bit_vector always starts from the beginning of a WORD.

Cheers,
Bo
 
V

Victor Bazarov

Bo said:
Victor said:
Bo said:
I am using std::vector<bool> (bit_vector) to store my bit sequence.
To access the same sequence from C (to expose to a python module), I
need to know the pointer and offset of vector::<bool>::iterator (or
reference). However, given a std::vector<bool> a, all a.begin(), a[0]
etc are instances of a proxy class so I can not do things like
&*a.begin(). Is there a safe way to get the information I need?



No. Bits have no separate address.

Really? What I need is actually the starting point of the bits. I.e.,
&*vec.begin() if the vector is not vector<bool>. It should not be too
diffcult since bit_vector always starts from the beginning of a WORD.

There is no requirement that 'vector<bool>' keeps its bits in the same
array, same value, or same {whatever_other_collection_you_can_think_of}.

So, if you _know_ that those bits are a contiguous representation of some
kind of integral value (or whatever you think they are), you know already
where and/or how to get them.

All I can suggest is to repackage them as you need them, just like Alf
explained.

V
 
B

Bo Peng

Victor said:
All I can suggest is to repackage them as you need them, just like Alf
explained.

The problem is that the goal of this python module is to *expose* the
underlying vector<bool> (other vectors are easy and have been done) and
allow users to read/write the vector directly. For example

v = getVector(); # return a python object v
print v
print v[2]
v[0] = 1
v[3] = 0

Bo
 
A

Alf P. Steinbach

* Bo Peng:
Victor said:
All I can suggest is to repackage them as you need them, just like Alf
explained.

The problem is that the goal of this python module is to *expose* the
underlying vector<bool> (other vectors are easy and have been done) and
allow users to read/write the vector directly. For example

v = getVector(); # return a python object v
print v
print v[2]
v[0] = 1
v[3] = 0

For that you need to provide a wrapper to the python side.
 
B

Bo Peng

For that you need to provide a wrapper to the python side.

Exactly. I have already working code for other vectors. The python
wrapper object knows vector type and starting pointer and can correctly
read/write vector items. In the case of vector<bool>, I am unable to get
the starting address.

The only way out may be extending python in C++, instead of C. I mean,
store a vector reference directly instead of pointer. I really do not
know how to do this since all examples of extending Python is written in C.

Cheers,
Bo
 
B

Bob Hairgrove

Exactly. I have already working code for other vectors. The python
wrapper object knows vector type and starting pointer and can correctly
read/write vector items. In the case of vector<bool>, I am unable to get
the starting address.

vector<bool> is an exception to all other vectors. You cannot access
its storage directly like a buffer, neither in C++ nor in any other
language. As the C++ standard says, the reference returned by
vector<bool>::eek:perator[] is some kind of proxy class that lets you
read and write individual bool elements, but that's all.

If you can't do it in C++, "extending Python" won't help, either.
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top