Connecting Tech Pros Worldwide Forums | Help | Site Map

range iterator

toton
Guest
 
Posts: n/a
#1: Aug 24 '06
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<intvec;
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.


Kai-Uwe Bux
Guest
 
Posts: n/a
#2: Aug 24 '06

re: range iterator


toton wrote:
Quote:
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<intvec;
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
toton
Guest
 
Posts: n/a
#3: Aug 24 '06

re: range iterator



Kai-Uwe Bux wrote:
Quote:
toton wrote:
>
Quote:
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<intvec;
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?
Quote:
Best
>
Kai-Uwe Bux
toton
Guest
 
Posts: n/a
#4: Aug 24 '06

re: range iterator



Kai-Uwe Bux wrote:
Quote:
toton wrote:
>
Quote:
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<intvec;
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.
Quote:
Best
>
Kai-Uwe Bux
Closed Thread