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

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

P: n/a
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
Jan 31 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 31 '06 #2

P: n/a
Bo Peng wrote:
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
Jan 31 '06 #3

P: n/a
Victor Bazarov wrote:
Bo Peng wrote:
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
Jan 31 '06 #4

P: n/a
Bo Peng wrote:
Victor Bazarov wrote:
Bo Peng wrote:
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
Jan 31 '06 #5

P: n/a
Victor Bazarov wrote:
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
Feb 1 '06 #6

P: n/a
>
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
Feb 1 '06 #7

P: n/a
* Bo Peng:
Victor Bazarov wrote:
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 1 '06 #8

P: n/a
On Tue, 31 Jan 2006 18:54:00 -0600, Bo Peng <bp***@rice.edu> wrote:

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.


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>::operator[] 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.
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


--
Bob Hairgrove
No**********@Home.com
Feb 1 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.