From: Pekka Riikonen Date: Sun, 21 Sep 2008 12:25:23 +0000 (+0300) Subject: Types: Added SilcCompare generic comparison function X-Git-Tag: 1.2.beta5~6 X-Git-Url: http://git.silc.fi/gitweb/?p=runtime.git;a=commitdiff_plain;h=0591a422cd4b71e5af6581bcd3d00d184d84a017 Types: Added SilcCompare generic comparison function The SilcCompare can be used in various comparison functions. Returns SilcCompareValue. --- diff --git a/lib/silcutil/silctypes.h b/lib/silcutil/silctypes.h index fd2da8f8..5d768d81 100644 --- a/lib/silcutil/silctypes.h +++ b/lib/silcutil/silctypes.h @@ -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__) diff --git a/lib/silcutil/tests/test_silclist.c b/lib/silcutil/tests/test_silclist.c index 86de91e7..d56a482a 100644 --- a/lib/silcutil/tests/test_silclist.c +++ b/lib/silcutil/tests/test_silclist.c @@ -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"));