Re: Additions to Glib's GPtrArray API



Eric Monsler <emonsler beamreachnetworks com> writes:

> Hi,
> 
> I was looking at the API for GPtrArray manipulation.
> 
> For my needs, I need the equivalent of the g_list_foreach()
> functionality, like g_ptr_array_foreach(), which would be easy enough to
> do from userland.
> 
> In addition, I need a function along the lines of 
> 
> gpointer g_ptr_array_until(GPtrArray          *array,
>                            GPointerUserFunc   func,
>                            gpointer           user_data);
> 
> where the specified function should look like:
> 
> gpointer (*GPointerUserFunc) (gpointer  ptr_from_array,
>                               gpointer  prev_re_user_data);
> 
> and which does 
> 
> {
>   guint this = 0;
>   while( (user_data != NULL) && (this < array->len) )
>   {
>     user_data = (*func)(array->pdata[this++],user_data);
>   }
> 
>   return user_data;
> }
> 
> 
> So, that code is still pseudo, but I think communiates what I mean.
> 
> If I were to properly error-check, use correct types, and add comments,
> etc., would this be a welcome addition to GLib before GTK+ 2.0?
> 
> Or, would this type of operations be better left in userland?

The question that I would ask is whether using a foreach function
here is significiantly clearer than writing:

 for (i = 0; i < array->len; i++)
   {
     MyThing *thing = array->pdata[i]; // or equivalently, thing = g_ptr_array_index (array, i);

     if (thing is the right one)
       break;
   }

Or whatever. Certainly using a predicate function performs less well;
One could say that you use a GArray / GPtrArray because it is directly
indexable like a normal array.

I'm personally not a huge fan of C in a functional style taken
to the extreme; without real closures, it tends to be more awkward
and hard to read in many cases than simply writing a loop.

Other people probably feel differently. 

[ This is the problem with deciding what goes in glib - there are
thousands and thousands of functions that make some sort of sense and
that some people will find usefull, but if you add all of them,
everybody will agree that the library is bloated. ]

Regards,
                                        Owen





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]