text stringlengths 12 786k |
|---|
type bunch = { objs : objdata array ; wp : block Weak . t ; } ; ; |
let data = Array . init size ( fun i -> let n = 1 + random_int size in { objs = Array . make n ( Absent 0 ) ; wp = Weak . create n ; } ) ; ; |
let gccount ( ) = let res = ( Gc . quick_stat ( ) ) . Gc . major_collections in res |
type change = No_change | Fill | Erase ; ; |
let check_and_change data i j = let gc1 = gccount ( ) in let change = match data . ( i ) . objs . ( j ) , Weak . check data . ( i ) . wp j with | Present x , false -> assert false | Absent n , true -> assert ( gc1 <= n + 2 ) ; No_change | Absent _ , false -> Fill ... |
let dummy = ref [ ] ; ; || |
let run index ( ) = let domain_data = Array . init 100 ( fun i -> let n = 1 + random_int 100 in { objs = Array . make n ( Absent 0 ) ; wp = Weak . create n ; } ) in while gccount ( ) < 5 do dummy := Array . make ( random_int 300 ) 0 ; let i = ( random_int ( siz... |
let _ = for index = 0 to 4 do let domains = Array . init ( num_domains - 1 ) ( fun i -> Domain . spawn ( run ( ( i + index ) mod 5 ) ) ) in run ( ( num_domains - 1 + index ) mod 5 ) ( ) ; Array . iter Domain . join domains done ; print_endline " ok " |
module type S = sig type data type t val create : int -> t val clear : t -> unit val merge : t -> data -> data val fold : ( data -> ' a -> ' a ) -> t -> ' a -> ' a val actually_weak : bool end |
module MakeStrong ( H : Hashtbl . HashedType ) = struct module Set = Set . Make ( struct type t = H . t let compare x y = if H . equal x y then 0 else let hx = H . hash x in let hy = H . hash y in hx - hy end ) type data = H . t type t = { mutable set : Set . t } let create n = ... |
module MakeWeak ( H : Hashtbl . HashedType ) = struct let actually_weak = true let limit = 8 type t = { mutable extent : int ; mutable array : H . t Weak . t ; } type data = H . t let is_array xs = Weak . length xs . array <= limit let create n = { extent = 0 ; array = Weak . ... |
module Hashed = struct type t = string list ; ; let equal x y = eprintf " equal : % s / % s \ n " ( List . hd x ) ( List . hd y ) ; x = y ; ; let hash x = Hashtbl . hash ( List . hd x ) ; ; end ; ; |
module HT = Weak . Make ( Hashed ) ; ; |
let tbl = HT . create 7 ; ; |
let r = ref [ ] ; ; |
let bunch = if Array . length Sys . argv < 2 then 10000 else int_of_string Sys . argv . ( 1 ) ; ; |
let random_string n = String . init n ( fun _ -> Char . chr ( 32 + Random . int 95 ) ) ; ; |
let added = ref 0 ; ; |
let mistakes = ref 0 ; ; |
let print_status ( ) = let ( len , entries , sumbuck , buckmin , buckmed , buckmax ) = HT . stats tbl in if entries > bunch * ( ! added + 1 ) then begin if debug then begin printf " \ n ===================\ n " ; printf " len = % d \ n " len ; printf " entries = % d ... |
module type G = sig type t module V : Sig . COMPARABLE val iter_vertex : ( V . t -> unit ) -> t -> unit val iter_succ : ( V . t -> unit ) -> t -> V . t -> unit end |
type ' a element = | Vertex of ' a | Component of ' a * ' a t |
module Make ( G : G ) = struct module HT = Hashtbl . Make ( G . V ) let recursive_scc g root_g = let stack = Stack . create ( ) in let dfn = HT . create 1024 in let num = ref 0 in let partition = ref [ ] in G . iter_vertex ( fun v -> HT . add dfn v 0 ) g ; let rec visit ver... |
module Component = struct type t = { id : int ; kind : kind ; } [ @@ deriving compare , sexp ] and kind = | Node of Cfg . Node . t | Cycle of { head : Cfg . Node . t ; components : t list ; } [ @@ deriving compare , sexp ] end |
type t = Component . t list [ @@ deriving compare , sexp ] |
let create ~ cfg ~ entry_index ~ successors = let depth_first_numbers = Int . Table . create ( ) in let stack = Stack . create ( ) in let current_depth_first_number = ref 0 in let make_depth_first_number ( ) = incr current_depth_first_number ; ! current_depth_first_number in let get_depth_... |
let assert_wto ( ? entry_index = 0 ) cfg_nodes expected_wto = let cfg = Int . Table . create ( ) in let insert_node node = Hashtbl . set cfg ~ key ( : Cfg . Node . id node ) ~ data : node in List . iter ~ f : insert_node cfg_nodes ; let actual_wto = WeakTopologicalOrder . create... |
let cfg_node ( ? predecessors = [ ] ) ( ? successors = [ ] ) id = Cfg . Node . create id Cfg . Node . Normal ( Int . Set . of_list predecessors ) ( Int . Set . of_list successors ) |
let wto_node id = { WeakTopologicalOrder . Component . id = 0 ; kind = Node ( cfg_node id ) } |
let wto_cycle ~ head ~ components = { WeakTopologicalOrder . Component . id = 0 ; kind = Cycle { head = cfg_node head ; components } } |
let test_empty _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ ] ] : [ wto_node 0 ] |
let test_sequential _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ] ~ successors [ : 2 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 3 ] ; cfg_node 3 ~ predecessors [ : 2 ] ~ successors [ ... |
let test_branch _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ] ~ successors [ : 2 ; 3 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 4 ] ; cfg_node 3 ~ predecessors [ : 1 ] ~ successors [... |
let test_nested_branch _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ] ~ successors [ : 2 ; 3 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 4 ; 5 ] ; cfg_node 3 ~ predecessors [ : 1 ] ... |
let test_unreachable _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 2 ] ; cfg_node 1 ~ predecessors [ : 3 ] ~ successors [ : 2 ] ; cfg_node 2 ~ predecessors [ : 0 ; 1 ] ~ successors [ ] ; : cfg_node 3 ~ predecessors [ ] : ~ successors [ ... |
let test_loop _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ; 2 ] ~ successors [ : 2 ; 3 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 1 ] ; cfg_node 3 ~ predecessors [ : 1 ] ~ success... |
let test_loop_with_branch _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ; 5 ] ~ successors [ : 2 ; 6 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 3 ; 4 ] ; cfg_node 3 ~ predecessors [ ... |
let test_nested_loop _ = assert_wto [ cfg_node 0 ~ predecessors [ ] : ~ successors [ : 1 ] ; cfg_node 1 ~ predecessors [ : 0 ; 4 ] ~ successors [ : 2 ; 3 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 4 ; 5 ] ; cfg_node 3 ~ predecessors [ : 1 ... |
let test_bourdon _ = assert_wto ~ entry_index : 1 [ cfg_node 1 ~ predecessors [ ] : ~ successors [ : 2 ] ; cfg_node 2 ~ predecessors [ : 1 ] ~ successors [ : 3 ; 8 ] ; cfg_node 3 ~ predecessors [ : 2 ; 7 ] ~ successors [ : 4 ] ; cfg_node 4 ~ predecessors ... |
let ( ) = " weakTopologicalOrder " >::: [ " empty " >:: test_empty ; " sequential " >:: test_sequential ; " branch " >:: test_branch ; " nested_branch " >:: test_nested_branch ; " unreachable " >:: test_unreachable ; " loop " >:: test_loop ; " loop_with_branch " ... |
type ( ' a , ' b ) t = { entry_by_key : ( ' a , ' b Weak_pointer . t ) Hashtbl . t ; keys_with_unused_data : ' a Thread_safe_queue . t ; mutable thread_safe_run_when_unused_data : unit -> unit } |
module Using_hashable = struct let create ? growth_allowed ? size hashable = { entry_by_key = Hashtbl . Using_hashable . create ~ hashable ? growth_allowed ? size ( ) ; keys_with_unused_data = Thread_safe_queue . create ( ) ; thread_safe_run_when_unused_data = ignore } ; ; end |
let create ? growth_allowed ? size m = Using_hashable . create ? growth_allowed ? size ( Hashtbl . Hashable . of_key m ) ; ; |
let set_run_when_unused_data t ~ thread_safe_f = t . thread_safe_run_when_unused_data <- thread_safe_f ; ; ; |
let remove t key = Hashtbl . remove t . entry_by_key key |
let reclaim_space_for_keys_with_unused_data t = while Thread_safe_queue . length t . keys_with_unused_data > 0 do let key = Thread_safe_queue . dequeue_exn t . keys_with_unused_data in match Hashtbl . find t . entry_by_key key with | None -> ( ) | Some entry -> if Weak_pointer . is_none entry ... |
let get_entry t key = Hashtbl . find_or_add t . entry_by_key key ~ default ( : fun ( ) -> Weak_pointer . create ( ) ) ; ; ; |
let mem t key = match Hashtbl . find t . entry_by_key key with | None -> false | Some entry -> Weak_pointer . is_some entry ; ; |
let key_is_using_space t key = Hashtbl . mem t . entry_by_key key |
let set_data t key entry data = Weak_pointer . set entry data ; Gc . Expert . add_finalizer_last data ( fun ( ) -> Thread_safe_queue . enqueue t . keys_with_unused_data key ; t . thread_safe_run_when_unused_data ( ) ) ; ; ; |
let replace t ~ key ~ data = set_data t key ( get_entry t key ) data |
let add_exn t ~ key ~ data = let entry = get_entry t key in if Weak_pointer . is_some entry then failwiths ~ here [ :% here ] " Weak_hashtbl . add_exn of key in use " t [ % sexp_of : ( _ , _ ) t ] ; set_data t key entry data ; ; ; |
let find t key = match Hashtbl . find t . entry_by_key key with | None -> None | Some entry -> Weak_pointer . get entry ; ; |
let find_or_add t key ~ default = let entry = get_entry t key in match Weak_pointer . get entry with | Some v -> v | None -> let data = default ( ) in set_data t key entry data ; data ; ; |
let create ? growth_allowed ? size hashable = let t = create ? growth_allowed ? size hashable in let reclaim_will_happen = ref false in let reclaim ( ) = reclaim_will_happen := false ; reclaim_space_for_keys_with_unused_data t in set_run_when_unused_data t ~ thread_safe_f ( : fun ( ) -> if not ... |
let reclaim_space_for_keys_with_unused_data ` Do_not_use = assert false |
let set_run_when_unused_data ` Do_not_use = assert false |
type t = out_channel -> LP . doc -> unit |
let ( ) @@ f x = f x |
module Markdown = struct let output_code io = String . iter ( function | ' \ n ' -> output_string io " \ n " | c -> output_char io c ) let code io = function | LP . Str ( _ , str ) -> output_code io str | LP . Ref ( str ) -> fprintf io " <<% s " >> str let chunk io = fun... |
let formats = let add map ( keys , value ) = List . fold_left ( fun m k -> SM . add k value m ) map keys in List . fold_left add SM . empty [ [ " plain " ; " markdown " ] , Markdown . weave ] |
let lookup fmt = try SM . find fmt formats with Not_found -> raise ( NoSuchFormat fmt ) |
let formats = List . map fst @@ SM . bindings formats |
let choose_routers_without_path_builds_paths _ ( ) = let router = Sihl . Web . ( choose [ get " " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " foo " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " bar "... |
let choose_routers_with_empty_scope_builds_paths _ ( ) = let router = Sihl . Web . ( choose ~ scope " " : [ get " " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " " / ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ... |
let choose_routers_with_slash_scope_builds_paths _ ( ) = let router = Sihl . Web . ( choose ~ scope " " :/ [ get " " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " foo " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " ... |
let choose_routers_builds_paths _ ( ) = let router = Sihl . Web . ( choose ~ scope " :/ root " [ get " " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " " / ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; g... |
let choose_nested_routers_builds_paths _ ( ) = let router = Sihl . Web . ( choose ~ scope " :/ root " [ choose ~ scope " : sub " [ get " " ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) ; get " foo " ( fun _ -> Lwt . return @@ Opium . R... |
let externalize_link _ ( ) = let actual = Sihl . Web . externalize_path ~ prefix " : prefix " " foo / bar " in Alcotest . ( check string " prefixes path " " prefix / foo / bar " actual ) ; let actual = Sihl . Web . externalize_path ~ prefix " : prefix " " foo / bar ... |
let find_bearer_token _ ( ) = let token_value = " tokenvalue123 " in let token_header = Format . sprintf " Bearer % s " token_value in let req = Opium . Request . get " / some / path / login " |> Opium . Request . add_header ( " authorization " , token_header ) in let handl... |
let find_bearer_token_with_space _ ( ) = let token_value = " tokenvalue123 and after space " in let token_header = Format . sprintf " Bearer % s " token_value in let req = Opium . Request . get " / some / path / login " |> Opium . Request . add_header ( " authorization " , to... |
let suite = [ ( " router " , [ test_case " choose routers without path builds paths " ` Quick choose_routers_without_path_builds_paths ; test_case " choose routers with empty scope builds paths " ` Quick choose_routers_with_empty_scope_builds_paths ; test_case " choose routers with slash ... |
let ( ) = Logs . set_level ( Sihl . Log . get_log_level ( ) ) ; Logs . set_reporter ( Sihl . Log . cli_reporter ( ) ) ; Lwt_main . run ( Alcotest_lwt . run " web " suite ) ; ; |
type context = { arguments : ( string * string ) list ; connection : Cohttp_lwt_unix . Server . conn ; request : Cohttp . Request . t ; body : Cohttp_lwt . Body . t } |
let headers = let h = Cohttp . Header . init_with " Access - Control - Allow - Origin " " " * in let h = Cohttp . Header . add h " content - type " " application / json " in h |
let string_response ( ? headers = headers ) = Server . respond_string ~ headers |
let error_response ( ? headers = headers ) ( ? status = ` Internal_server_error ) ( errors : Result_util . message list ) : ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t = let error_msg : string = JsonUtil . string_of_write ( JsonUtil . write_list Result_util . ... |
let api_result_response ( ~ string_of_success : ' ok -> string ) : ' ok Api . result -> ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t = function | { Result_util . value = Result . Ok ok ; Result_util . status ; _ } -> let body : string = string_of_success o... |
let kasa_response ~ string_of_success x = api_result_response ~ string_of_success ( Api_common . result_kasa x ) |
let method_not_allowed_respond meths = let headers = Cohttp . Header . add_multi ( Cohttp . Header . init ( ) ) " Allow " ( List . map Cohttp . Code . string_of_method meths ) in Server . respond ~ headers ~ status ` : Method_not_allowed ~ body : Cohttp_lwt . Body . empty ( ... |
let options_respond methods = let meths_str = List . map Cohttp . Code . string_of_method methods in let headers = Cohttp . Header . init_with " Access - Control - Allow - Origin " " " * in let headers = Cohttp . Header . add headers " Access - Control - Allow - Headers " " Conten... |
type ' a route = { path : string ; operation : ' a } |
type route_handler = ( context : context -> ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t ) route |
type route_filter = ( context : context -> chain ( : context : context -> ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t ) -> ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t ) route |
type url_matcher = { re : Re . re ; labels : string list ; route : string } |
let label_prepattern = Re . rep1 ( Re . alt [ Re . alnum ; Re . char ' _ ' ] ) |
let variable_pattern = Re . compile ( Re . seq [ Re . char ' { ' ; label_prepattern ; Re . char ' } ' ] ) |
let label_pattern = Re . compile label_prepattern |
let create_url_matcher ( url : string ) : url_matcher = let labels = Re . matches variable_pattern url in let labels = List . flatten ( List . map ( Re . matches label_pattern ) labels ) in let pattern = Re . split_full variable_pattern url |> List . map ( function | ` Text s -> Re ... |
let rec match_url ( url_matchers : ( ' a * url_matcher ) list ) ( url : string ) : ( ' a * ( string * string ) list ) list = match url_matchers with | ( arg , matcher ) :: tail -> ( try let matching = Re . exec matcher . re url |> Re . Group . all |> Array . to_lis... |
let request_handler context = function | [ ] -> Server . respond_not_found ~ uri ( : Request . uri context . request ) ( ) | [ route , arguments ] -> Lwt . catch ( fun ( ) -> let context = { context with arguments = arguments } in route . operation ~ context ) ( fun ... |
let route_handler ( routes : route_handler list ) : context : context -> ( Cohttp . Response . t * Cohttp_lwt . Body . t ) Lwt . t = let url_matchers : ( route_handler * url_matcher ) list = List . map ( fun route -> ( route , create_url_matcher route . path ) ) routes in f... |
let inputString ( m : input ) : Pla . t = match m with | IContext -> Pla . string " processor . context " | IReal name | IInt name | IBool name -> Pla . string name |
let inputName ( i , acc ) s = match s with | IContext -> i , Pla . string " processor . context " :: acc | _ -> i + 1 , [ % pla { | in_ <# i # i [ > n ] } ] | :: acc |
let performFunctionCall module_name ( config : config ) = let args = List . fold_left inputName ( 0 , [ ] ) config . process_inputs |> snd |> List . rev |> Pla . join_sep Pla . comma in let copy = match config . process_outputs with | [ ] -> Pla . unit | [ _ ] -> let val... |
let noteFunctions ( params : params ) = let module_name = params . module_name in let on_args = Pla . map_sep Pla . comma inputString params . config . noteon_inputs in let off_args = Pla . map_sep Pla . comma inputString params . config . noteoff_inputs in ( [ % pla { | node . note... |
let controlChangeFunction ( params : params ) = let module_name = params . module_name in let ctrl_args = Pla . map_sep Pla . comma inputString params . config . controlchange_inputs in [ % pla { | node . controlChange = function ( control , value , channel ) { processor . <# mod... |
let rec removeContext inputs = match inputs with | IContext :: t -> removeContext t | _ -> inputs |
let get ( params : params ) runtime code : ( Pla . t * FileKind . t ) list = let config = params . config in let module_name = params . module_name in let inputs = removeContext config . process_inputs in let nprocess_inputs = List . length inputs in let nprocess_outputs = List . length c... |
let oslo_polygon = { { | " type " : " Feature " , " geometry " : { " type " : " Polygon " , " coordinates " : [ [ [ 10 . 489165172838884 , 60 . 017259872374645 ] , [ 10 . 580764868996987 , 60 . 0762384207017 ] , [ 10 . 592122568549627 , ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.