473,230 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

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

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
8 3504
* 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
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
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
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
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
>
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
* 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim Campbell | last post by:
Suppose I wanted to a class to contain a pointer, but I didn't want to make the copy constructor or assignment operator reinitialize this pointer. (Think of a class that contains an mmapped pointer...
1
by: Ruediger Knoerig | last post by:
In my current project I used templates to provide an universial interface to vector objects. For this purpose I declared two methods with an additional template argument: template<class Vector>...
4
by: Daniel Aarno | last post by:
Consider the following code: typedef int A; class B { public: B(A& var) : m_var(var) {} virtual ~B() {} protected:
3
by: C# Expert | last post by:
I am using managed c++ code. I have created i function with one argument that is reference to double. fun(double& d) When I see it in object browser in visual studio 2005, it shows pointer...
11
by: asdf | last post by:
C++ allows a reference to a pointer, but doesn't allow a pointer to a reference, why?
7
by: Mohan | last post by:
Hi, What are the advantages/disadvantages of using a pointer instead of Reference in the Copy Constructor ? For Example, Writing the Copy constructor for the Class "Temp" as below, ...
9
by: sulekhasweety | last post by:
dear all, can anyone explain the differences between pointer variable and reference variables ?
6
by: swathi amireddy | last post by:
Currently im trying to migrate a project from .net 1.1 to 2.0.While running the application its giving an exception for the below code while doing the update operation: protected void...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.