Commit fa3934fb by POTTIER Francois

Remove [Misc.index_map].

parent e3837df3
......@@ -110,18 +110,17 @@ let terminated_iter_to_string printer terminator iter =
let terminated_list_to_string printer terminator xs =
terminated_iter_to_string printer terminator (fun f -> List.iter f xs)
let index_map string_map =
let n = StringMap.cardinal string_map in
let a = Array.make n None in
let conv, _ = StringMap.fold
(fun k v (conv, idx) ->
a.(idx) <- Some (k, v);
StringMap.add k idx conv, idx + 1)
string_map (StringMap.empty, 0)
in
((fun n -> snd (unSome a.(n))),
(fun k -> StringMap.find k conv),
(fun n -> fst (unSome a.(n))))
let inverse (a : 'a array) : 'a -> int =
let table = Hashtbl.create (Array.length a) in
Array.iteri (fun i data ->
assert (not (Hashtbl.mem table data));
Hashtbl.add table data i
) a;
fun data ->
try
Hashtbl.find table data
with Not_found ->
assert false
let support_assoc l x =
try
......@@ -414,3 +413,9 @@ module ListExtras = struct
let hash hash xs =
Hashtbl.hash (List.map hash xs)
end
let nth = function
| 1 -> "first"
| 2 -> "second"
| 3 -> "third"
| i -> Printf.sprintf "%dth" i
......@@ -92,17 +92,12 @@ val separated_list_to_string: ('a -> string) -> string -> 'a list -> string
val terminated_list_to_string: ('a -> string) -> string -> 'a list -> string
val terminated_iter_to_string: ('a -> string) -> string -> 'a iter -> string
(* [index_map f] returns a triple (indexed_f, domain_indexation, domain_array).
[indexed_f] is a mapping from [0..n-1] to the elements of the map [f]
([n] being the size of the image of [f]).
[domain_indexation] is a mapping from the domain of the map [f] to indexes.
[domain_array] is a mapping from the indexes to the domain of [f].
The indexation implements [f] ie:
- forall x in domain(m), indexed_f (domain_indexation x) = f (x).
- forall x in domain(m), domain_array (domain_indexation x) = x. *)
val index_map
: 'a StringMap.t -> (int -> 'a) * (string -> int) * (int -> string)
(* If [a] is an array, therefore a mapping of integers to elements, then
[inverse a] computes its inverse, a mapping of elements to integers.
The type ['a] of elements must support the use of OCaml's generic
equality and hashing functions. *)
val inverse: 'a array -> ('a -> int)
(* [support_assoc l x] returns the second component of the first couple
in [l] whose first component is [x]. If it does not exist, it returns
......@@ -225,3 +220,7 @@ module ListExtras : sig
val equal: ('a -> 'a -> bool) -> 'a list -> 'a list -> bool
val hash: ('a -> int) -> 'a list -> int
end
(* A nice way of printing "nth" in English, for concrete values of [n]. *)
val nth: int -> string
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment