text stringlengths 12 786k |
|---|
type ' a tbl = Empty | Node of ' a tbl * ' a data * ' a tbl * int { ident : t ; data : ' a ; previous : ' a data option } |
let mknode l d r = let hl = match l with Empty -> 0 | Node ( _ , _ , _ , h ) -> h and hr = match r with Empty -> 0 | Node ( _ , _ , _ , h ) -> h in Node ( l , d , r , ( if hl >= hr then hl + 1 else hr + 1 ) ) |
let balance l d r = let hl = match l with Empty -> 0 | Node ( _ , _ , _ , h ) -> h and hr = match r with Empty -> 0 | Node ( _ , _ , _ , h ) -> h in if hl > hr + 1 then match l with | Node ( ll , ld , lr , _ ) when ( match ll with Empty -> 0 | Node ( _ , _ ... |
let rec add id data = function Empty -> Node ( Empty , { ident = id ; data = data ; previous = None } , Empty , 1 ) | Node ( l , k , r , h ) -> let c = compare id . name k . ident . name in if c = 0 then Node ( l , { ident = id ; data = data ; previous = Some k ... |
let rec find_stamp s = function None -> raise Not_found | Some k -> if k . ident . stamp = s then k . data else find_stamp s k . previous |
let rec find_same id = function Empty -> raise Not_found | Node ( l , k , r , _ ) -> let c = compare id . name k . ident . name in if c = 0 then if id . stamp = k . ident . stamp then k . data else find_stamp id . stamp k . previous else find_same id ( if c < 0 then l else r ) |
let rec find_name name = function Empty -> raise Not_found | Node ( l , k , r , _ ) -> let c = compare name k . ident . name in if c = 0 then k . data else find_name name ( if c < 0 then l else r ) |
let rec keys_aux stack accu = function Empty -> begin match stack with [ ] -> accu | a :: l -> keys_aux l accu a end | Node ( l , k , r , _ ) -> keys_aux ( l :: stack ) ( k . ident :: accu ) r |
let keys tbl = keys_aux [ ] [ ] tbl |
let print_duration ppf = function | n -> Printf . fprintf ppf " . % 3f s " n |
let print_bytes ppf = function | n when n < 1000 . -> Printf . fprintf ppf " % 3 . 0f B " n | n when n < 1000 . . * 1024 . -> let n = n . / 1024 . in Printf . fprintf ppf " % 3 . * f kB " ( if n < 10 . then 1 else 0 ) n | n when n < 1000 . . * 1024 . ... |
let identify filename = let trace = Reader . open_ ~ filename in let info = Reader . info trace in Printf . printf " Trace file % s ( % a ) \ n " filename print_bytes ( Int64 . to_float ( Reader . size_bytes trace ) ) ; let start_time = Timestamp . to_int64 info . start_time in l... |
let ( ) = if Array . length Sys . argv <> 2 then Printf . fprintf stderr " Usage : % s < trace file >\ n " Sys . executable_name else identify Sys . argv . ( 1 ) |
type t = { symbols : Symbol . Set . t ; variables : Variable . Set . t ; simples : Simple . Set . t ; consts : Reg_width_const . Set . t ; code_ids : Code_id . Set . t ; continuations : Continuation . Set . t } |
let empty = { symbols = Symbol . Set . empty ; variables = Variable . Set . empty ; simples = Simple . Set . empty ; consts = Reg_width_const . Set . empty ; code_ids = Code_id . Set . empty ; continuations = Continuation . Set . empty } |
let create ( ? symbols = Symbol . Set . empty ) ( ? variables = Variable . Set . empty ) ( ? simples = Simple . Set . empty ) ( ? consts = Reg_width_const . Set . empty ) ( ? code_ids = Code_id . Set . empty ) ( ? continuations = Continuation . Set . empty ) (... |
let singleton_variable var = create ~ variables ( : Variable . Set . singleton var ) ( ) |
let singleton_code_id code_id = create ~ code_ids ( : Code_id . Set . singleton code_id ) ( ) |
let singleton_continuation cont = create ~ continuations ( : Continuation . Set . singleton cont ) ( ) |
let singleton_symbol symbol = create ~ symbols ( : Symbol . Set . singleton symbol ) ( ) |
let add_const t const = { t with consts = Reg_width_const . Set . add const t . consts } |
let add_variable t var = { t with variables = Variable . Set . add var t . variables } |
let add_symbol t sym = { t with symbols = Symbol . Set . add sym t . symbols } |
let add_name t name = Name . pattern_match name ~ var ( : add_variable t ) ~ symbol ( : add_symbol t ) |
let add_simple t simple = let simples = if Coercion . is_id ( Simple . coercion simple ) then t . simples else Simple . Set . add simple t . simples in let t = { t with simples } in Simple . pattern_match simple ~ const ( : add_const t ) ~ name ( : fun name ~ coercion : _ -> add_n... |
let add_code_id t code_id = { t with code_ids = Code_id . Set . add code_id t . code_ids } |
let add_continuation t continuation = { t with continuations = Continuation . Set . add continuation t . continuations } |
let from_simple simple = let simples = if Coercion . is_id ( Simple . coercion simple ) then Simple . Set . empty else Simple . Set . singleton simple in Simple . pattern_match simple ~ const ( : fun const -> create ~ simples ~ consts ( : Reg_width_const . Set . singleton const ) ( ... |
let union t1 t2 = { symbols = Symbol . Set . union t1 . symbols t2 . symbols ; variables = Variable . Set . union t1 . variables t2 . variables ; simples = Simple . Set . union t1 . simples t2 . simples ; consts = Reg_width_const . Set . union t1 . consts t2 . consts ; cod... |
let rec union_list ts = match ts with [ ] -> empty | t :: ts -> union t ( union_list ts ) |
module type BaseId = sig type t val equal : t -> t -> bool val compare : t -> t -> int val hash : t -> int val name : t -> string option val to_string : t -> string val output : out_channel -> t -> unit val print : Format . formatter -> t -> unit end |
module type Id = sig include BaseId val create : ? name : string -> unit -> t end |
module type UnitId = sig module Compilation_unit : Identifiable . Thing include BaseId val create : ? name : string -> Compilation_unit . t -> t val unit : t -> Compilation_unit . t end |
module Id ( ) : Id = struct type t = int * string let empty_string = " " let create = let r = ref 0 in fun ( ? name = empty_string ) ( ) -> incr r ; ! r , name let equal ( t1 , _ ) ( t2 , _ ) = ( t1 : int ) = t2 let compare ( t1 , _ ) ( t2 , _ ) = t1 ... |
module UnitId ( Innerid : Id ) ( Compilation_unit : Identifiable . Thing ) : UnitId with module Compilation_unit := Compilation_unit = struct type t = { id : Innerid . t ; unit : Compilation_unit . t ; } let compare x y = let c = Innerid . compare x . id y . id in if c <> 0 then ... |
module Record = struct type t = { a : int ; b : int } end |
module Variant = struct type t = | T of { a : int ; b : int } end if should_swap then { Record . a = b ; b = a } else { Record . a ; b } let a , b = if should_swap then b , a else a , b in { Record . a ; b } if should_swap then Variant . T { a = b ; b = a } else Var... |
let main_desired ~ should_swap ~ a ~ b = let { Record . a ; b } = help_desired ~ should_swap ~ a ~ b in a - b |
let main_wrap ~ should_swap ~ a ~ b = let { Record . a ; b } = help_wrap ~ should_swap ~ a ~ b in a - b |
let main_variant ~ should_swap ~ a ~ b = let ( Variant . T { a ; b } ) = help_variant ~ should_swap ~ a ~ b in a - b |
let main_wrap_variant ~ should_swap ~ a ~ b = let ( Variant . T { a ; b } ) = help_wrap_variant ~ should_swap ~ a ~ b in a - b |
let main_tuple ~ should_swap ~ a ~ b = let a , b = help_tuple ~ should_swap ~ a ~ b in a - b |
let main_interior_if ~ should_swap ~ a ~ b = let { Record . a ; b } = help_interior_if ~ should_swap ~ a ~ b in a - b |
let main_no_if ~ should_swap ~ a ~ b = let { Record . a ; b } = help_no_if ~ should_swap ~ a ~ b in a - b |
let main_no_if_tuple ~ should_swap ~ a ~ b = let a , b = help_no_if_tuple ~ should_swap ~ a ~ b in a - b |
let main_hand_inline ~ should_swap ~ a ~ b = let { Record . a ; b } = if should_swap then { Record . a = b ; b = a } else { Record . a ; b } in a - b |
let main_hand_inline_tuple ~ should_swap ~ a ~ b = let a , b = if should_swap then b , a else a , b in a - b |
module Pixmap = struct open Bigarray type pixmap8 = ( int , int8_unsigned_elt , c_layout ) Array2 . t type pixmap16 = ( int , int16_unsigned_elt , c_layout ) Array2 . t type t = | Pix8 of pixmap8 | Pix16 of pixmap16 let create8 : int -> int -> t = fun w h -> Pix8 ( Array2 . create int... |
type pixmap = | Grey of Pixmap . t | GreyA of Pixmap . t * Pixmap . t | RGB of Pixmap . t * Pixmap . t * Pixmap . t | RGBA of Pixmap . t * Pixmap . t * Pixmap . t * Pixmap . t |
type image = { width : int ; height : int ; max_val : int ; pixels : pixmap } |
module type ReadImage = sig val extensions : string list val size : chunk_reader -> int * int val parsefile : chunk_reader -> image end |
module type ReadImageStreaming = sig include ReadImage type read_state val read_streaming : chunk_reader -> read_state option -> image option * int * read_state option end |
module type WriteImage = sig val write : chunk_writer -> image -> unit end |
let create_rgb ( ? alpha = false ) ( ? max_val = 255 ) width height = if not ( 1 <= max_val && max_val <= 65535 ) then raise ( Invalid_argument " create_rgb : false : ( 1 <= max_val && max_val <= 65535 ) " ) ; if not ( width > 0 && height > 0 ) then raise ( Invalid... |
let create_grey ( ? alpha = false ) ( ? max_val = 255 ) width height = if not ( 1 <= max_val && max_val <= 65535 ) then raise ( Invalid_argument " create_grey : false : ( 1 <= max_val && max_val <= 65535 ) " ) ; if not ( width > 0 && height > 0 ) then raise ( Inval... |
let read_rgba i x y fn = match i . pixels with | RGB ( r , g , b ) -> let r = Pixmap . get r x y in let g = Pixmap . get g x y in let b = Pixmap . get b x y in let a = i . max_val in fn r g b a | RGBA ( r , g , b , a ) -> let r = Pixmap . get r x y in let g = Pixmap . get g x ... |
let read_rgb i x y fn = match i . pixels with | RGB ( r , g , b ) | RGBA ( r , g , b , _ ) -> ( try let r = Pixmap . get r x y in let g = Pixmap . get g x y in let b = Pixmap . get b x y in fn r g b with Invalid_argument _ -> Printf . eprintf " x :% d y :% d \ n % d : % d... |
let read_greya i x y fn = match i . pixels with | RGB ( r , g , b ) -> let r = Pixmap . get r x y in let g = Pixmap . get g x y in let b = Pixmap . get b x y in let g = ( r + g + b ) / 3 in let a = i . max_val in fn g a | RGBA ( r , g , b , a ) -> let r = Pixmap . get r ... |
let read_grey i x y fn = match i . pixels with | RGB ( r , g , b ) | RGBA ( r , g , b , _ ) -> let r = Pixmap . get r x y in let g = Pixmap . get g x y in let b = Pixmap . get b x y in let g = ( r + g + b ) / 3 in fn g | Grey ( g ) | GreyA ( g , _ ) -> let g = P... |
let write_rgba i x y r g b a = match i . pixels with | RGB ( rr , gg , bb ) -> begin Pixmap . set rr x y r ; Pixmap . set gg x y g ; Pixmap . set bb x y b end | RGBA ( rr , gg , bb , aa ) -> begin Pixmap . set rr x y r ; Pixmap . set gg x y g ; Pixmap . set bb x y b ; Pixm... |
let write_rgb i x y r g b = match i . pixels with | RGB ( rr , gg , bb ) | RGBA ( rr , gg , bb , _ ) -> begin Pixmap . set rr x y r ; Pixmap . set gg x y g ; Pixmap . set bb x y b end | Grey ( gg ) | GreyA ( gg , _ ) -> begin let g = ( r + g + b ) / 3 in Pixmap ... |
let write_greya i x y g a = match i . pixels with | RGB ( rr , gg , bb ) -> begin Pixmap . set rr x y g ; Pixmap . set gg x y g ; Pixmap . set bb x y g end | RGBA ( rr , gg , bb , aa ) -> begin Pixmap . set rr x y g ; Pixmap . set gg x y g ; Pixmap . set bb x y g ; Pixmap ... |
let write_grey i x y g = match i . pixels with | RGB ( rr , gg , bb ) | RGBA ( rr , gg , bb , _ ) -> begin Pixmap . set rr x y g ; Pixmap . set gg x y g ; Pixmap . set bb x y g end | Grey ( gg ) | GreyA ( gg , _ ) -> begin Pixmap . set gg x y g end |
let fill_rgb ? alpha i r g b : unit = let fill_rgb ~ rr ~ gg ~ bb = Pixmap . fill rr r ; Pixmap . fill gg g ; Pixmap . fill bb b in match i . pixels , alpha with | RGBA ( rr , gg , bb , aa ) , _ -> fill_rgb ~ rr ~ gg ~ bb ; ( match alpha with | None -> ( ) | Some a -> ... |
let fill_alpha i c = match i . pixels with | Grey _ | RGB _ -> ( ) | RGBA ( _ , _ , _ , aa ) | GreyA ( _ , aa ) -> Pixmap . fill aa c |
let copy i = let open Pixmap in { i with pixels = match i . pixels with | Grey ii -> Grey ( copy ii ) | GreyA ( ii , aa ) -> GreyA ( copy ii , copy aa ) | RGB ( rr , gg , bb ) -> RGB ( copy rr , copy gg , copy bb ) | RGBA ( rr , gg , bb , aa ) -> RGBA ( copy rr ... |
let compare_pixmap p1 p2 = match p1 , p2 with | Grey g1 , Grey g2 -> Pixmap . compare g1 g2 | GreyA ( g1 , a1 ) , GreyA ( g2 , a2 ) -> let diff = Pixmap . compare g1 g2 in if diff <> 0 then diff else Pixmap . compare a1 a2 | RGB ( r1 , g1 , b1 ) , RGB ( r2 , g2 , b2 ) ... |
let compare_image { width ; height ; max_val ; pixels } { width = width2 ; height = height2 ; max_val = max_val2 ; pixels = pixels2 } : int = match Stdlib . compare width width2 , Stdlib . compare height height2 , Stdlib . compare max_val max_val2 with | 0 , 0 , 0 -> compare... |
module Resize : sig val scale_copy_layer : image -> src : image -> float -> image let [ @ inline ] s2rgba gamma ( s : int array ) = let max_val = 255 in let max_valf = 255 . in let exp2linear pixel gamma = Float . pow ( ( float_of_int pixel ) . / max_valf ) gamma in let num = match... |
let max_dimension = 1 lsl 15 |
type errors = [ ` Bmp_error of string | chunk_reader_error ] |
module BmpUtils = struct let bind_result res fn = match res with | Ok v -> fn v | Error e -> Error e let ( ) >>= = bind_result let get_bytes_res ( ich : chunk_reader ) num_bytes : ( string , [ > chunk_reader_error ] ) result = match ( ich ( ` Bytes num_bytes ) ) with | Ok x -> ... |
module FileHeader = struct type t = { typ : string ; image_size : int ; pixel_offset : int ; } let size = 14 let read ( ich : ImageUtil . chunk_reader ) : ( t , [ > errors ] ) result = get_bytes_res ich 2 >>= fun typ -> if typ <> " BM " then Error ( ` Bmp_error " BMP s... |
module Bitfield = struct type t = { shift : int ; len : int ; mask : Int32 . t } let of_mask ( m : Int32 . t ) : ( t , [ > errors ] ) result = let ( land ) = Int32 . logand in let ( lsr ) = Int32 . shift_right_logical in let rec get_shift ( ? acc = 0 ) m = if m ... |
module BitmapMetaData = struct type compression_method = | RGB | Bitfields type bits_per_pixel = | BPP_1 | BPP_4 | BPP_8 | BPP_16 | BPP_24 | BPP_32 module HeaderVersion = struct type t = | Info | V2 | V3 | V4 | V5 let of_size s = match s with | 40 -> Some Info | 52 -> Some V2 | 56 -> Some V3... |
module ReadBMP : ReadImage = struct let extensions = [ " bmp " ; " dib " ] let size ( ich : chunk_reader ) = let meta_res = BitmapMetaData . read ich in ImageUtil . close_chunk_reader ich ; match meta_res with | Ok meta -> meta . info_header . width , meta . info_header . heigh... |
type gif_header_data = { version : string ; image_size : int * int ; global_color_table : bool ; color_resolution : int ; sort : bool ; size_glob_col_tbl : int ; bg_color_index : int ; pix_aspect_ratio : int } |
type decoder_state = { carry_bits : int ; consumed_symbols_of_current_code : int ; consumed_symbols : int ; current_partial_symbol : int ; code_size : int ; min_code_size : int ; } |
let [ @ inline always ] calc_clear_code lzw_min_size = 1 lsl ( lzw_min_size - 1 ) |
let bin_of_str ~ clear_code ( t : decoder_state ) str = let code_size = ref t . code_size in let eoi_code = clear_code + 1 in let len = String . length str in let i = ref 0 in let acc = ref [ ] in let ctr = ref t . carry_bits in let codectr = ref t . consumed_symbols_of_current_code in let ... |
module ReadGIF : sig include ReadImage include ReadImageStreaming let extensions = [ " gif " ] let [ @ inline always ] uint16le ( ? off = 0 ) buf = ( ( int_of_char buf . [ off + 1 ] ) lsl 8 ) lor ( int_of_char buf . [ off ] ) let read_header ( ich : ImageUtil . chu... |
let write ( cw : chunk_writer ) ( original_image : image ) = if original_image . height > 0xffff || original_image . width > 0xffff then raise @@ Invalid_argument ( " Image dimensions too large for GIF " ) ; let module ColorTable : Hashtbl . S with type key = int = Hashtbl . Make (... |
module ReadJPG : ReadImage = struct let extensions = [ " jpg " ; " jpeg " ; " jpe " ; " jif " ; " jfif " ; " jfi " ] let read_marker ich = let ff = chunk_byte ich in if ff <> 0xff then raise ( Corrupted_image " Expected marker . . . " ) ; let rec read_first_n... |
let size ~ extension ich = let ext = String . lowercase_ascii extension in if List . mem ext ImagePNG . extensions then ImagePNG . size ich else if List . mem ext ImagePPM . extensions then ImagePPM . size ich else if List . mem ext ImageXCF . extensions then ImageXCF . size ich else if List . ... |
let openfile ~ extension ich : image = let ext = String . lowercase_ascii extension in if List . mem ext ImagePNG . extensions then ImagePNG . parsefile ich else if List . mem ext ImageGIF . extensions then ImageGIF . parsefile ich else if List . mem ext ImagePPM . extensions then ImagePPM . par... |
let openfile_streaming ~ extension ich state = let if_some f = function | _ , _ , None as x -> x | image , time , Some v -> image , time , Some ( f v ) in match state with | Some ( ` GIF t ) -> if_some ( fun v -> ` GIF v ) ( ImageGIF . read_streaming ich ( Some t ) ) |... |
let writefile ~ extension ( och : ImageUtil . chunk_writer ) i = let extension = String . lowercase_ascii extension in if List . mem extension ImagePNG . extensions then ImagePNG . write och i else if List . mem extension ImageGIF . extensions then ImageGIF . write och i else if List . mem ext... |
let warning fn msg = Printf . eprintf " [ WARNING imagelib ] file % s \ n " fn ; Printf . eprintf " % s \ n " msg ; Printf . eprintf " PNG is the preferred format !\ n " %! |
let convert fn fn ' = let ret = assert ( String . get fn 0 <> ' ' ) ; - assert ( String . get fn ' 0 <> ' ' ) ; - Unix . create_process " convert " [ | " convert " ; fn ; fn ' ] | Unix . stdin Unix . stdout Unix . stderr in if ret <> 0 then raise ( Fai... |
let rm fn = Sys . remove fn |
let size fn = let extension = ( get_extension ' fn ) in let ich = chunk_reader_of_path fn in try ImageLib . size ~ extension ich with | Image . Not_yet_implemented _ -> begin warning fn " No support for image size . . . " ; let fn ' = Filename . temp_file " image " " . png " ... |
let openfile fn : image = let extension = ( get_extension ' fn ) in let ich = chunk_reader_of_path fn in let fallback ( ) = warning fn " Cannot read this image format . . . " ; let fn ' = Filename . temp_file " image " " . png " in convert fn fn ' ; let ich ' = ImageUtil... |
let writefile fn i = let extension = get_extension ' fn in let och = ImageUtil_unix . chunk_writer_of_path fn in let fallback ( ) = warning fn " Cannot write to this image format . . . " ; let fn ' = Filename . temp_file " image " " . png " in ImagePNG . write_png och i ; conv... |
let debug = ref false |
type chunk = { chunk_type : string ; chunk_data : string ; } |
type ihdr_data = { image_size : int * int ; bit_depth : int ; colour_type : int ; compression_method : int ; filter_method : int ; interlace_method : int } |
type pixel = { r : int ; g : int ; b : int } |
module PNG_Zlib : sig val uncompress_string : string -> string val compress_string : string -> string module Decompress = De module Zlib = Zl module Zlib_inflate = Zlib . Inf module Zlib_deflate = Zlib . Def let blit_from_string src src_off dst dst_off len = let open Bigarray . Array1 in for i = 0 to ... |
module PNG_CRC = struct let ( ) >> = Int32 . shift_right_logical let ( ) & = Int32 . logand let ( ) ^ = Int32 . logxor let crc_table = let elem n = let c = ref ( Int32 . of_int n ) in for _ = 0 to 7 do c := ( ! c >> 1 ) ^ ( 0xedb88320l & ( Int32 . succ ( Int32 . ... |
module ReadPNG : ReadImage = struct let extensions = [ " png " ] let read_signature ( ich : ImageUtil . chunk_reader ) = let hdr = get_bytes ich 8 in if String . sub hdr 1 3 = " PNG " then ( if hdr <> png_signature then raise ( Corrupted_image " Corrupted header . . . " ) ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.