Safety commit
[SXSI/xpathcomp.git] / ata.ml
diff --git a/ata.ml b/ata.ml
index 3959ccf..8c3fa18 100644 (file)
--- a/ata.ml
+++ b/ata.ml
@@ -58,8 +58,8 @@ struct
       match f.pos with
        | False -> 0
        | True -> 1
-       | Or (f1,f2) -> HASHINT3(PRIME2,f1.Node.id, f2.Node.id)
-       | And (f1,f2) -> HASHINT3(PRIME3,f1.Node.id,f2.Node.id)
+       | Or (f1,f2) -> HASHINT3(PRIME2,Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
+       | And (f1,f2) -> HASHINT3(PRIME3,Uid.to_int f1.Node.id, Uid.to_int f2.Node.id)
        | Atom(d,p,s) -> HASHINT4(PRIME4,hash_const_variant d,vb p,s)       
     end
 
@@ -197,7 +197,9 @@ module Transition = struct
   type node = State.t*TagSet.t*bool*Formula.t*bool
   include Hcons.Make(struct
                       type t = node
-                      let hash (s,ts,m,f,b) = HASHINT5(s,TagSet.uid ts,Formula.uid f,vb m,vb b)
+                      let hash (s,ts,m,f,b) = HASHINT5(s,Uid.to_int (TagSet.uid ts),
+                                                       Uid.to_int (Formula.uid f),
+                                                       vb m,vb b)
                       let equal (s,ts,b,f,m) (s',ts',b',f',m') = 
                         s == s' && ts == ts' && b==b' && m==m' && f == f'
                     end)
@@ -284,7 +286,9 @@ module FormTable = Hashtbl.Make(struct
                                  let equal (f1,s1,t1) (f2,s2,t2) =
                                    f1 == f2 && s1 == s2 && t1 == t2
                                  let hash (f,s,t) = 
-                                   HASHINT3(Formula.uid f ,StateSet.uid s,StateSet.uid t)
+                                   HASHINT3(Uid.to_int (Formula.uid f),
+                                            Uid.to_int (StateSet.uid s),
+                                            Uid.to_int (StateSet.uid t))
                                end)
 module F = Formula
 
@@ -332,7 +336,10 @@ module FTable = Hashtbl.Make(struct
                               type t = Tag.t*Formlist.t*StateSet.t*StateSet.t
                               let equal (tg1,f1,s1,t1) (tg2,f2,s2,t2) =
                                 tg1 == tg2 && f1 == f2 &&  s1 == s2 && t1 == t2;;
-                              let hash (tg,f,s,t) =  HASHINT4(tg,Formlist.uid f ,StateSet.uid s,StateSet.uid t);;
+                              let hash (tg,f,s,t) =  
+                                HASHINT4(tg, Uid.to_int (Formlist.uid f),
+                                         Uid.to_int (StateSet.uid s),
+                                         Uid.to_int (StateSet.uid t))
                             end)
 
 
@@ -817,11 +824,40 @@ END
          
       module SListTable = Hashtbl.Make(struct type t = SList.t
                                              let equal = (==)
-                                             let hash t = t.SList.Node.id 
+                                             let hash t = Uid.to_int t.SList.Node.id
                                       end)
 
 
-      module TransCacheOld = 
+      module TransCache =
+      struct
+       type cell = { key : int;
+                     obj : Obj.t }
+       type 'a t = cell array
+       let dummy = { key = 0; obj = Obj.repr () }
+       let create n = Array.create 25000 dummy
+       let hash a b = HASHINT2(Obj.magic a, Uid.to_int b.SList.Node.id)
+
+       let find_slot t key =
+         let rec loop i =
+           if (t.(i)  != dummy) && (t.(i).key != key)
+           then loop ((i+1 mod 25000))
+           else i
+         in loop (key mod 25000)
+       ;;
+
+       let find t k1 k2 = 
+         let i = find_slot t (hash k1 k2) in
+         if t.(i) == dummy then raise Not_found
+         else Obj.magic (t.(i).obj)
+           
+       let add t k1 k2 v = 
+         let key = hash k1 k2 in
+         let i = find_slot t key in
+         t.(i)<- { key = key; obj = (Obj.repr v) }
+
+      end
+
+      module TransCache2 = 
       struct
        type 'a t = Obj.t array SListTable.t
        let create n = SListTable.create n
@@ -853,7 +889,7 @@ END
 
       end
 
-      module TransCache = 
+      module TransCacheOld = 
       struct 
        external get : 'a array -> int ->'a = "%array_unsafe_get"
        external set : 'a array -> int -> 'a -> unit = "%array_unsafe_set"
@@ -863,32 +899,22 @@ END
        let create n = Array.create n dummy_cell
        let dummy = fun _ _-> assert false
        let find h tag slist =
-         let tab = get h slist.SList.Node.id in
+         let tab = get h (Uid.to_int slist.SList.Node.id) in
          if tab == dummy_cell then raise Not_found
          else
          let res = get tab tag in
          if res == dummy then raise Not_found else res
 
        let add (h : t) tag slist (data : fun_tree) =
-         let tab = get h slist.SList.Node.id in
+         let tab = get h (Uid.to_int slist.SList.Node.id) in
          let tab = if tab == dummy_cell then
            let x = Array.create 10000 dummy in
-           (set h slist.SList.Node.id  x;x)
+           (set h (Uid.to_int slist.SList.Node.id) x;x)
          else tab
          in
          set tab tag data        
       end
        
-      module TransCache2 = struct
-       include Hashtbl.Make (struct
-                       type t = Tag.t*SList.t
-                       let equal (a,b) (c,d) = a==c && b==d
-                       let hash (a,b) = HASHINT2((Obj.magic a), b.SList.Node.id)
-                     end)
-
-       let add h t s d = add h (t,s) d
-       let find h t s = find h (t,s)
-      end
        
       let td_trans = TransCache.create 10000 (* should be number of tags *number of states^2
                                                in the document *)
@@ -900,10 +926,10 @@ END
             
       module FllTable = Hashtbl.Make (struct type t = Formlistlist.t
                                             let equal = (==)
-                                            let hash t = t.Formlistlist.Node.id
+                                            let hash t = Uid.to_int t.Formlistlist.Node.id
                                      end)
        
-      module Fold2Res = struct
+      module Fold2ResOld = struct
        external get : 'a array -> int ->'a = "%array_unsafe_get"
        external set : 'a array -> int -> 'a -> unit = "%array_unsafe_set"
        external field1 : 'a -> 'b = "%field1"
@@ -919,12 +945,12 @@ END
          let af = get h tag in
          if af == dummy then raise Not_found
          else 
-         let as1 = get af fl.Formlistlist.Node.id in
+         let as1 = get af (Uid.to_int fl.Formlistlist.Node.id) in
          if as1 == dummy then raise Not_found
          else 
-         let as2 = get as1 s1.SList.Node.id in
+         let as2 = get as1 (Uid.to_int s1.SList.Node.id) in
          if as2 == dummy then raise Not_found 
-         else let v = get as2 s2.SList.Node.id in
+         else let v = get as2 (Uid.to_int s2.SList.Node.id) in
          if field1 v == 2 then raise Not_found 
          else v
        
@@ -939,33 +965,37 @@ END
            else x
          in
          let as1 = 
-           let x = get af fl.Formlistlist.Node.id in 
+           let x = get af (Uid.to_int fl.Formlistlist.Node.id) in 
            if x == dummy then
            begin
              let y = Array.make 10000 dummy in
-             set af fl.Formlistlist.Node.id y;y
+             set af (Uid.to_int fl.Formlistlist.Node.id) y;y
            end
            else x
          in
          let as2 = 
-           let x = get as1 s1.SList.Node.id in 
+           let x = get as1 (Uid.to_int s1.SList.Node.id) in 
            if x == dummy then 
            begin
              let y = Array.make 10000 dummy_val in
-             set as1 s1.SList.Node.id y;y
+             set as1 (Uid.to_int s1.SList.Node.id) y;y
            end
            else x
          in
-         set as2 s2.SList.Node.id data    
+         set as2 (Uid.to_int s2.SList.Node.id) data    
       end
+
+
        
-      module Fold2Res2 = struct
+      module Fold2Res3 = struct
        include Hashtbl.Make(struct 
                               type t = Tag.t*Formlistlist.t*SList.t*SList.t
                               let equal (a,b,c,d) (x,y,z,t) =
                                 a == x && b == y && c == z && d == t
-                              let hash (a,b,c,d) = HASHINT4 (a,b.Formlistlist.Node.id,
-                                                             c.SList.Node.id,d.SList.Node.id)
+                              let hash (a,b,c,d) = HASHINT4 (a,
+                                                             Uid.to_int b.Formlistlist.Node.id,
+                                                             Uid.to_int c.SList.Node.id,
+                                                             Uid.to_int d.SList.Node.id)
                             end)
        let add h t f s1 s2 d =
          add h (t,f,s1,s2) d
@@ -973,6 +1003,38 @@ END
          find h (t,f,s1,s2)
       end
 
+      module Fold2Res =
+      struct
+       type cell = { key : int;
+                     obj : Obj.t }
+       type 'a t = cell array
+       let dummy = { key = 0; obj = Obj.repr () }
+       let create n = Array.create 25000 dummy
+       let hash a b c d = HASHINT4(Obj.magic a, 
+                                   Uid.to_int b.Formlistlist.Node.id, 
+                                   Uid.to_int c.SList.Node.id,
+                                   Uid.to_int d.SList.Node.id)
+
+       let find_slot t key =
+         let rec loop i =
+           if (t.(i)  != dummy) && (t.(i).key != key)
+           then loop ((i+1 mod 25000))
+           else i
+         in loop (key mod 25000)
+       ;;
+
+       let find t k1 k2 k3 k4 = 
+         let i = find_slot t (hash k1 k2 k3 k4) in
+         if t.(i) == dummy then raise Not_found
+         else Obj.magic (t.(i).obj)
+           
+       let add t k1 k2 k3 k4 v = 
+         let key = hash k1 k2 k3 k4 in
+         let i = find_slot t key in
+         t.(i)<- { key = key; obj = (Obj.repr v) }
+
+      end
+
       let h_fold2 = Fold2Res.create 10000
       
       let top_down ?(noright=false) a tree t slist ctx slot_size =     
@@ -1189,7 +1251,7 @@ END
            if Ptss.mem s c.sets then
              { c with results = IMap.add s (RS.concat r (IMap.find s c.results)) c.results}
            else
-             { hash = HASHINT2(c.hash,Ptset.Int.uid s);
+             { hash = HASHINT2(c.hash,Uid.to_int (Ptset.Int.uid s));
                sets = Ptss.add s c.sets;
                results = IMap.add s r c.results
              }
@@ -1223,7 +1285,7 @@ END
            in
            let h,s =
              Ptss.fold 
-               (fun s (ah,ass) -> (HASHINT2(ah,Ptset.Int.uid s),
+               (fun s (ah,ass) -> (HASHINT2(ah, Uid.to_int (Ptset.Int.uid s)),
                                    Ptss.add s ass))
                (Ptss.union c1.sets c2.sets) (0,Ptss.empty)
            in
@@ -1270,7 +1332,7 @@ END
        let h_trans = Hashtbl.create 4096
 
        let get_up_trans slist ptag a tree =      
-         let key = (HASHINT2(SList.uid slist,ptag)) in
+         let key = (HASHINT2(Uid.to_int slist.SList.Node.id ,ptag)) in
            try
          Hashtbl.find h_trans key              
          with