getting a slice off a vector

P

persres

Hello,
I want to take subsequence from a vector and store it in a
different vector. I would like to avoid copying. Is there something
available in boost or stl.

Say,
vector<int> v(0,1,2,3,4,5,6,7,8,9,10);

I want vector<int> slice to hold 5,6,7,8.

I should be able to do:


Is there a wrapper class like this -

template<class T>
struct SliceOfVector
{
SliceOfVector(pair said:
pair< vector<T>::iterator , vector<T>::iterator > m_range;
vector<T>& m_original;
};

SliceOfVector should have all semantics same as vector. May be even
derives from vector<T>.

I should be able to treat SliceOfVector just like a vector, just that
it would be the subsequence specified by m_range of the original
vector.

Am I making sense. Please let me know if you have any suggestions.
 
V

Victor Bazarov

Hello,
I want to take subsequence from a vector and store it in a
different vector. I would like to avoid copying. Is there something
available in boost or stl.

Say,
vector<int> v(0,1,2,3,4,5,6,7,8,9,10);

I want vector<int> slice to hold 5,6,7,8.

I should be able to do:


Is there a wrapper class like this -

template<class T>
struct SliceOfVector
{

pair< vector<T>::iterator , vector<T>::iterator> m_range;
vector<T>& m_original;
};

SliceOfVector should have all semantics same as vector. May be even
derives from vector<T>.

I should be able to treat SliceOfVector just like a vector, just that
it would be the subsequence specified by m_range of the original
vector.

Am I making sense. Please let me know if you have any suggestions.

Storing iterators is dangerous. Storing std::vector iterators is
dangerous squared. Basically you need to make sure that your vector
does not reallocate its memory because that action invalidates all
iterators, pointers and references to vector's elements.

I recommend storing indices. In the debug version of your program I'd
actually check against the size of 'm_original' every time the vector is
accessed using the index.

V
 
J

Jeff Flinn

Hello,
I want to take subsequence from a vector and store it in a
different vector. I would like to avoid copying. Is there something
available in boost or stl.

Say,
vector<int> v(0,1,2,3,4,5,6,7,8,9,10);

I want vector<int> slice to hold 5,6,7,8.

That's a range or sub-range not a slice. IIRC, a slice would be more
akin to a row or column of a 2d matrix, or a 2d matrix from a 3d array.

Have you looked at:
http://www.boost.org/doc/libs/1_47_0/libs/range/doc/html/index.html

It differs from your implementation, in that both iterator_range and
it's derivate, sub_range hold iterators. While your implementation holds
a vector ref. The boost range holding vector iterators would be
invalidated if the underlying vector invalidates those iterators.

Jeff
 
P

persres

Yes, true about iterators. Methinks its worth having a light weight
make_range function that will live for a short time. I can't see how
it could be done with the existing std::vector.
Thanks
 
G

Goran

Hello,
         I want to take subsequence from a vector and store it in a
different vector. I would like to avoid copying. Is there something
available in boost or stl.

Say,
vector<int> v(0,1,2,3,4,5,6,7,8,9,10);

I want vector<int> slice to hold 5,6,7,8.

I should be able to do:

Is there a wrapper class like this -

template<class T>
struct SliceOfVector
{
      SliceOfVector(pair< vector<T>::iterator , vector<T>::iterator> )  ;

pair< vector<T>::iterator , vector<T>::iterator  >  m_range;
vector<T>& m_original;

};

SliceOfVector   should have all semantics same as vector. May be even
derives from vector<T>.

I should be able to treat SliceOfVector  just like a vector, just that
it would be the subsequence specified by m_range of the original
vector.

Am I making sense. Please let me know if you have any suggestions.

Not sure about making sense. If slice IS-A vector, what does it mean
WRT vector modifiers? If you insert value in the slice, it's inserted
in the vector? (same for removals and element modification) Or you
don't want modifiers (not a vector then)? Or...?

If your vector is const while your slice is alive, then first/last
iterator pair is just fine. You only get random access, and that
"const" might prove to be a big requirement.

If not, if you want something richer, look into wrapping vector into
class(es) that supports slicing as you want it.

Finally, did you make __any__ measurement on actual use-case, and
taking into consideration the rest of the processing? If not, that "I
want to avoid copying" part smacks of premature optimization ;-).

Goran.
 
G

Goran

Hello,
         I want to take subsequence from a vector and store it in a
different vector. I would like to avoid copying. Is there something
available in boost or stl.

Say,
vector<int> v(0,1,2,3,4,5,6,7,8,9,10);

I want vector<int> slice to hold 5,6,7,8.

I should be able to do:

Is there a wrapper class like this -

template<class T>
struct SliceOfVector
{
      SliceOfVector(pair< vector<T>::iterator , vector<T>::iterator> )  ;

pair< vector<T>::iterator , vector<T>::iterator  >  m_range;
vector<T>& m_original;

};

SliceOfVector   should have all semantics same as vector. May be even
derives from vector<T>.

I should be able to treat SliceOfVector  just like a vector, just that
it would be the subsequence specified by m_range of the original
vector.

Am I making sense. Please let me know if you have any suggestions.

Not sure about making sense. If slice IS-A vector, what does it mean
WRT vector modifiers? If you insert value in the slice, it's inserted
in the vector? (same for removals and element modification) Or you
don't want modifiers (not a vector then)? Or...?

If your vector is const while your slice is alive, then first/last
iterator pair is just fine. You only get random access, and that
"const" might prove to be a big requirement.

If not, if you want something richer, look into wrapping vector into
class(es) that supports slicing as you want it.

Finally, did you make __any__ measurement on actual use-case, and
taking into consideration the rest of the processing? If not, that "I
want to avoid copying" part smacks of premature optimization ;-).

Goran.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top