[pacman-dev] [PATCH] Refactor out common code in pkghash add functions

Dan McGee dpmcgee at gmail.com
Mon Feb 7 21:27:48 EST 2011


On Mon, Feb 7, 2011 at 5:15 PM, Pang Yan Han <pangyanhan at gmail.com> wrote:
> The overlapping code in _alpm_pkghash_add() and
> _alpm_pkghash_add_sorted() are now in a new static function
> _alpm_pkghash_add_pkg(). This function has a third flag
> parameter which determines whether the package should be added
> in sorted order.
>
> Signed-off-by: Pang Yan Han <pangyanhan at gmail.com>
> ---
>  lib/libalpm/pkghash.c |   44 ++++++++++++++------------------------------
>  1 files changed, 14 insertions(+), 30 deletions(-)
>
> diff --git a/lib/libalpm/pkghash.c b/lib/libalpm/pkghash.c
> index 5480527..233985f 100644
> --- a/lib/libalpm/pkghash.c
> +++ b/lib/libalpm/pkghash.c
> @@ -148,7 +148,7 @@ static pmpkghash_t *rehash(pmpkghash_t *oldhash)
>        return(newhash);
>  }
>
> -pmpkghash_t *_alpm_pkghash_add(pmpkghash_t *hash, pmpkg_t *pkg)
> +static pmpkghash_t *_alpm_pkghash_add_pkg(pmpkghash_t *hash, pmpkg_t *pkg, int sorted)
More convention, but if it is a static function we don't use the
_alpm_ prefix. I'll fix it. And you can also wrap your commit messages
a bit longer- I think the built-in gitcommit syntax for vim sets
textwidth at 76, which makes lines never spill over in `git log`
viewing with an 80-char terminal.

>  {
>        alpm_list_t *ptr;
>        size_t position;
> @@ -173,41 +173,24 @@ pmpkghash_t *_alpm_pkghash_add(pmpkghash_t *hash, pmpkg_t *pkg)
>        ptr->prev = ptr;
>
>        hash->hash_table[position] = ptr;
> -       hash->list = alpm_list_join(hash->list, ptr);
> -       hash->entries += 1;
> +       if(!sorted){
> +               hash->list = alpm_list_join(hash->list, ptr);
> +       }else{
> +               hash->list = alpm_list_mmerge(hash->list, ptr, _alpm_pkg_cmp);
> +       }
>
> +       hash->entries += 1;
>        return(hash);
>  }
>
> -pmpkghash_t *_alpm_pkghash_add_sorted(pmpkghash_t *hash, pmpkg_t *pkg)
> +pmpkghash_t *_alpm_pkghash_add(pmpkghash_t *hash, pmpkg_t *pkg)
>  {
> -       if(!hash) {
> -               return(_alpm_pkghash_add(hash, pkg));
> -       }
> -
> -       alpm_list_t *ptr;
> -       size_t position;
> -
> -       if((hash->entries + 1) / MAX_HASH_LOAD > hash->buckets) {
> -               hash = rehash(hash);
> -       }
> -
> -       position = get_hash_position(pkg->name_hash, hash);
> -
> -       ptr = calloc(1, sizeof(alpm_list_t));
> -       if(ptr == NULL) {
> -               return(hash);
> -       }
> -
> -       ptr->data = pkg;
> -       ptr->next = NULL;
> -       ptr->prev = ptr;
> -
> -       hash->hash_table[position] = ptr;
> -       hash->list = alpm_list_mmerge(hash->list, ptr, _alpm_pkg_cmp);
> -       hash->entries += 1;
> +       return(_alpm_pkghash_add_pkg(hash, pkg, 0));
> +}
>
> -       return(hash);
> +pmpkghash_t *_alpm_pkghash_add_sorted(pmpkghash_t *hash, pmpkg_t *pkg)
> +{
> +       return(_alpm_pkghash_add_pkg(hash, pkg, 1));
>  }
>
>  static size_t move_one_entry(pmpkghash_t *hash, size_t start, size_t end)
> @@ -344,3 +327,4 @@ pmpkg_t *_alpm_pkghash_find(pmpkghash_t *hash, const char *name)
>
>        return(NULL);
>  }
> +/* vim: set ts=2 sw=2 noet: */
> --
> 1.7.4
>
>
>


More information about the pacman-dev mailing list