Types: Added SilcCompare generic comparison function
authorPekka Riikonen <priikone@silcnet.org>
Sun, 21 Sep 2008 12:25:23 +0000 (15:25 +0300)
committerPekka Riikonen <priikone@silcnet.org>
Sun, 21 Sep 2008 12:25:23 +0000 (15:25 +0300)
The SilcCompare can be used in various comparison functions.  Returns
SilcCompareValue.

lib/silcutil/silctypes.h
lib/silcutil/tests/test_silclist.c

index fd2da8f8df8ba71734fbc9fb11d22ee857117bbf..5d768d819ab70828b844996265ef97ce2befddc3 100644 (file)
@@ -341,6 +341,44 @@ typedef SilcUInt32 SilcParam;
 #define SILC_PARAM_ALLOC         0x00010000     /* Allocate, bitmask */
 #define SILC_PARAM_REPLACE       0x00020000    /* Replace, bitmask */
 
+/****d* silcutil/SilcCompareValue
+ *
+ * NAME
+ *
+ *    typedef enum { ... } SilcCompareValue
+ *
+ * DESCRIPTION
+ *
+ *    Values that can be returned by the SilcCompare function.  Note that
+ *    not all routines may respect all of the return values.
+ *
+ * SOURCE
+ */
+typedef enum {
+  SILC_COMPARE_LESS_THAN_EQUAL_TO    = -2,  /* Value 1 <= value 2 */
+  SILC_COMPARE_LESS_THAN             = -1,  /* Value 1 < value 2 */
+  SILC_COMPARE_EQUAL_TO              = 0,   /* Value 1 == value 2 */
+  SILC_COMPARE_GREATER_THAN          = 1,   /* Value 1 > value 2 */
+  SILC_COMPARE_GREATER_THAN_EQUAL_TO = 2,   /* Value 1 >= value 2 */
+  SILC_COMPARE_STOP                  = 3,   /* Stop comparison */
+} SilcCompareValue;
+/***/
+
+/****f* silcutil/SilcCompare
+ *
+ * SYNOPSIS
+ *
+ *    typedef SilcCompareValue (*SilcCompare)(void *value1, void *value2,
+ *                                            void *context);
+ *
+ * DESCRIPTION
+ *
+ *   A comparison function used by many routines in SILC Runtime Toolkit.
+ *
+ ***/
+typedef SilcCompareValue (*SilcCompare)(void *value1, void *value2,
+                                       void *context);
+
 /* Macros */
 
 #if (defined(SILC_I486) || defined(SILC_X86_64)) && defined(__GNUC__)
index 86de91e78a5c79e2a3afe4dcf17113e26142fcd2..d56a482ac02c2ad184aac1e5ea291275c9752f2e 100644 (file)
@@ -8,14 +8,18 @@ struct foo {
   struct foo *prev;
 };
 
-static int compare(void *e1, void *e2, void *context)
+static SilcCompareValue compare(void *e1, void *e2, void *context)
 {
   struct foo *ee1 = e1, *ee2 = e2;
   SILC_LOG_DEBUG(("entry %d, %p, next=%p, prev=%p", ee1->i, ee1, ee1->next,
                   ee1->prev));
   SILC_LOG_DEBUG(("> entry %d, %p, next=%p, prev=%p", ee2->i, ee2, ee2->next,
                   ee2->prev));
-  return ee1->i - ee2->i;
+  if (ee1->i > ee2->i)
+    return SILC_COMPARE_GREATER_THAN;
+  if (ee1->i < ee2->i)
+    return SILC_COMPARE_LESS_THAN;
+  return SILC_COMPARE_EQUAL_TO;
 }
 
 int main(int argc, char **argv)
@@ -200,7 +204,7 @@ int main(int argc, char **argv)
     SILC_LOG_DEBUG(("entry %d, %p, next=%p, prev=%p", f->i, f, f->next,
                   f->prev));
 
-  SILC_LOG_DEBUG(("Sorting"));  
+  SILC_LOG_DEBUG(("Sorting"));
   silc_list_sort(list, compare, NULL);
 
   SILC_LOG_DEBUG(("Sorted list"));