@@ -969,10 +969,11 @@ type ('k, 'v) cache = {
969969To create a cache we just create the data structures:
970970
971971``` ocaml
972- # let cache ?hashed_type capacity =
973- { space = Loc.make capacity;
974- table = Hashtbl.create ?hashed_type ();
975- order = Dllist.create () }
972+ # let cache ?hashed_type capacity = {
973+ space = Loc.make capacity;
974+ table = Hashtbl.create ?hashed_type ();
975+ order = Dllist.create ()
976+ }
976977val cache : ?hashed_type:'a Hashtbl.hashed_type -> int -> ('a, 'b) cache =
977978 <fun>
978979```
@@ -997,30 +998,30 @@ val get_opt : xt:'a Xt.t -> ('b, 'c) cache -> 'b -> 'c option = <fun>
997998that, as explained previously, moves the node corresponding to the accessed
998999association to the left end of the list.
9991000
1000- To introduce associations we provide the ` set ` operation
1001+ To introduce associations we provide the ` set_blocking ` operation
10011002
10021003``` ocaml
1003- # let set ~xt {table; order; space; _} key value =
1004+ # let set_blocking ~xt {table; order; space; _} key value =
10041005 let node =
10051006 match Hashtbl.Xt.find_opt ~xt table key with
10061007 | None ->
10071008 if 0 = Xt.update ~xt space (fun n -> Int.max 0 (n-1)) then
1008- Dllist.Xt.take_opt_r ~xt order
1009- |> Option.iter ( Hashtbl.Xt.remove ~xt table) ;
1009+ Dllist.Xt.take_blocking_r ~xt order
1010+ |> Hashtbl.Xt.remove ~xt table;
10101011 Dllist.Xt.add_l ~xt key order
10111012 | Some (node, _) ->
10121013 Dllist.Xt.move_l ~xt node order;
10131014 node
10141015 in
10151016 Hashtbl.Xt.replace ~xt table key (node, value)
1016- val set : xt:'a Xt.t -> ('b, 'c) cache -> 'b -> 'c -> unit = <fun>
1017+ val set_blocking : xt:'a Xt.t -> ('b, 'c) cache -> 'b -> 'c -> unit = <fun>
10171018```
10181019
10191020that, like ` get_opt ` , either moves or adds the node of the accessed association
10201021to the left end of the list. In case a new association is added, the space is
1021- decremented. If there was no space, an association is first removed. As
1022- described previously, the association to remove is determined by removing the
1023- rightmost element from the list.
1022+ decremented. If there was no space, an association is first removed, which will
1023+ block in case capacity is 0. As described previously, the association to remove
1024+ is determined by removing the rightmost element from the list.
10241025
10251026We can then test that the cache works as expected:
10261027
@@ -1029,16 +1030,16 @@ We can then test that the cache works as expected:
10291030val a_cache : (int, string) cache =
10301031 {space = <abstr>; table = <abstr>; order = <abstr>}
10311032
1032- # Xt.commit { tx = set a_cache 101 "basics" }
1033+ # Xt.commit { tx = set_blocking a_cache 101 "basics" }
10331034- : unit = ()
10341035
1035- # Xt.commit { tx = set a_cache 42 "answer" }
1036+ # Xt.commit { tx = set_blocking a_cache 42 "answer" }
10361037- : unit = ()
10371038
10381039# Xt.commit { tx = get_opt a_cache 101 }
10391040- : string option = Some "basics"
10401041
1041- # Xt.commit { tx = set a_cache 2023 "year" }
1042+ # Xt.commit { tx = set_blocking a_cache 2023 "year" }
10421043- : unit = ()
10431044
10441045# Xt.commit { tx = get_opt a_cache 42 }
0 commit comments