6 * $Date: 2012-07-11 15:28:17 +0200 (Mi, 11. Jul 2012) $
7 ***************************************************************/
10 * \brief Declaration and implementation of a simple freelist and an
11 * index pool which generates unique indices for elements.
13 * \author Martin Gronemann
16 * This file is part of the Open Graph Drawing Framework (OGDF).
20 * See README.txt in the root directory of the OGDF installation for details.
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * Version 2 or 3 as published by the Free Software Foundation;
26 * see the file LICENSE.txt included in the packaging of this file
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public
37 * License along with this program; if not, write to the Free
38 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
39 * Boston, MA 02110-1301, USA.
41 * \see http://www.gnu.org/copyleft/gpl.html
42 ***************************************************************/
49 #ifndef OGDF_EFREE_LIST_H
50 #define OGDF_EFREE_LIST_H
52 #include <ogdf/basic/EList.h>
56 template<typename E
, E
* E::*next
> class EFreeList
;
57 template<typename E
, E
* E::*next
, int E::*index
> class EFreeListIndexPool
;
59 template<typename E
, E
* E::*next
> class EFreeListTypes
;
62 //! Simple implementation of a FreeList which buffers the memory allocation of an embedded list item.
63 template<typename E
, E
* E::*next
>
66 friend class EFreeListTypes
<E
,next
>;
68 //! Constructs a new freelist
69 inline EFreeList() { EFreeListTypes
<E
,next
>::FreeStack::init(this); }
71 //! Destructor. Releases the mem used by the remaining elements on the stack.
72 ~EFreeList() { this->freeFreeList(); }
74 //! Returns a new instance of E by either using an instance from the stack or creating a new one.
77 if (!EFreeListTypes
<E
,next
>::FreeStack::empty(this))
78 return EFreeListTypes
<E
,next
>::FreeStack::popRet(this);
83 //! Returns true if the stack is empty.
84 inline bool empty() const { return EFreeListTypes
<E
,next
>::FreeStack::empty(this); }
86 //! Frees an item buy putting it onto the stack of free instances
87 inline void free(E
* ptr
) { EFreeListTypes
<E
,next
>::FreeStack::push(this, ptr
); }
90 //! deletes all instances in the list
91 inline void freeFreeList()
93 while (!EFreeListTypes
<E
,next
>::FreeStack::empty(this)) { delete EFreeListTypes
<E
,next
>::FreeStack::popRet(this); }
99 //! Typedef for the embedded stack
100 //typedef EStack<EFreeList<E, next>, E, &EFreeList<E, next>::m_pTop, next> FreeStack;
103 //! Type declarations for EFreeList.
104 template<typename E
, E
* E::*next
>
108 typedef EStack
<EFreeList
<E
, next
>, E
, &EFreeList
<E
, next
>::m_pTop
, next
> FreeStack
;
112 //! More complex implementation of a FreeList, which is able to generate indeices for the elements.
113 template<typename E
, E
* E::*next
, int E::*index
>
114 class EFreeListIndexPool
117 //! Creates a new IndexPool and a FreeList.
118 EFreeListIndexPool() : m_nextFreeIndex(0) { }
120 //! Frees an element using the FreeList
121 inline void free(E
* ptr
) { m_freeList
.free(ptr
); }
123 //! The value indicates that all indices in 0..numUsedIndices-1 might be in use.
124 inline int numUsedIndices() const { return m_nextFreeIndex
; }
126 //! Allocates a new Element by either using the free list or allocating a new one with a brand new index.
129 if (m_freeList
.empty())
132 res
->*index
= m_nextFreeIndex
++;
136 return m_freeList
.alloc();
141 //! The next brand new index.
144 //! The free list for allocating the memory.
145 EFreeList
<E
, next
> m_freeList
;
148 } // end of namespace ogdf