forked from ocaml/ocaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddrmap.h
98 lines (79 loc) · 3.05 KB
/
addrmap.h
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
/**************************************************************************/
/* */
/* OCaml */
/* */
/* Stephen Dolan, University of Cambridge */
/* */
/* Copyright 2015 University of Cambridge */
/* */
/* All rights reserved. This file is distributed under the terms of */
/* the GNU Lesser General Public License version 2.1, with the */
/* special exception on linking described in the file LICENSE. */
/* */
/**************************************************************************/
#include "mlvalues.h"
#ifndef CAML_ADDRMAP_H
#define CAML_ADDRMAP_H
/* An addrmap is a value -> value hashmap, where
the values are blocks */
struct addrmap_entry { value key, value; };
struct addrmap {
struct addrmap_entry* entries;
uintnat size;
};
#define ADDRMAP_INIT {0,0}
int caml_addrmap_contains(struct addrmap* t, value k);
value caml_addrmap_lookup(struct addrmap* t, value k);
#define ADDRMAP_NOT_PRESENT ((value)(0))
#define ADDRMAP_INVALID_KEY ((value)(0))
value* caml_addrmap_insert_pos(struct addrmap* t, value k);
/* must not already be present */
void caml_addrmap_insert(struct addrmap* t, value k, value v);
void caml_addrmap_init(struct addrmap* t);
void caml_addrmap_clear(struct addrmap* t);
void caml_addrmap_iter(struct addrmap* t, void (*f)(value, value));
/* iteration */
typedef uintnat addrmap_iterator;
Caml_inline int caml_addrmap_iter_ok(struct addrmap* t, addrmap_iterator i)
{
if (i < t->size) {
CAMLassert(t->entries[i].key != ADDRMAP_INVALID_KEY);
return 1;
} else {
return 0;
}
}
Caml_inline addrmap_iterator caml_addrmap_next(struct addrmap* t,
addrmap_iterator i)
{
if (!t->entries) return (uintnat)(-1);
i++;
while (i < t->size && t->entries[i].key == ADDRMAP_INVALID_KEY) {
i++;
}
caml_addrmap_iter_ok(t, i); /* just for assert-checks */
return i;
}
Caml_inline value caml_addrmap_iter_key(struct addrmap* t,
addrmap_iterator i)
{
CAMLassert(caml_addrmap_iter_ok(t, i));
return t->entries[i].key;
}
Caml_inline value caml_addrmap_iter_value(struct addrmap* t,
addrmap_iterator i)
{
CAMLassert(caml_addrmap_iter_ok(t, i));
return t->entries[i].value;
}
Caml_inline value* caml_addrmap_iter_val_pos(struct addrmap* t,
addrmap_iterator i)
{
CAMLassert(caml_addrmap_iter_ok(t, i));
return &t->entries[i].value;
}
Caml_inline addrmap_iterator caml_addrmap_iterator(struct addrmap* t)
{
return caml_addrmap_next(t, (uintnat)(-1));
}
#endif