Symphony Of Empires
freelist.hpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // freelist.hpp
20 //
21 // Abstract:
22 // Does important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <cstddef>
28 #include <cassert>
29 #include <optional>
30 #include "eng3d/utils.hpp"
31 
32 namespace Eng3D {
33  template<typename T>
34  struct Freelist {
35  size_t add(T& e) {
36  size_t index = 0;
37  if(slots.empty()) {
38  index = data.size();
39  data.emplace_back(e);
40  } else {
41  index = slots.back();
42  slots.pop_back();
43  data[index].emplace(e);
44  }
45  return index;
46  }
47 
48  void remove(size_t index) {
49  assert(data[index].has_value());
50  data[index].reset();
51  slots.push_back(index);
52  }
53 
54  T& operator[](size_t index) {
55  assert(index < data.size());
56  return data[index].value();
57  }
58 
59  const T& operator[](size_t index) const {
60  assert(index < data.size());
61  return data[index].value();
62  }
63 
64  T& at(size_t index) {
65  assert(index < data.size());
66  return data[index].value();
67  }
68 
69  const T& at(size_t index) const {
70  assert(index < data.size());
71  return data[index].value();
72  }
73 
74  template<typename F>
75  void for_each(const F& lambda) const {
76  for(const auto& e : data)
77  if(e.has_value())
78  lambda(e.value());
79  }
80 
81  template<typename F>
82  void for_each(const F& lambda) {
83  for(auto& e : data)
84  if(e.has_value())
85  lambda(e.value());
86  }
87 
88  std::vector<std::optional<T>> data;
89  std::vector<size_t> slots;
90  };
91 }
const T & operator[](size_t index) const
Definition: freelist.hpp:59
size_t add(T &e)
Definition: freelist.hpp:35
void for_each(const F &lambda) const
Definition: freelist.hpp:75
T & operator[](size_t index)
Definition: freelist.hpp:54
const T & at(size_t index) const
Definition: freelist.hpp:69
void for_each(const F &lambda)
Definition: freelist.hpp:82
T & at(size_t index)
Definition: freelist.hpp:64
std::vector< std::optional< T > > data
Definition: freelist.hpp:88
std::vector< size_t > slots
Definition: freelist.hpp:89
void remove(size_t index)
Definition: freelist.hpp:48