text
stringlengths 0
601k
|
---|
let make ? bufsize len default = let buf = Array . make ( match bufsize with None -> len | Some n -> n ) n default in { len = len ; buf = buf ; default = default } default |
let clear x = x . len <- 0 |
let reset x = x . len <- 0 ; x . buf <- Array . make 0 x . default |
let copy x = { len = x . len ; buf = Array . copy x . buf ; default = x . default } default |
let sub x pos len = { len = len ; buf = Array . sub x . buf pos len ; default = x . default } default |
let add_element x e = expand x ( x . len + 1 ) 1 ; x . buf ( . x . len ) len <- e ; x . len <- x . len + 1 |
let add_array x a = expand x ( x . len + Array . length a ) a ; Array . blit a 0 x . buf x . len ( Array . length a ) a ; x . len <- x . len + Array . length a |
let add_xarray x1 x2 = expand x1 ( x1 . len + x2 . len ) len ; Array . blit x2 . buf 0 x1 . buf x1 . len x2 . len ; x1 . len <- x1 . len + x2 . len |
let shrink x len = x . len <- min x . len ( max 0 len ) len |
let append x1 x2 = let buf = Array . make ( x1 . len + x2 . len ) len x1 . default in Array . blit x1 . buf 0 buf 0 x1 . len ; Array . blit x2 . buf 0 buf x1 . len x2 . len ; { len = x1 . len + x2 . len ; buf = buf ; default = x1 . default } default |
let array_of x = Array . sub x . buf 0 x . len |
let iter proc x = for i = 0 to x . len - 1 do proc x . buf ( . i ) i done |
type ' a expr = ( ' a , Biniou . biniou_repr ) Mapping . mapping |
type ' a def = ( ' a , Biniou . biniou_repr ) Mapping . def |
type ' a grouped_defs = ( bool * ' a def list ) list |
type name = ( loc * string ) |
type names = { field_names : name list list ; variant_names : name list list ; } |
let rec extract_names_from_expr acc ( x : ' a expr ) = match x with Unit _ | Bool _ | Int _ | Float _ | String _ -> acc | Sum ( _ , va , _ , _ ) -> let l , ( fn , vn ) = Array . fold_left extract_names_from_variant ( [ ] , acc ) va in ( fn , List . rev l :: vn ) | Record ( _ , fa , _ , _ ) -> let l , ( fn , vn ) = Array . fold_left extract_names_from_field ( [ ] , acc ) fa in ( List . rev l :: fn , vn ) | Tuple ( _ , ca , _ , _ ) -> Array . fold_left extract_names_from_cell acc ca | List ( _ , x , _ , _ ) | Option ( _ , x , _ , _ ) | Nullable ( _ , x , _ , _ ) | Wrap ( _ , x , _ , _ ) -> extract_names_from_expr acc x | Name ( _ , _ , l , _ , _ ) -> List . fold_left extract_names_from_expr acc l | External ( _ , _ , l , _ , _ ) -> List . fold_left extract_names_from_expr acc l | Tvar _ -> acc let l = ( x . var_loc , x . var_cons ) :: l in match x . var_arg with None -> ( l , acc ) | Some x -> ( l , extract_names_from_expr acc x ) let l = ( x . f_loc , x . f_name ) :: l in ( l , extract_names_from_expr acc x . f_value ) extract_names_from_expr acc x . cel_value |
let extract_ocaml_names_from_defs l = let fn , vn = List . fold_left ( fun acc def -> match def . def_value with None -> acc | Some x -> extract_names_from_expr acc x ) ( [ ] , [ ] ) l in { field_names = List . rev fn ; variant_names = List . rev vn ; } |
let check_duplicate_hashes kind l = let tbl = Hashtbl . create 100 in List . iter ( fun ( loc , s ) -> let h = Bi_io . hash_name s in try let loc0 , s0 = Hashtbl . find tbl h in Error . error2 loc0 ( sprintf " Definition of % s % s . " kind s0 ) loc ( sprintf " \ kind s s0 s h ) with Not_found -> Hashtbl . add tbl h ( loc , s ) ) l |
let check_hashes x = List . iter ( check_duplicate_hashes " record field name " ) x . field_names ; List . iter ( check_duplicate_hashes " variant name " ) x . variant_names |
let check ( l : ' a grouped_defs ) = extract_ocaml_names_from_defs ( List . concat_map snd l ) |> check_hashes let tbl = Hashtbl . create ( 2 * List . length l ) in List . iter ( fun s -> let h = Bi_io . hash_name s in let r = try Hashtbl . find tbl h with Not_found -> let r = ref [ ] in Hashtbl . add tbl h r ; r in r := s :: ! r ) l ; let clashes = Hashtbl . fold ( fun h r acc -> let l = ! r in if List . length l >= 2 then List . rev l :: acc else acc ) tbl [ ] in let clashes = List . sort compare clashes in List . iter ( fun l -> print_endline ( String . concat " " l ) ) clashes ) * |
type auth = [ | ` Fingerprint of string | ` Trust_anchor of string |
type t = { version : int ; jid : Xjid . full_jid ; priority : int option ; hostname : string option ; port : int option ; password : string option ; authenticator : auth ; otr_config : Otr . State . config ; dsa : Mirage_crypto_pk . Dsa . priv ; certificate_hostname : string option ; notification_callback : string option ; log_top : bool ; muc_max_stanzas : int option ; } |
let dsa_of_cfg_sexp t = match t with | Sexp . List l -> ( List . fold_left ( fun ( dsa , cfg ) cfg -> function | Sexp . List [ Sexp . Atom " otr_config " ; Sexp . List data ] -> List . fold_left ( fun ( dsa , cfg ) cfg -> function | Sexp . List [ Sexp . Atom " policies " ; _ ] as p -> ( dsa , p :: cfg ) cfg | Sexp . List [ Sexp . Atom " versions " ; _ ] as v -> ( dsa , v :: cfg ) cfg | Sexp . List [ Sexp . Atom " dsa " ; d ] -> ( Some ( Mirage_crypto_pk . Dsa . priv_of_sexp d ) d , cfg ) cfg | _ -> raise ( Invalid_argument " broken sexp while trying to find dsa ) ) " ( dsa , cfg ) cfg data | _ -> ( dsa , cfg ) cfg ) cfg ( None , [ ] ) l ) l | _ -> raise ( Invalid_argument " couldn ' t parse sexp ( dsa ) dsa ) " |
let t_of_sexp dsa t = let dsa , otr_cfg = match dsa , dsa_of_cfg_sexp t with | None , ( Some dsa , cfg ) cfg -> dsa , cfg | Some x , ( _ , cfg ) cfg -> x , cfg | _ -> raise ( Invalid_argument " broken config ) " in let otr_config = Otr . State . config_of_sexp ( Sexp . List otr_cfg ) otr_cfg in match t with | Sexp . List l -> ( match List . fold_left ( fun ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas -> function | Sexp . List [ Sexp . Atom " version " ; v ] -> assert ( ver = None ) None ; let version = int_of_sexp v in ( Some version , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " jid " ; Sexp . Atom v ] -> assert ( jid = None ) None ; let jid = match Xjid . string_to_full_jid v with | None -> raise ( Invalid_argument " parse error in jid ) " | Some x -> x in ( ver , Some jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " priority " ; p ] -> assert ( prio = None ) None ; let prio = option_of_sexp int_of_sexp p in ( ver , jid , Some prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " hostname " ; h ] -> assert ( host = None ) None ; let host = option_of_sexp string_of_sexp h in ( ver , jid , prio , Some host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " port " ; p ] -> assert ( port = None ) None ; let port = match ver with | Some x when x < 3 -> let p = int_of_sexp p in if p = 5222 then None else Some p | _ -> option_of_sexp int_of_sexp p in ( ver , jid , prio , host , Some port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " password " ; password ] -> assert ( pass = None ) None ; let pass = match ver with | Some x when x < 3 -> string_of_sexp password | _ -> raise ( Invalid_argument " password stored in V3 + file ) " in ( ver , jid , prio , host , port , Some pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " trust_anchor " ; trust_anchor ] -> assert ( auth = None ) None ; ( match ver with | Some 0 -> let auth = Some ( ` Trust_anchor ( string_of_sexp trust_anchor ) trust_anchor ) trust_anchor in ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Some 1 -> ( match option_of_sexp string_of_sexp trust_anchor with | None -> ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Some x -> ( ver , jid , prio , host , port , pass , Some ( ` Trust_anchor x ) x , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas ) | _ -> raise ( Invalid_argument " unexpected element : trust_anchor ) " ) | Sexp . List [ Sexp . Atom " tls_fingerprint " ; tls_fp ] -> assert ( auth = None ) None ; ( match ver with | Some 1 -> ( match option_of_sexp string_of_sexp tls_fp with | None -> ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Some x -> ( ver , jid , prio , host , port , pass , Some ( ` Fingerprint x ) x , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas ) | _ -> raise ( Invalid_argument " unexpected element : tls_fingerprint ) " ) | Sexp . List [ Sexp . Atom " authenticator " ; authenticator ] -> assert ( auth = None ) None ; ( ver , jid , prio , host , port , pass , Some ( auth_of_sexp authenticator ) authenticator , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " otr_config " ; _ ] -> ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " certificate_hostname " ; c ] -> assert ( cert = None ) None ; let cert = option_of_sexp string_of_sexp c in ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " notification_callback " ; c ] -> assert ( notification = None ) None ; let notification = option_of_sexp string_of_sexp c in ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " log_top " ; c ] -> let log_top = bool_of_sexp c in ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas | Sexp . List [ Sexp . Atom " muc_max_stanzas " ; p ] -> assert ( muc_max_stanzas = None ) None ; let mms = option_of_sexp int_of_sexp p in ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , mms ) mms | _ -> ( ver , jid , prio , host , port , pass , auth , cert , notification , log_top , muc_max_stanzas ) muc_max_stanzas ) muc_max_stanzas ( None , None , None , None , None , None , None , None , None , false , None ) None l with | Some version , Some jid , Some priority , Some hostname , Some port , password , Some authenticator , certificate_hostname , notification_callback , log_top , muc_max_stanzas -> { version ; jid ; priority ; hostname ; port ; password ; authenticator ; otr_config ; dsa ; certificate_hostname ; notification_callback ; log_top ; muc_max_stanzas } | Some version , Some jid , None , Some hostname , Some port , password , Some authenticator , certificate_hostname , notification_callback , log_top , muc_max_stanzas -> { version ; jid ; priority = None ; hostname ; port ; password ; authenticator ; otr_config ; dsa ; certificate_hostname ; notification_callback ; log_top ; muc_max_stanzas } | Some version , Some jid , Some priority , None , Some port , password , Some authenticator , certificate_hostname , notification_callback , log_top , muc_max_stanzas -> { version ; jid ; priority ; hostname = None ; port ; password ; authenticator ; otr_config ; dsa ; certificate_hostname ; notification_callback ; log_top ; muc_max_stanzas } | Some version , Some jid , None , None , Some port , password , Some authenticator , certificate_hostname , notification_callback , log_top , muc_max_stanzas -> { version ; jid ; priority = None ; hostname = None ; port ; password ; authenticator ; otr_config ; dsa ; certificate_hostname ; notification_callback ; log_top ; muc_max_stanzas } | _ -> raise ( Invalid_argument " broken config ) " ) | _ -> raise ( Invalid_argument " broken config ) " |
let record kvs = Sexp . List List ( . map ( fun ( k , v ) v -> ( Sexp . List [ Sexp . Atom k ; v ] v ) v ) v kvs ) kvs |
let sexp_of_t t = record [ " version " , sexp_of_int t . version ; " jid " , sexp_of_string ( Xjid . full_jid_to_string t . jid ) jid ; " hostname " , sexp_of_option sexp_of_string t . hostname ; " port " , sexp_of_option sexp_of_int t . port ; " priority " , sexp_of_option sexp_of_int t . priority ; " authenticator " , sexp_of_auth t . authenticator ; " otr_config " , Otr . State . sexp_of_config t . otr_config ; " certificate_hostname " , sexp_of_option sexp_of_string t . certificate_hostname ; " notification_callback " , sexp_of_option sexp_of_string t . notification_callback ; " log_top " , sexp_of_bool t . log_top ; " muc_max_stanzas " , sexp_of_option sexp_of_int t . muc_max_stanzas ; ] |
let load_config dsa bytes = t_of_sexp dsa ( Sexp . of_string bytes ) bytes |
let store_config t = Bytes . of_string ( Sexp . to_string_hum ( sexp_of_t t ) t ) t |
let create kind n = FFT . Array1 . create kind fortran_layout n |
let log2 = log 2 . |
let minpow2 l = truncate ( 2 . ( ** ceil ( log ( float l ) . / log2 ) ) ) |
let copy0 ? n ? ofsx ( x : D . vec ) ( ? ofsy = 1 ) ( y : D . vec ) = let n = match n with None -> Array1 . dim x | Some n -> n in for i = 1 to ofsy - 1 do y . { i } <- 0 . done ; ignore ( D . copy ~ n ? ofsx x ~ ofsy ~ y ) ; for i = ofsy + n to Array1 . dim y do y . { i } <- 0 . done |
type scale = Biased | Unbiased | Coeff |
let xcorr ? maxlag ? scale ( a : D . vec ) ( b : D . vec ) = let dima = Array1 . dim a and dimb = Array1 . dim b in if dima < 1 || dimb < 1 then invalid_arg " xcorr : array size < 1 " ; let m = max dima dimb in let maxlag = match maxlag with Some i -> i | None -> m - 1 in let ilag0 = maxlag + 1 in let dim_xcorr = 2 * maxlag + 1 in let idx0 = max m ( maxlag + 1 ) in let dim_correlation = 2 * idx0 - 1 in let fill0 xcorr = for i = 1 to ilag0 - m do xcorr . { i } <- 0 . done ; for i = ilag0 + m to dim_xcorr do xcorr . { i } <- 0 . done ; xcorr in let xcorr = if a == b then begin let n = minpow2 ( 2 * m - 1 ) in let x = create FFT . float n and y = create FFT . complex ( n / 2 + 1 ) in let fft = FFT . Array1 . r2c x y and ifft = FFT . Array1 . c2r y x in copy0 a x ; FFT . exec fft ; for i = 1 to Array1 . dim y do y . { i } <- { Complex . re = Complex . norm2 y . { i } ; im = 0 . } done ; FFT . exec ifft ; D . scal ( 1 . . / float n ) x ; let xcorr = create FFT . float dim_xcorr in let l = min m ( maxlag + 1 ) in ignore ( D . copy ~ n : l x ~ ofsy : ilag0 ~ y : xcorr ) ; ignore ( D . copy ~ n ( : l - 1 ) ~ ofsx ( : n - l + 2 ) x ~ ofsy ( : ilag0 - l + 1 ) ~ y : xcorr ) ; fill0 xcorr end else begin if dima < 10 * dimb && dimb < 10 * dima then ( let n = minpow2 dim_correlation in let n_complex = n / 2 + 1 in let x = create FFT . float n and a ' = create FFT . complex n_complex and b ' = create FFT . complex n_complex in let ffta = FFT . Array1 . r2c x a ' and fftb = FFT . Array1 . r2c x b ' in let ifft = FFT . Array1 . c2r a ' x in copy0 a ~ ofsy : idx0 x ; FFT . exec ffta ; copy0 b x ; FFT . exec fftb ; for i = 1 to n_complex do a ' . { i } <- Complex . mul a ' . { i } ( Complex . conj b ' . { i } ) ; done ; FFT . exec ifft ; D . scal ( 1 . . / float n ) x ; let xcorr = Array1 . sub x ( max 1 ( idx0 - maxlag ) ) dim_xcorr in fill0 xcorr ) else ( let a , b , permuted = if Array1 . dim a >= Array1 . dim b then a , b , false else b , a , true in let dima = Array1 . dim a and dimb = Array1 . dim b in let l = dimb in let lcorr = l + dimb - 1 in let n = minpow2 lcorr in let n_complex = n / 2 + 1 in let x = create FFT . float n and b ' = create FFT . complex n_complex in let fftb = FFT . Array1 . r2c x b ' in copy0 b x ; FFT . exec fftb ; for i = 1 to n_complex do b ' . { i } <- Complex . conj b ' . { i } ; done ; let c = create FFT . float dim_correlation in Array1 . fill c 0 . ; let inc_seg = if permuted then - 1 else 1 in let idx_seg = if permuted then idx0 - l + 2 else idx0 - dimb in let shift_seg = if permuted then ( fun i -> idx_seg - i ) else ( fun i -> idx_seg + i ) in let y = create FFT . complex n_complex in let fft_xy = FFT . Array1 . r2c x y and fft_yx = FFT . Array1 . c2r y x in let i = ref 1 in while ! i <= dima do let l ' = min l ( dima - ! i + 1 ) in copy0 ~ n : l ' ~ ofsx :! i a ~ ofsy : dimb x ; FFT . exec fft_xy ; ignore ( Z . Vec . mul y b ' ~ z : y ) ; FFT . exec fft_yx ; D . axpy x c ~ ofsy ( : shift_seg ! i ) ~ incy : inc_seg ~ n ( : l ' + dimb - 1 ) ; i := ! i + l ; done ; D . scal ( 1 . . / float n ) c ; let xcorr = Array1 . sub c ( max 1 ( idx0 - maxlag ) ) dim_xcorr in fill0 xcorr ) end in ( match scale with | None -> ( ) | Some Biased -> D . scal ( 1 . . / float m ) xcorr | Some Unbiased -> let l = min ( m - 1 ) maxlag in for i = ilag0 - l to ilag0 + l do xcorr . { i } <- xcorr . { i } . / float ( m - abs ( ilag0 - i ) ) done | Some Coeff -> if a == b then D . scal ( 1 . . / xcorr . { ilag0 } ) xcorr else D . scal ( 1 . . / ( D . nrm2 a . * D . nrm2 b ) ) xcorr ) ; xcorr |
let ( ) = Lacaml . Io . pp_float_el_default := ( fun ppf el -> fprintf ppf " . % 4g " el ) |
let pp_fvec ( ? nl = true ) xname x = printf " [ @< 2 >% s = [ " xname ; Lacaml . Io . pp_rfvec std_formatter x ; printf " ] ] " ; @ if nl then printf " @\ n " |
let pp_cvec xname x = printf " [ @< 2 > % s = [ " xname ; Lacaml . Io . pp_rcvec std_formatter x ; printf " ] ] " @ |
let approx_eq ( ? eps = 1e - 10 ) a b = if Vec . dim a <> Vec . dim b then false else begin let eq x y = ( ( y = 0 . && abs_float x <= eps ) || abs_float ( x . - y ) <= eps . * abs_float y ) in let is_eq = ref true in for i = 1 to Vec . dim a do is_eq := ! is_eq && eq a . { i } b . { i } done ; ! is_eq end |
let test text a rep = printf " % s [ @= [ " text ; Lacaml . Io . pp_rfvec std_formatter a ; if approx_eq a rep then printf " ] ] @ ( ok ) @\ n " else ( printf " ] @\ n [ " ; <> Lacaml . Io . pp_rfvec std_formatter rep ; printf " ] ] @@\ n " ) |
let ( ) = let x = Vec . of_array [ | 1 . ; 2 . ; 3 . ; 4 . ] | and y = Vec . of_array [ | 1 . ; 2 . ] | in pp_fvec " x " x ; pp_fvec " y " y ; test " xcorr x x " ( xcorr x x ) ( Vec . of_array [ | 4 . ; 11 . ; 20 . ; 30 . ; 20 . ; 11 . ; 4 . ] ) ; | test " xcorr x x ~ scale : Biased " ( xcorr x x ~ scale : Biased ) ( Vec . of_array [ | 1 . ; 2 . 75 ; 5 . ; 7 . 5 ; 5 . ; 2 . 75 ; 1 . ] ) ; | test " xcorr x x ~ scale : Unbiased " ( xcorr x x ~ scale : Unbiased ) ( Vec . of_array [ | 4 . ; 5 . 5 ; 20 . . / 3 . ; 7 . 5 ; 20 . . / 3 . ; 5 . 5 ; 4 . ] ) ; | let rep = Vec . of_array [ | 0 . 4 ; 1 . 1 ; 2 . ; 3 . ; 2 . ; 1 . 1 ; 0 . 4 ] | in scal ( 1 . . / 3 . ) rep ; test " xcorr x x ~ scale : Coeff " ( xcorr x x ~ scale : Coeff ) rep ; test " xcorr x x ~ maxlag : 5 " ( xcorr x x ~ maxlag : 5 ) ( Vec . of_array [ | 0 . ; 0 . ; 4 . ; 11 . ; 20 . ; 30 . ; 20 . ; 11 . ; 4 . ; 0 . ; 0 . ] ) ; | test " xcorr x x ~ maxlag : 2 " ( xcorr x x ~ maxlag : 2 ) ( Vec . of_array [ | 11 . ; 20 . ; 30 . ; 20 . ; 11 . ] ) ; | test " xcorr x y " ( xcorr x y ) ( Vec . of_array [ | 0 . ; 0 . ; 2 . ; 5 . ; 8 . ; 11 . ; 4 . ] ) ; | test " xcorr x y ~ maxlag : 5 " ( xcorr x y ~ maxlag : 5 ) ( Vec . of_array [ | 0 . ; 0 . ; 0 . ; 0 . ; 2 . ; 5 . ; 8 . ; 11 . ; 4 . ; 0 . ; 0 . ] ) ; | test " xcorr x y ~ maxlag : 2 " ( xcorr x y ~ maxlag : 2 ) ( Vec . of_array [ | 0 . ; 2 . ; 5 . ; 8 . ; 11 . ] ) ; | test " xcorr y x " ( xcorr y x ) ( Vec . of_array [ | 4 . ; 11 . ; 8 . ; 5 . ; 2 . ; 0 . ; 0 . ] ) ; | let z = Vec . init 25 ( fun i -> float i ) in pp_fvec " z " z ; let rep = Vec . of_array [ | 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 0 . ; 4 . ; 11 . ; 20 . ; 30 . ; 40 . ; 50 . ; 60 . ; 70 . ; 80 . ; 90 . ; 100 . ; 110 . ; 120 . ; 130 . ; 140 . ; 150 . ; 160 . ; 170 . ; 180 . ; 190 . ; 200 . ; 210 . ; 220 . ; 230 . ; 240 . ; 146 . ; 74 . ; 25 . ] | in test " xcorr z x " ( xcorr z x ) rep ; |
type t = { env : string -> string option ; win32 : bool ; home_dir : string ; mutable cache_dir : string ; mutable config_dir : string ; mutable data_dir : string ; mutable runtime_dir : string option } |
let ( / ) = Filename . concat |
let make t env_var unix_default win32_default = let default = if t . win32 then win32_default else unix_default in match t . env env_var with | None -> default | Some s when Filename . is_relative s -> default | Some s -> s |
let cache_dir t = let home = t . home_dir in make t " XDG_CACHE_HOME " ( home / " . cache " ) ( home / " Local Settings " / " Cache " ) |
let config_dir t = let home = t . home_dir in make t " XDG_CONFIG_HOME " ( home / " . config " ) ( home / " Local Settings " ) |
let data_dir t = let home = t . home_dir in make t " XDG_DATA_HOME " ( home / " . local " / " share " ) ( match t . env " AppData " with | Some s -> s | None -> " " ) |
let create ? win32 ~ env ( ) = let win32 = match win32 with | None -> Sys . win32 | Some s -> s in let home_dir = match env " HOME " with | Some s -> s | None -> if win32 then match env " AppData " with | None -> " " | Some s -> s else " " in let t = { env ; win32 ; home_dir ; cache_dir = " " ; config_dir = " " ; data_dir = " " ; runtime_dir = None } in t . cache_dir <- cache_dir t ; t . config_dir <- config_dir t ; t . data_dir <- data_dir t ; t . runtime_dir <- env " XDG_RUNTIME_DIR " ; t |
let home_dir t = t . home_dir |
let config_dir t = t . config_dir |
let data_dir t = t . data_dir |
let cache_dir t = t . cache_dir |
let runtime_dir t = t . runtime_dir |
type pos = float * float |
type bounding_box = pos * pos |
type node_layout = { n_name : string ; n_pos : pos ; n_bbox : bounding_box ; n_draw : XDotDraw . operation list ; n_ldraw : XDotDraw . operation list ; } |
type cluster_layout = { c_pos : pos ; c_bbox : bounding_box ; c_draw : XDotDraw . operation list ; c_ldraw : XDotDraw . operation list ; } |
type edge_layout = { e_draw : XDotDraw . operation list ; e_ldraw : XDotDraw . operation list ; e_hdraw : XDotDraw . operation list ; e_tdraw : XDotDraw . operation list ; e_hldraw : XDotDraw . operation list ; e_tldraw : XDotDraw . operation list ; } |
let mk_node_layout ~ name ~ pos ~ bbox ~ draw ~ ldraw = { n_name = name ; n_pos = pos ; n_bbox = bbox ; n_draw = draw ; n_ldraw = ldraw } |
let mk_cluster_layout ~ pos ~ bbox ~ draw ~ ldraw = { c_pos = pos ; c_bbox = bbox ; c_draw = draw ; c_ldraw = ldraw } |
let mk_edge_layout ~ draw ~ ldraw ~ hdraw ~ tdraw ~ hldraw ~ tldraw = { e_draw = draw ; e_ldraw = ldraw ; e_hdraw = hdraw ; e_tdraw = tdraw ; e_hldraw = hldraw ; e_tldraw = tldraw ; } |
let read_pos s = Scanf . sscanf s " % f , % f " ( fun x y -> x , . - y ) |
let bounding_box ( x , y ) w h = let lower_left = x . - w , y . - h in let upper_right = x . + w , y . + h in lower_left , upper_right |
let get_dot_string = function | Dot_ast . String s -> s | Dot_ast . Ident s -> s | Dot_ast . Number s -> s | Dot_ast . Html s -> s |
let read_common_layout mk_layout attr_list = let fold ( ( p , w , h , draw , ldraw ) as attrs ) = function | ( Dot_ast . Ident " pos " ) , Some ( Dot_ast . String s ) -> ( Some s ) , w , h , draw , ldraw | ( Dot_ast . Ident " width " ) , Some ( Dot_ast . String s ) -> p , ( Some s ) , h , draw , ldraw | ( Dot_ast . Ident " height " ) , Some ( Dot_ast . String s ) -> p , w , ( Some s ) , draw , ldraw | ( Dot_ast . Ident " _draw_ " ) , Some ( Dot_ast . String draw ) -> p , w , h , XDotDraw . parse draw , ldraw | ( Dot_ast . Ident " _ldraw_ " ) , Some ( Dot_ast . String ldraw ) -> p , w , h , draw , XDotDraw . parse ldraw | _ -> attrs in let fold_attr acc attr_list = List . fold_left fold acc attr_list in let attrs = List . fold_left fold_attr ( None , None , None , [ ] , [ ] ) attr_list in match attrs with | Some pos , Some w , Some h , draw , ldraw -> let pos = read_pos pos in let coord = bounding_box pos ( float_of_string w ) ( . ( - float_of_string h ) ) in mk_layout ~ pos ~ bbox : coord ~ draw ~ ldraw | _ , _ , _ , draw , ldraw -> let pos = ( 0 . , 0 . ) in let bbox = ( 0 . , 0 . ) , ( 0 . , 0 . ) in mk_layout ~ pos ~ bbox ~ draw ~ ldraw |
let read_node_layout ( id , _ ) attrs = let f = read_common_layout ( fun ~ pos ~ bbox ~ draw ~ ldraw -> mk_node_layout ~ pos ~ bbox ~ draw ~ ldraw ) attrs in f ~ name ( : get_dot_string id ) |
let read_cluster_layout = read_common_layout mk_cluster_layout |
let read_edge_layout attr_list = let draw = ref [ ] in let ldraw = ref [ ] in let hdraw = ref [ ] in let tdraw = ref [ ] in let hldraw = ref [ ] in let tldraw = ref [ ] in let fill_draw_ops = function | ( Dot_ast . Ident " _draw_ " ) , Some ( Dot_ast . String s ) -> draw := XDotDraw . parse s | ( Dot_ast . Ident " _ldraw_ " ) , Some ( Dot_ast . String s ) -> ldraw := XDotDraw . parse s | ( Dot_ast . Ident " _hdraw_ " ) , Some ( Dot_ast . String s ) -> hdraw := XDotDraw . parse s | ( Dot_ast . Ident " _tdraw_ " ) , Some ( Dot_ast . String s ) -> tdraw := XDotDraw . parse s | ( Dot_ast . Ident " _hldraw_ " ) , Some ( Dot_ast . String s ) -> hldraw := XDotDraw . parse s | ( Dot_ast . Ident " _tldraw_ " ) , Some ( Dot_ast . String s ) -> tldraw := XDotDraw . parse s | _ -> ( ) in List . iter ( List . iter fill_draw_ops ) attr_list ; let draw , ldraw = ! draw , ! ldraw in let hdraw , tdraw , hldraw , tldraw = ! hdraw , ! tdraw , ! hldraw , ! tldraw in mk_edge_layout ~ draw ~ ldraw ~ hdraw ~ tdraw ~ hldraw ~ tldraw |
let read_bounding_box str = let x1 , y1 , x2 , y2 = Scanf . sscanf str " % f , % f , % f , % f " ( fun a b c d -> a , b , c , d ) in let lower_left = ( x1 , . - y2 ) and upper_right = x2 , . - y1 in lower_left , upper_right |
module Make ( G : Graphviz . GraphWithDotAttrs ) = struct module HV = Hashtbl . Make ( G . V ) module HE = Map . Make ( struct type t = G . E . t let compare = G . E . compare end ) module HT = Hashtbl . Make ( Util . HTProduct ( Util . HTProduct ( G . V ) ( G . V ) ) ( struct type t = string let equal = ( ) = let hash = Hashtbl . hash end ) ) type graph_layout = { vertex_layouts : node_layout HV . t ; edge_layouts : edge_layout HE . t ; cluster_layouts : ( string , cluster_layout ) Hashtbl . t ; bbox : bounding_box } exception Found of string let get_edge_comment e = let al = G . edge_attributes e in try List . iter ( function ` Comment c -> raise ( Found c ) | _ -> ( ) ) al ; None with Found c -> Some c let get_dot_comment ( al : Dot_ast . attr list ) = try List . iter ( List . iter ( function | Ident " comment " , Some c -> raise ( Found ( get_dot_string c ) ) | _ -> ( ) ) ) al ; " " with Found c -> c let strip_quotes = function | " " -> " " | s -> let len = String . length s in if s . [ 0 ] = ' " ' && s . [ len - 1 ] = ' " ' then String . sub s 1 ( len - 2 ) else s let parse_graph_attr id conv stmts = let read_attr = function | Ident ident , Some ( String attr ) when ident = id -> raise ( Found attr ) | _ -> ( ) in let read_stmt = function | Attr_graph attrs -> List . iter ( List . iter read_attr ) attrs | _ -> ( ) in try List . iter read_stmt stmts ; failwith ( " Could not find the graph attribute named " ^ id ) with Found attr -> conv attr let parse_bounding_box = parse_graph_attr " bb " read_bounding_box let parse_layouts g stmts = let name_to_vertex = Hashtbl . create 97 in let vertices_comment_to_edge = HT . create 97 in let vertex_layouts = HV . create 97 in let edge_layouts = ref HE . empty in let cluster_layouts = Hashtbl . create 97 in G . iter_vertex ( fun v -> let name = strip_quotes ( G . vertex_name v ) in Hashtbl . add name_to_vertex name v ) g ; G . iter_edges_e ( fun e -> let comment = match get_edge_comment e with | Some c -> strip_quotes c | None -> " " in let vs = G . E . src e , G . E . dst e in HT . add vertices_comment_to_edge ( vs , comment ) e ) g ; let find_vertex ( id , _ ) = let name = get_dot_string id in try Hashtbl . find name_to_vertex name with Not_found -> failwith ( " Could not find vertex named " ^ name ) in let find_edge v v ' comment = try HT . find vertices_comment_to_edge ( ( v , v ' ) , comment ) with Not_found -> raise Not_found in let rec collect_layouts cluster stmt = try match stmt with | Node_stmt ( node_id , al ) -> let v = find_vertex node_id in HV . add vertex_layouts v ( read_node_layout node_id al ) | Edge_stmt ( NodeId id , [ NodeId id ' ] , al ) -> let v = find_vertex id in let v ' = find_vertex id ' in let comment = get_dot_comment al in let e = find_edge v v ' comment in edge_layouts := HE . add e ( read_edge_layout al ) ! edge_layouts | Subgraph ( SubgraphDef ( Some id , stmts ) ) -> let cluster = get_dot_string id in List . iter ( collect_layouts ( Some cluster ) ) stmts | Subgraph ( SubgraphDef ( _ , stmts ) ) -> List . iter ( collect_layouts cluster ) stmts | Attr_graph al -> ( match cluster with | Some c -> Hashtbl . add cluster_layouts c ( read_cluster_layout al ) | None -> ( ) ) | _ -> ( ) with Not_found -> ( ) in List . iter ( collect_layouts None ) stmts ; vertex_layouts , edge_layouts , cluster_layouts let parse g dot_ast = let v_layouts , e_layouts , c_layouts = parse_layouts g dot_ast . stmts in let bbox = parse_bounding_box dot_ast . stmts in { vertex_layouts = v_layouts ; edge_layouts = ! e_layouts ; cluster_layouts = c_layouts ; bbox = bbox } exception DotError of string let layout_of_xdot ~ xdot_file g = let dot_ast = Dot . parse_dot_ast xdot_file in parse g dot_ast let layout_of_dot ( ? cmd " = dot " ) ~ dot_file g = let base_name = try Filename . basename ( Filename . chop_extension dot_file ) with Invalid_argument _ -> dot_file in let xdot_file = Filename . temp_file base_name " . xdot " in let dot_cmd = sprintf " % s - Txdot % s > % s " cmd dot_file xdot_file in match Sys . command dot_cmd with | 0 -> let l = layout_of_xdot ~ xdot_file g in Sys . remove xdot_file ; l | _ -> Sys . remove xdot_file ; raise ( DotError " Error during dot execution " ) end |
type pos = float * float |
type align = Left | Center | Right |
type style_attr = | Filled | Invisible | Diagonals | Rounded | Dashed | Dotted | Solid | Bold | StyleString of string |
type operation = | Filled_ellipse of pos * width * height | Unfilled_ellipse of pos * width * height | Filled_polygon of pos array | Unfilled_polygon of pos array | Polyline of pos array | Bspline of pos array | Filled_bspline of pos array | Text of pos * align * width * string | Fill_color of string | Pen_color of string | Font of float * string | Style of style_attr list |
type draw_state = { mutable fill_color : string ; mutable pen_color : string ; mutable font : float * string ; mutable style : style_attr list } |
let default_draw_state ( ) = { fill_color = " # FFFFFF " ; pen_color = " # 000000 " ; font = 0 . , " " ; style = [ ] } |
let set_fill_color st c = st . fill_color <- c |
let set_pen_color st c = st . pen_color <- c |
let set_font st c = st . font <- c |
let set_style st s = st . style <- s |
let suffix s i = try String . sub s i ( String . length s - i ) with Invalid_argument _ -> " " |
let split c s = let rec split_from n = try let p = String . index_from s n c in ( String . sub s n ( p - n ) ) :: ( split_from ( p + 1 ) ) with Not_found -> [ suffix s n ] in if s " " = then [ ] else split_from 0 ; ; |
let string_scale_size ( ~ fontMeasure : fontName : string -> fontSize : int -> string -> ( int * int ) ) font size s = let width , height = fontMeasure ~ fontName : font ~ fontSize ( : int_of_float size ) s in let width = float width in let linear_width = size . * ( float ( String . length s ) ) in size . * width . / linear_width , float height |
let normalize_color s = try let h , s , v = Scanf . sscanf s " % f % f % f " ( fun a b c -> ( a , b , c ) ) in let h ' = 360 . . * h . / 60 . in let hi = truncate h ' mod 6 in let f = h ' . - floor h ' in let p = v . * ( 1 . . - s ) in let q = v . * ( 1 . . - f . * s ) in let t = v . * ( 1 . . - ( 1 . . - f ) . * s ) in let r , g , b = match hi with | 0 -> v , t , p | 1 -> q , v , p | 2 -> p , v , t | 3 -> p , q , v | 4 -> t , p , v | 5 -> v , p , q | _ -> 1 . , 1 . , 1 . in let to_hex x = Printf . sprintf " % 02X " ( truncate ( x . * 255 . ) ) in " " # ^ to_hex r ^ to_hex g ^ to_hex b with Scanf . Scan_failure _ -> s |
type state = { mutable operations : operation list ; mutable cur : int ; str : string ; } |
let mk_state s = { operations = [ ] ; cur = 0 ; str = s } |
let char state = state . str . [ state . cur ] |
let incr state = state . cur <- state . cur + 1 |
let over state = state . cur >= String . length state . str |
let add_operation i state = state . operations <- i :: state . operations |
let get_n n st = let s = String . sub st . str st . cur n in st . cur <- st . cur + n ; s |
let is_space = function | ' ' | ' \ t ' | ' \ n ' -> true | _ -> false |
let is_token = function | " E " | " e " | " P " | " p " | " L " | " B " | " b " | " T " | " C " | " c " | " F " | " S " -> true | _ -> false |
let skip_spaces state = let rec loop ( ) = if not ( over state ) then if is_space ( char state ) then begin incr state ; loop ( ) end in loop ( ) |
let get_word state = skip_spaces state ; let start = state . cur in let rec get ' ( ) = if over state then if start = String . length state . str then None else Some ( String . sub state . str start ( state . cur - start ) ) else if not ( is_space ( char state ) ) then begin incr state ; get ' ( ) end else Some ( String . sub state . str start ( state . cur - start ) ) in get ' ( ) |
let get_op_id state = let tok = get_word state in match tok with | None -> raise NoOperationId | Some tok ' -> if is_token tok ' then tok ' else raise NoOperationId |
let get_int state = match get_word state with | Some w -> begin try int_of_string w with Failure _ -> raise ( ParseError " Cannot parse int " ) end | None -> raise ( ParseError " Cannot parse int " ) |
let get_float state = match get_word state with | Some w -> begin try float_of_string w with Failure _ -> raise ( ParseError " Cannot parse float " ) end | None -> raise ( ParseError " Cannot parse float " ) |
let get_pos state = try let x0 = get_float state in let y0 = get_float state in ( x0 , y0 ) with ParseError _ -> raise ( ParseError " Cannot parse point in position " ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.