c_list.h

Go to the documentation of this file.
00001 /*
00002  * c_list -- a doubly-linked list
00003  *
00004  * This code is based on glist.{h,c} from glib
00005  *   ftp://ftp.gtk.org/pub/gtk/
00006  * Copyright (c) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
00007  * Copyright (c) 2006-2008  Andreas Schneider <mail@csyncapses.org>
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU General Public License
00011  * as published by the Free Software Foundation; either version 2
00012  * of the License, or (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software Foundation,
00021  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022  *
00023  * vim: ts=2 sw=2 et cindent
00024  */
00025 
00026 #ifndef _C_LIST_H
00027 #define _C_LIST_H
00028 
00029 /**
00030  * c_list -- a doubly-linked list.
00031  *
00032  * The c_list_t structure and its associated functions provide a standard
00033  * doubly-linked list data structure. Each node has two links: one points to
00034  * the previous node, or points to a null value or empty list if it is the
00035  * first  node; and one points to the next, or points to a null value or empty
00036  * list if it is the final node.
00037  *
00038  * The data contained in each element can be simply pointers to any type of
00039  * data. You are the owner of the data, this means you have to free the memory
00040  * you have allocated for the data.
00041  *
00042  * @file   c_list.h
00043  */
00044 
00045 
00046 /**
00047  * @typedef c_list_t
00048  * Creates a type name for c_list_s
00049  */
00050 typedef struct c_list_s c_list_t;
00051 /**
00052  * @struct c_list_s
00053  *
00054  * Used for each element in a doubly-linked list. The list can hold
00055  * any kind of data.
00056  */
00057 struct c_list_s {
00058   /** Link to the next element in the list */
00059   struct c_list_s *next;
00060   /** Link to the previous element in the list */
00061   struct c_list_s *prev;
00062 
00063   /**
00064    * Holds the element's data, which can be a pointer to any kind of
00065    * data.
00066    */
00067   void *data;
00068 };
00069 
00070 /**
00071  * Specifies the type of a comparison function used to compare two values. The
00072  * value which should be returned depends on the context in which the
00073  * c_list_compare_fn is used.
00074  *
00075  * @param a             First parameter to compare with.
00076  *
00077  * @param b             Second parameter to compare with.
00078  *
00079  * @return              The function should return a number > 0 if the first
00080  *                      parameter comes after the second parameter in the sort
00081  *                      order.
00082  */
00083 typedef int (*c_list_compare_fn) (const void *a, const void *b);
00084 
00085 /**
00086  * Adds a new element on to the end of the list.
00087  *
00088  * @param list          A pointer to c_list.
00089  *
00090  * @param data          The data for the new element.
00091  *
00092  * @return              New start of the list, which may have changed, so make
00093  *                      sure you store the new value.
00094  */
00095 c_list_t *c_list_append(c_list_t *list, void *data);
00096 
00097 /**
00098  * Adds a new element on at the beginning of the list.
00099  *
00100  * @param list          A pointer to c_list.
00101  *
00102  * @param data          The data for the new element.
00103  *
00104  * @return              New start of the list, which may have changed, so make
00105  *                      sure you store the new value.
00106  */
00107 c_list_t *c_list_prepend(c_list_t *list, void *data);
00108 
00109 /**
00110  * Inserts a new element into the list at the given position. If the position
00111  * is lesser than 0, the new element gets appended to the list, if the position
00112  * is 0, we prepend the element and if the given position is greater than the
00113  * length of the list, the element gets appended too.
00114  *
00115  * @param list          A pointer to c_list.
00116  *
00117  * @param data          The data for the new element.
00118  *
00119  * @param position      The position to insert the element.
00120  *
00121  * @return              New start of the list, which may have changed, so make
00122  *                      sure you store the new value.
00123  */
00124 c_list_t *c_list_insert(c_list_t *list, void *data, long position);
00125 
00126 /**
00127  * Inserts a new element into the list, using the given comparison function to
00128  * determine its position.
00129  *
00130  * @param list          A pointer to c_list.
00131  *
00132  * @param data          The data for the new element.
00133  *
00134  * @param func          The function to compare elements in the list. It
00135  *                      should return a number > 0 if the first parameter comes
00136  *                      after the second parameter in the sort order.
00137  *
00138  * @return              New start of the list, which may have changed, so make
00139  *                      sure you store the new value.
00140  */
00141 c_list_t *c_list_insert_sorted(c_list_t *list, void *data, c_list_compare_fn func);
00142 
00143 /**
00144  * Allocates space for one c_list_t element.
00145  *
00146  * @return             A pointer to the newly-allocated element.
00147  */
00148 c_list_t *c_list_alloc(void);
00149 
00150 /**
00151  * Removes an element from a c_list. If two elements contain the same data,
00152  * only the first is removed.
00153  *
00154  * @param list          A pointer to c_list.
00155  *
00156  * @param data          The data of the element to remove.
00157  */
00158 c_list_t *c_list_remove(c_list_t *list, void *data);
00159 
00160 /**
00161  * Frees all elements from a c_list.
00162  *
00163  * @param list          A pointer to c_list.
00164  */
00165 void c_list_free(c_list_t *list);
00166 
00167 /**
00168  * Gets the next element in a c_list.
00169  *
00170  * @param               An element in a c_list.
00171  *
00172  * @return              The next element, or NULL if there are no more
00173  *                      elements.
00174  */
00175 c_list_t *c_list_next(c_list_t *list);
00176 
00177 /**
00178  * Gets the previous element in a c_list.
00179  *
00180  * @param               An element in a c_list.
00181  *
00182  * @return              The previous element, or NULL if there are no more
00183  *                      elements.
00184  */
00185 c_list_t *c_list_prev(c_list_t *list);
00186 
00187 /**
00188  * Gets the number of elements in a c_list
00189  *
00190  * @param list          A pointer to c_list.
00191  *
00192  * @return              The number of elements
00193  */
00194 unsigned long c_list_length(c_list_t *list);
00195 
00196 /**
00197  * Gets the first element in a c_list
00198  *
00199  * @param list          A pointer to c_list.
00200  *
00201  * @return              New start of the list, which may have changed, so make
00202  *                      sure you store the new value.
00203  */
00204 c_list_t *c_list_first(c_list_t *list);
00205 
00206 /**
00207  * Gets the last element in a c_list
00208  *
00209  * @param list          A pointer to c_list.
00210  *
00211  * @return              New start of the list, which may have changed, so make
00212  *                      sure you store the new value.
00213  */
00214 c_list_t *c_list_last(c_list_t *list);
00215 
00216 /**
00217  * Gets the element at the given positon in a c_list.
00218  *
00219  * @param list          A pointer to c_list.
00220  *
00221  * @param position      The position of the element, counting from 0.
00222  *
00223  * @return              New start of the list, which may have changed, so make
00224  *                      sure you store the new value.
00225  */
00226 c_list_t *c_list_position(c_list_t *list, long position);
00227 
00228 /**
00229  * Finds the element in a c_list_t which contains the given data.
00230  *
00231  * @param list          A pointer to c_list.
00232  *
00233  * @param data          The data of the element to remove.
00234  *
00235  * @return              The found element or NULL if it is not found.
00236  */
00237 c_list_t *c_list_find(c_list_t *list, void *data);
00238 
00239 /**
00240  * Finds an element, using a supplied function to find the desired
00241  * element.
00242  *
00243  * @param list          A pointer to c_list.
00244  *
00245  * @param data          The data of the element to remove.
00246  *
00247  * @param func          The function to call for each element. It should
00248  *                      return 0 when the desired element is found.
00249  *
00250  * @return              The found element or NULL if it is not found.
00251  */
00252 c_list_t *c_list_find_custom(c_list_t *list, void *data, c_list_compare_fn func);
00253 
00254 /**
00255  * Sorts the elements of a c_list.
00256  * The algorithm used is Mergesort, because that works really well
00257  * on linked lists, without requiring the O(N) extra space it needs
00258  * when you do it on arrays.
00259  *
00260  * @param list          A pointer to c_list.
00261  *
00262  * @param func          The comparison function used to sort the c_list. This
00263  *                      function is passed 2 elements of the GList and should
00264  *                      return 0 if they are equal, a negative value if the first
00265  *                      element comes before the second, or a positive value if the
00266  *                      first element comes after the second.
00267  *
00268  * @return              New start of the list, which may have changed, so make
00269  *                      sure you store the new value.
00270  */
00271 c_list_t *c_list_sort(c_list_t *list, c_list_compare_fn func);
00272 
00273 #endif /* _C_LIST_H */
00274 

Generated on Wed Feb 25 18:38:50 2009 for csync by  doxygen 1.5.6