Pino Toscano
2016-Sep-23 16:05 UTC
[Libguestfs] [PATCH 1/2] mllib: move remove_duplicates from v2v
Simple code motion. --- mllib/common_utils.ml | 9 +++++++++ mllib/common_utils.mli | 6 ++++++ v2v/utils.ml | 9 --------- v2v/utils.mli | 3 --- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml index 81d8202..78618f5 100644 --- a/mllib/common_utils.ml +++ b/mllib/common_utils.ml @@ -297,6 +297,15 @@ let sort_uniq ?(cmp = Pervasives.compare) xs let xs = uniq ~cmp xs in xs +let remove_duplicates xs + let h = Hashtbl.create (List.length xs) in + let rec loop = function + | [] -> [] + | x :: xs when Hashtbl.mem h x -> xs + | x :: xs -> Hashtbl.add h x true; x :: loop xs + in + loop xs + let push_back xsp x = xsp := !xsp @ [x] let push_front x xsp = xsp := x :: !xsp let pop_back xsp diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli index 68c0d54..ad43345 100644 --- a/mllib/common_utils.mli +++ b/mllib/common_utils.mli @@ -147,6 +147,12 @@ val uniq : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list val sort_uniq : ?cmp:('a -> 'a -> int) -> 'a list -> 'a list (** Sort and uniquify a list. *) +val remove_duplicates : 'a list -> 'a list +(** Remove duplicates from an unsorted list; useful when the order + of the elements matter. + + Please use [sort_uniq] when the order does not matter. *) + val push_back : 'a list ref -> 'a -> unit val push_front : 'a -> 'a list ref -> unit val pop_back : 'a list ref -> 'a diff --git a/v2v/utils.ml b/v2v/utils.ml index d1ddee7..fb0b802 100644 --- a/v2v/utils.ml +++ b/v2v/utils.ml @@ -81,15 +81,6 @@ let compare_app2_versions app1 app2 compare_version app1.Guestfs.app2_release app2.Guestfs.app2_release ) -let remove_duplicates xs - let h = Hashtbl.create (List.length xs) in - let rec loop = function - | [] -> [] - | x :: xs when Hashtbl.mem h x -> xs - | x :: xs -> Hashtbl.add h x true; x :: loop xs - in - loop xs - let du filename (* There's no OCaml binding for st_blocks, so run coreutils 'du'. *) let cmd diff --git a/v2v/utils.mli b/v2v/utils.mli index 97d98ff..2bd1329 100644 --- a/v2v/utils.mli +++ b/v2v/utils.mli @@ -46,9 +46,6 @@ val find_uefi_firmware : string -> Uefi.uefi_firmware val compare_app2_versions : Guestfs.application2 -> Guestfs.application2 -> int (** Compare two app versions. *) -val remove_duplicates : 'a list -> 'a list -(** Remove duplicates from a list. *) - val du : string -> int64 (** Return the true size of a file in bytes, including any wasted space caused by internal fragmentation (the overhead of using -- 2.7.4
Pino Toscano
2016-Sep-23 16:05 UTC
[Libguestfs] [PATCH 2/2] dib: use remove_duplicates instead of own code
Use a common function to remove duplicates in an unsorted list. Just refactoring, with no behaviour change. --- dib/cmdline.ml | 2 +- dib/utils.ml | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/dib/cmdline.ml b/dib/cmdline.ml index 1fd6c71..144e5a7 100644 --- a/dib/cmdline.ml +++ b/dib/cmdline.ml @@ -107,7 +107,7 @@ read the man page virt-dib(1). let formats = ref ["qcow2"] in let set_format arg - let fmts = remove_dups (String.nsplit "," arg) in + let fmts = remove_duplicates (String.nsplit "," arg) in List.iter ( function | "qcow2" | "tar" | "raw" | "vhd" | "docker" -> () diff --git a/dib/utils.ml b/dib/utils.ml index a2046cb..3df5171 100644 --- a/dib/utils.ml +++ b/dib/utils.ml @@ -91,10 +91,6 @@ let digit_prefix_compare a b let do_mkdir dir mkdir_p dir 0o755 -let rec remove_dups = function - | [] -> [] - | x :: xs -> x :: (remove_dups (List.filter ((<>) x) xs)) - let require_tool tool try ignore (which tool) with Executable_not_found tool -> -- 2.7.4
Richard W.M. Jones
2016-Sep-23 18:17 UTC
Re: [Libguestfs] [PATCH 2/2] dib: use remove_duplicates instead of own code
On Fri, Sep 23, 2016 at 06:05:25PM +0200, Pino Toscano wrote:> Use a common function to remove duplicates in an unsorted list. > > Just refactoring, with no behaviour change.Except it's no longer O(n^2) :-) ACK series. Rich.> --- > dib/cmdline.ml | 2 +- > dib/utils.ml | 4 ---- > 2 files changed, 1 insertion(+), 5 deletions(-) > > diff --git a/dib/cmdline.ml b/dib/cmdline.ml > index 1fd6c71..144e5a7 100644 > --- a/dib/cmdline.ml > +++ b/dib/cmdline.ml > @@ -107,7 +107,7 @@ read the man page virt-dib(1). > > let formats = ref ["qcow2"] in > let set_format arg > - let fmts = remove_dups (String.nsplit "," arg) in > + let fmts = remove_duplicates (String.nsplit "," arg) in > List.iter ( > function > | "qcow2" | "tar" | "raw" | "vhd" | "docker" -> () > diff --git a/dib/utils.ml b/dib/utils.ml > index a2046cb..3df5171 100644 > --- a/dib/utils.ml > +++ b/dib/utils.ml > @@ -91,10 +91,6 @@ let digit_prefix_compare a b > let do_mkdir dir > mkdir_p dir 0o755 > > -let rec remove_dups = function > - | [] -> [] > - | x :: xs -> x :: (remove_dups (List.filter ((<>) x) xs)) > - > let require_tool tool > try ignore (which tool) > with Executable_not_found tool -> > -- > 2.7.4 > > _______________________________________________ > Libguestfs mailing list > Libguestfs@redhat.com > https://www.redhat.com/mailman/listinfo/libguestfs-- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
Possibly Parallel Threads
- [PATCH 2/2] dib: use remove_duplicates instead of own code
- [PATCH] v2v: utils: Replace "remove_duplicates" function with call to sort_uniq.
- [PATCH 0/6] dib: various improvements
- [PATCH v6 04/41] mllib: Split ‘Common_utils’ into ‘Std_utils’ + ‘Common_utils’.
- [PATCH v3 0/2] common/mlstdutils: Extend the List module.