eg_elist.ex.c

Go to the documentation of this file.
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 "EGlib.h"
00026 /* ========================================================================= */
00027 /**@brief example of integer number lists structure based on embeded lists. */
00028 typedef struct
00029 {
00030   uint32_t 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   EGrandState_t g;
00046   EGeList_t head,
00047     back_up,
00048    *it,
00049    *itn;
00050   int i;
00051   EGlib_info();
00052   EGlib_version();
00053 
00054   /* initialize the list */
00055   EGrandInit(&g);
00056   EGeListInit (&head);
00057   EGeListInit (&back_up);
00058   /* set signal and limits */
00059   EGsigSet(rval,CLEANUP);
00060   EGsetLimits(3600.0,4294967295UL);
00061   /* check that the list is non empty */
00062   if (EGeListIsEmpty (&head))
00063     fprintf (stderr, "List is empty at beginning\n");
00064   /* add all elements to the list and set some value */
00065   for (i = 0; i < 20; i++)
00066   {
00067     number[i].n = EGrand (&g) % 500;
00068     EGeListAddBefore (&(number[i].cn), &head);
00069   }
00070   /* display all elements in the list */
00071   fprintf (stderr, "Numbers in the list:\n");
00072   for (it = head.next, i = 0; it != &head; it = it->next, i++)
00073   {
00074     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00075     fprintf (stderr, "(%u,%u) ", n_ptr->n, number[i].n);
00076   }
00077   fprintf (stderr, "\n");
00078   /* now we eliminate all even elements in the list, and display the
00079    * deleted element */
00080   fprintf (stderr, "Eliminating values:\n");
00081   for (i = 0; i < 10; i++)
00082   {
00083     it = EGeListDel (&(number[i << 1].cn));
00084     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00085     fprintf (stderr, "%d ", n_ptr->n);
00086   }
00087   fprintf (stderr, "\n");
00088   /* and display the remaining list */
00089   fprintf (stderr, "Numbers in the list:\n");
00090   for (it = head.next, i = 0; it != &head; it = it->next, i++)
00091   {
00092     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00093     fprintf (stderr, "(%d,%d) ", n_ptr->n, number[(i << 1) + 1].n);
00094   }
00095   fprintf (stderr, "\n");
00096   /* now we move halve of the members in the list to back_up list, note
00097    * however that as it is we can't move tyhe current element and then ask for
00098    * the next one, because after we move the entry, the next in the for loop
00099    * is the head of the back_up list */
00100   for (it = head.next, i = 0; it != &head; i++)
00101   {
00102     itn = it->next;
00103     if (i % 2 == 0)
00104       EGeListMoveBefore (it, &back_up);
00105     it = itn;
00106   }
00107   /* now we display both lists */
00108   fprintf (stderr, "head: ");
00109   for (it = head.next; it != &head; it = it->next)
00110   {
00111     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00112     fprintf (stderr, "%d ", n_ptr->n);
00113   }
00114   fprintf (stderr, "\nback_up: ");
00115   for (it = back_up.next; it != &back_up; it = it->next)
00116   {
00117     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00118     fprintf (stderr, "%d ", n_ptr->n);
00119   }
00120   fprintf (stderr, "\n");
00121   /* now we splice both lists into back_up list, and reset the head list */
00122   EGeListSplice (&head, &back_up);
00123   EGeListInit (&head);
00124   /* now we display both lists */
00125   fprintf (stderr, "head: ");
00126   for (it = head.next; it != &head; it = it->next)
00127   {
00128     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00129     fprintf (stderr, "%d ", n_ptr->n);
00130   }
00131   fprintf (stderr, "\nback_up: ");
00132   for (it = back_up.next; it != &back_up; it = it->next)
00133   {
00134     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00135     fprintf (stderr, "%d ", n_ptr->n);
00136   }
00137   fprintf (stderr, "\n");
00138   /* now we replace one member in back_up list with one of the deleted ones
00139    * from the complete original list and print the resulting list */
00140   EGeListReplace (back_up.next, &(number[0].cn));
00141   fprintf (stderr, "back_up: ");
00142   for (it = back_up.next; it != &back_up; it = it->next)
00143   {
00144     n_ptr = EGcontainerOf (it, integer_list_t, cn);
00145     fprintf (stderr, "%d ", n_ptr->n);
00146   }
00147   fprintf (stderr, "\n");
00148   CLEANUP:
00149   return rval;
00150 }
00151 
00152 /* ========================================================================= */
00153 /** @} */