struct int_list *radix_sort(struct int_list *arr) { int i,j, pos = 10; struct int_list *head, *next_element, *digits[10]; for(i = 0; i < 10; i++) { digits[i] = NULL; } for(j = 0; j < 10; j++) { /* Group Phase */ for(i = 0; i < ARR_SIZE; i++) { next_element = list_entry(arr->list.next, struct int_list, list); if(digits[(arr->val % pos) / (pos / 10)] == NULL) { digits[(arr->val % pos) / (pos / 10)] = arr; _list_del(arr); _list_add(arr, arr, arr); } else { _list_del(arr); _list_add(arr, list_entry(digits[(arr->val % pos) / (pos / 10)]->list.prev, struct int_list, list), digits[(arr->val % pos) / (pos / 10)]); } arr = next_element; } /* Merge phase */ head = arr = NULL; for(i = 0; i < 10; i++) { if(digits[i] != NULL) { if(head == NULL) head = digits[i]; if(arr != NULL) { struct list_head *ap = arr->list.prev; arr->list.prev->next = &digits[i]->list; /* a'->next */ arr->list.prev = digits[i]->list.prev; /* a->prev */ digits[i]->list.prev->next = &arr->list; /*d'->next */ digits[i]->list.prev = ap; /*d->prev */ } else { arr = head; /* This is here because I want to skip the first loop. */ } digits[i] = NULL; } } pos *= 10; } return head; }
void list_move(struct list *list, struct list *head) { if (list->prev != head) { _list_del(list); list_add(list, head); } }
void BX_Dict_Del(struct BX_Dict *dict) { for (size_t i = 0; i < _primes[dict->_pridx]; ++i) _list_del(dict->items[i]); free(dict->items); free(dict); }
static void _list_del(struct BX_DictItem *list) { if (list) { _list_del(list->tail); BX_DecRef(list->key); BX_DecRef(list->val); free(list); } }
static bool _enlarge(struct BX_Dict *dict) { struct BX_DictItem *item; size_t pridx = dict->_pridx; size_t length = dict->length; struct BX_DictItem **items = dict->items; size_t old_width = _primes[pridx]; size_t new_width = _primes[pridx + 1]; dict->_pridx += 1; dict->length = 0; dict->items = malloc(new_width * sizeof(struct BX_DictItem *)); if (dict->items == NULL) return false; // LCOV_EXCL_LINE for (size_t i = 0; i < new_width; ++i) dict->items[i] = (struct BX_DictItem *) NULL; for (size_t i = 0; i < old_width; ++i) { for (item = items[i]; item; item = item->tail) { if (!_dict_insert(dict, item->key, item->val)) { /* LCOV_EXCL_START */ for (size_t j = 0; j < i; ++j) _list_del(dict->items[j]); free(dict->items); dict->_pridx = pridx; dict->length = length; dict->items = items; return false; /* LCOV_EXCL_STOP */ } } } for (size_t i = 0; i < old_width; ++i) _list_del(items[i]); free(items); return true; }
void BX_Dict_Clear(struct BX_Dict *dict) { for (size_t i = 0; i < _primes[dict->_pridx]; ++i) { if (dict->items[i]) { _list_del(dict->items[i]); dict->items[i] = (struct BX_DictItem *) NULL; } } dict->length = 0; }
/* Remove an element from the list */ void list_del(struct int_list *cur) { _list_del(cur); free (cur); }
void list_move_tail(struct list *list, struct list *head) { _list_del(list); list_add_tail(list, head); }
/** * Remove the element from the list it is in. Using this function will reset * the pointers to/from this element so it is removed from the list. It does * NOT free the element itself or manipulate it otherwise. * * Using list_del on a pure list head (like in the example at the top of * this file) will NOT remove the first element from * the list but rather reset the list as empty list. * * Example: * list_del(&foo->entry); * * @param entry The element to remove. */ void list_del(struct list *entry) { _list_del(entry); list_init(entry); }
/** * list_move_tail - delete from one list and add as another's tail. * * @param[in] list the entry to move * @param[in] head the head that will follow our entry * * @return none * * @note none * @see list_move * @authors deeve * @date 2013/10/19 */ void list_move_tail(struct list_head *list, struct list_head *head) { _check_head(head); _list_del(list->prev, list->next); list_add_tail(list, head); }
/** * list_del_init - deletes entry from list and reinitialize it. * deletes entry from list, the entry point itslef * * @param[in] entry the element to delete from the list * * @return none * * @see list_del * @authors deeve * @date 2013/10/19 */ void list_del_init(struct list_head *entry) { _list_del(entry->prev, entry->next); INIT_LIST_HEAD(entry); }
/** * list_del - deletes entry from list. * deletes entry from list, the entry still point it's prev/next node * but it's prev/next node do not point it again * * @param[in] entry the element to delete from the list * * @return none * * @note list_is_empty on entry does not return true after this, * the entry is in an undefined state * @see * @authors deeve * @date 2013/10/19 */ void list_del(struct list_head *entry) { _list_del(entry->prev, entry->next); }