mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-09 04:35:29 -05:00
merge from master
This commit is contained in:
commit
7d3742e765
21 changed files with 170 additions and 158 deletions
4
Makefile
4
Makefile
|
|
@ -62,9 +62,11 @@ QTESTABLE=$(filter-out $(DONTTEST), \
|
||||||
qtest-clean:
|
qtest-clean:
|
||||||
@rm -rf qtest/
|
@rm -rf qtest/
|
||||||
|
|
||||||
|
QTEST_PREAMBLE='open CCFun;; '
|
||||||
|
|
||||||
qtest-build: qtest-clean build
|
qtest-build: qtest-clean build
|
||||||
@mkdir -p qtest
|
@mkdir -p qtest
|
||||||
@qtest extract -o qtest/qtest_all.ml $(QTESTABLE) 2> /dev/null
|
@qtest extract --preamble $(QTEST_PREAMBLE) -o qtest/qtest_all.ml $(QTESTABLE) 2> /dev/null
|
||||||
@ocamlbuild $(OPTIONS) -pkg oUnit,QTest2Lib \
|
@ocamlbuild $(OPTIONS) -pkg oUnit,QTest2Lib \
|
||||||
-I core -I misc -I string \
|
-I core -I misc -I string \
|
||||||
qtest/qtest_all.native
|
qtest/qtest_all.native
|
||||||
|
|
|
||||||
32
_oasis
32
_oasis
|
|
@ -44,10 +44,10 @@ Library "containers"
|
||||||
Path: core
|
Path: core
|
||||||
Modules: CCVector, CCDeque, CCGen, CCSequence, CCFQueue, CCMultiMap,
|
Modules: CCVector, CCDeque, CCGen, CCSequence, CCFQueue, CCMultiMap,
|
||||||
CCMultiSet, CCBV, CCPrint, CCPersistentHashtbl, CCError,
|
CCMultiSet, CCBV, CCPrint, CCPersistentHashtbl, CCError,
|
||||||
CCHeap, CCList, CCOpt, CCPair, CCFun, CCHash, CCCat,
|
CCHeap, CCList, CCOpt, CCPair, CCFun, CCHash,
|
||||||
CCKList, CCInt, CCBool, CCArray, CCBatch, CCOrd, CCIO,
|
CCKList, CCInt, CCBool, CCArray, CCOrd, CCIO,
|
||||||
CCRandom, CCLinq, CCKTree, CCTrie, CCString, CCHashtbl,
|
CCRandom, CCKTree, CCTrie, CCString, CCHashtbl,
|
||||||
CCFlatHashtbl
|
CCFlatHashtbl, CCSexp
|
||||||
FindlibName: containers
|
FindlibName: containers
|
||||||
|
|
||||||
Library "containers_string"
|
Library "containers_string"
|
||||||
|
|
@ -57,6 +57,14 @@ Library "containers_string"
|
||||||
FindlibName: string
|
FindlibName: string
|
||||||
FindlibParent: containers
|
FindlibParent: containers
|
||||||
|
|
||||||
|
Library "containers_advanced"
|
||||||
|
Path: advanced
|
||||||
|
Pack: true
|
||||||
|
Modules: CCLinq, CCBatch, CCCat
|
||||||
|
FindlibName: advanced
|
||||||
|
FindlibParent: containers
|
||||||
|
BuildDepends: containers
|
||||||
|
|
||||||
Library "containers_pervasives"
|
Library "containers_pervasives"
|
||||||
Path: pervasives
|
Path: pervasives
|
||||||
Modules: CCPervasives
|
Modules: CCPervasives
|
||||||
|
|
@ -70,7 +78,7 @@ Library "containers_misc"
|
||||||
Modules: Cache, FHashtbl, FlatHashtbl, Hashset,
|
Modules: Cache, FHashtbl, FlatHashtbl, Hashset,
|
||||||
Heap, LazyGraph, PersistentGraph,
|
Heap, LazyGraph, PersistentGraph,
|
||||||
PHashtbl, SkipList, SplayTree, SplayMap, Univ,
|
PHashtbl, SkipList, SplayTree, SplayMap, Univ,
|
||||||
Bij, PiCalculus, Bencode, Sexp, RAL,
|
Bij, PiCalculus, Bencode, RAL,
|
||||||
UnionFind, SmallSet, AbsSet, CSM,
|
UnionFind, SmallSet, AbsSet, CSM,
|
||||||
ActionMan, BencodeOnDisk, TTree, PrintBox,
|
ActionMan, BencodeOnDisk, TTree, PrintBox,
|
||||||
HGraph, Automaton, Conv, Bidir, Iteratee, BTree,
|
HGraph, Automaton, Conv, Bidir, Iteratee, BTree,
|
||||||
|
|
@ -140,7 +148,7 @@ Executable benchs
|
||||||
CompiledObject: native
|
CompiledObject: native
|
||||||
Build$: flag(bench)
|
Build$: flag(bench)
|
||||||
MainIs: benchs.ml
|
MainIs: benchs.ml
|
||||||
BuildDepends: containers,containers.string,containers.misc,bench
|
BuildDepends: containers,containers.string,containers.misc,bench,containers.advanced
|
||||||
|
|
||||||
Executable bench_conv
|
Executable bench_conv
|
||||||
Path: benchs/
|
Path: benchs/
|
||||||
|
|
@ -218,12 +226,12 @@ Executable lambda
|
||||||
BuildDepends: containers,containers.misc
|
BuildDepends: containers,containers.misc
|
||||||
|
|
||||||
Executable id_sexp
|
Executable id_sexp
|
||||||
Path: examples/
|
Path: examples/
|
||||||
Install: false
|
Install: false
|
||||||
CompiledObject: native
|
CompiledObject: native
|
||||||
MainIs: id_sexp.ml
|
MainIs: id_sexp.ml
|
||||||
Build$: flag(misc)
|
Build$: flag(misc)
|
||||||
BuildDepends: containers,containers.misc
|
BuildDepends: containers
|
||||||
|
|
||||||
SourceRepository head
|
SourceRepository head
|
||||||
Type: git
|
Type: git
|
||||||
|
|
|
||||||
|
|
@ -173,25 +173,23 @@ module PMap = struct
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let to_list m = m.to_seq |> CCSequence.to_rev_list
|
let to_list m = CCSequence.to_rev_list m.to_seq
|
||||||
|
|
||||||
let to_coll m = Seq m.to_seq
|
let to_coll m = Seq m.to_seq
|
||||||
|
|
||||||
let reverse ~build m =
|
let reverse ~build m =
|
||||||
let build = make ~build () in
|
let build = make ~build () in
|
||||||
to_seq m
|
let seq = CCSequence.map (fun (x,y) -> y,x) (to_seq m) in
|
||||||
|> CCSequence.map (fun (x,y) -> y,x)
|
multimap_of_seq ~build seq
|
||||||
|> multimap_of_seq ~build
|
|
||||||
|
|
||||||
let reverse_multimap ~build m =
|
let reverse_multimap ~build m =
|
||||||
let build = make ~build () in
|
let build = make ~build () in
|
||||||
to_seq m
|
let seq = to_seq m in
|
||||||
|> CCSequence.flatMap
|
let seq = CCSequence.flat_map
|
||||||
(fun (x,l) ->
|
(fun (x,l) -> CCSequence.map (fun y -> y,x) (CCSequence.of_list l)
|
||||||
CCSequence.of_list l
|
) seq
|
||||||
|> CCSequence.map (fun y -> y,x)
|
in
|
||||||
)
|
multimap_of_seq ~build seq
|
||||||
|> multimap_of_seq ~build
|
|
||||||
end
|
end
|
||||||
|
|
||||||
type 'a search_result =
|
type 'a search_result =
|
||||||
|
|
@ -280,7 +278,7 @@ module Coll = struct
|
||||||
| List [] -> fail ()
|
| List [] -> fail ()
|
||||||
| List (x::_) -> x
|
| List (x::_) -> x
|
||||||
| Seq s ->
|
| Seq s ->
|
||||||
begin match CCSequence.take 1 s |> CCSequence.to_list with
|
begin match CCSequence.to_list (CCSequence.take 1 s) with
|
||||||
| [x] -> x
|
| [x] -> x
|
||||||
| _ -> fail ()
|
| _ -> fail ()
|
||||||
end
|
end
|
||||||
|
|
@ -304,15 +302,14 @@ module Coll = struct
|
||||||
with MySurpriseExit -> ()
|
with MySurpriseExit -> ()
|
||||||
|
|
||||||
let take_while p c =
|
let take_while p c =
|
||||||
to_seq c |> _seq_take_while p |> of_seq
|
of_seq (_seq_take_while p (to_seq c))
|
||||||
|
|
||||||
let distinct ~cmp c = set_of_seq ~cmp (to_seq c)
|
let distinct ~cmp c = set_of_seq ~cmp (to_seq c)
|
||||||
|
|
||||||
let sort cmp c = match c with
|
let sort cmp c = match c with
|
||||||
| List l -> List (List.sort cmp l)
|
| List l -> List (List.sort cmp l)
|
||||||
| Seq s -> List (List.sort cmp (CCSequence.to_rev_list s))
|
| Seq s -> List (List.sort cmp (CCSequence.to_rev_list s))
|
||||||
| _ ->
|
| _ -> set_of_seq ~cmp (to_seq c)
|
||||||
to_seq c |> set_of_seq ~cmp
|
|
||||||
|
|
||||||
let search obj c =
|
let search obj c =
|
||||||
let _search_seq obj seq =
|
let _search_seq obj seq =
|
||||||
|
|
@ -327,7 +324,7 @@ module Coll = struct
|
||||||
| None -> obj#failure
|
| None -> obj#failure
|
||||||
| Some x -> x
|
| Some x -> x
|
||||||
in
|
in
|
||||||
to_seq c |> _search_seq obj
|
_search_seq obj (to_seq c)
|
||||||
|
|
||||||
let contains (type elt) ~eq x c = match c with
|
let contains (type elt) ~eq x c = match c with
|
||||||
| List l -> List.exists (eq x) l
|
| List l -> List.exists (eq x) l
|
||||||
|
|
@ -336,17 +333,13 @@ module Coll = struct
|
||||||
let module S = (val m : CCSequence.Set.S
|
let module S = (val m : CCSequence.Set.S
|
||||||
with type elt = elt and type t = 'b) in
|
with type elt = elt and type t = 'b) in
|
||||||
(* XXX: here we don't use the equality relation *)
|
(* XXX: here we don't use the equality relation *)
|
||||||
try
|
S.mem x set
|
||||||
let y = S.find x set in
|
|
||||||
assert (eq x y);
|
|
||||||
true
|
|
||||||
with Not_found -> false
|
|
||||||
|
|
||||||
let do_join ~join c1 c2 =
|
let do_join ~join c1 c2 =
|
||||||
let build1 =
|
let build1 =
|
||||||
to_seq c1
|
let seq = to_seq c1 in
|
||||||
|> CCSequence.map (fun x -> join.join_key1 x, x)
|
let seq = CCSequence.map (fun x -> join.join_key1 x, x) seq in
|
||||||
|> PMap.multimap_of_seq ~build:(PMap.make ~build:join.join_build ())
|
PMap.multimap_of_seq ~build:(PMap.make ~build:join.join_build ()) seq
|
||||||
in
|
in
|
||||||
let l = CCSequence.fold
|
let l = CCSequence.fold
|
||||||
(fun acc y ->
|
(fun acc y ->
|
||||||
|
|
@ -386,9 +379,8 @@ module Coll = struct
|
||||||
let build = PMap.make ~build () in
|
let build = PMap.make ~build () in
|
||||||
to_seq c1 (fun x -> PMap.add build x ());
|
to_seq c1 (fun x -> PMap.add build x ());
|
||||||
to_seq c2 (fun x -> PMap.add build x ());
|
to_seq c2 (fun x -> PMap.add build x ());
|
||||||
PMap.to_seq (PMap.build_get build)
|
let seq = PMap.to_seq (PMap.build_get build) in
|
||||||
|> CCSequence.map fst
|
of_seq (CCSequence.map fst seq)
|
||||||
|> of_seq
|
|
||||||
|
|
||||||
type inter_status =
|
type inter_status =
|
||||||
| InterLeft
|
| InterLeft
|
||||||
|
|
@ -415,9 +407,8 @@ module Coll = struct
|
||||||
to_seq c2 (fun x -> PMap.add build x ());
|
to_seq c2 (fun x -> PMap.add build x ());
|
||||||
let map = PMap.build_get build in
|
let map = PMap.build_get build in
|
||||||
(* output elements of [c1] not in [map] *)
|
(* output elements of [c1] not in [map] *)
|
||||||
to_seq c1
|
let seq = to_seq c1 in
|
||||||
|> CCSequence.filter (fun x -> not (PMap.mem map x))
|
of_seq (CCSequence.filter (fun x -> not (PMap.mem map x)) seq)
|
||||||
|> of_seq
|
|
||||||
end
|
end
|
||||||
|
|
||||||
(** {2 Query operators} *)
|
(** {2 Query operators} *)
|
||||||
|
|
@ -487,7 +478,7 @@ let of_array a =
|
||||||
Start (Coll.of_array a)
|
Start (Coll.of_array a)
|
||||||
|
|
||||||
let of_array_i a =
|
let of_array_i a =
|
||||||
Start (CCSequence.of_array_i a |> Coll.of_seq)
|
Start (Coll.of_seq (CCSequence.of_array_i a))
|
||||||
|
|
||||||
let of_hashtbl h =
|
let of_hashtbl h =
|
||||||
Start (Coll.of_seq (CCSequence.of_hashtbl h))
|
Start (Coll.of_seq (CCSequence.of_hashtbl h))
|
||||||
|
|
@ -496,13 +487,13 @@ let of_seq seq =
|
||||||
Start (Coll.of_seq seq)
|
Start (Coll.of_seq seq)
|
||||||
|
|
||||||
let of_queue q =
|
let of_queue q =
|
||||||
Start (CCSequence.of_queue q |> Coll.of_seq)
|
Start (Coll.of_seq (CCSequence.of_queue q))
|
||||||
|
|
||||||
let of_stack s =
|
let of_stack s =
|
||||||
Start (CCSequence.of_stack s |> Coll.of_seq)
|
Start (Coll.of_seq (CCSequence.of_stack s))
|
||||||
|
|
||||||
let of_string s =
|
let of_string s =
|
||||||
Start (CCSequence.of_str s |> Coll.of_seq)
|
Start (Coll.of_seq (CCSequence.of_str s))
|
||||||
|
|
||||||
(** {6 Execution} *)
|
(** {6 Execution} *)
|
||||||
|
|
||||||
|
|
@ -562,12 +553,11 @@ let _do_unary : type a b. (a,b) unary -> a -> b
|
||||||
| Fold (f, acc) -> Coll.fold f acc c
|
| Fold (f, acc) -> Coll.fold f acc c
|
||||||
| FoldMap (f, acc) -> PMap.fold f acc c
|
| FoldMap (f, acc) -> PMap.fold f acc c
|
||||||
| Reduce (safety, start, mix, stop) ->
|
| Reduce (safety, start, mix, stop) ->
|
||||||
let acc = Coll.to_seq c
|
let acc = CCSequence.fold
|
||||||
|> CCSequence.fold
|
(fun acc x -> match acc with
|
||||||
(fun acc x -> match acc with
|
| None -> Some (start x)
|
||||||
| None -> Some (start x)
|
| Some acc -> Some (mix x acc)
|
||||||
| Some acc -> Some (mix x acc)
|
) None (Coll.to_seq c)
|
||||||
) None
|
|
||||||
in
|
in
|
||||||
begin match acc, safety with
|
begin match acc, safety with
|
||||||
| Some x, Implicit -> stop x
|
| Some x, Implicit -> stop x
|
||||||
|
|
@ -588,13 +578,11 @@ let _do_unary : type a b. (a,b) unary -> a -> b
|
||||||
| Get (Implicit, k) -> PMap.get_exn c k
|
| Get (Implicit, k) -> PMap.get_exn c k
|
||||||
| Get (Explicit, k) -> PMap.get_err c k
|
| Get (Explicit, k) -> PMap.get_err c k
|
||||||
| GroupBy (build,f) ->
|
| GroupBy (build,f) ->
|
||||||
Coll.to_seq c
|
let seq = CCSequence.map (fun x -> f x, x) (Coll.to_seq c) in
|
||||||
|> CCSequence.map (fun x -> f x, x)
|
PMap.multimap_of_seq ~build:(PMap.make ~build ()) seq
|
||||||
|> PMap.multimap_of_seq ~build:(PMap.make ~build ())
|
|
||||||
| Contains (eq, x) -> Coll.contains ~eq x c
|
| Contains (eq, x) -> Coll.contains ~eq x c
|
||||||
| Count build ->
|
| Count build ->
|
||||||
Coll.to_seq c
|
PMap.count_of_seq ~build:(PMap.make ~build ()) (Coll.to_seq c)
|
||||||
|> PMap.count_of_seq ~build:(PMap.make ~build ())
|
|
||||||
| Lazy -> Lazy.force c
|
| Lazy -> Lazy.force c
|
||||||
|
|
||||||
let _do_binary : type a b c. (a, b, c) binary -> a -> b -> c
|
let _do_binary : type a b c. (a, b, c) binary -> a -> b -> c
|
||||||
|
|
@ -706,18 +694,20 @@ module M = struct
|
||||||
Unary (GeneralMap (fun m -> Coll.of_seq m.PMap.to_seq), q)
|
Unary (GeneralMap (fun m -> Coll.of_seq m.PMap.to_seq), q)
|
||||||
|
|
||||||
let flatten q =
|
let flatten q =
|
||||||
let f m = m.PMap.to_seq
|
let f m =
|
||||||
|> CCSequence.flatMap
|
let seq = CCSequence.flat_map
|
||||||
(fun (k,v) -> Coll.to_seq v |> CCSequence.map (fun v' -> k,v'))
|
(fun (k,v) -> CCSequence.map (fun v' -> k,v') (Coll.to_seq v))
|
||||||
|> Coll.of_seq
|
m.PMap.to_seq
|
||||||
|
in Coll.of_seq seq
|
||||||
in
|
in
|
||||||
Unary (GeneralMap f, q)
|
Unary (GeneralMap f, q)
|
||||||
|
|
||||||
let flatten' q =
|
let flatten' q =
|
||||||
let f m = m.PMap.to_seq
|
let f m =
|
||||||
|> CCSequence.flatMap
|
let seq = CCSequence.flatMap
|
||||||
(fun (k,v) -> CCSequence.of_list v |> CCSequence.map (fun v' -> k,v'))
|
(fun (k,v) -> CCSequence.map (fun v' -> k,v') (CCSequence.of_list v))
|
||||||
|> Coll.of_seq
|
m.PMap.to_seq
|
||||||
|
in Coll.of_seq seq
|
||||||
in
|
in
|
||||||
Unary (GeneralMap f, q)
|
Unary (GeneralMap f, q)
|
||||||
|
|
||||||
|
|
@ -895,7 +885,7 @@ let to_array q =
|
||||||
QueryMap ((fun c -> Array.of_list (Coll.to_list c)), q)
|
QueryMap ((fun c -> Array.of_list (Coll.to_list c)), q)
|
||||||
|
|
||||||
let to_seq q =
|
let to_seq q =
|
||||||
QueryMap ((fun c -> Coll.to_seq c |> CCSequence.persistent), q)
|
QueryMap ((fun c -> CCSequence.persistent (Coll.to_seq c)), q)
|
||||||
|
|
||||||
let to_hashtbl q =
|
let to_hashtbl q =
|
||||||
QueryMap ((fun c -> CCSequence.to_hashtbl (Coll.to_seq c)), q)
|
QueryMap ((fun c -> CCSequence.to_hashtbl (Coll.to_seq c)), q)
|
||||||
|
|
@ -919,9 +909,7 @@ module AdaptSet(S : Set.S) = struct
|
||||||
return (Coll.of_seq (fun k -> S.iter k set))
|
return (Coll.of_seq (fun k -> S.iter k set))
|
||||||
|
|
||||||
let to_set q =
|
let to_set q =
|
||||||
let f c =
|
let f c = CCSequence.fold (fun set x -> S.add x set) S.empty (Coll.to_seq c) in
|
||||||
Coll.to_seq c |> CCSequence.fold (fun set x -> S.add x set) S.empty
|
|
||||||
in
|
|
||||||
query_map f q
|
query_map f q
|
||||||
|
|
||||||
let run q = run (to_set q)
|
let run q = run (to_set q)
|
||||||
|
|
@ -944,13 +932,12 @@ module AdaptMap(M : Map.S) = struct
|
||||||
|
|
||||||
let to_map q =
|
let to_map q =
|
||||||
let f c =
|
let f c =
|
||||||
Coll.to_seq c
|
CCSequence.fold (fun m (x,y) -> M.add x y m) M.empty (Coll.to_seq c)
|
||||||
|> CCSequence.fold (fun m (x,y) -> M.add x y m) M.empty
|
|
||||||
in
|
in
|
||||||
query_map f q
|
query_map f q
|
||||||
|
|
||||||
let run q = run (q |> to_map)
|
let run q = run (to_map q)
|
||||||
let run_exn q = run_exn (q |> to_map)
|
let run_exn q = run_exn (to_map q)
|
||||||
end
|
end
|
||||||
|
|
||||||
module IO = struct
|
module IO = struct
|
||||||
|
|
@ -1017,20 +1004,21 @@ module IO = struct
|
||||||
|
|
||||||
let lines q =
|
let lines q =
|
||||||
(* sequence of lines *)
|
(* sequence of lines *)
|
||||||
let f s = _lines s 0 |> Coll.of_seq in
|
let f s = Coll.of_seq (_lines s 0) in
|
||||||
query_map f q
|
query_map f q
|
||||||
|
|
||||||
let lines' q =
|
let lines' q =
|
||||||
let f s = lazy (_lines s 0 |> CCSequence.to_list) in
|
let f s = lazy (CCSequence.to_list (_lines s 0)) in
|
||||||
lazy_ (query_map f q)
|
lazy_ (query_map f q)
|
||||||
|
|
||||||
let _join ~sep ?(stop="") l =
|
let _join ~sep ?(stop="") l =
|
||||||
let buf = Buffer.create 128 in
|
let buf = Buffer.create 128 in
|
||||||
Coll.to_seq l
|
let seq = Coll.to_seq l in
|
||||||
|> CCSequence.iteri
|
CCSequence.iteri
|
||||||
(fun i x ->
|
(fun i x ->
|
||||||
if i>0 then Buffer.add_string buf sep;
|
if i>0 then Buffer.add_string buf sep;
|
||||||
Buffer.add_string buf x);
|
Buffer.add_string buf x)
|
||||||
|
seq;
|
||||||
Buffer.add_string buf stop;
|
Buffer.add_string buf stop;
|
||||||
Buffer.contents buf
|
Buffer.contents buf
|
||||||
|
|
||||||
|
|
@ -1043,12 +1031,11 @@ module IO = struct
|
||||||
lazy_ (query_map f q)
|
lazy_ (query_map f q)
|
||||||
|
|
||||||
let out oc q =
|
let out oc q =
|
||||||
run_exn q |> output_string oc
|
output_string oc (run_exn q)
|
||||||
|
|
||||||
let out_lines oc q =
|
let out_lines oc q =
|
||||||
run_exn q
|
let x = run_exn q in
|
||||||
|> Coll.to_seq
|
CCSequence.iter (fun l -> output_string oc l; output_char oc '\n') (Coll.to_seq x)
|
||||||
|> CCSequence.iter (fun l -> output_string oc l; output_char oc '\n')
|
|
||||||
|
|
||||||
let to_file_exn filename q =
|
let to_file_exn filename q =
|
||||||
_with_file_out filename (fun oc -> out oc q)
|
_with_file_out filename (fun oc -> out oc q)
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
(** benchmark CCBatch *)
|
(** benchmark CCBatch *)
|
||||||
|
|
||||||
|
open Containers_advanced
|
||||||
|
|
||||||
module type COLL = sig
|
module type COLL = sig
|
||||||
val name : string
|
val name : string
|
||||||
include CCBatch.COLLECTION
|
include CCBatch.COLLECTION
|
||||||
|
|
|
||||||
|
|
@ -78,6 +78,7 @@ let () =
|
||||||
Printf.printf "list of 5 elements...\n";
|
Printf.printf "list of 5 elements...\n";
|
||||||
bench_list [1,2; 3,4; 5,6; 7,8; 9,10];
|
bench_list [1,2; 3,4; 5,6; 7,8; 9,10];
|
||||||
|
|
||||||
|
let open CCFun in
|
||||||
let l = CCGen.(1 -- 100 |> map (fun x->x,x) |> to_rev_list) in
|
let l = CCGen.(1 -- 100 |> map (fun x->x,x) |> to_rev_list) in
|
||||||
Printf.printf "list of %d elements...\n" (List.length l);
|
Printf.printf "list of %d elements...\n" (List.length l);
|
||||||
bench_list l;
|
bench_list l;
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ let rec eq t1 t2 = match t1, t2 with
|
||||||
let rec hash_tree t h = match t with
|
let rec hash_tree t h = match t with
|
||||||
| Empty -> CCHash.string_ "empty" h
|
| Empty -> CCHash.string_ "empty" h
|
||||||
| Node (i, l) ->
|
| Node (i, l) ->
|
||||||
h |> CCHash.string_ "node" |> CCHash.int_ i |> CCHash.list_ hash_tree l
|
CCHash.list_ hash_tree l (CCHash.int_ i (CCHash.string_ "node" h))
|
||||||
|
|
||||||
module Box = Containers_misc.PrintBox
|
module Box = Containers_misc.PrintBox
|
||||||
|
|
||||||
|
|
|
||||||
18
core/CCIO.ml
18
core/CCIO.ml
|
|
@ -471,17 +471,17 @@ module File = struct
|
||||||
if Sys.is_directory d
|
if Sys.is_directory d
|
||||||
then
|
then
|
||||||
let arr = Sys.readdir d in
|
let arr = Sys.readdir d in
|
||||||
Seq.of_array arr
|
Seq.map_pure make (Seq.of_array arr)
|
||||||
|> Seq.map_pure make
|
|
||||||
else Seq.empty
|
else Seq.empty
|
||||||
|
|
||||||
let rec _walk d () =
|
let rec _walk d () =
|
||||||
if Sys.is_directory d
|
if Sys.is_directory d
|
||||||
then
|
then
|
||||||
let arr = Sys.readdir d in
|
let arr = Sys.readdir d in
|
||||||
let tail = Seq.of_array arr
|
let tail = Seq.of_array arr in
|
||||||
|> Seq.flat_map
|
let tail = Seq.flat_map
|
||||||
(fun s -> return (_walk (Filename.concat d s) ()))
|
(fun s -> return (_walk (Filename.concat d s) ()))
|
||||||
|
tail
|
||||||
in Seq.cons (`Dir,d) tail
|
in Seq.cons (`Dir,d) tail
|
||||||
else Seq.singleton (`File, d)
|
else Seq.singleton (`File, d)
|
||||||
|
|
||||||
|
|
@ -501,14 +501,14 @@ module File = struct
|
||||||
if Sys.is_directory d
|
if Sys.is_directory d
|
||||||
then
|
then
|
||||||
let arr = Sys.readdir d in
|
let arr = Sys.readdir d in
|
||||||
Seq.of_array arr
|
let arr = Seq.of_array arr in
|
||||||
|> Seq.map_pure (fun s -> Filename.concat d s)
|
let arr = Seq.map_pure (fun s -> Filename.concat d s) arr in
|
||||||
|> Seq.flat_map
|
Seq.flat_map
|
||||||
(fun s ->
|
(fun s ->
|
||||||
if Sys.is_directory s
|
if Sys.is_directory s
|
||||||
then return (_read_dir_rec s ())
|
then return (_read_dir_rec s ())
|
||||||
else return (Seq.singleton s)
|
else return (Seq.singleton s)
|
||||||
)
|
) arr
|
||||||
else Seq.empty
|
else Seq.empty
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -178,7 +178,7 @@ let find ?pset f t =
|
||||||
| None -> _find_kl f l'
|
| None -> _find_kl f l'
|
||||||
| Some _ as res -> res
|
| Some _ as res -> res
|
||||||
in
|
in
|
||||||
bfs ?pset t |> _find_kl f
|
_find_kl f (bfs ?pset t)
|
||||||
|
|
||||||
(** {2 Pretty printing in the DOT (graphviz) format} *)
|
(** {2 Pretty printing in the DOT (graphviz) format} *)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,9 +29,10 @@ type 'a or_error = [ `Ok of 'a | `Error of string ]
|
||||||
type 'a sequence = ('a -> unit) -> unit
|
type 'a sequence = ('a -> unit) -> unit
|
||||||
type 'a gen = unit -> 'a option
|
type 'a gen = unit -> 'a option
|
||||||
|
|
||||||
type t =
|
type t = [
|
||||||
| Atom of string
|
| `Atom of string
|
||||||
| List of t list
|
| `List of t list
|
||||||
|
]
|
||||||
|
|
||||||
let equal a b = a = b
|
let equal a b = a = b
|
||||||
|
|
||||||
|
|
@ -39,19 +40,21 @@ let compare a b = Pervasives.compare a b
|
||||||
|
|
||||||
let hash a = Hashtbl.hash a
|
let hash a = Hashtbl.hash a
|
||||||
|
|
||||||
let of_int x = Atom (string_of_int x)
|
let of_int x = `Atom (string_of_int x)
|
||||||
let of_float x = Atom (string_of_float x)
|
let of_float x = `Atom (string_of_float x)
|
||||||
let of_bool x = Atom (string_of_bool x)
|
let of_bool x = `Atom (string_of_bool x)
|
||||||
let of_string x = Atom x
|
let atom x = `Atom x
|
||||||
let of_unit = List []
|
let of_unit = `List []
|
||||||
let of_list l = List l
|
let of_list l = `List l
|
||||||
let of_pair (x,y) = List[x;y]
|
let of_rev_list l = `List (List.rev l)
|
||||||
let of_triple (x,y,z) = List[x;y;z]
|
let of_pair (x,y) = `List[x;y]
|
||||||
|
let of_triple (x,y,z) = `List[x;y;z]
|
||||||
|
let of_quad (x,y,z,u) = `List[x;y;z;u]
|
||||||
|
|
||||||
let of_variant name args = List (Atom name :: args)
|
let of_variant name args = `List (`Atom name :: args)
|
||||||
let of_field name t = List [Atom name; t]
|
let of_field name t = `List [`Atom name; t]
|
||||||
let of_record l =
|
let of_record l =
|
||||||
List (List.map (fun (n,x) -> of_field n x) l)
|
`List (List.map (fun (n,x) -> of_field n x) l)
|
||||||
|
|
||||||
let _with_in filename f =
|
let _with_in filename f =
|
||||||
let ic = open_in filename in
|
let ic = open_in filename in
|
||||||
|
|
@ -89,11 +92,11 @@ let _must_escape s =
|
||||||
with Exit -> true
|
with Exit -> true
|
||||||
|
|
||||||
let rec to_buf b t = match t with
|
let rec to_buf b t = match t with
|
||||||
| Atom s when _must_escape s -> Printf.bprintf b "\"%s\"" (String.escaped s)
|
| `Atom s when _must_escape s -> Printf.bprintf b "\"%s\"" (String.escaped s)
|
||||||
| Atom s -> Buffer.add_string b s
|
| `Atom s -> Buffer.add_string b s
|
||||||
| List [] -> Buffer.add_string b "()"
|
| `List [] -> Buffer.add_string b "()"
|
||||||
| List [x] -> Printf.bprintf b "(%a)" to_buf x
|
| `List [x] -> Printf.bprintf b "(%a)" to_buf x
|
||||||
| List l ->
|
| `List l ->
|
||||||
Buffer.add_char b '(';
|
Buffer.add_char b '(';
|
||||||
List.iteri
|
List.iteri
|
||||||
(fun i t' -> (if i > 0 then Buffer.add_char b ' '; to_buf b t'))
|
(fun i t' -> (if i > 0 then Buffer.add_char b ' '; to_buf b t'))
|
||||||
|
|
@ -106,11 +109,11 @@ let to_string t =
|
||||||
Buffer.contents b
|
Buffer.contents b
|
||||||
|
|
||||||
let rec print fmt t = match t with
|
let rec print fmt t = match t with
|
||||||
| Atom s when _must_escape s -> Format.fprintf fmt "\"%s\"" (String.escaped s)
|
| `Atom s when _must_escape s -> Format.fprintf fmt "\"%s\"" (String.escaped s)
|
||||||
| Atom s -> Format.pp_print_string fmt s
|
| `Atom s -> Format.pp_print_string fmt s
|
||||||
| List [] -> Format.pp_print_string fmt "()"
|
| `List [] -> Format.pp_print_string fmt "()"
|
||||||
| List [x] -> Format.fprintf fmt "@[<hov2>(%a)@]" print x
|
| `List [x] -> Format.fprintf fmt "@[<hov2>(%a)@]" print x
|
||||||
| List l ->
|
| `List l ->
|
||||||
Format.open_hovbox 2;
|
Format.open_hovbox 2;
|
||||||
Format.pp_print_char fmt '(';
|
Format.pp_print_char fmt '(';
|
||||||
List.iteri
|
List.iteri
|
||||||
|
|
@ -120,11 +123,11 @@ let rec print fmt t = match t with
|
||||||
Format.close_box ()
|
Format.close_box ()
|
||||||
|
|
||||||
let rec print_noindent fmt t = match t with
|
let rec print_noindent fmt t = match t with
|
||||||
| Atom s when _must_escape s -> Format.fprintf fmt "\"%s\"" (String.escaped s)
|
| `Atom s when _must_escape s -> Format.fprintf fmt "\"%s\"" (String.escaped s)
|
||||||
| Atom s -> Format.pp_print_string fmt s
|
| `Atom s -> Format.pp_print_string fmt s
|
||||||
| List [] -> Format.pp_print_string fmt "()"
|
| `List [] -> Format.pp_print_string fmt "()"
|
||||||
| List [x] -> Format.fprintf fmt "(%a)" print_noindent x
|
| `List [x] -> Format.fprintf fmt "(%a)" print_noindent x
|
||||||
| List l ->
|
| `List l ->
|
||||||
Format.pp_print_char fmt '(';
|
Format.pp_print_char fmt '(';
|
||||||
List.iteri
|
List.iteri
|
||||||
(fun i t' -> (if i > 0 then Format.pp_print_char fmt ' '; print_noindent fmt t'))
|
(fun i t' -> (if i > 0 then Format.pp_print_char fmt ' '; print_noindent fmt t'))
|
||||||
|
|
@ -468,7 +471,7 @@ let _error ps msg =
|
||||||
let rec _next ps : t partial_result =
|
let rec _next ps : t partial_result =
|
||||||
match Lexer.next ps.ps_d with
|
match Lexer.next ps.ps_d with
|
||||||
| `Ok (Lexer.Atom s) ->
|
| `Ok (Lexer.Atom s) ->
|
||||||
_push ps (Atom s)
|
_push ps (`Atom s)
|
||||||
| `Ok Lexer.Open ->
|
| `Ok Lexer.Open ->
|
||||||
ps.ps_stack <- [] :: ps.ps_stack;
|
ps.ps_stack <- [] :: ps.ps_stack;
|
||||||
_next ps
|
_next ps
|
||||||
|
|
@ -477,7 +480,7 @@ let rec _next ps : t partial_result =
|
||||||
| [] -> _error ps "unbalanced ')'"
|
| [] -> _error ps "unbalanced ')'"
|
||||||
| l :: stack ->
|
| l :: stack ->
|
||||||
ps.ps_stack <- stack;
|
ps.ps_stack <- stack;
|
||||||
_push ps (List (List.rev l))
|
_push ps (`List (List.rev l))
|
||||||
end
|
end
|
||||||
| `Error msg -> `Error msg
|
| `Error msg -> `Error msg
|
||||||
| `Await -> `Await
|
| `Await -> `Await
|
||||||
|
|
@ -598,8 +601,8 @@ module Traverse = struct
|
||||||
| None -> _list_any f tl
|
| None -> _list_any f tl
|
||||||
|
|
||||||
let list_any f e = match e with
|
let list_any f e = match e with
|
||||||
| Atom _ -> None
|
| `Atom _ -> None
|
||||||
| List l -> _list_any f l
|
| `List l -> _list_any f l
|
||||||
|
|
||||||
let rec _list_all f acc l = match l with
|
let rec _list_all f acc l = match l with
|
||||||
| [] -> List.rev acc
|
| [] -> List.rev acc
|
||||||
|
|
@ -609,12 +612,12 @@ module Traverse = struct
|
||||||
| None -> _list_all f acc tl
|
| None -> _list_all f acc tl
|
||||||
|
|
||||||
let list_all f e = match e with
|
let list_all f e = match e with
|
||||||
| Atom _ -> []
|
| `Atom _ -> []
|
||||||
| List l -> _list_all f [] l
|
| `List l -> _list_all f [] l
|
||||||
|
|
||||||
let _try_atom e f = match e with
|
let _try_atom e f = match e with
|
||||||
| List _ -> None
|
| `List _ -> None
|
||||||
| Atom x -> try Some (f x) with _ -> None
|
| `Atom x -> try Some (f x) with _ -> None
|
||||||
|
|
||||||
let to_int e = _try_atom e int_of_string
|
let to_int e = _try_atom e int_of_string
|
||||||
let to_bool e = _try_atom e bool_of_string
|
let to_bool e = _try_atom e bool_of_string
|
||||||
|
|
@ -622,25 +625,25 @@ module Traverse = struct
|
||||||
let to_string e = _try_atom e (fun x->x)
|
let to_string e = _try_atom e (fun x->x)
|
||||||
|
|
||||||
let to_pair e = match e with
|
let to_pair e = match e with
|
||||||
| List [x;y] -> Some (x,y)
|
| `List [x;y] -> Some (x,y)
|
||||||
| _ -> None
|
| _ -> None
|
||||||
|
|
||||||
let to_triple e = match e with
|
let to_triple e = match e with
|
||||||
| List [x;y;z] -> Some (x,y,z)
|
| `List [x;y;z] -> Some (x,y,z)
|
||||||
| _ -> None
|
| _ -> None
|
||||||
|
|
||||||
let to_list e = match e with
|
let to_list e = match e with
|
||||||
| List l -> Some l
|
| `List l -> Some l
|
||||||
| Atom _ -> None
|
| `Atom _ -> None
|
||||||
|
|
||||||
let rec _get_field name l = match l with
|
let rec _get_field name l = match l with
|
||||||
| List [Atom n; x] :: _ when name=n -> Some x
|
| `List [`Atom n; x] :: _ when name=n -> Some x
|
||||||
| _ :: tl -> _get_field name tl
|
| _ :: tl -> _get_field name tl
|
||||||
| [] -> None
|
| [] -> None
|
||||||
|
|
||||||
let get_field name e = match e with
|
let get_field name e = match e with
|
||||||
| List l -> _get_field name l
|
| `List l -> _get_field name l
|
||||||
| Atom _ -> None
|
| `Atom _ -> None
|
||||||
|
|
||||||
let field name f e =
|
let field name f e =
|
||||||
get_field name e >>= f
|
get_field name e >>= f
|
||||||
|
|
@ -651,9 +654,9 @@ module Traverse = struct
|
||||||
| _ :: tl -> _get_variant s args tl
|
| _ :: tl -> _get_variant s args tl
|
||||||
|
|
||||||
let get_variant l e = match e with
|
let get_variant l e = match e with
|
||||||
| List (Atom s :: args) -> _get_variant s args l
|
| `List (`Atom s :: args) -> _get_variant s args l
|
||||||
| List _ -> None
|
| `List _ -> None
|
||||||
| Atom s -> _get_variant s [] l
|
| `Atom s -> _get_variant s [] l
|
||||||
|
|
||||||
let get_exn e = match e with
|
let get_exn e = match e with
|
||||||
| None -> failwith "Sexp.Traverse.get_exn"
|
| None -> failwith "Sexp.Traverse.get_exn"
|
||||||
|
|
@ -33,26 +33,30 @@ type 'a gen = unit -> 'a option
|
||||||
|
|
||||||
(** {2 Basics} *)
|
(** {2 Basics} *)
|
||||||
|
|
||||||
type t =
|
type t = [
|
||||||
| Atom of string
|
| `Atom of string
|
||||||
| List of t list
|
| `List of t list
|
||||||
|
]
|
||||||
|
|
||||||
val equal : t -> t -> bool
|
val equal : t -> t -> bool
|
||||||
val compare : t -> t -> int
|
val compare : t -> t -> int
|
||||||
val hash : t -> int
|
val hash : t -> int
|
||||||
|
|
||||||
|
val atom : string -> t (** Build an atom directly from a string *)
|
||||||
|
|
||||||
val of_int : int -> t
|
val of_int : int -> t
|
||||||
val of_bool : bool -> t
|
val of_bool : bool -> t
|
||||||
val of_list : t list -> t
|
val of_list : t list -> t
|
||||||
val of_string : string -> t
|
val of_rev_list : t list -> t (** Reverse the list *)
|
||||||
val of_float : float -> t
|
val of_float : float -> t
|
||||||
val of_unit : t
|
val of_unit : t
|
||||||
val of_pair : t * t -> t
|
val of_pair : t * t -> t
|
||||||
val of_triple : t * t * t -> t
|
val of_triple : t * t * t -> t
|
||||||
|
val of_quad : t * t * t * t -> t
|
||||||
|
|
||||||
val of_variant : string -> t list -> t
|
val of_variant : string -> t list -> t
|
||||||
(** [of_variant name args] is used to encode algebraic variants
|
(** [of_variant name args] is used to encode algebraic variants
|
||||||
into a S-expr. For instance [of_variant "some" (of_int 1)]
|
into a S-expr. For instance [of_variant "some" [of_int 1]]
|
||||||
represents the value [Some 1] *)
|
represents the value [Some 1] *)
|
||||||
|
|
||||||
val of_field : string -> t -> t
|
val of_field : string -> t -> t
|
||||||
|
|
@ -471,13 +471,14 @@ module Make(W : WORD) = struct
|
||||||
else None, alternatives
|
else None, alternatives
|
||||||
| Some (Node (_, map), trail) ->
|
| Some (Node (_, map), trail) ->
|
||||||
let alternatives =
|
let alternatives =
|
||||||
_seq_map map
|
let seq = _seq_map map in
|
||||||
|> _filter_map_seq
|
let seq = _filter_map_seq
|
||||||
(fun (c', t') -> if p c c'
|
(fun (c', t') -> if p c c'
|
||||||
then Some (t', _difflist_add trail c')
|
then Some (t', _difflist_add trail c')
|
||||||
else None
|
else None
|
||||||
)
|
) seq
|
||||||
|> _seq_append_list alternatives
|
in
|
||||||
|
_seq_append_list alternatives seq
|
||||||
in
|
in
|
||||||
begin try
|
begin try
|
||||||
let t' = M.find c map in
|
let t' = M.find c map in
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,11 @@
|
||||||
let () =
|
let () =
|
||||||
if Array.length Sys.argv <> 2 then failwith "usage: id_sexp file";
|
if Array.length Sys.argv <> 2 then failwith "usage: id_sexp file";
|
||||||
let f = Sys.argv.(1) in
|
let f = Sys.argv.(1) in
|
||||||
let s = Sexp.L.of_file f in
|
let s = CCSexp.L.of_file f in
|
||||||
match s with
|
match s with
|
||||||
| `Ok l ->
|
| `Ok l ->
|
||||||
List.iter
|
List.iter
|
||||||
(fun s -> Format.printf "@[%a@]@." Sexp.print s)
|
(fun s -> Format.printf "@[%a@]@." CCSexp.print s)
|
||||||
l
|
l
|
||||||
| `Error msg ->
|
| `Error msg ->
|
||||||
Format.printf "error: %s@." msg
|
Format.printf "error: %s@." msg
|
||||||
|
|
|
||||||
|
|
@ -546,6 +546,8 @@ end
|
||||||
|
|
||||||
(* tests *)
|
(* tests *)
|
||||||
|
|
||||||
|
let (@@) f x = f x
|
||||||
|
|
||||||
module Point = struct
|
module Point = struct
|
||||||
type t = {
|
type t = {
|
||||||
x : int;
|
x : int;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
open OUnit
|
open OUnit
|
||||||
|
open CCFun
|
||||||
|
|
||||||
module Gen = CCGen
|
module Gen = CCGen
|
||||||
module GR = Gen.Restart
|
module GR = Gen.Restart
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
(* quickcheck for Levenshtein *)
|
(* quickcheck for Levenshtein *)
|
||||||
|
|
||||||
module Levenshtein = Containers_string.Levenshtein
|
module Levenshtein = Containers_string.Levenshtein
|
||||||
|
open CCFun
|
||||||
|
|
||||||
(* test that automaton accepts its string *)
|
(* test that automaton accepts its string *)
|
||||||
let test_automaton =
|
let test_automaton =
|
||||||
|
|
|
||||||
|
|
@ -15,9 +15,9 @@ let test_mvar () =
|
||||||
()
|
()
|
||||||
|
|
||||||
let test_parallel () =
|
let test_parallel () =
|
||||||
let l = CCSequence.(1 -- 300)
|
let l = CCSequence.(1 -- 300) in
|
||||||
|> CCSequence.map (fun _ -> Future.spawn (fun () -> Thread.delay 0.1; 1))
|
let l = CCSequence.map (fun _ -> Future.spawn (fun () -> Thread.delay 0.1; 1)) l in
|
||||||
|> CCSequence.to_list in
|
let l = CCSequence.to_list l in
|
||||||
let l' = List.map Future.get l in
|
let l' = List.map Future.get l in
|
||||||
OUnit.assert_equal 300 (List.fold_left (+) 0 l');
|
OUnit.assert_equal 300 (List.fold_left (+) 0 l');
|
||||||
()
|
()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue