-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdict.c
151 lines (118 loc) · 3.48 KB
/
dict.c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* This file is part of mtrace-ng.
* Copyright (C) 2018 Stefani Seibold <[email protected]>
*
* This work was sponsored by Rohde & Schwarz GmbH & Co. KG, Munich/Germany.
*
* 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; either version 2 of the
* License, or (at your option) any later version.
*
* 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 St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <malloc.h>
#include "common.h"
#include "dict.h"
#include "list.h"
struct dict_entry {
struct list_head list;
unsigned long key;
const void *value;
};
struct dict {
unsigned int size;
unsigned int (*key2hash)(unsigned long);
int (*key_cmp)(unsigned long, unsigned long);
struct list_head buckets[0];
};
struct dict *dict_init(unsigned int size, unsigned int (*key2hash)(unsigned long), int (*key_cmp)(unsigned long, unsigned long))
{
struct dict *d;
unsigned int i;
d = malloc(sizeof(*d) + sizeof(d->buckets[0]) * size);
d->size = size;
d->key2hash = key2hash;
d->key_cmp = key_cmp;
for (i = 0; i < d->size; i++)
INIT_LIST_HEAD(&d->buckets[i]);
return d;
}
void dict_clear(struct dict *d)
{
unsigned int i;
struct list_head *it, *next;
for (i = 0; i < d->size; i++) {
list_for_each_safe(it, next, &d->buckets[i]) {
struct dict_entry *entry = container_of(it, struct dict_entry, list);
free(entry);
}
}
free(d);
}
static struct dict_entry *_dict_find_entry(struct dict *d, unsigned long key, unsigned int hash)
{
struct list_head *it;
list_for_each(it, &d->buckets[hash]) {
struct dict_entry *entry = container_of(it, struct dict_entry, list);
if (!d->key_cmp(key, entry->key))
return entry;
}
return NULL;
}
int dict_add(struct dict *d, unsigned long key, const void *value)
{
struct dict_entry *newentry;
unsigned int hash = d->key2hash(key) % d->size;
if (_dict_find_entry(d, key, hash))
return -1;
newentry = malloc(sizeof(*newentry));
newentry->key = key;
newentry->value = value;
INIT_LIST_HEAD(&newentry->list);
list_add(&newentry->list, &d->buckets[hash]);
return 0;
}
const void *dict_remove_entry(struct dict *d, unsigned long key)
{
unsigned int hash = d->key2hash(key) % d->size;
struct dict_entry *entry = _dict_find_entry(d, key, hash);
if (entry) {
const void *value = entry->value;
list_del(&entry->list);
free(entry);
return value;
}
return NULL;
}
const void *dict_find_entry(struct dict *d, unsigned long key)
{
unsigned int hash = d->key2hash(key) % d->size;
struct dict_entry *entry = _dict_find_entry(d, key, hash);
return entry ? entry->value : NULL;
}
int dict_apply_to_all(struct dict *d, int (*func)(unsigned long key, const void *value, void *data), void *data)
{
unsigned int i;
for (i = 0; i < d->size; i++) {
struct list_head *it, *next;
list_for_each_safe(it, next, &d->buckets[i]) {
struct dict_entry *entry = container_of(it, struct dict_entry, list);
if (func(entry->key, entry->value, data))
return -1;
}
}
return 0;
}