source: mainline/common/adt/list.c@ cad7b7e

topic/simplify-dev-export
Last change on this file since cad7b7e was ad9178bf, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 20 months ago

Deduplicate ADT

  • Property mode set to 100644
File size: 3.2 KB
RevLine 
[ee7736e]1/*
[df4ed85]2 * Copyright (c) 2004 Jakub Jermar
[ee7736e]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[fadd381]29/** @addtogroup libc
[b2951e2]30 * @{
31 */
[b72efe8]32
33/**
34 * @file
[1ab4aca]35 * @brief Functions completing doubly linked circular list implementation.
[b72efe8]36 *
37 * This file contains some of the functions implementing doubly linked circular lists.
38 * However, this ADT is mostly implemented in @ref list.h.
[b2951e2]39 */
40
[d9c8c81]41#include <adt/list.h>
[3e6a98c5]42#include <stdbool.h>
[36795edf]43#include <stdint.h>
[ee7736e]44
45/** Check for membership
46 *
[b72efe8]47 * Check whether link is contained in a list.
48 * Membership is defined as pointer equivalence.
[ee7736e]49 *
[b72efe8]50 * @param link Item to look for.
51 * @param list List to look in.
[ee7736e]52 *
[bfbdfee]53 * @return true if link is contained in list, false otherwise.
[ee7736e]54 *
55 */
[df13836]56bool list_member(const link_t *link, const list_t *list)
[ee7736e]57{
[b72efe8]58 bool found = false;
59 link_t *hlp = list->head.next;
[a35b458]60
[b72efe8]61 while (hlp != &list->head) {
[ee7736e]62 if (hlp == link) {
[b72efe8]63 found = true;
[ee7736e]64 break;
65 }
66 hlp = hlp->next;
67 }
[a35b458]68
[ee7736e]69 return found;
70}
71
[30eab78]72/** Moves items of one list into another after the specified item.
[ee7736e]73 *
[30eab78]74 * Inserts all items of @a list after item at @a pos in another list.
75 * Both lists may be empty.
[ee7736e]76 *
[30eab78]77 * @param list Source list to move after pos. Empty afterwards.
78 * @param pos Source items will be placed after this item.
[ee7736e]79 */
[30eab78]80void list_splice(list_t *list, link_t *pos)
[ee7736e]81{
[1b20da0]82 if (list_empty(list))
[ee7736e]83 return;
[a35b458]84
[30eab78]85 /* Attach list to destination. */
86 list->head.next->prev = pos;
87 list->head.prev->next = pos->next;
[a35b458]88
[30eab78]89 /* Link destination list to the added list. */
90 pos->next->prev = list->head.prev;
91 pos->next = list->head.next;
[a35b458]92
[30eab78]93 list_initialize(list);
[b72efe8]94}
[2246de6]95
96/** Count list items
97 *
98 * Return the number of items in the list.
99 *
[b72efe8]100 * @param list List to count.
101 * @return Number of items in the list.
[2246de6]102 */
[36795edf]103size_t list_count(const list_t *list)
[2246de6]104{
[36795edf]105 size_t count = 0;
[a35b458]106
[feeac0d]107 link_t *link = list_first(list);
108 while (link != NULL) {
[2246de6]109 count++;
[feeac0d]110 link = list_next(link, list);
[2246de6]111 }
[a35b458]112
[2246de6]113 return count;
114}
115
[fadd381]116/** @}
[b2951e2]117 */
Note: See TracBrowser for help on using the repository browser.