]> matita.cs.unibo.it Git - helm.git/blob - helm/interface/uriManager.ml.implementazione_doppia
Initial revision
[helm.git] / helm / interface / uriManager.ml.implementazione_doppia
1 (* "cic:/a/b/c.con" => [| "cic:/a" ; "cic:/a/b" ; "cic:/a/b/c.con" ; "c" |] *)
2 type uri = string array;;
3
4 let eq uri1 uri2 =
5  uri1 == uri2
6 ;;
7
8 let string_of_uri uri = uri.(Array.length uri - 2);;
9 let name_of_uri uri = uri.(Array.length uri - 1);;
10 let buri_of_uri uri = uri.(Array.length uri - 3);;
11 let depth_of_uri uri = Array.length uri - 2;;
12
13 (*CSC: ora e' diventato poco efficiente, migliorare *)
14 let relative_depth curi uri cookingsno =
15  let rec length_of_current_prefix l1 l2 =
16   match (l1, l2) with
17      (he1::tl1, he2::tl2) when he1 == he2 ->
18        1 + length_of_current_prefix tl1 tl2
19    | (_,_) -> 0
20  in
21   depth_of_uri uri -
22    length_of_current_prefix
23     (Array.to_list (Array.sub curi 0 (Array.length curi - (2 + cookingsno))))
24     (Array.to_list (Array.sub uri 0 (Array.length uri - 2)))
25   (*CSC: vecchio codice da eliminare
26   if eq curi uri then 0
27   else
28    depth_of_uri uri -
29     length_of_current_prefix (Array.to_list curi) (Array.to_list uri)
30   *)
31 ;;
32
33 module OrderedStrings =
34  struct
35   type t = string
36   let compare (s1 : t) (s2 : t) = compare s1 s2
37  end
38 ;;
39
40 module SetOfStrings = Map.Make(OrderedStrings);;
41
42 (*CSC: commento obsoleto ed errato *)
43 (* Invariant: the map is the identity function,      *)
44 (*  i.e. (SetOfStrings.find str !set_of_uri) == str  *)
45 let set_of_uri = ref SetOfStrings.empty;;
46 let set_of_prefixes = ref SetOfStrings.empty;;
47
48 (* similar to uri_of_string, but used for prefixes of uris *)
49 let normalize prefix =
50  try
51   SetOfStrings.find prefix !set_of_prefixes
52  with
53   Not_found ->
54    set_of_prefixes := SetOfStrings.add prefix prefix !set_of_prefixes ;
55    prefix
56 ;;
57
58 exception IllFormedUri of string;;
59
60 let mk_prefixes str =
61  let rec aux curi =
62   function
63      [he] ->
64       let prefix_uri = curi ^ "/" ^ he
65       and name = List.hd (Str.split (Str.regexp "\.") he) in
66        [ normalize prefix_uri ; name ]
67    | he::tl ->
68       let prefix_uri = curi ^ "/" ^ he in
69        (normalize prefix_uri)::(aux prefix_uri tl)
70    | _ -> raise (IllFormedUri str)
71  in
72   let tokens = (Str.split (Str.regexp "/") str) in
73    (* ty = "cic:" *)
74    let (ty, sp) = (List.hd tokens, List.tl tokens) in
75     aux ty sp
76 ;;
77
78 let uri_of_string str =
79  try
80   SetOfStrings.find str !set_of_uri
81  with
82   Not_found ->
83    let uri = Array.of_list (mk_prefixes str) in
84     set_of_uri := SetOfStrings.add str uri !set_of_uri ;
85     uri
86 ;;