Re: dynamically growing 2D aray of structues



On 11/26/09 12:23, Ian Liu wrote:
You can always map a 2d coordinate intto 1d:
(i, j) -> i * w + j
where w is the width of the matrix


That works poorly for dynamic arrays--every time you resize the array, in either dimension, you basically have to copy all the elements to new locations.

The general solution to multidimensional arrays in C is to use arrays of pointers:

ptr0 ->  e00 ee1 e02 e03 e04...
ptr1 ->  e10 e11 e12 e13 e14...
ptr2 ->  e20 e21 e22 e23 e24...
ptr3 ->  e30 e31 e32 e33 e34...
  .
  .
  .

This makes resizing fairly efficient--using reallocs--and can be implemented using GArray



Ian L.

On Thu, Nov 26, 2009 at 3:04 PM, <mauede alice it <mailto:mauede alice it>> wrote:

    As far as I know a GArray grows an array of pointers in 1D not 2D.
    I thought of a GArray myself but my question, although not clear, is
    how can I use a GArray to grow a 2D array of structures ?
    Can you please provide an example ?


    Thank you in advance,
    Maura

    -----Messaggio originale-----
    Da: tristan van berkom gmail com
    <mailto:tristan van berkom gmail com> per conto di Tristan Van Berkom
    Inviato: gio 26/11/2009 6.31
    A: mauede alice it <mailto:mauede alice it>
    Cc: gtk-list gnome org <mailto:gtk-list gnome org>
    Oggetto: Re: dynamically growing 2D aray of structues


    On Wed, Nov 25, 2009 at 9:17 PM, <mauede alice it
    <mailto:mauede alice it>> wrote:
    > I dare ask for some guidelines as I am not a programmer and
    never came
    > across glib before.
    > At the time being I have to embed some code into a package
    (Galopps: a
    > Genetic Algorithm) which is totally implemented in C language.
    > To be consistent with Galopps style I have to use glib functions.
    > I have just started to use the simplest glib primitives and data
    types.
    > Now I am stuck with the following problem. I know there are a
    lot of experts
    > out there who can give me a couples of hints.
    > I have to generate a 2D array of structures which grows
    dynamically in both
    > directions.
    > The structure is defined as follows:
    >
    > typedef struct {
    >         gchar *id;
    >         gchar *description;
    >         gchar *sequence;
    >         gint sequence_len;
    >  } fasta_sequence;
    >

    Use a GArray:
    http://library.gnome.org/devel/glib/stable/glib-Arrays.html

    Cheers,
          -Tristan

    >
    > Any suggestion is welcome.
    > Thank you in advance,
    > Maura
    >
    >
    >
    >
    > Alice Messenger ;-) chatti anche con gli amici di Windows Live
    Messenger e
    > tutti i telefonini TIM!
    > Vai su
    http://maileservizi.alice.it/alice_messenger/index.html?pmk=footer
    >
    > _______________________________________________
    > gtk-list mailing list
    > gtk-list gnome org <mailto:gtk-list gnome org>
    > http://mail.gnome.org/mailman/listinfo/gtk-list
    >
    >


    _______________________________________________
    gtk-list mailing list
    gtk-list gnome org <mailto:gtk-list gnome org>
    http://mail.gnome.org/mailman/listinfo/gtk-list


------------------------------------------------------------------------

_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list



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