10
1
mirror of https://gitlab.com/scemama/QCaml.git synced 2024-07-25 04:07:24 +02:00
QCaml/Utils/Zkey.ml

209 lines
6.0 KiB
OCaml
Raw Normal View History

2018-02-19 16:01:13 +01:00
open Powers
2018-02-14 19:23:23 +01:00
type t =
{
2018-02-25 01:40:12 +01:00
mutable left : int ;
mutable right : int ;
kind : int ;
2018-02-14 19:23:23 +01:00
}
2018-02-25 01:40:12 +01:00
let of_int kind right =
{ left = 0 ; right ; kind }
2018-02-14 19:23:23 +01:00
2018-02-25 01:40:12 +01:00
let (<|) z x =
z.left <- z.right;
z.right <- x;
z
2018-01-22 23:19:24 +01:00
2018-02-25 01:40:12 +01:00
let (<<) z x =
z.right <- (z.right lsl 10) lor x ;
z
2018-01-17 18:19:38 +01:00
2018-02-25 01:40:12 +01:00
let (<+) z x =
z.right <- (z.right lsl 15) lor x ;
z
2018-01-17 18:19:38 +01:00
2018-01-19 20:20:19 +01:00
type kind =
2018-02-19 16:01:13 +01:00
| 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)
2018-02-25 01:40:12 +01:00
let of_powers_three { x=a ; y=b ; z=c ; _ } = of_int 3 a <+ b <+ c
2018-02-22 01:38:47 +01:00
let of_powers_six { x=a ; y=b ; z=c ; _ } { x=d ; y=e ; z=f ; _ } =
2018-02-25 01:40:12 +01:00
of_int 6 a << b << c << d << e << f
2018-02-22 01:38:47 +01:00
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 ; _ } =
2018-02-25 01:40:12 +01:00
of_int 12 a << b << c << d << e << f
2018-02-22 01:38:47 +01:00
<| 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 ; _ } =
2018-02-25 01:40:12 +01:00
of_int 9 a << b << c << d << e << f
2018-02-22 01:38:47 +01:00
<| g << h << i
2018-02-19 16:01:13 +01:00
let of_powers a =
2018-01-19 20:20:19 +01:00
match a with
2018-02-25 01:40:12 +01:00
| Three a -> of_powers_three a
| Six (a,b) -> of_powers_six a b
| Twelve (a,b,c,d) -> of_powers_twelve a b c d
| Nine (a,b,c) -> of_powers_nine a b c
2018-02-14 19:23:23 +01:00
2018-01-19 20:20:19 +01:00
2018-02-14 19:23:23 +01:00
let mask10 = 0x3ff
and mask15 = 0x7fff
2018-01-19 20:20:19 +01:00
2018-02-25 01:40:12 +01:00
2018-01-17 18:19:38 +01:00
(** Transform the Zkey into an int array *)
2018-02-25 01:40:12 +01:00
let to_int_array { left ; right ; kind } =
2018-01-17 18:19:38 +01:00
match kind with
2018-02-25 01:40:12 +01:00
| 3 -> [|
2018-02-14 19:23:23 +01:00
mask15 land (right lsr 30) ;
mask15 land (right lsr 15) ;
mask15 land right
|]
2018-02-25 01:40:12 +01:00
| 6 -> [|
2018-02-14 19:23:23 +01:00
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
|]
2018-02-25 01:40:12 +01:00
| 12 -> [|
2018-02-14 19:23:23 +01:00
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
|]
2018-02-25 01:40:12 +01:00
| 9 -> [|
2018-02-14 19:23:23 +01:00
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
|]
2018-02-25 01:40:12 +01:00
| _ -> invalid_arg (__FILE__^": to_int_array")
2018-02-14 19:23:23 +01:00
2018-01-19 20:20:19 +01:00
2018-01-17 18:19:38 +01:00
2018-01-22 23:19:24 +01:00
(** Transform the Zkey into an int tuple *)
2018-02-25 01:40:12 +01:00
let to_powers { left ; right ; kind } =
2018-01-19 20:20:19 +01:00
match kind with
2018-02-25 01:40:12 +01:00
| 3 -> Three (Powers.of_int_tuple (
2018-02-14 19:23:23 +01:00
mask15 land (right lsr 30) ,
mask15 land (right lsr 15) ,
mask15 land right
2018-02-19 16:01:13 +01:00
))
2018-02-14 19:23:23 +01:00
2018-02-25 01:40:12 +01:00
| 6 -> Six (Powers.of_int_tuple
2018-02-14 19:23:23 +01:00
( mask10 land (right lsr 50) ,
mask10 land (right lsr 40) ,
mask10 land (right lsr 30)),
2018-02-25 01:40:12 +01:00
Powers.of_int_tuple
2018-02-14 19:23:23 +01:00
( mask10 land (right lsr 20) ,
mask10 land (right lsr 10) ,
mask10 land right )
)
2018-02-25 01:40:12 +01:00
| 12 -> Twelve (Powers.of_int_tuple
2018-02-14 19:23:23 +01:00
( mask10 land (left lsr 50) ,
mask10 land (left lsr 40) ,
mask10 land (left lsr 30)),
2018-02-19 16:01:13 +01:00
Powers.of_int_tuple
2018-02-14 19:23:23 +01:00
( mask10 land (left lsr 20) ,
mask10 land (left lsr 10) ,
mask10 land left ) ,
2018-02-19 16:01:13 +01:00
Powers.of_int_tuple
2018-02-14 19:23:23 +01:00
( mask10 land (right lsr 50) ,
mask10 land (right lsr 40) ,
mask10 land (right lsr 30)),
2018-02-19 16:01:13 +01:00
Powers.of_int_tuple
2018-02-14 19:23:23 +01:00
( mask10 land (right lsr 20) ,
mask10 land (right lsr 10) ,
mask10 land right )
)
2018-02-25 01:40:12 +01:00
| 9 -> Nine (Powers.of_int_tuple
2018-02-14 19:23:23 +01:00
( mask10 land (left lsr 20) ,
mask10 land (left lsr 10) ,
mask10 land left ) ,
2018-02-19 16:01:13 +01:00
Powers.of_int_tuple
2018-02-14 19:23:23 +01:00
( mask10 land (right lsr 50) ,
mask10 land (right lsr 40) ,
mask10 land (right lsr 30)),
2018-02-19 16:01:13 +01:00
Powers.of_int_tuple
2018-02-14 19:23:23 +01:00
( mask10 land (right lsr 20) ,
mask10 land (right lsr 10) ,
mask10 land right )
)
2018-02-25 01:40:12 +01:00
| _ -> invalid_arg (__FILE__^": to_powers")
2018-02-14 19:23:23 +01:00
2018-02-22 01:48:30 +01:00
let hash = Hashtbl.hash
2018-02-14 19:23:23 +01:00
let equal
2018-02-25 01:40:12 +01:00
{ right = r1 ; left = l1 ; kind = k1 }
{ right = r2 ; left = l2 ; kind = k2 } =
r1 = r2 && l1 = l2 && k1 = k2
2018-02-14 19:23:23 +01:00
let cmp
2018-02-25 01:40:12 +01:00
{ right = r1 ; left = l1 ; kind = k1 }
{ right = r2 ; left = l2 ; kind = k2 } =
if k1 <> k2 then invalid_arg (__FILE__^": cmp");
2018-02-14 19:23:23 +01:00
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
2018-02-25 01:40:12 +01:00
let to_string { left ; right ; kind } =
2018-02-14 19:23:23 +01:00
"< " ^ string_of_int left ^ string_of_int right ^ " | " ^ (
2018-02-25 01:40:12 +01:00
to_int_array { left ; right ; kind }
2018-01-22 23:19:24 +01:00
|> Array.map string_of_int
|> Array.to_list
|> String.concat ", "
) ^ " >"
2018-02-14 20:00:12 +01:00
(*
2018-01-17 18:19:38 +01:00
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
2018-02-14 19:23:23 +01:00
(*
< 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 >
*)
2018-01-17 18:19:38 +01:00
let () = debug ()
2018-02-14 20:00:12 +01:00
*)