473,395 Members | 1,949 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,395 software developers and data experts.

Input bidirectional custom iterator

Hi,

I know iterator categories as presented
by many authors: Stroustrup, Josuttis and Koenig&Moo:

Input <---|
|<--- Forward <--- Bidirectional <--- Random
Output <---|
Is it a requirement that Forward, Bidirectional and Random iterators
provide operations of both categories: Input and Output?

Here are my objectives:
1. I'd like to create custom iterator of Input
category - read-only access.
2. I'd also like to define my read-only access iterator to act as
bidirectional or random access (my sequence is random access compatible).

Last question, is it a valid and STL-compatible iterator which:
- enables user to travers both directions or even to get random access
- provides *only* read-only access?

Simply, it's a little cofusing me that i.e. Bidirectional iterator
is always assumed to provide read and write access but it is not assumed
to provide only read-only access.

Thanks for giving me some light on this subject.
Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Jan 3 '06 #1
8 2423
"Mateusz Loskot" <se****@signature.net> wrote in message
news:dp**********@inews.gazeta.pl...
Hi,

I know iterator categories as presented
by many authors: Stroustrup, Josuttis and Koenig&Moo:

Input <---|
|<--- Forward <--- Bidirectional <--- Random
Output <---|
Is it a requirement that Forward, Bidirectional and Random iterators
provide operations of both categories: Input and Output?

Here are my objectives:
1. I'd like to create custom iterator of Input
category - read-only access.
2. I'd also like to define my read-only access iterator to act as
bidirectional or random access (my sequence is random access compatible).

Last question, is it a valid and STL-compatible iterator which:
- enables user to travers both directions or even to get random access
- provides *only* read-only access?

Simply, it's a little cofusing me that i.e. Bidirectional iterator
is always assumed to provide read and write access but it is not assumed
to provide only read-only access.

Thanks for giving me some light on this subject.
Cheers
--
Mateusz Loskot
http://mateusz.loskot.net


The key thing to understand about iterator classifications is that they do
not represent inheritance. Actually, making a set of iterators which derive
from one another is dubious practice except for deriving an iterator from a
const_iterator. Otherwise it is generally better to derive all iterators
from std::iterator so they can be tagged appropriately. Of course the usual
techniques of composition and/or derivation from a common base class may be
applicable. HTH

Cy
Jan 3 '06 #2
Cy Edmunds wrote:
"Mateusz Loskot" <se****@signature.net> wrote in message:

Last question, is it a valid and STL-compatible iterator which: -
enables user to travers both directions or even to get random
access - provides *only* read-only access?

Simply, it's a little cofusing me that i.e. Bidirectional iterator
is always assumed to provide read and write access but it is not
assumed to provide only read-only access.

Thanks for giving me some light on this subject. Cheers -- Mateusz
Loskot http://mateusz.loskot.net

The key thing to understand about iterator classifications is that
they do not represent inheritance.


I know that. I did say nothing about inheritance.
Actually, making a set of iterators which derive from one another is
dubious practice except for deriving an iterator from a
const_iterator.
or std::iterator
Otherwise it is generally better to derive all iterators from
std::iterator so they can be tagged appropriately.
Yes
Of course the usual techniques of composition and/or derivation from
a common base class may be applicable. HTH


Hmm, I understand everything what you wrote but it does not apply to my
question at all.

I'm not asking about inheritance and how to define hierarchies of
iterators. I'm asking relation [1] between categories of iterators and
if some special cases [2] of custom iterators can be considered as valid
in terms of STL (usage in STL algorithms).

[1] relation as described by Stroustrup but not as inheritance; Forward
iterator relates to Input and Output in terms of Forward includes all
operations of Input and Output iterators.

[2] e.g. is read-only Forward iterator valid in terms of STL. As
Stroustrup explained Forward iterator is a combination of operations
provided by Input and Output. What if my Forward iterator won't offer
operations Output iterator category?

Cheers
--
Mateusz oskot
http://mateusz.loskot.net
Jan 3 '06 #3
On 2006-01-03, Mateusz oskot <se****@signature.net> wrote:
"Mateusz Loskot" <se****@signature.net> wrote in message:

I'm not asking about inheritance and how to define hierarchies
of iterators. I'm asking relation [1] between categories of
iterators and if some special cases [2] of custom iterators can
be considered as valid in terms of STL (usage in STL
algorithms).


Yes. vector<int>::const_iterator is equivalent to the random
access, read-only iterator you are considering.

--
Neil Cerutti
Jan 3 '06 #4
Neil Cerutti wrote:
On 2006-01-03, Mateusz £oskot <se****@signature.net> wrote:
"Mateusz Loskot" <se****@signature.net> wrote in message:


I'm not asking about inheritance and how to define hierarchies of
iterators. I'm asking relation [1] between categories of iterators
and if some special cases [2] of custom iterators can be
considered as valid in terms of STL (usage in STL algorithms).

Yes. vector<int>::const_iterator is equivalent to the random access,
read-only iterator you are considering.


const_iterator says only about how value_type is accessed
(dereferenced): const T* and const T& as opposite to T* and T& in
non-const iterator.

I'm talking about iterator categories in terms of operations they provide.

Obviously, my english may be a limitation here.

I'll try to explain it more clear.
Input iterator category is based on following set of operations
(from Josuttis' STL book):

*iter
++iter
iter++
iter1 == iter2
iter1 != iter2
TYPE(iter)

Output iterator category operations list:
*iter = value (assignment)
++iter
iter++
TYPE(iter)

Forward iterator category is a combination of both sets of operations above.

Now, I'm asking if I can define list of operations of my custom iterator
this way:

### Input ###
*iter
++iter
iter++
iter1 == iter2
iter1 != iter2
TYPE(iter)
### Bidirectional ###
--iter
iter++

As you can see, I've not applied set of operations from Output iterator
(or even from Forward iterator).
But Bidirectional iterator is assumed to provide those operations.
So, how about that?

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Jan 3 '06 #5
Mateusz Łoskot wrote:

### Bidirectional ###
--iter
iter++


Sorry, but here I did small typo:
Not iter++ but iter--, so:

### Bidirectional ###
--iter
iter--

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Jan 3 '06 #6
On 2006-01-03, Mateusz ?oskot <se****@signature.net> wrote:
Neil Cerutti wrote:
On 2006-01-03, Mateusz oskot <se****@signature.net> wrote: I'm talking about iterator categories in terms of operations
they provide.

Obviously, my english may be a limitation here.

I'll try to explain it more clear.
Input iterator category is based on following set of operations
(from Josuttis' STL book):

*iter
++iter
iter++
iter1 == iter2
iter1 != iter2
TYPE(iter)

Output iterator category operations list:
*iter = value (assignment)
++iter
iter++
TYPE(iter)

Forward iterator category is a combination of both sets of
operations above.

Now, I'm asking if I can define list of operations of my custom
iterator this way:

### Input ###
*iter
++iter
iter++
iter1 == iter2
iter1 != iter2
TYPE(iter)
### Bidirectional ###
--iter
iter--

As you can see, I've not applied set of operations from Output
iterator (or even from Forward iterator). But Bidirectional
iterator is assumed to provide those operations. So, how about
that?


You need to support all the operations of Forward iterators in a
Bidirectional iterator, or it won't qualify as one, and might not
work with standard algorithms that require Bidirectional
iterators.

This quote is from the SGI docs:

The illustration below displays the five iterator categories
defined by the standard library, and shows their hierarchical
relationship. Because standard library iterator categories are
hierarchical, each category includes all the requirements of
the categories above it.

Requirements of Forward Iterators
<SNIP>

Requirements of Bidirectional Iterators
A bidirectional iterator must meet all the requirements for
forward iterators. In addition, the following expressions must
be valid
<SNIP>

If your design is the best type of iterator for your container,
then you'll have to tag it as an Input or Output iterator, which
happens to support a few more operations than normal. Clients
won't be able to use it as a Bidirectional iterator, despite that
it can go both ways.

--
Neil Cerutti
Jan 3 '06 #7
Neil Cerutti wrote:
On 2006-01-03, Mateusz ?oskot <se****@signature.net> wrote:
Neil Cerutti wrote:
On 2006-01-03, Mateusz £oskot <se****@signature.net> wrote:


I'm talking about iterator categories in terms of operations they
provide.

Obviously, my english may be a limitation here.

I'll try to explain it more clear. Input iterator category is based
on following set of operations (from Josuttis' STL book):

*iter ++iter iter++ iter1 == iter2 iter1 != iter2 TYPE(iter)

Output iterator category operations list: *iter = value
(assignment) ++iter iter++ TYPE(iter)

Forward iterator category is a combination of both sets of
operations above.

Now, I'm asking if I can define list of operations of my custom
iterator this way:


### Input ### *iter ++iter iter++ iter1 == iter2 iter1 != iter2
TYPE(iter) ### Bidirectional ### --iter iter--

As you can see, I've not applied set of operations from Output
iterator (or even from Forward iterator). But Bidirectional
iterator is assumed to provide those operations. So, how about
that?

You need to support all the operations of Forward iterators in a
Bidirectional iterator, or it won't qualify as one, and might not
work with standard algorithms that require Bidirectional iterators.

This quote is from the SGI docs:

[...]


That's asnwer I expected. I also know what SGI documentation says but
I just wanted to be sure and to get up-to-date answer.

Similarly, there is a definition of Sequence term in SGI docs.
In fact, (http://www.windevnet.com/documents/s...303a/0303a.htm)
user may define custom sequence which doesn't follow all SGI's Sequence
concept constraints but it still may be considered as "valid" sequence
in terms of STL iterators and algorithms (certainly, depending on
categories of those iterators).

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Jan 3 '06 #8
Neil Cerutti wrote:
On 2006-01-03, Mateusz £oskot <se****@signature.net> wrote:
"Mateusz Loskot" <se****@signature.net> wrote in message:


I'm not asking about inheritance and how to define hierarchies
of iterators. I'm asking relation [1] between categories of
iterators and if some special cases [2] of custom iterators can
be considered as valid in terms of STL (usage in STL
algorithms).

Yes. vector<int>::const_iterator is equivalent to the random
access, read-only iterator you are considering.


One more note from my side, after I did some deeper analysis of
iterators for my better understanding of the subject.
T::const_iterator provides read-only access to single element of
sequence (container fits STL better).
But iterator itself may provide read-only, write-only or both accesses
to the whole sequence (e.g. traverse is immutable for sequence as a
whole, but re-order items, remove items is mutable).

There were lack of this understanding in my previous considerations.

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Jan 4 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Wiseguy | last post by:
It seems very short sighted and silly that the bidirectional capabilities of ListIterator have not been implemented for collections based upon sets, but only those based upon lists. I need...
6
by: Fraser Ross | last post by:
Algorithms cannot be used with input stream iterators? Is copying the range to a temporary container before using the algorithm the usual thing to do? Fraser.
0
by: nick | last post by:
Hi, I need to manage a "layered" collection of objects, where each layer grows independently, e.g, o--+--+--+--+--+ 1st layer | o 2nd layer (empty) | o--+--+--+ 3rd...
2
by: fabricemarchant | last post by:
Hi ! I'm writing a tiny interpreted language. The interpreter will first be able to load a list of source files before giving the hand to the keyboard : I use GNU readline/history library that...
7
by: Max Odendahl | last post by:
Hi, my own declared class has a stl::list as a member variable. I now need access to those from the outside. Is a custom iterator for my class the best solution for this and how to do this? I...
9
by: nottheartistinquestion | last post by:
As an intellectual exercise, I've implemented an STL-esque List<and List<>::Iterator. Now, I would like a signed distance between two iterators which corresponds to their relative position in the...
13
by: cront | last post by:
I have a problem to work on: we will ask user to input anything and we will put that back onto the standard output with all set of brackets removed. We will not remove any single bracket e.g. ...
7
by: Christopher Pisz | last post by:
I found an article http://spec.winprog.org/streams/ as a starting point, but it seems to do alot of things that aren't very standard at all. One particular problem is, that he is using a vector as...
5
by: Kenneth Porter | last post by:
Is there a stylistically preferred way to apply an algorithm to a vector of vectors? For example, suppose I have: typedef vector< vector<int Array2D; Array2D array2D; I want to find the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

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.