static alpm_pkghash_t *pkghash_add_pkg(alpm_pkghash_t *hash, alpm_pkg_t *pkg, int sorted) { alpm_list_t *ptr; unsigned int position; if(pkg == NULL || hash == NULL) { return hash; } if(hash->entries >= hash->limit) { hash = rehash(hash); } position = get_hash_position(pkg->name_hash, hash); ptr = malloc(sizeof(alpm_list_t)); if(ptr == NULL) { return hash; } ptr->data = pkg; ptr->prev = ptr; ptr->next = NULL; hash->hash_table[position] = ptr; 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; }
/** * @brief Sort a list of size `n` using mergesort algorithm. * * @param list the list to sort * @param n the size of the list * @param fn the comparison function for determining order * * @return the resultant list */ alpm_list_t SYMEXPORT *alpm_list_msort(alpm_list_t *list, size_t n, alpm_list_fn_cmp fn) { if(n > 1) { size_t half = n / 2; size_t i = half - 1; alpm_list_t *left = list, *lastleft = list, *right; while(i--) { lastleft = lastleft->next; } right = lastleft->next; /* tidy new lists */ lastleft->next = NULL; right->prev = left->prev; left->prev = lastleft; left = alpm_list_msort(left, half, fn); right = alpm_list_msort(right, n - half, fn); list = alpm_list_mmerge(left, right, fn); } return list; }
/** * pacman_list_concat_sorted: * @first: A sorted #PacmanList. * @second: A sorted #PacmanList. * @func: A #GCompareFunc function. * * Creates a new list by appending @second to @first, in the correct order as determined by @func. * * Returns: A #PacmanList composed of @first and @second. Do not use or free @first or @second afterwards. */ PacmanList *pacman_list_concat_sorted (PacmanList *first, PacmanList *second, GCompareFunc func) { return alpm_list_mmerge (first, second, (alpm_list_fn_cmp) func); }