File: dict.c

package info (click to toggle)
openmpi 4.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 127,592 kB
  • sloc: ansic: 690,998; makefile: 43,047; f90: 19,220; sh: 7,182; java: 6,360; perl: 3,590; cpp: 2,227; python: 1,350; lex: 989; fortran: 61; tcl: 12
file content (106 lines) | stat: -rw-r--r-- 1,847 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * dict.c
 *
 * Implementation of generic dictionary routines.
 * Copyright (C) 2001-2004 Farooq Mela.
 *
 * $Id: dict.c,v 1.7 2001/11/25 06:00:49 farooq Exp farooq $
 */

#include <stdlib.h>

#include "dict.h"
#include "dict_private.h"

dict_malloc_func _dict_malloc = malloc;
dict_free_func _dict_free = free;

dict_malloc_func
dict_set_malloc(dict_malloc_func func)
{
	dict_malloc_func old = _dict_malloc;
	_dict_malloc = func ? func : malloc;
	return old;
}

dict_free_func
dict_set_free(dict_free_func func)
{
	dict_free_func old = _dict_free;
	_dict_free = func ? func : free;
	return old;
}

/*
 * In comparing, we cannot simply subtract because that might result in signed
 * overflow.
 */
int
dict_int_cmp(const void *k1, const void *k2)
{
	const int *a = (int*)k1, *b = (int*)k2;

	return (*a < *b) ? -1 : (*a > *b) ? +1 : 0;
}

int
dict_uint_cmp(const void *k1, const void *k2)
{
	const unsigned int *a = (unsigned int*)k1, *b = (unsigned int*)k2;

	return (*a < *b) ? -1 : (*a > *b) ? +1 : 0;
}

int
dict_long_cmp(const void *k1, const void *k2)
{
	const long *a = (long*)k1, *b = (long*)k2;

	return (*a < *b) ? -1 : (*a > *b) ? +1 : 0;
}

int
dict_ulong_cmp(const void *k1, const void *k2)
{
	const unsigned long *a = (unsigned long*)k1, *b = (unsigned long*)k2;

	return (*a < *b) ? -1 : (*a > *b) ? +1 : 0;
}

int
dict_ptr_cmp(const void *k1, const void *k2)
{
	return (k1 > k2) - (k1 < k2);
}

int
dict_str_cmp(const void *k1, const void *k2)
{
	const char *a = (char*)k1, *b = (char*)k2;
	char p, q;

	for (;;) {
		p = *a++; q = *b++;
		if (p == 0 || p != q)
			break;
	}
	return (p > q) - (p < q);
}

void
dict_destroy(dict *dct, int del)
{
	ASSERT(dct != NULL);

	dct->_destroy(dct->_object, del);
	FREE(dct);
}

void
dict_itor_destroy(dict_itor *itor)
{
	ASSERT(itor != NULL);

	itor->_destroy(itor->_itor);
	FREE(itor);
}