Skip to content

Commit 6eb5303

Browse files
committed
Cleanup and documentation.
1 parent f733944 commit 6eb5303

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src_lockfree/skiplist.ml

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
type '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

43
and 'a node = {
54
key : 'a;
@@ -15,28 +14,24 @@ let min = Obj.new_block 5 5 |> Obj.obj
1514
let max = Obj.new_block 5 5 |> Obj.obj
1615
let null_node = { key = max; height = 0; next = [||] }
1716

18-
(** create_dummy_node_array: Creates a new array with the different node for each index *)
1917
let[@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 *)
2320
let[@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 *)
3127
let[@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 *)
4035
let 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 *)
5146
let 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 *)
142135
let 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 *)
167159
let remove sl key =
168160
let preds = create_dummy_node_array sl in
169161
let succs = create_dummy_node_array sl in

src_lockfree/skiplist.mli

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1+
(** Skiplist TODO
2+
3+
[key] values are compared with [=] and thus should not be functions or
4+
objects.
5+
*)
6+
17
type 'a t
8+
(** The type of lock-free skiplist. *)
29

310
val create : ?max_height:int -> unit -> 'a t
4-
val mem : 'a t -> 'a -> bool
11+
(** [create ~max_height ()] returns a new empty skiplist. [~max_height] is the
12+
number of level used to distribute nodes. Its default value is 10 by default
13+
and can not be less than 1. *)
14+
515
val add : 'a t -> 'a -> bool
16+
(** [add s v] adds [v] to [s] if [v] is not already in [s] and returns
17+
[true]. If [v] is already in [s], it returns [false] and [v] is unchanged. *)
18+
619
val remove : 'a t -> 'a -> bool
20+
(** [remove s v] removes [v] of [s] if [v] is in [s] and returns [true]. If [v]
21+
is not in [s], it returns [false] and [v] is unchanged. *)
22+
23+
val mem : 'a t -> 'a -> bool
24+
(** [mem s v] returns [true] if v is in s and [false] otherwise. *)

0 commit comments

Comments
 (0)