list_t *list_reverse(list_t * self) { unsigned i; assert(self); for (i = 0; i < self->length / 2; i++) list_swap(self, i, self->length - 1 - i); return self; }
void dockchan(GtkWidget *w, struct gchan *c) { GtkWidget *win; struct gchan *tmp; char buf[30]; tmp = c; g_object_ref(G_OBJECT(tmp->frami)); gtk_container_remove(GTK_CONTAINER(GTK_WIDGET(tmp->frami)->parent), tmp->frami); win = gtk_window_new(GTK_WINDOW_TOPLEVEL); c->windock = win; list_swap(listachan, tmp->frami, c->windock); /* docking/crossfade bug hotfix --night */ switch(c->pos) { case 1: gtk_widget_hide(pack1.hscale); break; case 2: gtk_widget_hide(pack1.hscale); break; case 3: gtk_widget_hide(pack2.hscale); break; case 4: gtk_widget_hide(pack2.hscale); break; case 5: gtk_widget_hide(pack3.hscale); break; case 6: gtk_widget_hide(pack3.hscale); break; } list_set_pos(listachan, c->windock, 0); snprintf(buf, sizeof(buf), _("Channel[%u]"), tmp->idx); gtk_window_set_title(GTK_WINDOW(win), buf); g_signal_connect(G_OBJECT(win), "delete_event", G_CALLBACK(deletedocked), c); gtk_container_add(GTK_CONTAINER(win), tmp->frami); gtk_widget_destroy(GTK_WIDGET(c->dock)); c->dock = createpixmap(win, c->dock, dock_xpm, _("Dock Channel"), FALSE); gtk_box_pack_start(GTK_BOX(c->hbox), c->dock, FALSE, FALSE, 0); g_signal_connect(G_OBJECT(c->dock), "clicked", G_CALLBACK(undock), c); g_object_unref(G_OBJECT(tmp->frami)); gtk_widget_show_all(win); pack_refresh(listachan, NULL, true); }
static void list_inplace_sort(list_t *list, int first, int last, int compare(const void *a, const void *b)) { if (first >= last) { return; } else if ((last - first) == 1) { if (compare(&list->items[first], &list->items[last]) > 0) { list_swap(list, first, last); } } else { int mid = (int)((last + first) / 2); list_inplace_sort(list, first, mid, compare); list_inplace_sort(list, mid + 1, last, compare); list_inplace_merge(list, first, last, mid, compare); } }
/** Randomly shuffles all of the items in the list. Exits on failure, immediately returns without exiting if l is NULL. @param l The list to shuffle. */ void list_shuffle(list *l) { if(l == NULL) return; // https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle for(int i=l->length-1; i>=1; i--) { int j = list_rand_interval(0, i); // index to swap if(list_swap(l, i, j) == 0) { msg(FATAL, "Internal list error while shuffling\n"); exit(EXIT_FAILURE); } } }
void list_select_sort(struct list_head *phead, int (*cmp)(const struct list_head *a, const struct list_head *b)) { struct list_head *pi, *pj, *pmin, *Next; for (pi = phead->next; pi != phead; pi = Next) { Next = pi->next; pmin = pi; for (pj = pi->next; pj != phead; pj = pj->next) { if (cmp(pj,pmin) < 0) { pmin = pj; } } if (pmin != pi) { list_swap(pmin, pi); } } }
void list_bubble_sort(struct list_head *phead, int (*cmp)(const struct list_head *a, const struct list_head *b)) { struct list_head *index = phead->prev; struct list_head *cur = NULL; for (; index != phead; index = index->prev) { for (cur = phead->next; cur != index; cur = cur->next) { if (cmp(cur, cur->next) > 0) { list_swap(cur, cur->next); cur = cur->prev; } if (cur == index) { index = index->next; } } } }
void list_shuffle (struct list *list) { size_t elem_cnt; size_t i; ASSERT(list); srand(time(NULL)); elem_cnt = list_size(list); // shuffle elem_cnt times. for(i = 0; i < elem_cnt; ++i) { struct list_elem *e1 = list_find_nth(list, rand() % elem_cnt), *e2 = list_find_nth(list, rand() % elem_cnt); list_swap(e1, e2); } }
/** Reverses the order of items in a list (in place). @param The list to reverse. @return 1 if success, 0 if failure. Failure can occur if we failed to allocate temporary space to facilitate swapping the location of items in the array. If there is a failure, the list may be in a partially reversed state. */ int list_reverse(list *l) { /* No need to reverse an empty list or a list with only one * item. */ if(l->length < 2) return 1; for(int i=0; i < l->length/2; i++) { int otherEnd = l->length-i-1; if(list_swap(l, i, otherEnd) == 0) { msg(ERROR, "Failed to reverse the list."); return 0; } } return 1; }
/* simple bubble-sort */ void list_sort(list *lp, listsortfunc cmp, int reverse) { listitem *li, *last = 0; bool swapped = true; int c; while(swapped) { swapped = false; for(li=lp->first; li && li->next; li=li->next) { if(li->next == last) { last = li; break; } c = cmp(li->data, li->next->data); if(reverse ? c < 0 : c > 0) { list_swap(lp, li, li->next); swapped = true; } } } }
void undock(GtkWidget *w, struct gchan *c) { GtkWidget *container; container = c->frami; list_swap(listachan, c->windock, c->frami); g_object_ref(G_OBJECT(container)); gtk_container_remove(GTK_CONTAINER(c->windock), container); pack_chan_insert(container); gtk_widget_destroy(c->windock); gtk_widget_destroy(c->dock); c->dock = createpixmap(window, c->dock, dock_xpm, _("Undock Channel"), FALSE); gtk_box_pack_start(GTK_BOX(c->hbox), c->dock, FALSE, FALSE, 0); gtk_widget_show_all(c->dock); g_object_unref(G_OBJECT(container)); g_signal_connect(G_OBJECT(c->dock), "clicked", G_CALLBACK(dockchan), c); }
void ben_sort(BEN * node) { ITEM *item = NULL; ITEM *next = NULL; BEN *key1 = NULL; BEN *key2 = NULL; TUPLE *tuple_this = NULL; TUPLE *tuple_next = NULL; LONG switchcounter = 0; int result = 0; if (node == NULL) { fail("ben_sort( 1 )"); } if (node->t != BEN_DICT) { fail("ben_sort( 2 )"); } if (node->v.d == NULL) { return; } if (node->v.d->counter < 2) { return; } item = node->v.d->start; while (1) { next = list_next(item); /* Reached the end */ if (next == node->v.d->start) { if (switchcounter == 0) { /* The list is sorted now */ break; } /* Reset switchcounter ... */ switchcounter = 0; /* ... and start again */ item = node->v.d->start; next = list_next(item); } tuple_this = list_value(item); tuple_next = list_value(next); key1 = tuple_this->key; key2 = tuple_next->key; result = ben_str_compare(key1, key2); if (result > 0) { list_swap(node->v.d, item, next); switchcounter++; /* Continue moving up until start is reached */ if (next != node->v.d->start) { item = list_prev(next); } } else { /* Move down */ item = next; } } }
void mu_test_list() { my_t n1 = {{{0}, {0}}, 1}; my_t n2 = {{{0}, {0}}, 2}; my_t n3 = {{{0}, {0}}, 3}; my_t n4 = {{{0}, {0}}, 4}; my_t n5 = {{{0}, {0}}, 5}; my_t n6 = {{{0}, {0}}, 6}; list_t list; list_init(&list); // 5 -> 1 list_push_back(&n5.link, &list); list_insert_after(&n5.link, &n1.link, &list); mu_check( list_next(&n5.link) == &n1.link); mu_check( list_last(&n5.link) == &n1.link); mu_check( list_last(&n1.link) == &n1.link); mu_check( list_first(&n1.link) == &n5.link); // 5 -> 3 -> 1 list_insert_befor(&n1.link, &n3.link, &list); mu_check( list_next(&n5.link) == &n3.link); mu_check( list_last(&n5.link) == &n1.link); mu_check( list_prev(&n1.link) == &n3.link); mu_check( list_first(&n3.link) == &n5.link); // 1 -> 3 -> 5 list_swap(&n5.link, &n1.link, &list); mu_check( list_next(&n1.link) == &n3.link); mu_check( list_last(&n1.link) == &n5.link); mu_check( list_prev(&n5.link) == &n3.link); mu_check( list_first(&n3.link) == &n1.link); // 5 -> 3 -> 1 list_swap(&n5.link, &n1.link, &list); mu_check( list_next(&n5.link) == &n3.link); mu_check( list_last(&n5.link) == &n1.link); mu_check( list_prev(&n1.link) == &n3.link); mu_check( list_first(&n3.link) == &n5.link); // 2 -> 5 -> 3 -> 1 list_push_front(&n2.link, &list); mu_check( !list_prev(&n2.link)); mu_check( list_last(&n2.link) == &n1.link); mu_check( list_prev(&n5.link) == &n2.link); mu_check( list_first(&n3.link) == &n2.link); // 2 -> 5 -> 3 -> 1 -> 4 list_push_back(&n4.link, &list); print(list_first(&n2.link)); mu_check( !list_next(&n4.link)); mu_check( list_last(&n2.link) == &n4.link); mu_check( list_prev(&n4.link) == &n1.link); mu_check( list_next(&n1.link) == &n4.link); mu_check( list_first(&n4.link) == &n2.link); // 2 -> 5 -> 1 -> 4 list_remove(&n3.link, &list); mu_check( list_next(&n5.link) == &n1.link); mu_check( list_prev(&n1.link) == &n5.link); // 2 -> 6 -> 1 -> 4 list_replace(&n5.link, &n6.link, &list); mu_check( list_next(&n6.link) == &n1.link); mu_check( list_prev(&n1.link) == &n6.link); mu_check( list_prev(&n6.link) == &n2.link); // 2 -> 1 -> 6 -> 4 list_swap(&n6.link, &n1.link, &list); mu_check( list_next(&n1.link) == &n6.link); mu_check( list_prev(&n6.link) == &n1.link); mu_check( list_next(&n6.link) == &n4.link); // 2 -> 6 -> 1 -> 4 list_swap(&n6.link, &n1.link, &list); mu_check( list_next(&n6.link) == &n1.link); mu_check( list_prev(&n1.link) == &n6.link); mu_check( list_next(&n1.link) == &n4.link); mu_check( list_prev(&n6.link) == &n2.link); mu_check( list_front(&list) == &n2.link); mu_check( list_back(&list) == &n4.link); puts("pre sort:"); print(list_first(&n1.link)); list_sort(&list, my_cmp); puts("post sort:"); print(list_first(&n1.link)); }
int main(int argc, char *argv[]) { int c; if (strcmp(__progname, "swapon") == 0) return swapon_command(argc, argv); while ((c = getopt(argc, argv, "Aacdlkp:st:")) != -1) { switch (c) { case 'A': SET_COMMAND(CMD_A); break; case 'a': SET_COMMAND(CMD_a); break; case 'c': SET_COMMAND(CMD_c); break; case 'd': SET_COMMAND(CMD_d); break; case 'l': SET_COMMAND(CMD_l); break; case 'k': kflag = 1; break; case 'p': pflag = 1; /* XXX strtol() */ pri = atoi(optarg); break; case 's': SET_COMMAND(CMD_s); break; case 't': if (tflag != NULL) usage(); tflag = optarg; break; default: usage(); /* NOTREACHED */ } } argv += optind; argc -= optind; /* Did the user specify a command? */ if (command == 0) { if (argc == 0) SET_COMMAND(CMD_l); else usage(); } switch (argc) { case 0: if (command & REQUIRE_PATH) usage(); break; case 1: if (command & REQUIRE_NOPATH) usage(); break; default: usage(); } /* To change priority, you have to specify one. */ if ((command == CMD_c) && pflag == 0) usage(); /* Sanity-check -t */ if (tflag != NULL) { if (command != CMD_A) usage(); if (strcmp(tflag, "blk") != 0 && strcmp(tflag, "noblk") != 0) usage(); } /* Dispatch the command. */ switch (command) { case CMD_l: list_swap(pri, kflag, pflag, 1); break; case CMD_s: list_swap(pri, kflag, pflag, 0); break; case CMD_c: change_priority(argv[0]); break; case CMD_a: add_swap(argv[0]); break; case CMD_d: del_swap(argv[0]); break; case CMD_A: do_fstab(); break; } return (0); }