-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathht-type.c
More file actions
77 lines (60 loc) · 1.16 KB
/
ht-type.c
File metadata and controls
77 lines (60 loc) · 1.16 KB
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
/*
* Colibri Opening Addressing Hash Table Type
*
* Copyright (c) 2017-2021 Alexei A. Smekalkine
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <stdlib.h>
#include <data/ht.h>
void *ht_copy (const void *from)
{
const struct ht *src = from;
struct ht *o;
size_t i;
void *entry;
if ((o = malloc (sizeof (*o))) == NULL)
return NULL;
ht_init (o, src->type);
ht_foreach (i, entry, src)
o->table[i] = o->type->copy (entry);
return o;
}
void ht_free (void *o)
{
if (o == NULL)
return;
ht_fini (o);
free (o);
}
int ht_eq (const void *a, const void *b)
{
const struct ht *p = a, *q = b;
size_t i;
void *entry, *peer;
if (p->count != q->count || p->type != q->type)
return 0;
ht_foreach (i, entry, p)
if ((peer = ht_lookup (q, entry)) == NULL ||
!p->type->eq (entry, peer))
return 0;
return 1;
}
size_t ht_hash (const void *o, size_t iv)
{
const struct ht *p = o;
size_t i;
void *entry;
/*
* Calculate order-independed hash from entries order
*/
ht_foreach (i, entry, p)
iv ^= p->type->hash (entry, 0);
return iv;
}
const struct data_type ht_type = {
.copy = ht_copy,
.free = ht_free,
.eq = ht_eq,
.hash = ht_hash,
};