package acgtk

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file new_weight.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
module type Weight_sig =
sig
  type w
  val pp : Format.formatter -> w -> unit
  val init : w

  val is_better : w -> w -> bool
  (** [is better w1 w2] returns [true] if [w1] is strictly better
      than [w2] *)

  val is_equal : w -> w -> bool
  (** [is better w1 w2] returns [true] if [w1=w2] *)
    
  val update : current:w -> w -> w
  val up : w -> 'a -> w 
  val down : w -> 'a -> w
  val right : w -> 'a -> w
  module WMap : Map.S with type key = w
  val optimum : 'a WMap.t -> ( w * 'a ) option
end

module Weight_as_Depth =
struct
  type w = int
  let pp fmt w = Format.fprintf fmt "depth = %d" w
  let init = 1
  let is_better a b = a < b
  let is_equal a b = a=b
  let update ~current w = if is_better current w then current else w
  let up w _ = w - 1 
  let down w _ = w + 1
  let right w _ = w
  (*  let left w _ = w *)
  module WMap = Utils.IntMap
  let optimum = WMap.min_binding_opt
end

module Weight_as_Depth_and_Size =
struct
  type w = { current : int; max : int; size : int }
  let pp fmt w = Format.fprintf fmt "depth = %d, size = %d" w.max w.size
  let init = { current = 1; max = 1; size = 1 }
  let is_better w w' =
    match w.max - w'.max with
    | 0 ->
      (match w.size - w'.size with
       | 0 -> w.current < w'.current
       | i when i < 0 -> true
       | _ -> false)
    | i when i < 0 -> true
    | _ -> false
  let is_equal w w' = w = w'
  let update ~current w = if is_better current w then current else w
  let up w _ = { w with current = w.current - 1 }
  let down w  _ =
    let current = w.current + 1 in
    { current  ; size = w.size + 1; max = max current w.max }
  let right w _ = { w with size = w.size + 1 }
  module WMap = Map.Make (
    struct
      type t = w
      let compare w w' =
        match w.max - w'.max with
        | 0 ->
          (match w.size - w'.size with
           | 0 -> w.current -w'.current
           | r -> r)
        | r -> r
    end)
  let optimum = WMap.min_binding_opt
end

module MapMake(W:Weight_sig)=
struct
  type 'a t = (W.w * ('a list W.WMap.t)) option
  let empty = None
    
  let pp fmt = function
    | None -> Format.fprintf fmt "None"
    | Some (w, map) ->
      let pp_map fmt m =
        W.WMap.iter
          (fun k v ->
             Format.fprintf
               fmt
               "@[<hov>Bindings:@[ %a -> list of length %d@]@]@ "
               W.pp
               k
               (List.length v))
          m in
      Format.fprintf
        fmt
        "@[Optimum set to: %a@ @[<v> @[%a@]@]@]"
        W.pp
        w
        pp_map
        map
        
  let rec remove_empty_bindings map = 
    match W.optimum map with
    | None -> None
    | Some (w', []) -> remove_empty_bindings (W.WMap.remove w' map)
    | Some (w', _) -> Some (w', map)
                        
  let add weight state map =
    match map with
    | None -> Some (weight, W.WMap.add weight [state] W.WMap.empty)
    | Some (opt, map) when W.is_better weight opt ->
      (* weight is strictly better than opt, hence no binding for
           weight is present *)
      Some (weight, W.WMap.add weight [state] map)
    | Some (opt, map) when W.is_equal weight opt ->
      (* weight is opt *)
      let states = W.WMap.find opt map in
      (* Shouldn't raise a Not_found exception *)
      Some (opt, W.WMap.add opt (state :: states) map)
    | Some (opt, map) ->
      (* opt is trictly better than weight *)
      let states =
        match W.WMap.find_opt weight map with
        | None -> [state]
        | Some previous_states -> state :: previous_states in
      Some (opt, W.WMap.add weight states map)
        
  let pop_optimum m =
    match m with
    | None -> None
    | Some (w_opt, map) ->
      (match W.optimum map with
       | None -> failwith "Bug: optimum is set for an empty map"
       | Some (w', _) when w' <> w_opt -> failwith "Bug: optimum is not correctly set"
       | Some (_ , [] ) -> failwith "Bug: Should not occurr"
       | Some (w', [s]) ->
         Some (s, w', remove_empty_bindings (W.WMap.remove w' map))
       | Some (w', s :: states) ->
         Some (s, w', Some (w', W.WMap.add w' states map)))
      

  

end
OCaml

Innovation. Community. Security.