1 /* opkg_active_list.c - the opkg package management system
3 Tick Chen <tick@openmoko.com>
5 Copyright (C) 2008 Openmoko
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
19 #include <libopkg/active_list.h>
20 #include <active_list.h>
25 struct active_list list;
28 static struct active_test *active_test_new(char *str)
30 struct active_test *ans =
31 (struct active_test *)calloc(1, sizeof(struct active_test));
33 active_list_init(&ans->list);
37 static void active_test_add(struct active_list *head, struct active_test *node)
39 active_list_add(head, &node->list);
43 .--A---B----C----D-----E----F
49 Then the sequence will be
50 +: G M H I O J A B K N L C D E F
51 -: F E D C L N K B A J O I H M G
53 static void make_list(struct active_list *head)
55 struct active_test *A = active_test_new("A");
56 struct active_test *B = active_test_new("B");
57 struct active_test *C = active_test_new("C");
58 struct active_test *D = active_test_new("D");
59 struct active_test *E = active_test_new("E");
60 struct active_test *F = active_test_new("F");
61 struct active_test *G = active_test_new("G");
62 struct active_test *H = active_test_new("H");
63 struct active_test *I = active_test_new("I");
64 struct active_test *J = active_test_new("J");
65 struct active_test *K = active_test_new("K");
66 struct active_test *L = active_test_new("L");
67 struct active_test *M = active_test_new("M");
68 struct active_test *N = active_test_new("N");
69 struct active_test *O = active_test_new("O");
71 active_test_add(head, A);
72 active_test_add(head, B);
73 active_test_add(head, C);
74 active_test_add(head, D);
75 active_test_add(head, E);
76 active_test_add(head, F);
77 active_test_add(head, G);
78 active_test_add(head, H);
79 active_test_add(head, I);
80 active_test_add(head, J);
81 active_test_add(head, K);
82 active_test_add(head, L);
83 active_test_add(head, M);
84 active_test_add(head, N);
85 active_test_add(head, O);
88 static int active_test_compare(const void *a, const void *b)
90 struct active_list *first = (struct active_list *)a;
91 struct active_list *second = (struct active_list *)b;
92 return memcmp(list_entry(first, struct active_test, list),
93 list_entry(second, struct active_test, list),
94 sizeof(struct active_test));
97 static void show_list(struct active_list *head)
99 struct active_list *ptr;
100 struct active_test *test;
101 for (ptr = active_list_next(head, NULL); ptr;
102 ptr = active_list_next(head, ptr)) {
103 test = list_entry(ptr, struct active_test, list);
104 printf("%s ", test->str);
111 struct active_list head;
112 struct active_list *ptr;
113 struct active_test *test;
114 active_list_init(&head);
117 printf("pos order: ");
119 /* for(ptr = active_list_next(&head, &head); ptr ;ptr = active_list_next(&head, ptr)) {
120 test = list_entry(ptr, struct active_test, list);
121 printf ("%s ",test->str);
123 printf("neg order: ");
124 for (ptr = active_list_prev(&head, &head); ptr;
125 ptr = active_list_prev(&head, ptr)) {
126 test = list_entry(ptr, struct active_test, list);
127 printf("%s ", test->str);
129 printf("\npos order after sort: ");
130 active_list_sort(&head, &active_test_compare);
133 printf("after clear: ");
134 active_list_clear(&head);
135 for (ptr = active_list_next(&head, NULL); ptr;
136 ptr = active_list_next(&head, ptr)) {
137 test = list_entry(ptr, struct active_test, list);
138 printf("%s ", test->str);