Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
BlockAllocator.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_SPATIAL_BLOCK_ALLOCATOR_H
22 #define GF_SPATIAL_BLOCK_ALLOCATOR_H
23 
24 #include <cassert>
25 #include <vector>
26 
27 namespace gf {
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 inline namespace v1 {
30 #endif
31 
32  constexpr std::size_t NullIndex = -1;
33 
34  template<typename T>
36  public:
38  : m_firstFreeBlock(NullIndex)
39  , m_allocated(0)
40  {
41  }
42 
43  std::size_t allocate() {
44  std::size_t index = NullIndex;
45 
46  if (m_firstFreeBlock != NullIndex) {
47  index = m_firstFreeBlock;
48  m_firstFreeBlock = m_blocks[index].next;
49  m_blocks[index].next = NullIndex;
50  } else {
51  index = m_blocks.size();
52  m_blocks.push_back(Block());
53  }
54 
55  assert(index < m_blocks.size());
56  assert(m_blocks[index].next == NullIndex);
57 
58  ++m_allocated;
59  return index;
60  }
61 
62  void dispose(std::size_t index) {
63  assert(index < m_blocks.size());
64  m_blocks[index].next = m_firstFreeBlock;
65  m_firstFreeBlock = index;
66  --m_allocated;
67  }
68 
69  T& operator[](std::size_t index) {
70  assert(index < m_blocks.size());
71  assert(m_blocks[index].next == NullIndex);
72  return m_blocks[index].data;
73  }
74 
75  const T& operator[](std::size_t index) const {
76  assert(index < m_blocks.size());
77  assert(m_blocks[index].next == NullIndex);
78  return m_blocks[index].data;
79  }
80 
81  void clear() {
82  m_firstFreeBlock = NullIndex;
83  m_blocks.clear();
84  }
85 
86  std::size_t getAllocated() const {
87  return m_allocated;
88  }
89 
90  private:
91  struct Block {
92  T data;
93  std::size_t next = NullIndex;
94  };
95 
96  std::size_t m_firstFreeBlock;
97  std::vector<Block> m_blocks;
98  std::size_t m_allocated;
99  };
100 
101 
102 #ifndef DOXYGEN_SHOULD_SKIP_THIS
103 }
104 #endif
105 }
106 
107 
108 #endif // GF_SPATIAL_BLOCK_ALLOCATOR_H
const T & operator[](std::size_t index) const
Definition: BlockAllocator.h:75
void dispose(std::size_t index)
Definition: BlockAllocator.h:62
T & operator[](std::size_t index)
Definition: BlockAllocator.h:69
std::size_t getAllocated() const
Definition: BlockAllocator.h:86
The namespace for gf classes.
Definition: Action.h:35
Definition: BlockAllocator.h:35
std::size_t allocate()
Definition: BlockAllocator.h:43
The socket would have blocked.
BlockAllocator()
Definition: BlockAllocator.h:37
constexpr std::size_t NullIndex
Definition: BlockAllocator.h:32
void clear()
Definition: BlockAllocator.h:81