[systemd-devel] [PATCH 03/14] systemadm: display dependencies sorted

Maciej Marcin Piechotka uzytkownik2 at gmail.com
Mon Sep 19 10:34:12 PDT 2011


On Mon, 2011-09-19 at 13:24 +0200, Zbigniew Jędrzejewski-Szmek wrote:
> Maybe there's an easier way to sort strings in vala...

In libgee the code would be:

List<string> sorted = new ArrayList<string>();
sorted.add_all (dependencies);
sorted.sort ();

Or:

Collection<string> sorted = new TreeSet<string>();
sorted.add_all (dependencies);

> I admit
> that this patch is not very pretty.
> ---
>  src/systemadm.vala |    7 ++++++-
>  1 files changed, 6 insertions(+), 1 deletions(-)
> 
> diff --git a/src/systemadm.vala b/src/systemadm.vala
> index 21177bf..9861ae4 100644
> --- a/src/systemadm.vala
> +++ b/src/systemadm.vala
> @@ -442,6 +442,11 @@ public class MainWindow : Window {
>          }
>  
>          public string make_dependency_string(string? prefix, string word, string[] dependencies) {
> +                List<string> sorted = new List<string>();
> +                foreach (string i in dependencies)
> +                        sorted.append(i);
> +                sorted.sort(strcmp);
> +

The above code have O(n^2) complexity (each append have O(n) and there
is O(n) appends) and IIRC[1] list should start by NULL:

List<string>? sorted = NULL;
foreach (string i in dependencies)
    sorted.prepend(i);
sorted.reverse();
sorted.sort(strcmp);

or even better if dependencies is List:

List<unowned string>? sorted = dependencies.copy();
sorted.sort(strcmp);

[1] "There is no function to create a GList. NULL is considered to be
the empty list so you simply set a GList* to NULL." from
http://developer.gnome.org/glib/2.28/glib-Doubly-Linked-Lists.html


>                  bool first = true;
>                  string r;
>  
> @@ -450,7 +455,7 @@ public class MainWindow : Window {
>                  else
>                          r = prefix;
>  
> -                foreach (string i in dependencies) {
> +                foreach (string i in sorted) {
>                          if (r != "")
>                                  r += first ? "\n" : ",";
>  

Regards

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL: <http://lists.freedesktop.org/archives/systemd-devel/attachments/20110919/090ea0e4/attachment.pgp>


More information about the systemd-devel mailing list