Merge branch 'ryzom/ark-features' into main/gingo-test
[ryzomcore.git] / nel / src / 3d / fast_ptr_list.cpp
blobc9cab1069a0af53b73a8da2fdf0ff3de9497363e
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "std3d.h"
19 #include "nel/3d/fast_ptr_list.h"
21 using namespace std;
23 #ifdef DEBUG_NEW
24 #define new DEBUG_NEW
25 #endif
27 namespace NL3D
31 // ***************************************************************************
32 void CFastPtrListNode::unlink()
34 // If linked to a list, remove me.
35 if(_Owner)
37 _Owner->erase(this);
38 nlassert(_Owner==NULL);
43 // ***************************************************************************
44 CFastPtrListBase::~CFastPtrListBase()
46 clear();
50 // ***************************************************************************
51 void CFastPtrListBase::clear()
53 while(size())
55 CFastPtrListNode *node= _Nodes[0];
56 erase(node);
61 // ***************************************************************************
62 void CFastPtrListBase::insert(void *element, CFastPtrListNode *node)
64 nlassert(element);
65 nlassert(node);
67 // if this node is already linked to me, no-op!
68 if(node->_Owner==this)
69 return;
71 // first unlink the node from its older list if any.
72 node->unlink();
74 // then add the elements to the list, and update node info
75 _Elements.push_back(element);
76 _Nodes.push_back(node);
77 node->_Owner= this;
78 node->_IndexInOwner= (uint32)_Nodes.size()-1;
81 // ***************************************************************************
82 void CFastPtrListBase::erase(CFastPtrListNode *node)
84 // not mine?
85 if(node->_Owner!=this)
86 return;
88 // Take the indexes,
89 uint nodeIndex= node->_IndexInOwner;
90 uint lastIndex= (uint)_Nodes.size()-1;
92 // swap the last element and the erased one.
93 swap(_Elements[nodeIndex], _Elements[lastIndex]);
94 swap(_Nodes[nodeIndex], _Nodes[lastIndex]);
95 // change the swapped node index. NB: work also in the particular case nodeIndex==lastIndex
96 _Nodes[nodeIndex]->_IndexInOwner= nodeIndex;
97 // erase the last elements
98 _Elements.pop_back();
99 _Nodes.pop_back();
101 // reset erased node.
102 node->_Owner= NULL;
106 } // NL3D