forked from GPUOpen-Drivers/pal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
palListImpl.h
222 lines (187 loc) · 8.79 KB
/
palListImpl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
***********************************************************************************************************************
*
* Copyright (c) 2014-2023 Advanced Micro Devices, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************************************************************/
/**
***********************************************************************************************************************
* @file palListImpl.h
* @brief PAL utility collection List and ListIterator class implementations.
***********************************************************************************************************************
*/
#pragma once
#include "palList.h"
#include "palSysMemory.h"
namespace Util
{
// =====================================================================================================================
// Obtains a pointer to the data stored in the node pointed to by the iterator. Returns null if the iterator is
// pointing at the footer.
template<typename T, typename Allocator>
T* ListIterator<T, Allocator>::Get() const
{
// Assume that the iterator is pointing at either the header or the footer node, meaning the data in this node is
// invalid.
T* pRet = nullptr;
// Shouldn't be able to walk off the end of the list...
PAL_ASSERT(m_pCurrent != nullptr);
// Shouldn't be able to point the iterator to the header node either.
PAL_ASSERT(IsHeader() == false);
if (IsFooter() == false)
{
// Valid node, return a pointer to the data.
pRet = &m_pCurrent->data;
}
return pRet;
}
// =====================================================================================================================
// Advances the iterator to the next element in the list.
template<typename T, typename Allocator>
void ListIterator<T, Allocator>::Next()
{
// Prevent our iterator from walking off the end of the list.
if (IsFooter() == false)
{
m_pCurrent = m_pCurrent->pNext;
}
}
// =====================================================================================================================
// Advances the iterator to the previous element in the list. If the iterator currently points at the head, then the
// iterator is not changed.
template<typename T, typename Allocator>
void ListIterator<T, Allocator>::Prev()
{
// Prevent our iterator from ever pointing to the permanent header node. This prevents an "InsertBefore" call ever
// being made while the iterator is pointing at the header, which would be bad.
if (m_pList->IsHeader(m_pCurrent->pPrev) == false)
{
m_pCurrent = m_pCurrent->pPrev;
}
}
// =====================================================================================================================
// Inserts "pData" before the specified iterator. If the iterator has walked off the end of the list, this function
// will insert the new node as the tail of the list.
template<typename T, typename Allocator>
Result List<T, Allocator>::InsertBefore(
ListIterator<T, Allocator>* pIterator,
const T& data)
{
// Iterators are only a container for what we really need for the "insert" operation -- namely, a node in the list.
// So just call the real insert function using the iterators node.
return InsertBefore(pIterator->m_pCurrent, data);
}
// =====================================================================================================================
// Private method used that inserts "pData" before the specified node. If "pNode" is null then this function will
// assume it is adding to an empty list.
template<typename T, typename Allocator>
Result List<T, Allocator>::InsertBefore(
ListNode<T>* pBeforeMe,
const T& data)
{
// Assume this is going to work...
Result result = Result::_Success;
// There is a "fake" footer node, meaning there's always a node to "insert before".
PAL_ASSERT(pBeforeMe != nullptr);
// Can't insert before the fake header node.
PAL_ASSERT(IsHeader(pBeforeMe) == false);
ListNode<T>* pNewNode = PAL_NEW(ListNode<T>, m_pAllocator, AllocInternal);
if (pNewNode != nullptr)
{
pNewNode->data = data;
// Ok, there is an element in this list that we can insert before, so connect our new node into the list.
pNewNode->pNext = pBeforeMe;
pNewNode->pPrev = pBeforeMe->pPrev;
// The only node with a NULL "prev" pointer should be the header which we can't insert before.
PAL_ASSERT(pNewNode->pPrev != nullptr);
// And now connect the list into our new node.
pBeforeMe->pPrev = pNewNode;
pNewNode->pPrev->pNext = pNewNode;
m_numElements++;
}
else
{
// Couldn't allocate memory for a new node in the list.
result = Result::ErrorOutOfMemory;
}
PAL_ALERT(result != Result::_Success);
return result;
}
// =====================================================================================================================
// Public method used for removing the node pointed to be "pIterator" from the list. If the iterator has walked off
// the end of the list, then nothing happens.
template<typename T, typename Allocator>
void List<T, Allocator>::Erase(
ListIterator<T, Allocator>* pIterator)
{
// Should be impossible to get the iterator to point to the permanent header node.
PAL_ASSERT(pIterator->IsHeader() == false);
// Make sure we're not trying to destroy the permanent footer node.
if (pIterator->IsFooter() == false)
{
ListNode<T>* pDestroyMe = pIterator->m_pCurrent;
// Make sure this iterator hasn't walked off the end of the list... The "Next()" and "Prev()" functions should
// make this impossible.
PAL_ASSERT(pDestroyMe != nullptr);
// We're about to destroy this iterators node. In order to keep the iterator valid, we need to advance it.
// Don't advance to the dummy footer node unless there is no valid node available.
if (IsFooter(pDestroyMe->pNext) == false)
{
// Ok, our next node is not the dummy footer, so advance to there.
pIterator->m_pCurrent = pDestroyMe->pNext;
}
else
{
// Next node is the dummy footer. Move the iterator to the previous node instead.
pIterator->m_pCurrent = pDestroyMe->pPrev;
if (pIterator->IsHeader())
{
// Ok, the iterator is now pointing to the permanent header node, which isn't good either as now the
// iterator can't be used for "insert before" operations. Move to the header's "next", which should be
// the footer.
pIterator->m_pCurrent = pDestroyMe->pNext;
PAL_ASSERT(pIterator->IsFooter());
}
}
// And go ahead and destroy the node.
Erase(pDestroyMe);
}
}
// =====================================================================================================================
// Private method that removes "pNode" from the list. pNode can not be null.
template<typename T, typename Allocator>
void List<T, Allocator>::Erase(
ListNode<T>* pNode)
{
// Something bad has happened. We are trying to erase a node from an empty list?
PAL_ASSERT(m_numElements != 0);
// Can't erase a non-existant node!
PAL_ASSERT(pNode != nullptr);
// Make sure we're not trying to erase the permanent header or footer nodes.
PAL_ASSERT((IsHeader(pNode) == false) && (IsFooter(pNode) == false));
// Connect our previous node around.
pNode->pPrev->pNext = pNode->pNext;
// And connect our successor around as well.
pNode->pNext->pPrev = pNode->pPrev;
m_numElements--;
PAL_SAFE_DELETE(pNode, m_pAllocator);
}
} // Util