list.cpp revision 808f34a8cab52569bfca26cec6fd96740aa2ea25
1/*
2 * Copyright (C) 2010 The Android Open Source Project
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 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <list>
30
31namespace android {
32
33void ListNodeBase::swap(ListNodeBase& a, ListNodeBase& b) {
34    if (a.mNext != &a) {
35        if (b.mNext != &b) {
36            // a and b are not empty.
37            std::swap(a.mNext, b.mNext);
38            std::swap(a.mPrev, b.mPrev);
39            a.mNext->mPrev = a.mPrev->mNext = &a;
40            b.mNext->mPrev = b.mPrev->mNext = &b;
41        } else {
42            // a not empty but b is.
43            b.mNext = a.mNext;
44            b.mPrev = a.mPrev;
45            b.mNext->mPrev = b.mPrev->mNext = &b;
46            a.mNext = a.mPrev = &a;  // empty a
47        }
48    } else if (b.mNext != &b) {
49        // a is empty but b is not.
50        a.mNext = b.mNext;
51        a.mPrev = b.mPrev;
52        a.mNext->mPrev = a.mPrev->mNext = &a;
53        b.mNext = b.mPrev = &b;  // empty b
54    }
55}
56
57void ListNodeBase::hook(ListNodeBase *const pos) {
58    mNext = pos;
59    mPrev = pos->mPrev;
60    pos->mPrev->mNext = this;
61    pos->mPrev = this;
62}
63
64void ListNodeBase::unhook() {
65    ListNodeBase *const next = mNext;
66    ListNodeBase *const prev = mPrev;
67    prev->mNext = next;
68    next->mPrev = prev;
69}
70
71}  // namespace android
72