Open
Graph Drawing
Framework

 v.2012.05
 

EFreeList.h
Go to the documentation of this file.
00001 /*
00002  * $Revision: 2299 $
00003  *
00004  * last checkin:
00005  *   $Author: gutwenger $
00006  *   $Date: 2012-05-07 15:57:08 +0200 (Mon, 07 May 2012) $
00007  ***************************************************************/
00008 
00044 #ifdef _MSC_VER
00045 #pragma once
00046 #endif
00047 
00048 #ifndef OGDF_EFREELIST_H
00049 #define OGDF_EFREELIST_H
00050 
00051 #include <ogdf/basic/EList.h>
00052 
00053 namespace ogdf {
00054 
00055 template<typename E, E* E::*next> class EFreeList;
00056 template<typename E, E* E::*next, int E::*index> class EFreeListIndexPool;
00057 
00059 template<typename E, E* E::*next>
00060 class EFreeList
00061 {
00062 public:
00064     inline EFreeList()  { FreeStack::init(this); }
00065 
00067     ~EFreeList() { this->freeFreeList(); }
00068 
00070     inline E* alloc()
00071     {
00072         if (!FreeStack::empty(this))
00073             return FreeStack::popRet(this);
00074         else
00075             return new E();
00076     }
00077 
00079     inline bool empty() const { return FreeStack::empty(this); }
00080 
00082     inline void free(E* ptr) { FreeStack::push(this, ptr); }
00083 
00084 protected:
00086     inline void freeFreeList()
00087     {
00088         while (!FreeStack::empty(this)) { delete FreeStack::popRet(this); };
00089     }
00090 
00092     E* m_pTop;
00093 
00095     typedef EStack<EFreeList<E, next>, E, &EFreeList<E, next>::m_pTop, next> FreeStack;
00096 };
00097 
00099 template<typename E, E* E::*next, int E::*index>
00100 class EFreeListIndexPool
00101 {
00102 public:
00104     EFreeListIndexPool() : m_nextFreeIndex(0) { }
00105 
00107     inline void free(E* ptr) { m_freeList.free(ptr); }
00108 
00110     inline int numUsedIndices() const { return m_nextFreeIndex; };
00111 
00113     inline E* alloc()
00114     {
00115         if (m_freeList.empty())
00116         {
00117             E* res = new E();
00118             res->*index = m_nextFreeIndex++;
00119             return res;
00120         } else
00121         {
00122             return m_freeList.alloc();
00123         };
00124     }
00125 
00126 protected:
00128     int m_nextFreeIndex;
00129 
00131     EFreeList<E, next> m_freeList;
00132 };
00133 
00134 } // end of namespace ogdf
00135 
00136 #endif /* ELISTPOOL_H_ */