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

must iterator be bound to owner/source?

I have a deque-like container that I want to give segmented iterators.
The implementation uses pool allocation where iterators use indices instead of pointers.
The standard iterator type has a reference to the pool and an index.
The local_iterator type, which is used within a pool range, just needs
an index to operate.
But this means that a local iterator from one container instance
could be used as iterator to another. Does this by itself invalidate
my design?

Eg, it means operator== can only test the index, it can't detect container instance.
So it could claim that two iterators from different containers are identical.
Is this immoral? Do I have to add more data to the local_iterator to make it better behaved?

Thanks

homsan
May 27 '06 #1
2 1831
On 2006-05-28 00:55, homsan toft wrote:
I have a deque-like container that I want to give segmented iterators.
The implementation uses pool allocation where iterators use indices instead of pointers.
The standard iterator type has a reference to the pool and an index.
The local_iterator type, which is used within a pool range, just needs
an index to operate.
But this means that a local iterator from one container instance
could be used as iterator to another. Does this by itself invalidate
my design?

Eg, it means operator== can only test the index, it can't detect container instance.
So it could claim that two iterators from different containers are identical.
Is this immoral? Do I have to add more data to the local_iterator to make it better behaved?


In my draft of the standard it says that for forward iterators the
following must hold:

— If a and b are equal, then either a and b are both dereferenceable or
else neither is dereferenceable.

— If a and b are both dereferenceable, then a == b if and only if *a and
*b are the same object.

Note that bidirectional iterators and random access iterators both
fulfill the requirements of forward iterators.

Should you not be able to add a test in your operator== so that if the
indexes are equal then you test so that *it1 == *it2?

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
May 28 '06 #2
I misthought about the operator== example, maybe part of the problem remains:

Erik Wikström wrote:
On 2006-05-28 00:55, homsan toft wrote:
I have a deque-like container that I want to give segmented iterators. The local_iterator type, which is used within a pool range, just needs
an index to operate.
But this means that a local iterator from one container instance
could be used as iterator to another. Does this by itself invalidate
my design?
DequeLike a, b;
// ... fill a, b

DequeLike::iterator ia = a.begin();
DequeLike::iterator ib = b.begin();

// Use faster segemented iterator to a section of the container
while (ia != a.end() && ib != b.end()) {
DequeLike::local_iterator pa = a.local_begin(ia);
DequeLike::local_iterator pb = b.local_begin(ib);
// various operations here
ia = pa; // pa == a.local_end(ia) is true, so ia will find next segment
ib = pb; // ditto
}

// "various operations" includes programmer error
if (pa == pb)
// ...

(perhaps he meant: if (*pa == *pb) ... )

I would like the test pa==pb to be blocked by design somehow!

Is it enough that the test is always false?
I'd *want* to stop here and signal that it's a mistake.
Of course nothing in the type system could help - even if pa and pb
were iterators from different vector<int>'s, the test would be legal code.

So perhaps I can only assert in debug build, and leave the release less safe?
(Meaning the debug-build local_iterators have an extra member,
say a pointer to the owner, and assert on this in all comparisons)
(oh, now a voice is trying to command me to use the SafeSTL iterator wrappers anyway...
uuuhhg, have to look at it I guess...)
Eg, it means operator== can only test the index, it can't detect
container instance.
So it could claim that two iterators from different containers are
identical.
Is this immoral? Do I have to add more data to the local_iterator to
make it better behaved?

....
Should you not be able to add a test in your operator== so that if the
indexes are equal then you test so that *it1 == *it2?
Duh. Right. I confused myself trying to think about both
operator== when the object is eg an int, and of
operator< when I have a pointer to X.

Erik Wikström


Thanks for your answer!
(I use the C++0x draft standard, but it's hard to fit all pieces together)

homsan
May 28 '06 #3

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

Similar topics

1
by: geoff | last post by:
I'm trying to create a new database and new login to allow a client (through my asp.net page) to create their own database, username and password. I've tried using the stored procedures I've found...
4
by: pervinder | last post by:
Hi, I get an error on line #3 while compiling the program segment on aix (xlc - va6) While this warning is not seen with gcc "test.cpp" line 200.18: 1540-0152 (W) A template dependent name that...
4
by: John | last post by:
I was wondering why the STL RB Tree implementation is used when the iterators of map are incremented/decremented to yield successors and predecessors? This takes O(log n) time (and a lot of memory...
14
by: shawnk | last post by:
I searched the net to see if other developers have been looking for a writable iterator in C#. I found much discussion and thus this post. Currently (C# 2) you can not pass ref and out arguments...
7
by: PengYu.UT | last post by:
I'm wondering is the standard defined behavior of past bound iterator. In the following example it seems that afer first "--it", it point to -1 index. I'm wondering if it is true independent of...
2
by: Jim Langston | last post by:
I am working on a class design to contain the indices of triangle lists for body parts. This is for 3d modeling. As as start I have: class PartTriangles { public: PartTriangels( const...
1
by: Scott Gifford | last post by:
Hello, I'm working on an providing an iterator interface to a database. The basic thing I'm trying to accomplish is to have my iterator read rows from the database and return constructed...
7
by: Donos | last post by:
Hello I have a Queue which is declared as, std::queue<unsigned charm_Queue; I push data into this queue. Now i want to take the data out of this queue using Iterator.
4
by: qlin88 | last post by:
Hi, In STL multi-map, the lower_bound, upper_bound,equal_range all return an iterator. Now ifone wants to iterate from an upper bound towards a lower bound, what would be the best way to do it?...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.