-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathordered_hash_map.cppo.ml
111 lines (86 loc) · 2.78 KB
/
ordered_hash_map.cppo.ml
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
#if defined TYPE_FUNCTOR
module Make(H: Hashtbl.HashedType): (S with type key = H.t) =
struct
type key = H.t
type 'value t = (key,'value) Ordered_hash_map_gen.t
let key_index (h : _ t) key =
(H.hash key) land (Array.length h.data - 1)
let equal_key = H.equal
#elif defined TYPE_LOCAL_IDENT
type key = Ident.t
type 'value t = (key,'value) Ordered_hash_map_gen.t
let key_index (h : _ t) (key : key) =
(Bs_hash_stubs.hash_int key.stamp) land (Array.length h.data - 1)
let equal_key = Ext_ident.equal
#else
[%error "unknown type"]
#endif
open Ordered_hash_map_gen
let create = create
let clear = clear
let reset = reset
let iter = iter
let fold = fold
let length = length
let elements = elements
let choose = choose
let to_sorted_array = to_sorted_array
let rec small_bucket_mem key lst =
match lst with
| Empty -> false
| Cons rhs ->
equal_key key rhs.key ||
match rhs.next with
| Empty -> false
| Cons rhs ->
equal_key key rhs.key ||
match rhs.next with
| Empty -> false
| Cons rhs ->
equal_key key rhs.key ||
small_bucket_mem key rhs.next
let rec small_bucket_rank key lst =
match lst with
| Empty -> -1
| Cons rhs ->
if equal_key key rhs.key then rhs.ord
else match rhs.next with
| Empty -> -1
| Cons rhs ->
if equal_key key rhs.key then rhs.ord else
match rhs.next with
| Empty -> -1
| Cons rhs ->
if equal_key key rhs.key then rhs.ord else
small_bucket_rank key rhs.next
let rec small_bucket_find_value key (lst : (_,_) bucket) =
match lst with
| Empty -> raise Not_found
| Cons rhs ->
if equal_key key rhs.key then rhs.data
else match rhs.next with
| Empty -> raise Not_found
| Cons rhs ->
if equal_key key rhs.key then rhs.data else
match rhs.next with
| Empty -> raise Not_found
| Cons rhs ->
if equal_key key rhs.key then rhs.data else
small_bucket_find_value key rhs.next
let add h key value =
let i = key_index h key in
if not (small_bucket_mem key h.data.(i)) then
begin
h.data.(i) <- Cons {key; ord = h.size; data = value; next = h.data.(i)};
h.size <- h.size + 1 ;
if h.size > Array.length h.data lsl 1 then resize key_index h
end
let mem h key =
small_bucket_mem key (Array.unsafe_get h.data (key_index h key))
let rank h key =
small_bucket_rank key(Array.unsafe_get h.data (key_index h key))
let find_value h key =
small_bucket_find_value key (Array.unsafe_get h.data (key_index h key))
#if defined TYPE_FUNCTOR
end
#endif