Nazri Ramliy wrote:

> 2010/7/28 Dominique Pellé <dominique.pe...@gmail.com>:
>> I send this patch again with slightly more elegant way of doing it
>> saving 3 lines of code. Sorry for the noise.
>
> From your patch:
>
> +/* Used in qsort */
> +static int cmp_str(s1, s2)
> +    const void *s1;
> +    const void *s2;
> +{
> +    return STRCMP(*(char_u **)s1, *(char_u **)s2);
> +}
> +
> ...
> +       /* Sort and remove dupes which can happen when specifying multiple
> +        * directories in dirnames such as "{syntax,ftplugin,indent}"
> +        */
> +       qsort(*file, *num_file, sizeof(char_u *), cmp_str);
> +       for (i = *num_file - 1; i > 0; i--)
> +       {
> +           if (STRCMP((*file)[i - 1], (*file)[i]) == 0)
> +           {
> +               /* Remove dupe (*file)[i] */
> +               vim_free((*file)[i]);
> +               if (*num_file - i - 1 > 0)
> +                   mch_memmove(&(*file)[i], &(*file)[i + 1],
> +                                       (*num_file - i - 1)*sizeof(char *));
> +               --*num_file;
> +           }
> +       }
>
> The code to achieve this (sort and remove duplicates) are already implemented
> and you can reuse it in place of the codes above.
>
> The functions misc2.c:sort_strings() and misc1.c:remove_duplicates()
> can be used as-is.
>
> You'll have to export misc1.c:remove_duplicates() though.
>
> See the sample usage in misc1.c:9455.
>
> nazri.


Thanks for having a look Nazri.  I'll update the patch later today with
your comments.  Some remarks though:

* yes, I can reuse sort_strings() as-is (I'll do that)

* I could also reuse remove_duplicates() but it has a slightly
  different behavior since it calls fnamecmp() instead of
  STRCMP() to check for dupes. I wonder whether that's
  desirable here.  I think it's better to use STRCMP() in my case.

Remark about remove_duplicate() in misc1.c: It'd be slightly better
if remove_duplicates(...) looped backward (as in my function) to
avoid doing more moves than necessary when there are dupes (and
to compare with 0 in exit condition which is also cheaper).  Say you
have to remove dupes in this pathological case:
"x", "x", "x", ... "x", "x"  (n times)
remove_duplicate() as currently implemented would do  (n-1)*n/2
moves (that's O(n^2)) whereas when looping backward() as in my
function, it does 0 moves. Looping backward will always do less
moves since it avoids moving duplicates already removed.

Cheers
-- Dominique

-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Raspunde prin e-mail lui