File: shash.c

package info (click to toggle)
lowpan-tools 0.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,940 kB
  • ctags: 957
  • sloc: sh: 11,792; ansic: 2,825; python: 262; yacc: 162; lex: 83; makefile: 83
file content (101 lines) | stat: -rw-r--r-- 2,320 bytes parent folder | download | duplicates (2)
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
/*
 * Linux IEEE 802.15.4 userspace tools
 *
 * Copyright (C) 2008, 2009 Sismens AG
 *
 * Written-by: Dmitry Eremin-Solenikov
 * Written-by: Sergey Lapin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <libcommon.h>
#include <stdlib.h>

struct shash_elem {
	const void *key;
	void *data;
	struct shash_elem *next;
	struct shash_elem **prev;
};

struct simple_hash {
	shash_hash hashfn;
	shash_eq eqfn;
	/* FIXME: it's a fake hash currently */
	struct shash_elem *elem;
};

struct simple_hash *shash_new(shash_hash hashfn, shash_eq eqfn)
{
	struct simple_hash *hash = calloc(1, sizeof(struct simple_hash));
	if (hash) {
		hash->hashfn = hashfn;
		hash->eqfn = eqfn;
	}

	return hash;
}
void *shash_insert(struct simple_hash *hash, const void *key, void *data)
{
	struct shash_elem *elem;
	void *old;

	for (elem = hash->elem; elem; elem = elem->next) {
		if (!hash->eqfn(elem->key, key)) {
			old = elem->data;
			elem->data = data;
			return old;
		}
	}

	elem = calloc(1, sizeof(*elem));
	elem->key = key;
	elem->data = data;

	elem->next = hash->elem;
	elem->prev = &hash->elem;
	hash->elem = elem;
	if (elem->next)
		elem->next->prev = &elem->next;

	return NULL;
}
void *shash_get(struct simple_hash *hash, const void *key)
{
	struct shash_elem *elem;

	for (elem = hash->elem; elem; elem = elem->next) {
		if (!hash->eqfn(elem->key, key)) {
			return elem->data;
		}
	}

	return NULL;
}
void *shash_drop(struct simple_hash *hash, const void *key)
{
	struct shash_elem *elem;

	for (elem = hash->elem; elem; elem = elem->next) {
		if (!hash->eqfn(elem->key, key)) {
			*(elem->prev) = elem->next;
		}
	}

	return elem;
}