eg_elist.ex.c

This is a working (althought useless) example on EGeList.

00001 /* EGlib "Efficient General Library" provides some basic structures and
00002  * algorithms commons in many optimization algorithms.
00003  *
00004  * Copyright (C) 2005 Daniel Espinoza and Marcos Goycoolea.
00005  * 
00006  * This library is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation; either version 2.1 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but 
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00013  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00019  * */
00020 /** @file
00021  * @ingroup EGeList
00022  * */
00023 /** @addtogroup EGeList */
00024 /** @{ */
00025 #include "eg_elist.h"
00026 /* ========================================================================= */
00027 /**@brief example of integer number lists structure based on embeded lists. */
00028 typedef struct
00029 {
00030   int n;        /**< actual information contained in the list */
00031   EGeList_t cn; /**< structure to put this together in a list */
00032 }
00033 integer_list_t;
00034 
00035 /* ========================================================================= */
00036 /** @brief A simple example of using embeded lists
00037  * @return zero on success, non-zero otherwise.
00038  * @par Description:
00039  * Show how to use embeded lists in a dynamic way and in a static fashion. */
00040 int main (void)
00041 {
00042   int rval = 0;
00043   integer_list_t number[20],
00044    *n_ptr;
00045   EGeList_t head,
00046     back_up,
00047    *it,
00048    *itn;
00049   int i;
00050 
00051   /* initialize the list */
00052   EGeListInit (&head);
00053   EGeListInit (&back_up);
00054   /* check that the list is non empty */
00055   if (EGeListIsEmpty (&head))
00056     fprintf (stderr, "List is empty at beginning\n");
00057   /* add all elements to the list and set some value */
00058   for (i = 0; i < 20; i++)
00059   {
00060     number[i].n = random () % 500;
00061     EGeListAddBefore (&(number[i].cn), &head);
00062   }
00063   /* display all elements in the list */
00064   fprintf (stderr, "Numbers in the list:\n");
00065   for (it = head.next, i = 0; it != &head; it = it->next, i++)
00066   {
00067     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00068     fprintf (stderr, "(%d,%d) ", n_ptr->n, number[i].n);
00069   }
00070   fprintf (stderr, "\n");
00071   /* now we eliminate all even elements in the list, and display the
00072    * deleted element */
00073   fprintf (stderr, "Eliminating values:\n");
00074   for (i = 0; i < 10; i++)
00075   {
00076     it = EGeListDel (&(number[i << 1].cn));
00077     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00078     fprintf (stderr, "%d ", n_ptr->n);
00079   }
00080   fprintf (stderr, "\n");
00081   /* and display the remaining list */
00082   fprintf (stderr, "Numbers in the list:\n");
00083   for (it = head.next, i = 0; it != &head; it = it->next, i++)
00084   {
00085     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00086     fprintf (stderr, "(%d,%d) ", n_ptr->n, number[(i << 1) + 1].n);
00087   }
00088   fprintf (stderr, "\n");
00089   /* now we move halve of the members in the list to back_up list, note
00090    * however that as it is we can't move tyhe current element and then ask for
00091    * the next one, because after we move the entry, the next in the for loop
00092    * is the head of the back_up list */
00093   for (it = head.next, i = 0; it != &head; i++)
00094   {
00095     itn = it->next;
00096     if (i % 2 == 0)
00097       EGeListMoveBefore (it, &back_up);
00098     it = itn;
00099   }
00100   /* now we display both lists */
00101   fprintf (stderr, "head: ");
00102   for (it = head.next; it != &head; it = it->next)
00103   {
00104     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00105     fprintf (stderr, "%d ", n_ptr->n);
00106   }
00107   fprintf (stderr, "\nback_up: ");
00108   for (it = back_up.next; it != &back_up; it = it->next)
00109   {
00110     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00111     fprintf (stderr, "%d ", n_ptr->n);
00112   }
00113   fprintf (stderr, "\n");
00114   /* now we splice both lists into back_up list, and reset the head list */
00115   EGeListSplice (&head, &back_up);
00116   EGeListInit (&head);
00117   /* now we display both lists */
00118   fprintf (stderr, "head: ");
00119   for (it = head.next; it != &head; it = it->next)
00120   {
00121     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00122     fprintf (stderr, "%d ", n_ptr->n);
00123   }
00124   fprintf (stderr, "\nback_up: ");
00125   for (it = back_up.next; it != &back_up; it = it->next)
00126   {
00127     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00128     fprintf (stderr, "%d ", n_ptr->n);
00129   }
00130   fprintf (stderr, "\n");
00131   /* now we replace one member in back_up list with one of the deleted ones
00132    * from the complete original list and print the resulting list */
00133   EGeListReplace (back_up.next, &(number[0].cn));
00134   fprintf (stderr, "back_up: ");
00135   for (it = back_up.next; it != &back_up; it = it->next)
00136   {
00137     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00138     fprintf (stderr, "%d ", n_ptr->n);
00139   }
00140   fprintf (stderr, "\n");
00141   return rval;
00142 }
00143 
00144 /* ========================================================================= */
00145 /** @} */

Generated on Mon Jan 30 08:48:52 2006 for EGlib by  doxygen 1.4.5