Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > range iterator

Reply
Thread Tools

range iterator

 
 
toton
Guest
Posts: n/a
 
      08-24-2006
Hi,
I want ro iterate over a few container class within a range
specified, instead of begin & end.
How to construct a range class, which takes start & end, and iteration
is available within that range only. Itaration may be const,
bidiractional, forward or backward.

Say I have a vector or other container class,
like vector<int> vec;
and want to return a range class like range(vec.begin()+5,
vec.end() - 10);
range will have a begin & end to denote start & end of the range,
but beyond the range is not allowed to be iteraded.
Actually, I have a large no of objects stored in a container, a range
of them belongs to a particular instance, who is allowed to modify it.
Those ranges are sometimes overlapping also.
some only needs a const iterator while other needs modification (may
not be deletion, which itself modifies the range!). I want each
instance to return it's associated range for iteration.
Thanks for any suggestion.

 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-24-2006
toton wrote:

> Hi,
> I want ro iterate over a few container class within a range
> specified, instead of begin & end.
> How to construct a range class, which takes start & end, and iteration
> is available within that range only. Itaration may be const,
> bidiractional, forward or backward.
>
> Say I have a vector or other container class,
> like vector<int> vec;
> and want to return a range class like range(vec.begin()+5,
> vec.end() - 10);
> range will have a begin & end to denote start & end of the range,
> but beyond the range is not allowed to be iteraded.
> Actually, I have a large no of objects stored in a container, a range
> of them belongs to a particular instance, who is allowed to modify it.
> Those ranges are sometimes overlapping also.
> some only needs a const iterator while other needs modification (may
> not be deletion, which itself modifies the range!). I want each
> instance to return it's associated range for iteration.


You could start with a simple adaptor like this:

template < typename Iterator >
class range {

Iterator start;
Iterator past_end;

public:

typedef typename iterator_traits< Iterator >::size_type size_type;

range ( Iterator const & from, Iterator const & to )
: start ( from )
, past_end ( to )
{}

size_type size ( void ) const {
typename iterator_traits< Iterator >::difference_type
dist = std::distance( this->start, this->past_end );
assert( dist >= 0 );
return ( dist );
}

Iterator begin ( void ) const {
return( start );
}

Iterator end ( void ) const {
return( this->past_end );
}

bool empty ( void ) const {
return( this->start == this->past_end );
}

}; // class range<>

The use of the assert is debatable. Ideally, one would check the validity of
the range upon construction and throw something if [from,to) is not sound.
However, that might be inefficient (for non-random access iterators) or
impossible (if from and to point to different containers).


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
 
toton
Guest
Posts: n/a
 
      08-24-2006

Kai-Uwe Bux wrote:
> toton wrote:
>
> > Hi,
> > I want ro iterate over a few container class within a range
> > specified, instead of begin & end.
> > How to construct a range class, which takes start & end, and iteration
> > is available within that range only. Itaration may be const,
> > bidiractional, forward or backward.
> >
> > Say I have a vector or other container class,
> > like vector<int> vec;
> > and want to return a range class like range(vec.begin()+5,
> > vec.end() - 10);
> > range will have a begin & end to denote start & end of the range,
> > but beyond the range is not allowed to be iteraded.
> > Actually, I have a large no of objects stored in a container, a range
> > of them belongs to a particular instance, who is allowed to modify it.
> > Those ranges are sometimes overlapping also.
> > some only needs a const iterator while other needs modification (may
> > not be deletion, which itself modifies the range!). I want each
> > instance to return it's associated range for iteration.

>
> You could start with a simple adaptor like this:
>
> template < typename Iterator >
> class range {
>
> Iterator start;
> Iterator past_end;
>
> public:
>
> typedef typename iterator_traits< Iterator >::size_type size_type;
>
> range ( Iterator const & from, Iterator const & to )
> : start ( from )
> , past_end ( to )
> {}
>
> size_type size ( void ) const {
> typename iterator_traits< Iterator >::difference_type
> dist = std::distance( this->start, this->past_end );
> assert( dist >= 0 );
> return ( dist );
> }
>
> Iterator begin ( void ) const {
> return( start );
> }
>
> Iterator end ( void ) const {
> return( this->past_end );
> }
>
> bool empty ( void ) const {
> return( this->start == this->past_end );
> }
>
> }; // class range<>
>
> The use of the assert is debatable. Ideally, one would check the validity of
> the range upon construction and throw something if [from,to) is not sound.
> However, that might be inefficient (for non-random access iterators) or
> impossible (if from and to point to different containers).
>

Yes, this is a good example. but someone can iterate past range, is it
not? like someone can go end+10 or start-5 also. Is there a way to
prevent out of range in iterator? I am not sure how to do it as the
iterator itself does not throw out_of_range.
Anything can be done to prevent out of range?
secondly, if I have a class instance which knows the range from the
constructor, is it better to store the range in object, and return the
range as reference? or store only the start & end location, and return
a constructed range object when range is needed?

> Best
>
> Kai-Uwe Bux


 
Reply With Quote
 
toton
Guest
Posts: n/a
 
      08-24-2006

Kai-Uwe Bux wrote:
> toton wrote:
>
> > Hi,
> > I want ro iterate over a few container class within a range
> > specified, instead of begin & end.
> > How to construct a range class, which takes start & end, and iteration
> > is available within that range only. Itaration may be const,
> > bidiractional, forward or backward.
> >
> > Say I have a vector or other container class,
> > like vector<int> vec;
> > and want to return a range class like range(vec.begin()+5,
> > vec.end() - 10);
> > range will have a begin & end to denote start & end of the range,
> > but beyond the range is not allowed to be iteraded.
> > Actually, I have a large no of objects stored in a container, a range
> > of them belongs to a particular instance, who is allowed to modify it.
> > Those ranges are sometimes overlapping also.
> > some only needs a const iterator while other needs modification (may
> > not be deletion, which itself modifies the range!). I want each
> > instance to return it's associated range for iteration.

>
> You could start with a simple adaptor like this:
>
> template < typename Iterator >
> class range {
>
> Iterator start;
> Iterator past_end;
>
> public:
>
> typedef typename iterator_traits< Iterator >::size_type size_type;
>
> range ( Iterator const & from, Iterator const & to )
> : start ( from )
> , past_end ( to )
> {}
>
> size_type size ( void ) const {
> typename iterator_traits< Iterator >::difference_type
> dist = std::distance( this->start, this->past_end );
> assert( dist >= 0 );
> return ( dist );
> }
>
> Iterator begin ( void ) const {
> return( start );
> }
>
> Iterator end ( void ) const {
> return( this->past_end );
> }
>
> bool empty ( void ) const {
> return( this->start == this->past_end );
> }
>
> }; // class range<>
>
> The use of the assert is debatable. Ideally, one would check the validity of
> the range upon construction and throw something if [from,to) is not sound.
> However, that might be inefficient (for non-random access iterators) or
> impossible (if from and to point to different containers).
>

Yes, this is a good example. but someone can iterate past range, is it
not? like someone can go end+10 or start-5 also. Is there a way to
prevent out of range in iterator? I am not sure how to do it as the
iterator itself does not throw out_of_range.
Anything can be done to prevent out of range?
secondly, if I have a class instance which knows the range from the
constructor, is it better to store the range in object, and return the
range as reference? or store only the start & end location, and return
a constructed range object when range is needed?
thanks for the help.
> Best
>
> Kai-Uwe Bux


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments