1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     ut_LinkList.cpp
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision:$
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/ut/ut_LinkList.h>
19 
20 namespace nw {
21 namespace ut {
22 namespace internal {
23 
24 //--------------------------------------------------------------------------
25 LinkListImpl::iterator
erase(iterator it)26 LinkListImpl::erase(iterator it)
27 {
28     NW_ASSERT(it.m_pPointer != &m_BaseNode);
29     iterator itNext = it;
30     (void)++itNext;
31     return erase(it,itNext);
32 }
33 
34 
35 //--------------------------------------------------------------------------
36 LinkListImpl::iterator
erase(iterator itFirst,iterator itLast)37 LinkListImpl::erase(iterator itFirst,iterator itLast)
38 {
39     Node* pIt = itFirst.m_pPointer;
40     Node* const pItLast = itLast.m_pPointer;
41     Node* pNext;
42 
43     for ( ; pIt != pItLast ; pIt = pNext)
44     {
45         pNext = pIt->m_pNext;
46         (void)erase(pIt);
47     }
48 
49     return itLast;
50 }
51 
52 //--------------------------------------------------------------------------
53 void
reverse(void)54 LinkListImpl::reverse(void)
55 {
56     if (empty()) { return; }
57 
58     Node* p = m_BaseNode.m_pNext;
59     Node* pNext;
60 
61     Initialize_();
62 
63     for ( ; p != &m_BaseNode ; p = pNext )
64     {
65         pNext = p->m_pNext;
66         p->m_pNext = NULL;
67         p->m_pPrev = NULL;
68         push_front(p);
69     }
70 }
71 
72 //--------------------------------------------------------------------------
73 LinkListImpl::iterator
insert(iterator it,pointer p)74 LinkListImpl::insert(iterator it, pointer p)
75 {
76     NW_NULL_ASSERT(p);
77     Node *const pIt = it.m_pPointer;
78     NW_NULL_ASSERT(pIt);
79 
80     Node *const pItPrev = pIt->m_pPrev;
81     NW_NULL_ASSERT(pItPrev);
82 
83     NW_ASSERT(p->m_pNext == NULL);
84     NW_ASSERT(p->m_pPrev == NULL);
85     p->m_pNext = pIt;
86     p->m_pPrev = pItPrev;
87 
88     pIt->m_pPrev = p;
89     pItPrev->m_pNext = p;
90 
91     ++m_Size;
92     return iterator(p);
93 }
94 
95 
96 //--------------------------------------------------------------------------
97 LinkListImpl::iterator
erase(pointer p)98 LinkListImpl::erase(pointer p)
99 {
100     NW_ASSERT(!empty());
101     NW_NULL_ASSERT(p);
102     NW_ASSERT(p != &m_BaseNode);
103 
104     Node* const pNext = p->m_pNext;
105     Node* const pPrev = p->m_pPrev;
106 
107     NW_NULL_ASSERT(pNext);
108     pNext->m_pPrev = pPrev;
109     NW_NULL_ASSERT(pPrev);
110     pPrev->m_pNext = pNext;
111     --m_Size;
112 
113     p->m_pNext = NULL;
114     p->m_pPrev = NULL;
115 
116     return iterator(pNext);
117 }
118 
119 }  // namespace internal
120 }  // namespace ut
121 }  // namespace nw
122