Re: opinion poll on text widget iterators
- From: Guillaume Laurent <glaurent worldnet fr>
- To: gtk-devel-list redhat com
- Subject: Re: opinion poll on text widget iterators
- Date: 28 Feb 2000 23:27:51 +0100
Havoc Pennington <hp@redhat.com> writes:
> Right now I have an opaque reference-counted iterator object,
> FrooTkxtIndex. The annoying thing about this object is that
> it sucks as an iterator.
Another annoying thing is the type name. :-)
> FrooTkxtIndex* iter;
> iter = froo_tkxt_buffer_get_char_index(buffer, 0);
> while (iter != NULL)
> {
> /* do stuff with iter */
>
> FrooTkxtIndex* freeme = iter;
> iter = froo_tkxt_index_next_char(iter);
> froo_tkxt_index_unref(freeme);
> }
>
> So, yuck.
>
> Proposed alternative is:
>
> FrooTkxtIndex iter;
> froo_tkxt_buffer_get_char_index(buffer, 0, &iter);
> while (froo_tkxt_index_next_char(&iter))
> {
> /* do stuff with iter */
> }
Actually, for C++, the first form is much preferable. An STL iterator
is usually used as follows :
for(iter = begin(); iterm != end(); iter++)
{
/* do stuff with iter */
}
So we need to have seperate operations for moving it forward, and
checking if we've reached end or not. The unref part is in
iter::operator++(), so no problem if it takes 3 lines, the programmer
won't see it.
> Misgiving #2: these index objects are 90% of the time not actually
> used for iteration; it will be both easier and more efficient to use
> _foreach() API's that are also provided for iterating over the
> buffer.
Not in C++, we already have for_each(), so having one to wrap looks
quite awkward. I wonder how Python or Perl manage this - my guess is
that the problem is similar.
What we need for an iterator is :
- a way to move it forward
- a way to test if it has reached past the last element
- a way to dereference it
All these operations have to be O(1) for an iterator to be
useable. Then depending on the feasability, goodies include :
- a way to move it backward
- a way to set it to a random position
> "Dereferencing" a FrooTkxtIndex is O(n) where n is the length
> of the line containing the index.
We've got a problem here... Why is that ?
> So misgiving #2 is that FrooTkxtIndex isn't actually inconvenient in
> the common use case.
But these "common use cases" are actually quite uncommon if you
consider how an iterator is used in general.
--
Guillaume.
http://www.telegraph-road.org
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]