View Full Version : Glib


maslan
:)

when i was reading the glib ref.
i found GList but i tried to test it so i created an test c code
& tried it but i didn't really knows how to use it.

i save a data in it , but i can get it back :)

uniball
/* Copyright (C) 2003 Mohammed Sameer
* License: GPL Version 2 or later.
* If it breakes, Keep the 2 pieces.
*/

#include <glib.h>

int
main ()
{
GSList *foo = NULL;
gint x = 0;
gint i;

for (x = 0; x < 5; x++)
{
gchar *bar = g_strdup_printf ("%i", x);
printf ("Adding: %s\n", bar);
foo = g_slist_append (foo, bar);
}
i = g_slist_length (foo);
printf ("Length: %i\n", i);

for (x = 0; x < i; x++)
{
gchar *zot = g_slist_nth_data (foo, x);
printf ("Got: %s\n", zot);
}

g_slist_foreach (foo, (GFunc) g_free, NULL);
g_slist_free (foo);
foo = NULL;
}

maslan
:D hey thanx 4 that code

alaa
ahhh the beauty of C++

#include <iostream>
#include <list>

using namespace std;

int main ()
{
list<int> foo;

for (int x = 0; x < 5; ++x)
{
cout<<"Adding: "<<x<<endl;
foo.push_back(x);
}
cout<<"Length "<<foo.size()<<endl;

for (list<int>::iterator x = foo.begin();
x != foo.end(); ++x)
{
cout<<"Got: "<<*x<<endl;
}
return 0;
}


note how it is shorter and more readable??
note how its all standard C++??
note how you don't need to manually free any memory??
note how we never use non type safe statements (the list can only accept ints, cout doesn't do any run time interpretation leading to security problems and buffer overflows)??

cheers,
Alaa

OneOfOne
Originally posted by alaa
ahhh the beauty of C++
note how it is shorter and more readable??
note how its all standard C++??
note how you don't need to manually free any memory??
note how we never use non type safe statements (the list can only accept ints, cout doesn't do any run time interpretation leading to security problems and buffer overflows)??
lol i agree, if i ever start real computer programming on linux my choice would be c++!
peace

uniball
Alaa, Stop your offtopic posts, We are not comparing C and C++, The guy asked
about something to be explained.
Your attitude is similar to what we said about not telling anyone to switch his
distro because he is missing a feature or a package or can't corerrectly
configure anything from the 1st time or .....

alaa
>Alaa, Stop your offtopic posts, We are not comparing C and C++

this is not offtopic, the topic is Glib and this explains to C++ programmers that they don't need the datastructure part of Glib.

>guy asked about something to be explained.

and someone sent him a perfect answer, in that respect the thread is complete but like all threads it can eveolve into something wider or do we close now??

>Your attitude is similar to what we said about not telling anyone to switch
> his distro because he is missing a feature or a package or can't corerrectly
> configure anything from the 1st time or .....

we never said so we only said it makes no sense to tell someone to switch over these issues but we never and should never tell someone you cannot give this advice it is not accepted (including an advice to switch to windows or even SCO Unix).

my attitude is similar to someone saying apt-get install foo in a thread about RPM hell (which a certain someone does from time to time :-)
and similar to telling someone about the benefits of GTKmm in a QT thread (that someone actually praised and thanked the poster for it).

now a certain someone would better change his attitude or else he'll find his processor without a heatsink :-)

cheers,
Alaa

alaa
BTW the C++ code sample is not more efficient than the Glib one.
internaly the list class does something similar to the glib list.
in fact the glib list might be slightly more efficient (but it is possible to write your own list class that will have all the benefits of the standard one plus the efficiency of glib's, again it'll look very much similar to glibs internaly).
the C++ list will definatly take more time to compile since templates cannot be precompiled yet (again you can roll your own wich will avoid this problem).

I also forget to mention than in the C++ example we never use casts or macros (and the list class doesn't use them either).

cheers,
Alaa

uniball
>this is not offtopic, the topic is Glib and this explains to C++ programmers that they don't >need the datastructure part of Glib.
reread what u typed after the code.

>we never said so we only said it makes no sense to tell someone to switch over these >issues but we never and should never tell someone you cannot give this advice it is not >accepted (including an advice to switch to windows or even SCO Unix).
So I suggest that you switch to GNU/Amiga :-)

>my attitude is similar to someone saying apt-get install foo in a thread about RPM hell >(which a certain someone does from time to time :-)
or emerge in a programming thread ;-)

> and similar to telling someone about the benefits of GTKmm in a QT thread (that someone > actually praised and thanked the poster for it).
because that someone asked, And both were C++

>now a certain someone would better change his attitude or else he'll find his processor >without a heatsink :-)
and the other one'll find himself without a processor to install the heatsink for :-)

peace man (-:

alaa
>reread what u typed after the code.

what I typed after the code is the reason why C++ programmers should prefer the STL list to the Glib one.

>So I suggest that you switch to GNU/Amiga :-)

OK that was nasty you'll pay for this.

>or emerge in a programming thread ;-)

I talked about programming in a programming thread, I talked about lists in a thread about lists, I compared something to glib in a glib thread, surely this is different.

>because that someone asked, And both were C++

nope that someone did not ask about gtkmm in the begining it was my suggestion that lead to further questions and ended with enlightment, now you want to prevent a poor soul like maslan from being enlightned??

>and the other one'll find himself without a processor to install the heatsink
>for :-)

only a vi user would stoop to low as to rob a 10 years old of her processor (the one she uses to play tuxracer at that)

>peace man (-:

what do you mean peace don't chiken out now ya gaban I just mentioned VI and you'll let me get away with it without turning this into a major flam war??

cheers,
Alaa

uniball
Note:
When you have a large list it'd be better to use:
g_slist_next()
so if you have a list with 100 elements in a for loop like this:

for (x = 0; x < 100; x++)
{
g_slist_nth_data();
}

each time you need an element g_slist_nth_data()'ll iterate over all the
previous items.
replace it with:
while (g_slist_next(foo))
{

}

I wasn't going to add this as we are not discussing effeciency and u need to
look at the reference manual, But my soulmate insisted!

uniball
>what I typed after the code is the reason why C++ programmers should prefer the STL list >to the Glib one.
So you are telling a redhat user to switch to mandrake again ?

>I talked about programming in a programming thread, I talked about lists in a thread about >lists, I compared something to glib in a glib thread, surely this is different.
Nop, It's a glib thread not a lists thread

>nope that someone did not ask about gtkmm in the begining it was my suggestion that > lead to further questions and ended with enlightment, now you want to prevent a poor soul > like maslan from being enlightned??

not true, the whole thread was about C++
not like discussing algol in a lisp thread ;-)

> only a vi user would stoop to low as to rob a 10 years old of her processor (the one she
> uses to play tuxracer at that)
yes because her elder brother gave it to him.

> what do you mean peace don't chiken out now ya gaban I just mentioned VI and you'll
> let me get away with it without turning this into a major flam war??

Yes you can use your dummy emacs while dreaming, but u'll be hit by a vimmer soon.
actually nothing can get near to vim or even dream about it.
hahahahaha
if you tell me one of the CVS branches of emacs "other than HEAD" I can forgive you, That's only to show your ignorance.

mborn
That's what I wanted, a discussion betn 2 gurus!
I like it!
Max

maslan
:D
sorry but i want 2 know how to use Doubly-Linked Lists
which is the GList not GSList
;)

uniball
oops, I missed this, but it should be the same.

maslan
:D
it should be the same , if u r sure thnx again :D

uniball
only the difference between singly and doubly linked lists ;-)

maslan
when i use g_list_nth_data(); does it start counting from 0 or from 1 ? :D

alaa
please don't use glist_nth_data, this is a complete waste of time, it is the most inefficient thing to do with a linked list.

you see there are two common data structures to use as collections or containers.

the array (or vector), and the linked list.

the array has constant time access but is usualy of a preset size (you can resize a vector in C++ but it is not a constant time operation, in fact it is a very inefficient one).
constant time means that it doesn't make a difference whether you're accessing the first element or the last one or anyother element they all take the same amount of operations.
this means you can randomly access elements in the array without worrying about the time it takes.

the list however is a linear time access structure, its size is dynamic (and it has other benefits like being faster to sort it, and more efficient to delete from it etc.)
this means that accessing the second element takes twice the time of accessing the first one and the third takes three times etc.
you cannot randomly access the list you can only itterate over it, so you should not use a list when you need random access.

this is why you should not use glist_nth_data() except in special circumstances (and never inside a loop).
because each time you'll want to access the nth element you'll iterate over all the preceding elemnts.
so when you seek element 0 you'll take k*1 steps, element 1 will take k*2 and element 2 will take k*3 etc.
this means that the time taken to traverse the whol list will be
k+2k+3k.....nk = k(n*(n+1)/2)
which gets big very fast, what takes 100k operations when using an array takes 5050k operation when using lists.

what you should do with a list is start from some element (preferably the first or the last one) and then moving the the next or previous element one by one using g_slist_next() like Uniball explained.

cheers,
Alaa

uniball
Originally posted by maslan
when i use g_list_nth_data(); does it start counting from 0 or from 1 ? :D
RTFM