getting a slice off a vector

Discussion in 'C++' started by persres@googlemail.com, Sep 19, 2011.

  1. Guest

    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.
     
    , Sep 19, 2011
    #1
    1. Advertising

  2. On 9/19/2011 8:13 AM, wrote:
    > 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.


    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
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 19, 2011
    #2
    1. Advertising

  3. Jeff Flinn Guest

    wrote:
    > 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
     
    Jeff Flinn, Sep 19, 2011
    #3
  4. Guest

    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
     
    , Sep 19, 2011
    #4
  5. Goran Guest

    On Sep 19, 2:13 pm, "" <>
    wrote:
    > 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.
     
    Goran, Sep 19, 2011
    #5
  6. Goran Guest

    On Sep 19, 2:13 pm, "" <>
    wrote:
    > 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.
     
    Goran, Sep 19, 2011
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    0
    Views:
    457
    Steve
    Jan 31, 2006
  2. pmatos
    Replies:
    6
    Views:
    23,802
  3. Replies:
    8
    Views:
    1,925
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    565
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    362
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page