llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
queue.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include <vector>
8 
9 
10 
14 template<typename T>
15 class Queue final {
16 public:
18  void Push(ID<T> item)
19  {
20  if (item >= dedup_.size()) {
21  dedup_.resize(item + 1);
22  }
23  if (!dedup_[item]) {
24  dedup_[item] = true;
25  placeQ_.push_back(item);
26  }
27  }
28 
31  {
32  if (takeQ_.empty()) {
33  takeQ_.reserve(placeQ_.size());
34  for (size_t i = 0, n = placeQ_.size(); i < n; ++i) {
35  takeQ_.push_back(placeQ_[n - i - 1]);
36  }
37  placeQ_.clear();
38  }
39 
40  const auto &item = takeQ_.back();
41  takeQ_.pop_back();
42  dedup_[item] = false;
43  return item;
44  }
45 
47  bool Empty() const
48  {
49  return takeQ_.empty() && placeQ_.empty();
50  }
51 
53  size_t Size() const { return placeQ_.size() + takeQ_.size(); }
54 
55 private:
57  std::vector<ID<T>> placeQ_;
59  std::vector<ID<T>> takeQ_;
61  std::vector<bool> dedup_;
62 };
Queue::Push
void Push(ID< T > item)
Adds an item to the end of the queue.
Definition: queue.h:18
Queue::Size
size_t Size() const
Returns the size of the queue.
Definition: queue.h:53
ID
Definition: id.h:19
Queue::Empty
bool Empty() const
Checks if the queue is empty.
Definition: queue.h:47
Queue::Pop
ID< T > Pop()
Pops an item from the queue.
Definition: queue.h:30
Queue
Definition: queue.h:15