Re: GList empty after iteration?

2011-09-21 Thread Olivier Sessink
On 09/12/2011 11:33 PM, Craig wrote:
 On Mon, 2011-09-12 at 22:20 +0200, Florian Müllner wrote:
 You are modifying the list in the loop until g_list_next() returns
 NULL ... 
 
 Right.  I understand this now.  However, I thought using g_list_first()
 would set GList *events back to the first object.  But, as you state,
 the darn thing is pointing to NULL, so getting the first object of NULL
 is problematic.
 
 Thanks for helping me hack through this.  I have created a solution.
 
 

if you want such a data structure use a GQueue. The data inside the
GQueue is still a list, but the GQueue manages the pointers to the tail
and the head for you (so append() and prepend() have equal speed as well).

Olivier

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: GList empty after iteration?

2011-09-13 Thread David King

On 2011-09-12 15:10, Craig craigbakal...@verizon.net wrote:

Hi All,

I am confused about GList. My application is parsing a file and creating
midi data with it. I am using GList to store the midi data.  I have
created a GtkListStore to view the parsed data (to insure I am parsing
the data properly).  So, below I iterate through the GList and copy
the data into a tree view.  But, I am shocked to see that after I
iterate through the GList, I cannot iterate through the list again.  I
have debugged the list after the iteration with g_list_length(events)
which shows the list length at 0.  What is up with this?  The first
while loop has data, the second while loop has not data. The code is


Another option to those offered in this thread is to use the handy 
g_list_foreach() to iterate over the list.


http://developer.gnome.org/glib/stable/glib-Doubly-Linked-Lists.html#g-list-foreach

This nicely separates out the loop into a function, something like 
(I have not tested this, and it can probably be simplified):


void my_list_foreach_store (gpointer data, gpointer user_data)
{
GtkTreeIter tree_iter;
struct midi_event *me = data;
GtkListStore list_store = GTK_LIST_STORE (user_data);

gtk_list_store_append (list_store, tree_iter);
gtk_list_store_set (list_store, tree_iter,
0, me-time_stamp,
1, me-event_type,
2, me-message1,
3, me-message2,
   -1);
}

void my_list_foreach_print (gpointer data, gpointer user_data)
{
g_print (midi event \n);
}

void a_function ()
{
 GList *events = NULL;
 GtkListStore *store = gtk_list_store_new();

 events = g_list_prepend (events, some_data);
 /* insert more items */

 events = g_list_reverse (events);
 /* No need for g_list_first (), already handled by 
  * g_list_reverse ().


 g_list_foreach (events, my_list_foreach_store, store);
 g_list_foreach (events, my_list_foreach_print, NULL);
}

--
http://amigadave.com/
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: GList empty after iteration?

2011-09-12 Thread Denis Washington

Am 12.09.2011 21:10, schrieb Craig:

Hi All,

I am confused about GList. My application is parsing a file and creating
midi data with it. I am using GList to store the midi data.  I have
created a GtkListStore to view the parsed data (to insure I am parsing
the data properly).  So, below I iterate through the GList and copy
the data into a tree view.  But, I am shocked to see that after I
iterate through the GList, I cannot iterate through the list again.  I
have debugged the list after the iteration with g_list_length(events)
which shows the list length at 0.  What is up with this?  The first
while loop has data, the second while loop has not data. The code is
below--

GtkTreeIter tree_iter;
events = g_list_reverse(events);
events = g_list_first(events);
while(events)
{
gtk_list_store_append (list_store,tree_iter);
struct midi_event *me = events-data;
gtk_list_store_set(list_store,tree_iter,
   0, me-time_stamp,
   1, me-event_type,
   2, me-message1,
   3, me-message2,
  -1);
events = g_list_next(events);   
}
/// this is where the list appears to be empty
events = g_list_first(events);
while(events)
{
g_print(midi event \n);
events = g_list_next(events);   
}

Thanks for any help!


This is because you overwrite events in the first loop until it is 
NULL (while (events)). So when you do g_list_first(events) 
afterwards, you naturally get NULL back.


You should use another GList pointer to iterate through the list, like so:


GtkTreeIter tree_iter;
GList *l;
events = g_list_reverse(events);
events = g_list_first(events);

for (l = events; l; l = l-next)
{
gtk_list_store_append (list_store,tree_iter);
struct midi_event *me = l-data;
gtk_list_store_set(list_store,tree_iter,
   0, me-time_stamp,
   1, me-event_type,
   2, me-message1,
   3, me-message2,
  -1);
}

for (l = events; l; l = l-next)
{
g_print(midi event \n);
}

Regards,
Denis
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: GList empty after iteration?

2011-09-12 Thread Florian Müllner
On lun, 2011-09-12 at 15:10 -0400, Craig wrote:
 I am shocked to see that after I iterate through the GList, I cannot
 iterate through the list again.

That's an easy one :-)


   while(events)
   {
   /* [...] */
   events = g_list_next(events);   
   }

You are modifying the list in the loop until g_list_next() returns
NULL ...


   /// this is where the list appears to be empty
   events = g_list_first(events);

so you are now trying to iterate over an empty list (events == NULL).
You probably want a dedicated variable for the iteration, e.g.

GList *iter;

events = g_list_reverse (events);
for (iter = events; iter; iter = iter-next)
  /* do stuff */;

for (iter = events; iter; iter = iter-next)
  /* do other stuff */;


Regards,
Florian

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: GList empty after iteration?

2011-09-12 Thread Bernhard Schuster
2011/9/12 Craig craigbakal...@verizon.net:
 Hi All,

 I am confused about GList. My application is parsing a file and creating
 midi data with it. I am using GList to store the midi data.  I have
 created a GtkListStore to view the parsed data (to insure I am parsing
 the data properly).  So, below I iterate through the GList and copy
 the data into a tree view.  But, I am shocked to see that after I
 iterate through the GList, I cannot iterate through the list again.  I
 have debugged the list after the iteration with g_list_length(events)
 which shows the list length at 0.  What is up with this?  The first
 while loop has data, the second while loop has not data. The code is
 below--

        GtkTreeIter tree_iter;
        events = g_list_reverse(events);
shallowcopy = events;
        events = g_list_first(events);
        while(events)
        {
                gtk_list_store_append (list_store, tree_iter);
                struct midi_event *me = events-data;
                gtk_list_store_set(list_store, tree_iter,
                                   0, me-time_stamp,
                                   1, me-event_type,
                                   2, me-message1,
                                   3, me-message2,
                                  -1);
                events = g_list_next(events);
        }
        /// this is where the list appears to be empty
events = shallowcopy;
        while(events)
        {
                g_print(midi event \n);
                events = g_list_next(events);
        }

 Thanks for any help!
Your first loop ands when your event is zer0. Youn need to use a
second pointer for your second iteration which still points to the
first item in the actual list.

Hope that helps.

Regards

Bernhard
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: GList empty after iteration?

2011-09-12 Thread Colomban Wendling
Le 12/09/2011 21:10, Craig a écrit :
 Hi All,
 
 I am confused about GList. My application is parsing a file and creating
 midi data with it. I am using GList to store the midi data.  I have
 created a GtkListStore to view the parsed data (to insure I am parsing
 the data properly).  So, below I iterate through the GList and copy
 the data into a tree view.  But, I am shocked to see that after I
 iterate through the GList, I cannot iterate through the list again.  I
 have debugged the list after the iteration with g_list_length(events)
 which shows the list length at 0.  What is up with this?  The first
 while loop has data, the second while loop has not data. The code is
 below--
 
   GtkTreeIter tree_iter;
   events = g_list_reverse(events);
   events = g_list_first(events);
   while(events)
   {
   gtk_list_store_append (list_store, tree_iter);
   struct midi_event *me = events-data;
   gtk_list_store_set(list_store, tree_iter,
  0, me-time_stamp,
  1, me-event_type,
  2, me-message1,
  3, me-message2,
 -1);
   events = g_list_next(events);   

You do this until events stop matching the loop's condition (events),
so until it becomes NULL (e.g. there are no more next).  So when you use
events below, it's NULL, and NULL is a valid empty list.

Better user a temporary iter like

GList *iter;

for (iter = list; iter; iter = iter-next) {
/* here use iter-data */
}

Cheers,
Colomban

   }
   /// this is where the list appears to be empty
   events = g_list_first(events);
   while(events)
   {
   g_print(midi event \n);
   events = g_list_next(events);   
   }
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: GList empty after iteration?

2011-09-12 Thread Andrey Maklakov
Hello Craig,
  I think this is because after first loop, variable events is NULL,
and you cant get first element of GList from it:

g_list_next()  --- Returns : the next element, or NULL if there are no
more elements.

 Hi All,

 I am confused about GList. My application is parsing a file and creating
 midi data with it. I am using GList to store the midi data.  I have
 created a GtkListStore to view the parsed data (to insure I am parsing
 the data properly).  So, below I iterate through the GList and copy
 the data into a tree view.  But, I am shocked to see that after I
 iterate through the GList, I cannot iterate through the list again.  I
 have debugged the list after the iteration with g_list_length(events)
 which shows the list length at 0.  What is up with this?  The first
 while loop has data, the second while loop has not data. The code is
 below--

        GtkTreeIter tree_iter;
        events = g_list_reverse(events);
        events = g_list_first(events);
        while(events)
        {
                gtk_list_store_append (list_store, tree_iter);
                struct midi_event *me = events-data;
                gtk_list_store_set(list_store, tree_iter,
                                   0, me-time_stamp,
                                   1, me-event_type,
                                   2, me-message1,
                                   3, me-message2,
                                  -1);
                events = g_list_next(events);
        }
        /// this is where the list appears to be empty
        events = g_list_first(events);
        while(events)
        {
                g_print(midi event \n);
                events = g_list_next(events);
        }

 Thanks for any help!
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list