[opensuse] dynamically growing 2D aray of structues
I dare ask for some guidelines as I am not a programmer and never came across glib before... Thasi may not be the right mailing list for such a question ....please, be linient 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; Any suggestion is welcome. Thank you in advance, Maura -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
On Wed, 2009-11-25 at 15:21 -0800, Maura Monville wrote:
I dare ask for some guidelines as I am not a programmer and never came across glib before... Thasi may not be the right mailing list for such a question ....please, be linient 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:
Make it a doubly linked list. Then you can add/remove anywhere in the list typedef struct _fasta_sequence { gchar *id; gchar *description; gchar *sequence; gint sequence_len; struct _fasta_sequence *next, *previous; } fasta_sequence; There are many discussions of manipulating double linked lists. The basic idea is you allocate the addition, and store it either as next or previous. There are details to watch. But google is your friend. If you were just adding to the end, you could use realloc(). But as you want to add to the beginning, this could be problematic. Also, with double linked lists, existing data never moves, so the pointers remain correct as new things are added on both directions. Same when you remove things anywhere in the list. -- You can't just ask customers what they want and then try to give that to them. By the time you get it built, they'll want something new. -- Steve Jobs Roger Oberholtzer Ramböll RST/OPQ Ramböll Sverige AB Krukmakargatan 21 P.O. Box 17009 SE-104 62 Stockholm, Sweden Office: Int +46 8-615 60 20 Mobile: Int +46 70-815 1696 -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
In <1259192047.12762.6.camel@manta.site>, Roger Oberholtzer wrote:
On Wed, 2009-11-25 at 15:21 -0800, Maura Monville wrote:
I dare ask for some guidelines as I am not a programmer and never came across glib before... Thasi may not be the right mailing list for such a question ....please, be linient 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:
Make it a doubly linked list. Then you can add/remove anywhere in the list
typedef struct _fasta_sequence {
But, don't use a struct name that starts with an underscore. From ISO/IEC 9899:1999 7.1.3: " * All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use. * All identifiers that begin with an underscore are always reserved for use as identifiers with file scope in both the ordinary and tag name spaces. " The "tag name space" includes the names of structures.
struct _fasta_sequence *next, *previous; } fasta_sequence;
There are many discussions of manipulating double linked lists. The basic idea is you allocate the addition, and store it either as next or previous. There are details to watch. But google is your friend.
The OP wanted a 2D array, not a 1D list structure. If you want a 2D list structure, you'll need "up" and "down" pointers similar to the "next" (or "right") and "prev" (or "left") pointers. If you have to do insertions in the middle, or at both ends of the structure, this will likely serve you best. It may also be good even if insertions only happen at one end of the structure. You probably don't want a 2D array, since that implies compact storage of elements which would require the majority of the array to be copied when expanding one of the dimensions. (In addition to any copies required because no more memory can be contiguously allocated.) [You may need this compact storage model, but it's unlikely.] If you only need to do insertions are one end, an array of (pointers to) arrays would also be a good solution. The tip and caveat I snipped are valuable, but need not be repeated any more than I already have. In addition, however you do things, make sure to initialize stuff ASAP after you have allocated it. Uninitialized pointers are a mess to debug manually, although valgrind can help a lot. -- Boyd Stephen Smith Jr. ,= ,-_-. =. bss@iguanasuicide.net ((_/)o o(\_)) ICQ: 514984 YM/AIM: DaTwinkDaddy `-'(. .)`-' http://iguanasuicide.net/ \_/
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Boyd Stephen Smith Jr. wrote:
In <1259192047.12762.6.camel@manta.site>, Roger Oberholtzer wrote:
There are many discussions of manipulating double linked lists. The basic idea is you allocate the addition, and store it either as next or previous. There are details to watch. But google is your friend.
The OP wanted a 2D array, not a 1D list structure.
If you want a 2D list structure, you'll need "up" and "down" pointers similar to the "next" (or "right") and "prev" (or "left") pointers. If you have to do insertions in the middle, or at both ends of the structure, this will likely serve you best. It may also be good even if insertions only happen at one end of the structure.
You probably don't want a 2D array, since that implies compact storage of elements which would require the majority of the array to be copied when expanding one of the dimensions. (In addition to any copies required because no more memory can be contiguously allocated.) [You may need this compact storage model, but it's unlikely.]
If you only need to do insertions are one end, an array of (pointers to) arrays would also be a good solution.
A sparse array implemented as a double linked list of double linked lists might fit, and abstracting the array handling function from the underlying data structures may help readability and improve the ability to define error and array handling functions. The following pseudo-code gives an outline of a possible data structure implementation.... typedef struct { int yIndex; struct data *data; struct yData *next,*previous; } yData; typedef struct { int xIndex; struct yData *yData; struct xData *next,*previous; } xData; where data is your data structure.... The existence and the type of the index field depends a bit on array indexing requirements and it should be noted that random retrieval performance may be very poor with large amounts of data. But this can be very memory efficient if the data objects are thinly spread out within the index space.
The tip and caveat I snipped are valuable, but need not be repeated any more than I already have. In addition, however you do things, make sure to initialize stuff ASAP after you have allocated it. Uninitialized pointers are a mess to debug manually, although valgrind can help a lot.
- -- ============================================================================== I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my telephone. Bjarne Stroustrup ============================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/ iEYEARECAAYFAksOVDEACgkQasN0sSnLmgKA6gCfQP9rIHq+C9L/ltOX7GeU4ycL Xc4AnjG5UfDUTwDsHegidsf/x0L2B18Z =vaMv -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (4)
-
Boyd Stephen Smith Jr.
-
G T Smith
-
Maura Monville
-
Roger Oberholtzer