Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

EFreeList.h
Go to the documentation of this file.
1 
33 #pragma once
34 
35 #include <ogdf/basic/EList.h>
36 
37 namespace ogdf {
38 
39 template<typename E, E* E::*next> class EFreeList;
40 template<typename E, E* E::*next, int E::*index> class EFreeListIndexPool;
41 
42 template<typename E, E* E::*next> class EFreeListTypes;
43 
44 
46 template<typename E, E* E::*next>
47 class EFreeList
48 {
49  friend class EFreeListTypes<E,next>;
50 public:
53 
55  ~EFreeList() { this->freeFreeList(); }
56 
58  inline E* alloc()
59  {
62  else
63  return new E();
64  }
65 
67  inline bool empty() const { return EFreeListTypes<E,next>::FreeStack::empty(this); }
68 
70  inline void free(E* ptr) { EFreeListTypes<E,next>::FreeStack::push(this, ptr); }
71 
72 protected:
74  inline void freeFreeList()
75  {
77  }
78 
80  E* m_pTop;
81 
82 #if 0
83  using FreeStack = EStack<EFreeList<E, next>, E, &EFreeList<E, next>::m_pTop, next>;
85 #endif
86 };
87 
89 template<typename E, E* E::*next>
90 class EFreeListTypes
91 {
92 public:
94 };
95 
96 
98 template<typename E, E* E::*next, int E::*index>
100 {
101 public:
103  EFreeListIndexPool() : m_nextFreeIndex(0) { }
104 
106  inline void free(E* ptr) { m_freeList.free(ptr); }
107 
109  inline int numUsedIndices() const { return m_nextFreeIndex; }
110 
112  inline E* alloc()
113  {
114  if (m_freeList.empty())
115  {
116  E* res = new E();
117  res->*index = m_nextFreeIndex++;
118  return res;
119  } else
120  {
121  return m_freeList.alloc();
122  }
123  }
124 
125 protected:
128 
131 };
132 
133 } // end of namespace ogdf
EFreeList()
Constructs a new freelist.
Definition: EFreeList.h:52
bool empty() const
Returns true if the stack is empty.
Definition: EFreeList.h:67
More complex implementation of a FreeList, which is able to generate indeices for the elements...
Definition: EFreeList.h:40
E * alloc()
Returns a new instance of E by either using an instance from the stack or creating a new one...
Definition: EFreeList.h:58
Simple implementation of a FreeList which buffers the memory allocation of an embedded list item...
Definition: EFreeList.h:39
EFreeListIndexPool()
Creates a new IndexPool and a FreeList.
Definition: EFreeList.h:103
Type declarations for EFreeList.
Definition: EFreeList.h:42
void freeFreeList()
deletes all instances in the list
Definition: EFreeList.h:74
Declaration and implementation of embedded stack and list functionality which is useful for embedded ...
E * alloc()
Allocates a new Element by either using the free list or allocating a new one with a brand new index...
Definition: EFreeList.h:112
EFreeList< E, next > m_freeList
The free list for allocating the memory.
Definition: EFreeList.h:130
static void init(S *pStack)
Initializes pStack as empty stack.
Definition: EList.h:101
int numUsedIndices() const
The value indicates that all indices in 0..numUsedIndices-1 might be in use.
Definition: EFreeList.h:109
static E * popRet(S *pStack)
Removes the top element and returns it.
Definition: EList.h:113
The embedded stack class template.
Definition: EList.h:89
static bool empty(const S *pStack)
Returns true if pStack is empty.
Definition: EList.h:134
static void push(S *pStack, E *pElem)
Pushes the new element pElem onto pStack.
Definition: EList.h:121
The namespace for all OGDF objects.
E * m_pTop
Top of the stack.
Definition: EFreeList.h:80
int m_nextFreeIndex
The next brand new index.
Definition: EFreeList.h:127
void free(E *ptr)
Frees an element using the FreeList.
Definition: EFreeList.h:106
void free(E *ptr)
Frees an item buy putting it onto the stack of free instances.
Definition: EFreeList.h:70
~EFreeList()
Destructor. Releases the mem used by the remaining elements on the stack.
Definition: EFreeList.h:55