Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.79 KB | None | 0 0
  1. #include "list.h"
  2. #include "../debug.h"
  3.  
  4. /* Our doubly linked lists have two header elements: the "head"
  5. just before the first element and the "tail" just after the
  6. last element. The `prev' link of the front header is null, as
  7. is the `next' link of the back header. Their other two links
  8. point toward each other via the interior elements of the list.
  9.  
  10. An empty list looks like this:
  11.  
  12. +------+ +------+
  13. <---| head |<--->| tail |--->
  14. +------+ +------+
  15.  
  16. A list with two elements in it looks like this:
  17.  
  18. +------+ +-------+ +-------+ +------+
  19. <---| head |<--->| 1 |<--->| 2 |<--->| tail |<--->
  20. +------+ +-------+ +-------+ +------+
  21. The symmetry of this arrangement eliminates lots of special
  22. cases in list processing. For example, take a look at
  23. list_remove(): it takes only two pointer assignments and no
  24. conditionals. That's a lot simpler than the code would be
  25. without header elements.
  26.  
  27. (Because only one of the pointers in each header element is used,
  28. we could in fact combine them into a single header element
  29. without sacrificing this simplicity. But using two separate
  30. elements allows us to do a little bit of checking on some
  31. operations, which can be valuable.) */
  32.  
  33. static bool is_sorted (struct list_elem *a, struct list_elem *b,
  34. list_less_func *less, void *aux) UNUSED;
  35.  
  36. /* Returns true if ELEM is a head, false otherwise. */
  37. static inline bool
  38. is_head (struct list_elem *elem)
  39. {
  40. return elem != NULL && elem->prev == NULL && elem->next != NULL;
  41. }
  42.  
  43. /* Returns true if ELEM is an interior element,
  44. false otherwise. */
  45. static inline bool
  46. is_interior (struct list_elem *elem)
  47. {
  48. return elem != NULL && elem->prev != NULL && elem->next != NULL;
  49. }
  50.  
  51. /* Returns true if ELEM is a tail, false otherwise. */
  52. static inline bool
  53. is_tail (struct list_elem *elem)
  54. {
  55. return elem != NULL && elem->prev != NULL && elem->next == NULL;
  56. }
  57.  
  58. /* Initializes LIST as an empty list. */
  59. void
  60. list_init (struct list *list)
  61. {
  62. ASSERT (list != NULL);
  63. list->head.prev = NULL;
  64. list->head.next = &list->tail;
  65. list->tail.prev = &list->head;
  66. list->tail.next = NULL;
  67. }
  68.  
  69. /* Returns the beginning of LIST. */
  70. struct list_elem *
  71. list_begin (struct list *list)
  72. {
  73. ASSERT (list != NULL);
  74. return list->head.next;
  75. }
  76.  
  77. /* Returns the element after ELEM in its list. If ELEM is the
  78. last element in its list, returns the list tail. Results are
  79. undefined if ELEM is itself a list tail. */
  80. struct list_elem *
  81. list_next (struct list_elem *elem)
  82. {
  83. ASSERT (is_head (elem) || is_interior (elem));
  84. return elem->next;
  85. }
  86.  
  87. /* Returns LIST's tail.
  88.  
  89. list_end() is often used in iterating through a list from
  90. front to back. See the big comment at the top of list.h for
  91. an example. */
  92. struct list_elem *
  93. list_end (struct list *list)
  94. {
  95. ASSERT (list != NULL);
  96. return &list->tail;
  97. }
  98.  
  99. /* Returns the LIST's reverse beginning, for iterating through
  100. LIST in reverse order, from back to front. */
  101. struct list_elem *
  102. list_rbegin (struct list *list)
  103. {
  104. ASSERT (list != NULL);
  105. return list->tail.prev;
  106. }
  107.  
  108. /* Returns the element before ELEM in its list. If ELEM is the
  109. first element in its list, returns the list head. Results are
  110. undefined if ELEM is itself a list head. */
  111. struct list_elem *
  112. list_prev (struct list_elem *elem)
  113. {
  114. ASSERT (is_interior (elem) || is_tail (elem));
  115. return elem->prev;
  116. }
  117.  
  118. /* Returns LIST's head.
  119.  
  120. list_rend() is often used in iterating through a list in
  121. reverse order, from back to front. Here's typical usage,
  122. following the example from the top of list.h:
  123.  
  124. for (e = list_rbegin (&foo_list); e != list_rend (&foo_list);
  125. e = list_prev (e))
  126. {
  127. struct foo *f = list_entry (e, struct foo, elem);
  128. ...do something with f...
  129. }
  130. */
  131. struct list_elem *
  132. list_rend (struct list *list)
  133. {
  134. ASSERT (list != NULL);
  135. return &list->head;
  136. }
  137.  
  138. /* Return's LIST's head.
  139.  
  140. list_head() can be used for an alternate style of iterating
  141. through a list, e.g.:
  142.  
  143. e = list_head (&list);
  144. while ((e = list_next (e)) != list_end (&list))
  145. {
  146. ...
  147. }
  148. */
  149. struct list_elem *
  150. list_head (struct list *list)
  151. {
  152. ASSERT (list != NULL);
  153. return &list->head;
  154. }
  155.  
  156. /* Return's LIST's tail. */
  157. struct list_elem *
  158. list_tail (struct list *list)
  159. {
  160. ASSERT (list != NULL);
  161. return &list->tail;
  162. }
  163.  
  164. /* Inserts ELEM just before BEFORE, which may be either an
  165. interior element or a tail. The latter case is equivalent to
  166. list_push_back(). */
  167. void
  168. list_insert (struct list_elem *before, struct list_elem *elem)
  169. {
  170. ASSERT (is_interior (before) || is_tail (before));
  171. ASSERT (elem != NULL);
  172.  
  173. elem->prev = before->prev;
  174. elem->next = before;
  175. before->prev->next = elem;
  176. before->prev = elem;
  177. }
  178.  
  179. /* Removes elements FIRST though LAST (exclusive) from their
  180. current list, then inserts them just before BEFORE, which may
  181. be either an interior element or a tail. */
  182. void
  183. list_splice (struct list_elem *before,
  184. struct list_elem *first, struct list_elem *last)
  185. {
  186. ASSERT (is_interior (before) || is_tail (before));
  187. if (first == last)
  188. return;
  189. last = list_prev (last);
  190.  
  191. ASSERT (is_interior (first));
  192. ASSERT (is_interior (last));
  193.  
  194. /* Cleanly remove FIRST...LAST from its current list. */
  195. first->prev->next = last->next;
  196. last->next->prev = first->prev;
  197.  
  198. /* Splice FIRST...LAST into new list. */
  199. first->prev = before->prev;
  200. last->next = before;
  201. before->prev->next = first;
  202. before->prev = last;
  203. }
  204.  
  205. /* Inserts ELEM at the beginning of LIST, so that it becomes the
  206. front in LIST. */
  207. void
  208. list_push_front (struct list *list, struct list_elem *elem)
  209. {
  210. list_insert (list_begin (list), elem);
  211. }
  212.  
  213. /* Inserts ELEM at the end of LIST, so that it becomes the
  214. back in LIST. */
  215. void
  216. list_push_back (struct list *list, struct list_elem *elem)
  217. {
  218. list_insert (list_end (list), elem);
  219. }
  220.  
  221. /* Removes ELEM from its list and returns the element that
  222. followed it. Undefined behavior if ELEM is not in a list.
  223.  
  224. A list element must be treated very carefully after removing
  225. it from its list. Calling list_next() or list_prev() on ELEM
  226. will return the item that was previously before or after ELEM,
  227. but, e.g., list_prev(list_next(ELEM)) is no longer ELEM!
  228.  
  229. The list_remove() return value provides a convenient way to
  230. iterate and remove elements from a list:
  231.  
  232. for (e = list_begin (&list); e != list_end (&list); e = list_remove (e))
  233. {
  234. ...do something with e...
  235. }
  236.  
  237. If you need to free() elements of the list then you need to be
  238. more conservative. Here's an alternate strategy that works
  239. even in that case:
  240.  
  241. while (!list_empty (&list))
  242. {
  243. struct list_elem *e = list_pop_front (&list);
  244. ...do something with e...
  245. }
  246. */
  247. struct list_elem *
  248. list_remove (struct list_elem *elem)
  249. {
  250. ASSERT (is_interior (elem));
  251. elem->prev->next = elem->next;
  252. elem->next->prev = elem->prev;
  253. return elem->next;
  254. }
  255.  
  256. /* Removes the front element from LIST and returns it.
  257. Undefined behavior if LIST is empty before removal. */
  258. struct list_elem *
  259. list_pop_front (struct list *list)
  260. {
  261. struct list_elem *front = list_front (list);
  262. list_remove (front);
  263. return front;
  264. }
  265.  
  266. /* Removes the back element from LIST and returns it.
  267. Undefined behavior if LIST is empty before removal. */
  268. struct list_elem *
  269. list_pop_back (struct list *list)
  270. {
  271. struct list_elem *back = list_back (list);
  272. list_remove (back);
  273. return back;
  274. }
  275.  
  276. /* Returns the front element in LIST.
  277. Undefined behavior if LIST is empty. */
  278. struct list_elem *
  279. list_front (struct list *list)
  280. {
  281. ASSERT (!list_empty (list));
  282. return list->head.next;
  283. }
  284.  
  285. /* Returns the back element in LIST.
  286. Undefined behavior if LIST is empty. */
  287. struct list_elem *
  288. list_back (struct list *list)
  289. {
  290. ASSERT (!list_empty (list));
  291. return list->tail.prev;
  292. }
  293.  
  294. /* Returns the number of elements in LIST.
  295. Runs in O(n) in the number of elements. */
  296. size_t
  297. list_size (struct list *list)
  298. {
  299. struct list_elem *e;
  300. size_t cnt = 0;
  301.  
  302. for (e = list_begin (list); e != list_end (list); e = list_next (e))
  303. cnt++;
  304. return cnt;
  305. }
  306.  
  307. /* Returns true if LIST is empty, false otherwise. */
  308. bool
  309. list_empty (struct list *list)
  310. {
  311. return list_begin (list) == list_end (list);
  312. }
  313.  
  314. /* Swaps the `struct list_elem *'s that A and B point to. */
  315. static void
  316. swap (struct list_elem **a, struct list_elem **b)
  317. {
  318. struct list_elem *t = *a;
  319. *a = *b;
  320. *b = t;
  321. }
  322.  
  323. /* Reverses the order of LIST. */
  324. void
  325. list_reverse (struct list *list)
  326. {
  327. if (!list_empty (list))
  328. {
  329. struct list_elem *e;
  330.  
  331. for (e = list_begin (list); e != list_end (list); e = e->prev)
  332. swap (&e->prev, &e->next);
  333. swap (&list->head.next, &list->tail.prev);
  334. swap (&list->head.next->prev, &list->tail.prev->next);
  335. }
  336. }
  337.  
  338. /* Returns true only if the list elements A through B (exclusive)
  339. are in order according to LESS given auxiliary data AUX. */
  340. static bool
  341. is_sorted (struct list_elem *a, struct list_elem *b,
  342. list_less_func *less, void *aux)
  343. {
  344. if (a != b)
  345. while ((a = list_next (a)) != b)
  346. if (less (a, list_prev (a), aux))
  347. return false;
  348. return true;
  349. }
  350.  
  351. /* Finds a run, starting at A and ending not after B, of list
  352. elements that are in nondecreasing order according to LESS
  353. given auxiliary data AUX. Returns the (exclusive) end of the
  354. run.
  355. A through B (exclusive) must form a non-empty range. */
  356. static struct list_elem *
  357. find_end_of_run (struct list_elem *a, struct list_elem *b,
  358. list_less_func *less, void *aux)
  359. {
  360. ASSERT (a != NULL);
  361. ASSERT (b != NULL);
  362. ASSERT (less != NULL);
  363. ASSERT (a != b);
  364.  
  365. do
  366. {
  367. a = list_next (a);
  368. }
  369. while (a != b && !less (a, list_prev (a), aux));
  370. return a;
  371. }
  372.  
  373. /* Merges A0 through A1B0 (exclusive) with A1B0 through B1
  374. (exclusive) to form a combined range also ending at B1
  375. (exclusive). Both input ranges must be nonempty and sorted in
  376. nondecreasing order according to LESS given auxiliary data
  377. AUX. The output range will be sorted the same way. */
  378. static void
  379. inplace_merge (struct list_elem *a0, struct list_elem *a1b0,
  380. struct list_elem *b1,
  381. list_less_func *less, void *aux)
  382. {
  383. ASSERT (a0 != NULL);
  384. ASSERT (a1b0 != NULL);
  385. ASSERT (b1 != NULL);
  386. ASSERT (less != NULL);
  387. ASSERT (is_sorted (a0, a1b0, less, aux));
  388. ASSERT (is_sorted (a1b0, b1, less, aux));
  389.  
  390. while (a0 != a1b0 && a1b0 != b1)
  391. if (!less (a1b0, a0, aux))
  392. a0 = list_next (a0);
  393. else
  394. {
  395. a1b0 = list_next (a1b0);
  396. list_splice (a0, list_prev (a1b0), a1b0);
  397. }
  398. }
  399.  
  400. /* Sorts LIST according to LESS given auxiliary data AUX, using a
  401. natural iterative merge sort that runs in O(n lg n) time and
  402. O(1) space in the number of elements in LIST. */
  403. void
  404. list_sort (struct list *list, list_less_func *less, void *aux)
  405. {
  406. size_t output_run_cnt; /* Number of runs output in current pass. */
  407.  
  408. ASSERT (list != NULL);
  409. ASSERT (less != NULL);
  410.  
  411. /* Pass over the list repeatedly, merging adjacent runs of
  412. nondecreasing elements, until only one run is left. */
  413. do
  414. {
  415. struct list_elem *a0; /* Start of first run. */
  416. struct list_elem *a1b0; /* End of first run, start of second. */
  417. struct list_elem *b1; /* End of second run. */
  418.  
  419. output_run_cnt = 0;
  420. for (a0 = list_begin (list); a0 != list_end (list); a0 = b1)
  421. {
  422. /* Each iteration produces one output run. */
  423. output_run_cnt++;
  424.  
  425. /* Locate two adjacent runs of nondecreasing elements
  426. A0...A1B0 and A1B0...B1. */
  427. a1b0 = find_end_of_run (a0, list_end (list), less, aux);
  428. if (a1b0 == list_end (list))
  429. break;
  430. b1 = find_end_of_run (a1b0, list_end (list), less, aux);
  431.  
  432. /* Merge the runs. */
  433. inplace_merge (a0, a1b0, b1, less, aux);
  434. }
  435. }
  436. while (output_run_cnt > 1);
  437.  
  438. ASSERT (is_sorted (list_begin (list), list_end (list), less, aux));
  439. }
  440.  
  441. /* Inserts ELEM in the proper position in LIST, which must be
  442. sorted according to LESS given auxiliary data AUX.
  443. Runs in O(n) average case in the number of elements in LIST. */
  444. void
  445. list_insert_ordered (struct list *list, struct list_elem *elem,
  446. list_less_func *less, void *aux)
  447. {
  448. struct list_elem *e;
  449.  
  450. ASSERT (list != NULL);
  451. ASSERT (elem != NULL);
  452. ASSERT (less != NULL);
  453.  
  454. for (e = list_begin (list); e != list_end (list); e = list_next (e))
  455. if (less (elem, e, aux))
  456. break;
  457. return list_insert (e, elem);
  458. }
  459.  
  460. /* Iterates through LIST and removes all but the first in each
  461. set of adjacent elements that are equal according to LESS
  462. given auxiliary data AUX. If DUPLICATES is non-null, then the
  463. elements from LIST are appended to DUPLICATES. */
  464. void
  465. list_unique (struct list *list, struct list *duplicates,
  466. list_less_func *less, void *aux)
  467. {
  468. struct list_elem *elem, *next;
  469.  
  470. ASSERT (list != NULL);
  471. ASSERT (less != NULL);
  472. if (list_empty (list))
  473. return;
  474.  
  475. elem = list_begin (list);
  476. while ((next = list_next (elem)) != list_end (list))
  477. if (!less (elem, next, aux) && !less (next, elem, aux))
  478. {
  479. list_remove (next);
  480. if (duplicates != NULL)
  481. list_push_back (duplicates, next);
  482. }
  483. else
  484. elem = next;
  485. }
  486.  
  487. /* Returns the element in LIST with the largest value according
  488. to LESS given auxiliary data AUX. If there is more than one
  489. maximum, returns the one that appears earlier in the list. If
  490. the list is empty, returns its tail. */
  491. struct list_elem *
  492. list_max (struct list *list, list_less_func *less, void *aux)
  493. {
  494. struct list_elem *max = list_begin (list);
  495. if (max != list_end (list))
  496. {
  497. struct list_elem *e;
  498.  
  499. for (e = list_next (max); e != list_end (list); e = list_next (e))
  500. if (less (max, e, aux))
  501. max = e;
  502. }
  503. return max;
  504. }
  505.  
  506. /* Returns the element in LIST with the smallest value according
  507. to LESS given auxiliary data AUX. If there is more than one
  508. minimum, returns the one that appears earlier in the list. If
  509. the list is empty, returns its tail. */
  510. struct list_elem *
  511. list_min (struct list *list, list_less_func *less, void *aux)
  512. {
  513. struct list_elem *min = list_begin (list);
  514. if (min != list_end (list))
  515. {
  516. struct list_elem *e;
  517.  
  518. for (e = list_next (min); e != list_end (list); e = list_next (e))
  519. if (less (e, min, aux))
  520. min = e;
  521. }
  522. return min;
  523. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement