1d38d8d6bc7c09e37c64dbbd02af2452f5ea2a42
[project/opkg-lede.git] / libopkg / active_list.c
1 /* active_list.h - the opkg package management system
2
3 Tick Chen <tick@openmoko.com>
4
5 Copyright (C) 2008 Openmoko Inc.
6
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.
11
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.
16 */
17
18
19 #include "active_list.h"
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24
25 void active_list_init(struct active_list *ptr) {
26 INIT_LIST_HEAD(&ptr->node);
27 INIT_LIST_HEAD(&ptr->depend);
28 ptr->depended = NULL;
29 }
30
31 /**
32 */
33 struct active_list * active_list_next(struct active_list *head, struct active_list *ptr) {
34 struct active_list *next=NULL;
35 if ( !head ) {
36 fprintf(stderr, "active_list_next head = %p, ptr = %p invalid value!!\n", head, ptr);
37 return NULL;
38 }
39 if ( !ptr )
40 ptr = head;
41 next = list_entry(ptr->node.next, struct active_list, node);
42 if ( next == head ) {
43 return NULL;
44 }
45 if ( ptr->depended && &ptr->depended->depend == ptr->node.next ) {
46 return ptr->depended;
47 }
48 while ( next->depend.next != &next->depend ) {
49 next = list_entry(next->depend.next, struct active_list, node);
50 }
51 return next;
52 }
53
54
55 struct active_list * active_list_prev(struct active_list *head, struct active_list *ptr) {
56 struct active_list *prev=NULL;
57 if ( !head ) {
58 fprintf(stderr, "active_list_prev head = %p, ptr = %p invalid value!!\n", head, ptr);
59 return NULL;
60 }
61 if ( !ptr )
62 ptr = head;
63 if ( ptr->depend.prev != &ptr->depend ) {
64 prev = list_entry(ptr->depend.prev, struct active_list, node);
65 return prev;
66 }
67 if ( ptr->depended && ptr->depended != head && &ptr->depended->depend == ptr->node.prev ) {
68 prev = list_entry(ptr->depended->node.prev, struct active_list, node);
69 } else
70 prev = list_entry(ptr->node.prev, struct active_list, node);
71 if ( prev == head )
72 return NULL;
73 return prev;
74 }
75
76
77 struct active_list *active_list_move_node(struct active_list *old_head, struct active_list *new_head, struct active_list *node) {
78 struct active_list *prev;
79 if (!old_head || !new_head || !node)
80 return NULL;
81 if (old_head == new_head)
82 return node;
83 prev = active_list_prev(old_head, node);
84 active_list_add(new_head, node);
85 return prev;
86 }
87
88 static void list_head_clear (struct list_head *head) {
89 struct active_list *next;
90 struct list_head *n, *ptr;
91 if (!head)
92 return;
93 list_for_each_safe(ptr, n , head) {
94 next = list_entry(ptr, struct active_list, node);
95 if (next->depend.next != &next->depend) {
96 list_head_clear(&next->depend);
97 }
98 active_list_init(next);
99 }
100 }
101 void active_list_clear(struct active_list *head) {
102 list_head_clear(&head->node);
103 if (head->depend.next != &head->depend) {
104 list_head_clear(&head->depend);
105 }
106 active_list_init(head);
107 }
108
109 void active_list_add_depend(struct active_list *node, struct active_list *depend) {
110 list_del_init(&depend->node);
111 list_add_tail(&depend->node, &node->depend);
112 depend->depended = node;
113 }
114
115 void active_list_add(struct active_list *head, struct active_list *node) {
116 list_del_init(&node->node);
117 list_add_tail(&node->node, &head->node);
118 node->depended = head;
119 }
120
121 struct active_list * active_list_head_new() {
122 struct active_list * head = calloc(1, sizeof(struct active_list));
123 active_list_init(head);
124 return head;
125 }
126
127 void active_list_head_delete(struct active_list *head) {
128 active_list_clear(head);
129 free(head);
130 }
131
132 /*
133 * Using insert sort.
134 * Note. the list should not be large, or it will be very inefficient.
135 *
136 */
137 struct active_list * active_list_sort(struct active_list *head, int (*compare)(const void *, const void *)) {
138 struct active_list tmphead;
139 struct active_list *node, *ptr;
140 if ( !head )
141 return NULL;
142 active_list_init(&tmphead);
143 for (node = active_list_next(head, NULL); node; node = active_list_next(head, NULL)) {
144 if (tmphead.node.next == &tmphead.node) {
145 active_list_move_node(head, &tmphead, node);
146 } else {
147 for (ptr = active_list_next(&tmphead, NULL); ptr; ptr=active_list_next(&tmphead, ptr)) {
148 if (compare(ptr, node) <= 0) {
149 break;
150 }
151 }
152 if (!ptr) {
153 active_list_move_node(head, &tmphead, node);
154 } else {
155 active_list_move_node(head, ptr, node);
156 }
157 }
158 node->depended = &tmphead;
159 }
160 for (ptr = active_list_prev(&tmphead, NULL); ptr; ptr=active_list_prev(&tmphead, NULL)) {
161 active_list_move_node(&tmphead, head, ptr);
162 }
163 return head;
164 }