Skip to content
This repository
tag: v1323
Fetching contributors…

Cannot retrieve contributors at this time

file 55 lines (43 sloc) 1.903 kb
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
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* CF mli *)

type ('a, 'b) t =
    {
      hash : ('a, 'b) Hashtbl.t;
      queue : ('a * 'b ref) Queue.t;
      mutable list : ('a * 'b ref) list
    }

let create i =
  {
    hash = Hashtbl.create i;
    queue = Queue.create ();
    list = []
  }
let clear t = Hashtbl.clear t.hash; Queue.clear t.queue; t.list <- []

let add t a b = let ab = (a, ref b) in Hashtbl.add t.hash a b; Queue.add ab t.queue; t.list <- ab::t.list
let replace t a b =
  if Hashtbl.mem t.hash a then begin
    Hashtbl.replace t.hash a b;
    List.iter (fun (u, t) -> if a = u then t := b else ()) t.list;
    Queue.iter (fun (u, t) -> if a = u then t := b else ()) t.queue end
  else add t a b

let find_opt t a = try Some (Hashtbl.find t.hash a) with Not_found -> None
let mem t = Hashtbl.mem t.hash

let iter f t = let ff (a, b) = f a !b in Queue.iter ff t.queue
let rev_iter f t = let ff (a, b) = f a !b in List.iter ff t.list

let fold_left f acc t = let ff acc (a, b) = f acc a !b in Queue.fold ff acc t.queue
let fold_right f t acc = let ff acc (a, b) = f a !b acc in List.fold_left ff acc t.list

let to_list t = List.rev_map (fun (a, b) -> (a, !b)) t.list
let to_rev_list t = Queue.fold (fun acc (a, b) -> (a, !b)::acc) [] t.queue

let length t = Hashtbl.length t.hash
Something went wrong with that request. Please try again.