open Powers (** Key for hastables that contain tuples of integers encoded in small integers *) type kind_array = | Kind_3 | Kind_6 | Kind_12 | Kind_9 type t = { left : int ; right : int ; } let of_int right = { left = 0 ; right } let (<|) { left ; right } x = { left=right ; right=x } let (<<) { left ; right } x = { left ; right = (right lsl 10) lor x } let (<+) { left ; right } x = { left ; right = (right lsl 15) lor x } (** Build a Zkey from an array or 1, 2, 3, 4, 6, 9, or 12 integers *) let of_int_array ~kind a = match kind with | Kind_3 -> of_int a.(0) <+ a.(1) <+ a.(2) | Kind_6 -> of_int a.(0) << a.(1) << a.(2) << a.(3) << a.(4) << a.(5) | Kind_12 -> of_int a.(0) << a.(1) << a.(2) << a.(3) << a.(4) << a.(5) <| a.(6) << a.(7) << a.(8) << a.(9) << a.(10) << a.(11) | Kind_9 -> of_int a.(0) << a.(1) << a.(2) << a.(3) << a.(4) << a.(5) <| a.(6) << a.(7) << a.(8) type kind = | Three of Powers.t | Six of (Powers.t * Powers.t) | Nine of (Powers.t * Powers.t * Powers.t) | Twelve of (Powers.t * Powers.t * Powers.t * Powers.t) let of_powers_three { x=a ; y=b ; z=c ; _ } = of_int a <+ b <+ c let of_powers_six { x=a ; y=b ; z=c ; _ } { x=d ; y=e ; z=f ; _ } = of_int a << b << c << d << e << f let of_powers_twelve { x=a ; y=b ; z=c ; _ } { x=d ; y=e ; z=f ; _ } { x=g ; y=h ; z=i ; _ } { x=j ; y=k ; z=l ; _ } = of_int a << b << c << d << e << f <| g << h << i << j << k << l let of_powers_nine { x=a ; y=b ; z=c ; _ } { x=d ; y=e ; z=f ; _ } { x=g ; y=h ; z=i ; _ } = of_int a << b << c << d << e << f <| g << h << i let of_powers a = match a with | Three { x=a ; y=b ; z=c ; _ } -> of_int a <+ b <+ c | Six ({ x=a ; y=b ; z=c },{ x=d ; y=e ; z=f }) -> of_int a << b << c << d << e << f | Twelve ({ x=a ; y=b ; z=c },{ x=d ; y=e ; z=f },{ x=g ; y=h ; z=i },{ x=j ; y=k ; z=l }) -> of_int a << b << c << d << e << f <| g << h << i << j << k << l | Nine ({ x=a ; y=b ; z=c },{ x=d ; y=e ; z=f },{ x=g ; y=h ; z=i }) -> of_int a << b << c << d << e << f <| g << h << i let mask10 = 0x3ff and mask15 = 0x7fff (** Transform the Zkey into an int array *) let to_int_array ~kind { left ; right } = match kind with | Kind_3 -> [| mask15 land (right lsr 30) ; mask15 land (right lsr 15) ; mask15 land right |] | Kind_6 -> [| mask10 land (right lsr 50) ; mask10 land (right lsr 40) ; mask10 land (right lsr 30) ; mask10 land (right lsr 20) ; mask10 land (right lsr 10) ; mask10 land right |] | Kind_12 -> [| mask10 land (left lsr 50) ; mask10 land (left lsr 40) ; mask10 land (left lsr 30) ; mask10 land (left lsr 20) ; mask10 land (left lsr 10) ; mask10 land left ; mask10 land (right lsr 50) ; mask10 land (right lsr 40) ; mask10 land (right lsr 30) ; mask10 land (right lsr 20) ; mask10 land (right lsr 10) ; mask10 land right |] | Kind_9 -> [| mask10 land (left lsr 20) ; mask10 land (left lsr 10) ; mask10 land left ; mask10 land (right lsr 50) ; mask10 land (right lsr 40) ; mask10 land (right lsr 30) ; mask10 land (right lsr 20) ; mask10 land (right lsr 10) ; mask10 land right |] (** Transform the Zkey into an int tuple *) let to_powers ~kind { left ; right } = match kind with | Kind_3 -> Three (Powers.of_int_tuple ( mask15 land (right lsr 30) , mask15 land (right lsr 15) , mask15 land right )) | Kind_6 -> Six (Powers.of_int_tuple ( mask10 land (right lsr 50) , mask10 land (right lsr 40) , mask10 land (right lsr 30)), Powers.of_int_tuple ( mask10 land (right lsr 20) , mask10 land (right lsr 10) , mask10 land right ) ) | Kind_12 -> Twelve (Powers.of_int_tuple ( mask10 land (left lsr 50) , mask10 land (left lsr 40) , mask10 land (left lsr 30)), Powers.of_int_tuple ( mask10 land (left lsr 20) , mask10 land (left lsr 10) , mask10 land left ) , Powers.of_int_tuple ( mask10 land (right lsr 50) , mask10 land (right lsr 40) , mask10 land (right lsr 30)), Powers.of_int_tuple ( mask10 land (right lsr 20) , mask10 land (right lsr 10) , mask10 land right ) ) | Kind_9 -> Nine (Powers.of_int_tuple ( mask10 land (left lsr 20) , mask10 land (left lsr 10) , mask10 land left ) , Powers.of_int_tuple ( mask10 land (right lsr 50) , mask10 land (right lsr 40) , mask10 land (right lsr 30)), Powers.of_int_tuple ( mask10 land (right lsr 20) , mask10 land (right lsr 10) , mask10 land right ) ) let hash = Hashtbl.hash let equal { right = r1 ; left = l1 } { right = r2 ; left = l2 } = r1 = r2 && l1 = l2 let cmp { right = r1 ; left = l1 } { right = r2 ; left = l2 } = if r1 < r2 then -1 else if r1 > r2 then 1 else if l1 < l2 then -1 else if l1 > l2 then 1 else 0 let to_string ~kind { left ; right } = "< " ^ string_of_int left ^ string_of_int right ^ " | " ^ ( to_int_array kind { left ; right } |> Array.map string_of_int |> Array.to_list |> String.concat ", " ) ^ " >" (* let debug () = and k3 = of_int_array Kind_3 [| 1 ; 2 ; 3 |] and k6 = of_int_array Kind_6 [| 1 ; 2 ; 3; 4 ; 5; 6|] and k12 = of_int_array Kind_12 [| 1 ; 2 ; 3; 4 ; 5; 6 ; 7 ; 8 ; 9 ; 10 ; 11; 12|] in print_endline @@ to_string Kind_3 k3 ; print_endline @@ to_string Kind_6 k6 ; print_endline @@ to_string Kind_12 k12 (* < 65538 | 1, 2 > < 1050627 | 1, 2, 3 > < 281483566841860 | 1, 2, 3, 4 > < 1128102155523078 | 1, 2, 3, 4, 5, 6 > < 20809811751934310026571282435288076 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 > *) let () = debug () *)