10
0
mirror of https://github.com/QuantumPackage/qp2.git synced 2024-11-05 05:33:52 +01:00
QuantumPackage/ocaml/Bitlist.ml

134 lines
2.9 KiB
OCaml
Raw Normal View History

2019-01-25 11:39:31 +01:00
open Qptypes
(*
Type for bits strings
=====================
list of Bits
*)
2019-08-21 21:13:19 +02:00
type t = int64 array
let n_int = Array.length
(* Create a zero bit list *)
let zero n_int =
Array.make (N_int_number.to_int n_int) 0L
2019-01-25 11:39:31 +01:00
(* String representation *)
let to_string b =
2019-08-21 21:13:19 +02:00
let int64_to_string x =
String.init 64 (fun i ->
if Int64.logand x @@ Int64.shift_left 1L i <> 0L then
'+'
else
'-')
2019-01-25 11:39:31 +01:00
in
2019-08-21 21:13:19 +02:00
Array.map int64_to_string b
|> Array.to_list
|> String.concat ""
2019-01-25 11:39:31 +01:00
let of_string ?(zero='0') ?(one='1') s =
2019-08-21 21:13:19 +02:00
let n_int = ( (String.length s - 1) lsr 6 ) + 1 in
let result = Array.make n_int 0L in
String.iteri (fun i c ->
if c = one then
begin
let iint = i lsr 6 in (* i / 64 *)
let k = i - (iint lsl 6) in
result.(iint) <- Int64.logor result.(iint) @@ Int64.shift_left 1L k;
end) s;
result
2019-01-25 11:39:31 +01:00
2019-08-21 21:13:19 +02:00
let of_string_mp = of_string ~zero:'-' ~one:'+'
2019-01-25 11:39:31 +01:00
(* Create a bit list from an int64 *)
2019-08-21 21:13:19 +02:00
let of_int64 i = [| i |]
2019-01-25 11:39:31 +01:00
(* Create an int64 from a bit list *)
2019-08-21 21:13:19 +02:00
let to_int64 = function
| [| i |] -> i
| _ -> failwith "N_int > 1"
2019-01-25 11:39:31 +01:00
2019-08-21 21:13:19 +02:00
(* Create a bit list from an array of int64 *)
external of_int64_array : int64 array -> t = "%identity"
external to_int64_array : t -> int64 array = "%identity"
2019-01-25 11:39:31 +01:00
2019-08-21 21:13:19 +02:00
2019-01-25 11:39:31 +01:00
(* Create a bit list from a list of int64 *)
let of_int64_list l =
2019-08-21 21:13:19 +02:00
Array.of_list l |> of_int64_array
2019-01-25 11:39:31 +01:00
(* Compute n_int *)
let n_int_of_mo_num mo_num =
let bit_kind_size = Bit_kind_size.to_int (Lazy.force Qpackage.bit_kind_size) in
N_int_number.of_int ( (mo_num-1)/bit_kind_size + 1 )
(* Create an int64 list from a bit list *)
let to_int64_list l =
2019-08-21 21:13:19 +02:00
to_int64_array l |> Array.to_list
2019-01-25 11:39:31 +01:00
(* Create a bit list from a list of MO indices *)
let of_mo_number_list n_int l =
2019-08-21 21:13:19 +02:00
let result = zero n_int in
List.iter (fun j ->
let i = (MO_number.to_int j) - 1 in
let iint = i lsr 6 in (* i / 64 *)
let k = i - (iint lsl 6) in
result.(iint) <- Int64.logor result.(iint) @@ Int64.shift_left 1L k;
) l;
result
2019-01-25 11:39:31 +01:00
let to_mo_number_list l =
2019-08-21 21:13:19 +02:00
let rec aux_one x shift accu = function
| -1 -> accu
| i -> if Int64.logand x (Int64.shift_left 1L i) <> 0L then
aux_one x shift ( (i+shift) ::accu) (i-1)
else
aux_one x shift accu (i-1)
2019-01-25 11:39:31 +01:00
in
2019-08-21 21:13:19 +02:00
Array.mapi (fun i x ->
let shift = (i lsr 6) lsl 6 + 1 in
aux_one x shift [] 63
) l
|> Array.to_list
|> List.concat
|> List.map MO_number.of_int
2019-01-25 11:39:31 +01:00
(* logical operations on bit_list *)
2019-08-21 21:13:19 +02:00
let and_operator a b = Array.map2 Int64.logand a b
let xor_operator a b = Array.map2 Int64.logxor a b
let or_operator a b = Array.map2 Int64.logor a b
let not_operator b = Array.map Int64.lognot b
2019-01-25 11:39:31 +01:00
2019-08-21 21:13:19 +02:00
let pop_sign =
let mask =
(Int64.pred (Int64.shift_left 1L 63))
in
fun x -> Int64.logand mask x
2019-01-25 11:39:31 +01:00
let popcnt b =
2019-08-21 21:13:19 +02:00
Array.fold_left (fun accu x ->
if x >= 0L then
accu + (Z.popcount @@ Z.of_int64 x)
else
accu + 1 + (Z.popcount @@ Z.of_int64 (pop_sign x))
) 0 b
2019-01-25 11:39:31 +01:00