Aleph-w  1.9
General library for algorithms and data structures
tpl_nodePool.H
1 
2 /* Aleph-w
3 
4  / \ | | ___ _ __ | |__ __ __
5  / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6  / ___ \| | __/ |_) | | | |_____\ V V / version 1.9b
7  /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8  |_|
9 
10  This file is part of Aleph-w library
11 
12  Copyright (c) 2002-2018 Leandro Rabindranath Leon & Alejandro Mujica
13 
14  This program is free software: you can redistribute it and/or modify
15  it under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  This program is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  General Public License for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27 # ifndef TPL_NODEPOOL_H
28 # define TPL_NODEPOOL_H
29 
30 # include <tpl_arrayStack.H>
31 
44  template <class Node>
45 class Node_Pool
46 {
47 
48  FixedStack<Node*> node_pool;
49  const size_t n;
50 
51 public:
52 
53  Node_Pool(size_t __n = 100) noexcept : node_pool(__n), n(__n) { /* empty */ }
54 
56  Node * allocate()
57  {
58  return node_pool.is_empty() ? new Node : node_pool.pop();
59  }
60 
74  Node * allocate(const typename Node::key_type & key)
75  {
76  return new (allocate()) Node (key);
77  }
78 
79  Node * allocate(typename Node::key_type && key)
80  {
81  return new (allocate()) Node (std::forward<typename Node::key_type>(key));
82  }
83 
102  void deallocate(Node * p) noexcept
103  {
104  if (node_pool.size() == n)
105  delete p;
106  else
107  node_pool.push(p);
108  }
109 
110  ~Node_Pool()
111  {
112  while (not node_pool.is_empty())
113  delete node_pool.pop();
114  }
115 };
116 
117 # endif // TPL_NODEPOOL_H
118 
Definition: tpl_nodePool.H:45
void deallocate(Node *p) noexcept
Definition: tpl_nodePool.H:102
T pop() noexcept
Pop by moving the top of stack.
Definition: tpl_arrayStack.H:430
size_t size() const noexcept
Return the number of elements stored in the stack.
Definition: tpl_arrayStack.H:483
Definition: tpl_arrayStack.H:302
Node * allocate(const typename Node::key_type &key)
Definition: tpl_nodePool.H:74
bool is_empty() const noexcept
Return true if stack is empty.
Definition: tpl_arrayStack.H:477
T & push(const T &data) noexcept
Definition: tpl_arrayStack.H:385
Node * allocate()
Definition: tpl_nodePool.H:56

Leandro Rabindranath León