forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathordered_hash_map.cppo.ml
110 lines (88 loc) · 2.71 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
#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 copy = copy
let iter = iter
let fold = fold
let length = length
let stats = stats
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(key1,_, _, rest) ->
equal_key key key1 ||
match rest with
| Empty -> false
| Cons(key2 , _,_, rest) ->
equal_key key key2 ||
match rest with
| Empty -> false
| Cons(key3,_, _, rest) ->
equal_key key key3 ||
small_bucket_mem key rest
let rec small_bucket_rank key lst =
match lst with
| Empty -> -1
| Cons(key1,i,_, rest) ->
if equal_key key key1 then i
else match rest with
| Empty -> -1
| Cons(key2,i2, _, rest) ->
if equal_key key key2 then i2 else
match rest with
| Empty -> -1
| Cons(key3,i3, _, rest) ->
if equal_key key key3 then i3 else
small_bucket_rank key rest
let rec small_bucket_find_value key (lst : (_,_) bucket) =
match lst with
| Empty -> raise Not_found
| Cons(key1,_,value, rest) ->
if equal_key key key1 then value
else match rest with
| Empty -> raise Not_found
| Cons(key2,_,value, rest) ->
if equal_key key key2 then value else
match rest with
| Empty -> raise Not_found
| Cons(key3, _ , value, rest) ->
if equal_key key key3 then value else
small_bucket_find_value key rest
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,h.size, value, 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