OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
ManagedIDs.h
Go to the documentation of this file.
1 //
2 // Class ManagedIDs
3 // Simple class to manage an ID pool.
4 //
5 // Previously freed ID's are redistributed on following requests.
6 //
7 // Copyright (c) 2010 - 2013, Yves Ineichen, ETH Zürich
8 // All rights reserved
9 //
10 // Implemented as part of the PhD thesis
11 // "Toward massively parallel multi-objective optimization with application to
12 // particle accelerators" (https://doi.org/10.3929/ethz-a-009792359)
13 //
14 // This file is part of OPAL.
15 //
16 // OPAL is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
23 //
24 #include <cstddef>
25 #include <queue>
26 
27 class ManagedIDs {
28 
29 public:
30 
32  {}
33 
35  size_t nextID() {
36 
37  size_t id = 0;
38 
39  if(freeids_.empty()) {
40  id = next_free_;
41  next_free_++;
42  } else {
43  id = freeids_.front();
44  freeids_.pop();
45  }
46 
47  return id;
48  }
49 
50 
52  void freeID(size_t id) {
53 
54  if(id == next_free_ - 1)
55  next_free_--;
56  else
57  freeids_.push(id);
58  }
59 
60 
61 private:
62 
64  std::queue<size_t> freeids_;
65 
67  size_t next_free_;
68 
69 };
size_t nextID()
return next free ID
Definition: ManagedIDs.h:35
void freeID(size_t id)
free previously allocated ID
Definition: ManagedIDs.h:52
std::queue< size_t > freeids_
queue to handle freed ID's
Definition: ManagedIDs.h:64
size_t next_free_
next free ID
Definition: ManagedIDs.h:67