After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 83729 - Patches to add iterators to GLib
Patches to add iterators to GLib
Status: RESOLVED WONTFIX
Product: glib
Classification: Platform
Component: general
2.0.x
Other All
: Normal enhancement
: ---
Assigned To: gtkdev
gtkdev
: 52084 (view as bug list)
Depends on:
Blocks: 76250
 
 
Reported: 2002-05-31 17:49 UTC by Joel Becker
Modified: 2011-02-18 16:07 UTC
See Also:
GNOME target: ---
GNOME version: Unversioned Enhancement


Attachments
Core GIterator code (9.15 KB, patch)
2002-05-31 17:55 UTC, Joel Becker
none Details | Review
GIterator convenience functions (10.71 KB, patch)
2002-05-31 17:57 UTC, Joel Becker
none Details | Review
GValueIterator code (18.43 KB, patch)
2002-05-31 17:57 UTC, Joel Becker
none Details | Review

Description Joel Becker 2002-05-31 17:49:53 UTC
[ Copy from gtk-devel-list post ]

Iterators for GLib and GObject

        Ok, here are my thoughts on iterators for GLib.
        First, a couple of terms.  I am discussing iterators as Python
or Java use them.  My original code was based on Java's Enumeration
interface, and is now based loosely on the Iterator interface.  Python
iterators behave similarly.  When I speak of an iterator "user," I do
not mean a person implementing an iterator.  That person I refer to as
the "implementor."  The point of an iterator is that the person
retrieving an iterator from a provided API (the "user") does not have
any idea how it is implemented.  They only know the access functions for
iterators.  If this isn't clear, hopefully it will become more clear as
this document progresses ;-)
        I got going on this from the great debate about returning static
versus allocated constructs.  The discussion mostly revolved around
returning static strings vs g_strdup()ing them.  The subject of
allocated vs static GList returns was brought up but not really
discussed.  At the time, various GTK+ API returned GLists, and they
varied whether you were required to free the list.  We've improved that
a lot now, thank heavens.
        In providing an iterator structure, I generally had the
following goals in mind:

o The implementor should have flexible control over how the iterator
  behaves.
o People linking libgobject should be able to take advantage of GValue.
o There should be usable iterators for people only linking libglib.
o The interface should be similar whether GValue is being used or
  not.

        I looked at trying to use the same exact API for GValue and
non-GValue iterators, but it ended up being non-obvious how to do it
cleanly.  The generic use is:

--8<--------------------------------------------------------------
iter = foo_something_iterate(foo);
while (g_iterator_has_more(iter))
{
    item = (FooItem *)g_iterator_get_next(iter);
    do_something(item); 
} 
g_iterator_free(iter);

v_iter = foo_something_else_iterate(foo);
while (g_value_iterator_has_more(v_iter);
{ 
    g_value_iterator_get_next(v_iter, &gvalue);
    do_something_else(gvalue);
} 
g_value_iterator_free(v_iter);
-->8--------------------------------------------------------------

        I've also added some nice convenience functions to do things
like iterate GLists and create a GValueIterator from a GIterator.  As
a side effect of the implementation, "generators" ala python are also
quite possible.  There is an example of a generator in testglib.c.
        Three patches are attached.  They are all against GLib HEAD.
The first (glib-2.0.3-giterator-00-iterator-core.patch) provides the
core GIterator data structure.  The second
(glib-2.0.3-iterator-10-giterator-funcs.patch) provides various
convenience functions to other parts of GLib.  The third
(glib-2.0.3-iterator-20-giterator-gvalue.patch) adds GValueIterator.
These patches are also in Bugzilla against the BugID #000000.
        Please have a look at this.  Folks who are interested, please
make your interest known.  I'm open to any and all feedback.
        I myself use a variation of the GIterator code in three
different projects and I enjoy the flexibility it provides.  I think it
would be a useful addition to GLib.
Comment 1 Joel Becker 2002-05-31 17:55:26 UTC
Created attachment 8893 [details] [review]
Core GIterator code
Comment 2 Joel Becker 2002-05-31 17:57:13 UTC
Created attachment 8894 [details] [review]
GIterator convenience functions
Comment 3 Joel Becker 2002-05-31 17:57:55 UTC
Created attachment 8895 [details] [review]
GValueIterator code
Comment 4 Matthias Clasen 2002-06-07 08:44:46 UTC
*** Bug 52084 has been marked as a duplicate of this bug. ***
Comment 5 Owen Taylor 2002-11-20 20:49:02 UTC
Pushing off to 2.4 API freeze, since it's definitely too
late for 2.2 now.

But I will say that I'm not particularly in favor of this
addition ... not that iterators are a bad idea, but I 
can't see how adding something this fundamental at this
stage in the GLib/GTK+ could ever be anything but
an yet-another-way-to-do-it and a source of further
inconsistency in our APIs.

I'll try to write this up more formally and send a mail
about that to gtk-devel-list early in the 2.4 cycle.

Comment 6 Matthias Clasen 2003-09-18 10:58:57 UTC
One argument in favour of iterators on hash tables that I've seen on 
the lists recently is that g_hashtable_foreach isn't cancelable. Maybe 
we can sneak this in by changing the signature of GHFunc to return


a boolean...
Comment 7 Owen Taylor 2004-01-21 22:53:24 UTC
Can't add a return value to GHFunc ... that's not a compatible
change. 

My feeling about cancelling iteration on GHashTable is that
it at most saves you 50% of your time... 0.5 * O(n) is still
O(n).

Just sent a writeup of my opinion as to why this is WONTFIX
to gtk-devel-list:

 http://mail.gnome.org/archives/gtk-devel-list/2004-January/msg00242.html