00001 #include "list.h"
00002 #include "../debug.h"
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 static bool is_sorted (struct list_elem *a, struct list_elem *b,
00035 list_less_func *less, void *aux) UNUSED;
00036
00037
00038 static inline bool
00039 is_head (struct list_elem *elem)
00040 {
00041 return elem != NULL && elem->prev == NULL && elem->next != NULL;
00042 }
00043
00044
00045
00046 static inline bool
00047 is_interior (struct list_elem *elem)
00048 {
00049 return elem != NULL && elem->prev != NULL && elem->next != NULL;
00050 }
00051
00052
00053 static inline bool
00054 is_tail (struct list_elem *elem)
00055 {
00056 return elem != NULL && elem->prev != NULL && elem->next == NULL;
00057 }
00058
00059
00060 void
00061 list_init (struct list *list)
00062 {
00063 ASSERT (list != NULL);
00064 list->head.prev = NULL;
00065 list->head.next = &list->tail;
00066 list->tail.prev = &list->head;
00067 list->tail.next = NULL;
00068 }
00069
00070
00071 struct list_elem *
00072 list_begin (struct list *list)
00073 {
00074 ASSERT (list != NULL);
00075 return list->head.next;
00076 }
00077
00078
00079
00080
00081 struct list_elem *
00082 list_next (struct list_elem *elem)
00083 {
00084 ASSERT (is_head (elem) || is_interior (elem));
00085 return elem->next;
00086 }
00087
00088
00089
00090
00091
00092
00093 struct list_elem *
00094 list_end (struct list *list)
00095 {
00096 ASSERT (list != NULL);
00097 return &list->tail;
00098 }
00099
00100
00101
00102 struct list_elem *
00103 list_rbegin (struct list *list)
00104 {
00105 ASSERT (list != NULL);
00106 return list->tail.prev;
00107 }
00108
00109
00110
00111
00112 struct list_elem *
00113 list_prev (struct list_elem *elem)
00114 {
00115 ASSERT (is_interior (elem) || is_tail (elem));
00116 return elem->prev;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 struct list_elem *
00133 list_rend (struct list *list)
00134 {
00135 ASSERT (list != NULL);
00136 return &list->head;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 struct list_elem *
00151 list_head (struct list *list)
00152 {
00153 ASSERT (list != NULL);
00154 return &list->head;
00155 }
00156
00157
00158 struct list_elem *
00159 list_tail (struct list *list)
00160 {
00161 ASSERT (list != NULL);
00162 return &list->tail;
00163 }
00164
00165
00166
00167
00168 void
00169 list_insert (struct list_elem *before, struct list_elem *elem)
00170 {
00171 ASSERT (is_interior (before) || is_tail (before));
00172 ASSERT (elem != NULL);
00173
00174 elem->prev = before->prev;
00175 elem->next = before;
00176 before->prev->next = elem;
00177 before->prev = elem;
00178 }
00179
00180
00181
00182
00183 void
00184 list_splice (struct list_elem *before,
00185 struct list_elem *first, struct list_elem *last)
00186 {
00187 ASSERT (is_interior (before) || is_tail (before));
00188 if (first == last)
00189 return;
00190 last = list_prev (last);
00191
00192 ASSERT (is_interior (first));
00193 ASSERT (is_interior (last));
00194
00195
00196 first->prev->next = last->next;
00197 last->next->prev = first->prev;
00198
00199
00200 first->prev = before->prev;
00201 last->next = before;
00202 before->prev->next = first;
00203 before->prev = last;
00204 }
00205
00206
00207
00208 void
00209 list_push_front (struct list *list, struct list_elem *elem)
00210 {
00211 list_insert (list_begin (list), elem);
00212 }
00213
00214
00215
00216 void
00217 list_push_back (struct list *list, struct list_elem *elem)
00218 {
00219 list_insert (list_end (list), elem);
00220 }
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256 struct list_elem *
00257 list_remove (struct list_elem *elem)
00258 {
00259 ASSERT (is_interior (elem));
00260 elem->prev->next = elem->next;
00261 elem->next->prev = elem->prev;
00262 return elem->next;
00263 }
00264
00265
00266
00267 struct list_elem *
00268 list_pop_front (struct list *list)
00269 {
00270 struct list_elem *front = list_front (list);
00271 list_remove (front);
00272 return front;
00273 }
00274
00275
00276
00277 struct list_elem *
00278 list_pop_back (struct list *list)
00279 {
00280 struct list_elem *back = list_back (list);
00281 list_remove (back);
00282 return back;
00283 }
00284
00285
00286
00287 struct list_elem *
00288 list_front (struct list *list)
00289 {
00290 ASSERT (!list_empty (list));
00291 return list->head.next;
00292 }
00293
00294
00295
00296 struct list_elem *
00297 list_back (struct list *list)
00298 {
00299 ASSERT (!list_empty (list));
00300 return list->tail.prev;
00301 }
00302
00303
00304
00305 size_t
00306 list_size (struct list *list)
00307 {
00308 struct list_elem *e;
00309 size_t cnt = 0;
00310
00311 for (e = list_begin (list); e != list_end (list); e = list_next (e))
00312 cnt++;
00313 return cnt;
00314 }
00315
00316
00317 bool
00318 list_empty (struct list *list)
00319 {
00320 return list_begin (list) == list_end (list);
00321 }
00322
00323
00324 static void
00325 swap (struct list_elem **a, struct list_elem **b)
00326 {
00327 struct list_elem *t = *a;
00328 *a = *b;
00329 *b = t;
00330 }
00331
00332
00333 void
00334 list_reverse (struct list *list)
00335 {
00336 if (!list_empty (list))
00337 {
00338 struct list_elem *e;
00339
00340 for (e = list_begin (list); e != list_end (list); e = e->prev)
00341 swap (&e->prev, &e->next);
00342 swap (&list->head.next, &list->tail.prev);
00343 swap (&list->head.next->prev, &list->tail.prev->next);
00344 }
00345 }
00346
00347
00348
00349 static bool
00350 is_sorted (struct list_elem *a, struct list_elem *b,
00351 list_less_func *less, void *aux)
00352 {
00353 if (a != b)
00354 while ((a = list_next (a)) != b)
00355 if (less (a, list_prev (a), aux))
00356 return false;
00357 return true;
00358 }
00359
00360
00361
00362
00363
00364
00365 static struct list_elem *
00366 find_end_of_run (struct list_elem *a, struct list_elem *b,
00367 list_less_func *less, void *aux)
00368 {
00369 ASSERT (a != NULL);
00370 ASSERT (b != NULL);
00371 ASSERT (less != NULL);
00372 ASSERT (a != b);
00373
00374 do
00375 {
00376 a = list_next (a);
00377 }
00378 while (a != b && !less (a, list_prev (a), aux));
00379 return a;
00380 }
00381
00382
00383
00384
00385
00386
00387 static void
00388 inplace_merge (struct list_elem *a0, struct list_elem *a1b0,
00389 struct list_elem *b1,
00390 list_less_func *less, void *aux)
00391 {
00392 ASSERT (a0 != NULL);
00393 ASSERT (a1b0 != NULL);
00394 ASSERT (b1 != NULL);
00395 ASSERT (less != NULL);
00396 ASSERT (is_sorted (a0, a1b0, less, aux));
00397 ASSERT (is_sorted (a1b0, b1, less, aux));
00398
00399 while (a0 != a1b0 && a1b0 != b1)
00400 if (!less (a1b0, a0, aux))
00401 a0 = list_next (a0);
00402 else
00403 {
00404 a1b0 = list_next (a1b0);
00405 list_splice (a0, list_prev (a1b0), a1b0);
00406 }
00407 }
00408
00409
00410
00411
00412 void
00413 list_sort (struct list *list, list_less_func *less, void *aux)
00414 {
00415 size_t output_run_cnt;
00416
00417 ASSERT (list != NULL);
00418 ASSERT (less != NULL);
00419
00420
00421
00422 do
00423 {
00424 struct list_elem *a0;
00425 struct list_elem *a1b0;
00426 struct list_elem *b1;
00427
00428 output_run_cnt = 0;
00429 for (a0 = list_begin (list); a0 != list_end (list); a0 = b1)
00430 {
00431
00432 output_run_cnt++;
00433
00434
00435
00436 a1b0 = find_end_of_run (a0, list_end (list), less, aux);
00437 if (a1b0 == list_end (list))
00438 break;
00439 b1 = find_end_of_run (a1b0, list_end (list), less, aux);
00440
00441
00442 inplace_merge (a0, a1b0, b1, less, aux);
00443 }
00444 }
00445 while (output_run_cnt > 1);
00446
00447 ASSERT (is_sorted (list_begin (list), list_end (list), less, aux));
00448 }
00449
00450
00451
00452
00453 void
00454 list_insert_ordered (struct list *list, struct list_elem *elem,
00455 list_less_func *less, void *aux)
00456 {
00457 struct list_elem *e;
00458
00459 ASSERT (list != NULL);
00460 ASSERT (elem != NULL);
00461 ASSERT (less != NULL);
00462
00463 for (e = list_begin (list); e != list_end (list); e = list_next (e))
00464 if (less (elem, e, aux))
00465 break;
00466 return list_insert (e, elem);
00467 }
00468
00469
00470
00471
00472
00473 void
00474 list_unique (struct list *list, struct list *duplicates,
00475 list_less_func *less, void *aux)
00476 {
00477 struct list_elem *elem, *next;
00478
00479 ASSERT (list != NULL);
00480 ASSERT (less != NULL);
00481 if (list_empty (list))
00482 return;
00483
00484 elem = list_begin (list);
00485 while ((next = list_next (elem)) != list_end (list))
00486 if (!less (elem, next, aux) && !less (next, elem, aux))
00487 {
00488 list_remove (next);
00489 if (duplicates != NULL)
00490 list_push_back (duplicates, next);
00491 }
00492 else
00493 elem = next;
00494 }
00495
00496
00497
00498
00499
00500 struct list_elem *
00501 list_max (struct list *list, list_less_func *less, void *aux)
00502 {
00503 struct list_elem *max = list_begin (list);
00504 if (max != list_end (list))
00505 {
00506 struct list_elem *e;
00507
00508 for (e = list_next (max); e != list_end (list); e = list_next (e))
00509 if (less (max, e, aux))
00510 max = e;
00511 }
00512 return max;
00513 }
00514
00515
00516
00517
00518
00519 struct list_elem *
00520 list_min (struct list *list, list_less_func *less, void *aux)
00521 {
00522 struct list_elem *min = list_begin (list);
00523 if (min != list_end (list))
00524 {
00525 struct list_elem *e;
00526
00527 for (e = list_next (min); e != list_end (list); e = list_next (e))
00528 if (less (e, min, aux))
00529 min = e;
00530 }
00531 return min;
00532 }