ocaml-lib / intreln.ml

Diff from to

intreln.ml

 
+module Intset = Intset.Intmap
+
 module Mask =
   struct
     type t = bool list
     val filter_restr : (string * int) list -> string list -> ((string * int) list -> bool) -> t -> t
     val iter : (int list -> unit) -> t -> unit
 
+    (* functions for accessing a relation as an association/map, for rels with dim>0 *)
+    val mem_assoc : int -> t -> bool
+    val assoc : int -> t -> t
+    val keys : t -> Intset.t
+    val fold_assoc : ('a -> int -> t -> 'a) -> 'a -> t -> 'a
+
     val project : Mask.t -> t -> t
     val group_by : Mask.t -> t -> t
     val restriction : int -> ?filter:(int list -> t -> bool) -> t -> t
 	
     (* utilities *)
 
+    let intmap d = (Obj.obj d : Obj.t M.t)
+
     type obj = R0 of bool | R1 of unit M.t | Rn of int * Obj.t M.t
 	
     let repr = function
       then
 	if Mask.size ps = r.dim
 	then r
-	else{dim = Mask.size ps;
-	     data = repr (project_obj ps (obj r.dim r.data))}
+	else {dim = Mask.size ps;
+	      data = repr (project_obj ps (obj r.dim r.data))}
       else raise Invalid_dimension)
     and project_obj ps = function
       | R0 b -> R0 b
 	    m
 
 
+    let rec mem_assoc x r = Common.prof "Intreln.mem_assoc" (fun () ->
+      if r.dim = 0
+      then raise Invalid_dimension
+      else M.mem x (intmap r.data))
+
+    let rec assoc x r = Common.prof "Intreln.assoc" (fun () ->
+      if r.dim = 0
+      then raise Invalid_dimension
+      else {dim = r.dim-1; data = repr (assoc_obj x (obj r.dim r.data))})
+    and assoc_obj x = function
+      | R0 _ -> assert false
+      | R1 s -> R0 (M.mem x s)
+      | Rn (n,m) -> obj (n-1) (M.get x m)
+
+    let keys r = Common.prof "Intreln.keys" (fun () ->
+      if r.dim = 0
+      then raise Invalid_dimension
+      else M.domain (intmap r.data))
+
+    let fold_assoc f init r = Common.prof "Intreln.fold_assoc" (fun () ->
+      if r.dim = 0
+      then raise Invalid_dimension
+      else M.fold (fun res x d -> f res x {dim=r.dim-1; data=d}) init (intmap r.data))
+
+
     let rec group_by ps r =
       if Mask.dim ps = r.dim
       then {dim = Mask.size ps;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.