text
stringlengths
12
786k
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...
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 . ; ...
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_di...
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 . ...
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 ) -> ...
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 ...
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 ...
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 ....
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 . ...
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 inc...
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 " )
let get_anchor state = let i = get_int state in match i with | - 1 -> Left | 0 -> Center | 1 -> Right | _ -> raise ( ParseError " Cannot parse anchor " )
let parse_bytes st = skip_spaces st ; let n = get_int st in skip_spaces st ; if char st <> ' ' - then raise ( ParseError " Cannot parse bytes " ) else begin incr st ; get_n n st end
let parse_ellipse constr state = let pos = get_pos state in let w = get_float state in let h = get_float state in constr ( pos , w , h )
let invert_y_pos ( x , y ) = ( x , . - y )
let parse_filled_ellipse = parse_ellipse ( fun ( p , w , h ) -> Filled_ellipse ( invert_y_pos p , w , h ) )
let parse_unfilled_ellipse = parse_ellipse ( fun ( p , w , h ) -> Unfilled_ellipse ( invert_y_pos p , w , h ) )
let parse_points state = let n = get_int state in Array . init n ( fun _ -> invert_y_pos ( get_pos state ) )
let parse_filled_polygon state = Filled_polygon ( parse_points state )
let parse_unfilled_polygon state = Unfilled_polygon ( parse_points state )
let parse_polyline state = Polyline ( parse_points state )
let parse_bspline state = Bspline ( parse_points state )
let parse_filled_bspline state = Filled_bspline ( parse_points state )
let parse_text state = let pos = invert_y_pos ( get_pos state ) in let anchor = get_anchor state in let width = get_float state in let str = parse_bytes state in Text ( pos , anchor , width , str )
let parse_fill_color state = Fill_color ( normalize_color ( parse_bytes state ) )
let parse_pen_color state = Pen_color ( normalize_color ( parse_bytes state ) )
let parse_font state = let size = get_float state in let font = parse_bytes state in Font ( size , font )
let parse_style state = let read = function | " filled " -> Filled | " invisible " -> Invisible | " diagonals " -> Diagonals | " rounded " -> Rounded | " dashed " -> Dashed | " dotted " -> Dotted | " solid " -> Solid | " bold " -> Bold | s -> StyleString s in let str ...
let parse_operation state = let operation ( ) = match get_op_id state with | " E " -> parse_filled_ellipse state | " e " -> parse_unfilled_ellipse state | " P " -> parse_filled_polygon state | " p " -> parse_unfilled_polygon state | " L " -> parse_polyline state | " B " -> pars...
let parse_with_state state = let rec loop ( ) = parse_operation state ; if over state then state . operations else loop ( ) in try List . rev ( loop ( ) ) with NoOperationId -> List . rev state . operations
let remove_backslashes s = let buf = Buffer . create 30 in let rec loop i = if i = String . length s then ( ) else if s . [ i ] = ' ' \\ && i < String . length s - 1 && s . [ i + 1 ] = ' \ n ' then loop ( i + 2 ) else begin Buffer . add_char buf s . [ i ] ; ...
let parse s = parse_with_state ( mk_state ( remove_backslashes s ) )
let draw_with ( f : draw_state -> operation -> unit ) operations = let st = default_draw_state ( ) in let draw_op = function | Fill_color c as op -> set_fill_color st c ; f st op | Pen_color c as op -> set_pen_color st c ; f st op | Font ( sty , font ) as op -> set_font st ( sty , font...
let listen = let doc = " Act as a server rather than a client . " in Arg ( . value & flag & info [ " l " ; " listen ] " ~ doc ) doc
let domid = Arg ( . required & pos 0 ( some int ) int None & info ~ docv " : DOMID " ~ doc " : Domain id of the remote endpoint . " [ ] )
let port = let port = Vchan . Port . of_string , fun f p -> Format . fprintf f " % s " ( Vchan . Port . to_string p ) p in let port = Arg . conv port in Arg ( . required & pos 1 ( some port ) port None & info ~ docv " : PORT " ~ doc " : Port id ( unique to this client + server...
let buffer_size = Arg ( . value & opt int 65536 & info ~ docv " : BUFFERSIZE " ~ doc " : Size in bytes of a buffer ( a total of 4 will be created ) created " [ " buffer - size " ] )
let sigint_t , sigint_u = Lwt . task ( )
let proxy buffer_size ( ic , oc ) oc ( stdin , stdout ) stdout = let a_buffer = Bytes . create buffer_size in let b_buffer = Bytes . create buffer_size in let rec proxy buffer a b = Lwt_io . read_into a buffer 0 buffer_size >>= function | 0 -> Lwt . fail End_of_file | n -> Lwt_io . write_from...
let client domid port buffer_size = open_client ~ domid ~ port ~ buffer_size ( ) >>= fun ( ic , oc ) oc -> Printf . fprintf stderr " Connected . \ n " ; %! proxy buffer_size ( ic , oc ) oc ( Lwt_io . stdin , Lwt_io . stdout ) stdout >>= fun ( ) -> Lwt_io . close ic >>= fun (...
let server domid port buffer_size = open_server ~ domid ~ port ~ buffer_size ( ) >>= fun ( ic , oc ) oc -> Printf . fprintf stderr " Connected . \ n " ; %! proxy buffer_size ( ic , oc ) oc ( Lwt_io . stdin , Lwt_io . stdout ) stdout >>= fun ( ) -> Lwt_io . close ic >>= fun (...
let node listen domid port buffer_size : unit = Lwt_main . run ( ( if listen then server else client ) client domid port buffer_size )
let cmd = let doc = " Establish vchan connections " in let man = [ ` S " DESCRIPTION " ; ` P " Establish a connection to a remote Xen domain and transfer data over stdin / stdout , in a similar way to ' nc ' " ; ` S " EXAMPLES " ; ` P " To listen to an incoming connection from domid ...
let ( ) = let ( _ : Lwt_unix . signal_handler_id ) signal_handler_id = Lwt_unix . on_signal Sys . sigint ( fun ( _ : int ) int -> Lwt . wakeup_later sigint_u ( ) ; ) in match Term . eval cmd with ` Error _ -> exit 1 | _ -> exit 0
module Make ( MakeXs : Xs_client_lwt . S ) S = struct type t = { ring_ref : string ; event_channel : string ; } [ @@ deriving sexp ] sexp let write ~ client_domid ~ port t = Xs . make ( ) >>= fun c -> Xs ( . immediate c ( fun h -> read h " domid ) ) " >>= fun server_dom...
type ( ' a , ' b ) result = | Ok of ' a | Error of ' b
module type IO = sig include Cohttp . S . IO val close : ( ic * oc ) -> unit t val open_connection : Uri . t -> ( ( ic * oc ) , exn ) result t val sleep : float -> unit t val gettimeofday : unit -> float end
module Make ( IO : IO ) = struct open IO type ic = IO . ic type oc = IO . oc module Request = Cohttp . Request . Make ( IO ) module Response = Cohttp . Response . Make ( IO ) type t = { uri : Uri . t ; mutable io : ( ic * oc ) option ; } let make uri = { uri = uri ; ...