11type 'a markable_reference = { node : 'a node ; marked : bool }
2- (* * markable reference: stores a reference to a node and has a field to specify if it is marked *)
32
43and 'a node = {
54 key : 'a ;
@@ -15,28 +14,24 @@ let min = Obj.new_block 5 5 |> Obj.obj
1514let max = Obj. new_block 5 5 |> Obj. obj
1615let null_node = { key = max; height = 0 ; next = [||] }
1716
18- (* * create_dummy_node_array: Creates a new array with the different node for each index *)
1917let [@ inline] create_dummy_node_array sl =
2018 Array. make (sl.max_height + 1 ) null_node
2119
22- (* * create_new_node: creates a new node with some value and height *)
2320let [@ inline] create_new_node value height =
2421 let next =
2522 Array. init (height + 1 ) (fun _ ->
2623 Atomic. make { node = null_node; marked = false })
2724 in
2825 { key = value; height; next }
2926
30- (* * Get a random level from 0 till max_height - 1 *)
3127let [@ inline] get_random_level sl =
3228 let rec count_level cur_level =
33- if Random. bool () then cur_level
29+ if Random. bool () then cur_level
3430 else if cur_level == sl.max_height then count_level 0
3531 else count_level (cur_level + 1 )
3632 in
3733 if sl.max_height = 0 then 0 else count_level 0
3834
39- (* * Create a new skiplist *)
4035let create ?(max_height = 10 ) () =
4136 let max_height = Int. max max_height 1 in
4237 let tail = create_new_node max (max_height - 1 ) in
@@ -49,13 +44,12 @@ let create ?(max_height = 10) () =
4944(* * Compares old_node and old_mark with the atomic reference and if they are the same then
5045 Replaces the value in the atomic with node and mark *)
5146let compare_and_set_mark_ref (atomic , old_node , old_mark , node , mark ) =
52- let current = Atomic. get atomic in
53- let set_mark_ref () =
54- Atomic. compare_and_set atomic current { node; marked = mark }
47+ let ({ node = current_node; marked = current_marked } as current) =
48+ Atomic. get atomic
5549 in
56- let current_node = current.node in
57- current_node == old_node && current.marked = old_mark
58- && ((current_node == node && current. marked = mark) || set_mark_ref () )
50+ current_node == old_node && current_marked = old_mark
51+ && (( current_node == node && current_marked = mark)
52+ || Atomic. compare_and_set atomic current { node; marked = mark } )
5953
6054(* * Returns true if key is found within the skiplist else false;
6155 Irrespective of return value, fills the preds and succs array with
@@ -138,7 +132,6 @@ let add sl key =
138132 in
139133 repeat ()
140134
141- (* * Returns true if the key is within the skiplist, else returns false *)
142135let mem sl key =
143136 let rec search (pred , curr , succ , mark , level ) =
144137 if mark then
@@ -163,7 +156,6 @@ let mem sl key =
163156 let { node = succ; marked = mark } = Atomic. get curr.next.(sl.max_height) in
164157 search (pred, curr, succ, mark, sl.max_height)
165158
166- (* * Returns true if the removal was successful and returns false if the key is not present within the skiplist *)
167159let remove sl key =
168160 let preds = create_dummy_node_array sl in
169161 let succs = create_dummy_node_array sl in
0 commit comments