root/ompi/mca/coll/libnbc/libdict/dict.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. dict_set_malloc
  2. dict_set_free
  3. dict_int_cmp
  4. dict_uint_cmp
  5. dict_long_cmp
  6. dict_ulong_cmp
  7. dict_ptr_cmp
  8. dict_str_cmp
  9. dict_destroy
  10. dict_itor_destroy

   1 /*
   2  * dict.c
   3  *
   4  * Implementation of generic dictionary routines.
   5  * Copyright (C) 2001-2004 Farooq Mela.
   6  *
   7  * $Id: dict.c,v 1.7 2001/11/25 06:00:49 farooq Exp farooq $
   8  */
   9 
  10 #include <stdlib.h>
  11 
  12 #include "dict.h"
  13 #include "dict_private.h"
  14 
  15 dict_malloc_func _dict_malloc = malloc;
  16 dict_free_func _dict_free = free;
  17 
  18 dict_malloc_func
  19 dict_set_malloc(dict_malloc_func func)
  20 {
  21         dict_malloc_func old = _dict_malloc;
  22         _dict_malloc = func ? func : malloc;
  23         return old;
  24 }
  25 
  26 dict_free_func
  27 dict_set_free(dict_free_func func)
  28 {
  29         dict_free_func old = _dict_free;
  30         _dict_free = func ? func : free;
  31         return old;
  32 }
  33 
  34 /*
  35  * In comparing, we cannot simply subtract because that might result in signed
  36  * overflow.
  37  */
  38 int
  39 dict_int_cmp(const void *k1, const void *k2)
  40 {
  41         const int *a = (int*)k1, *b = (int*)k2;
  42 
  43         return (*a < *b) ? -1 : (*a > *b) ? +1 : 0;
  44 }
  45 
  46 int
  47 dict_uint_cmp(const void *k1, const void *k2)
  48 {
  49         const unsigned int *a = (unsigned int*)k1, *b = (unsigned int*)k2;
  50 
  51         return (*a < *b) ? -1 : (*a > *b) ? +1 : 0;
  52 }
  53 
  54 int
  55 dict_long_cmp(const void *k1, const void *k2)
  56 {
  57         const long *a = (long*)k1, *b = (long*)k2;
  58 
  59         return (*a < *b) ? -1 : (*a > *b) ? +1 : 0;
  60 }
  61 
  62 int
  63 dict_ulong_cmp(const void *k1, const void *k2)
  64 {
  65         const unsigned long *a = (unsigned long*)k1, *b = (unsigned long*)k2;
  66 
  67         return (*a < *b) ? -1 : (*a > *b) ? +1 : 0;
  68 }
  69 
  70 int
  71 dict_ptr_cmp(const void *k1, const void *k2)
  72 {
  73         return (k1 > k2) - (k1 < k2);
  74 }
  75 
  76 int
  77 dict_str_cmp(const void *k1, const void *k2)
  78 {
  79         const char *a = (char*)k1, *b = (char*)k2;
  80         char p, q;
  81 
  82         for (;;) {
  83                 p = *a++; q = *b++;
  84                 if (p == 0 || p != q)
  85                         break;
  86         }
  87         return (p > q) - (p < q);
  88 }
  89 
  90 void
  91 dict_destroy(dict *dct, int del)
  92 {
  93         ASSERT(dct != NULL);
  94 
  95         dct->_destroy(dct->_object, del);
  96         FREE(dct);
  97 }
  98 
  99 void
 100 dict_itor_destroy(dict_itor *itor)
 101 {
 102         ASSERT(itor != NULL);
 103 
 104         itor->_destroy(itor->_itor);
 105         FREE(itor);
 106 }

/* [<][>][^][v][top][bottom][index][help] */