The bliss C++ API
kqueue.hh
1 #pragma once
2 
3 /*
4  Copyright (c) 2003-2021 Tommi Junttila
5  Released under the GNU Lesser General Public License version 3.
6 
7  This file is part of bliss.
8 
9  bliss is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, version 3 of the License.
12 
13  bliss is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License
19  along with bliss. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include <new>
23 #include <cassert>
24 
25 namespace bliss {
26 
30 template <class Type>
31 class KQueue
32 {
33 public:
38  KQueue();
39 
40  ~KQueue();
41 
45  void init(const unsigned int N);
46 
48  bool is_empty() const;
49 
51  unsigned int size() const;
52 
54  void clear();
55 
57  Type front() const;
58 
60  Type pop_front();
61 
63  void push_front(Type e);
64 
66  Type pop_back();
67 
69  void push_back(Type e);
70 private:
71  Type *entries, *end;
72  Type *head, *tail;
73 };
74 
75 template <class Type>
77 {
78  entries = nullptr;
79  end = nullptr;
80  head = nullptr;
81  tail = nullptr;
82 }
83 
84 template <class Type>
86 {
87  delete[] entries;
88  entries = nullptr;
89  end = nullptr;
90  head = nullptr;
91  tail = nullptr;
92 }
93 
94 template <class Type>
95 void KQueue<Type>::init(const unsigned int k)
96 {
97  assert(k > 0);
98  delete[] entries;
99  entries = new Type[k+1];
100  end = entries + k + 1;
101  head = entries;
102  tail = head;
103 }
104 
105 template <class Type>
107 {
108  head = entries;
109  tail = head;
110 }
111 
112 template <class Type>
114 {
115  return head == tail;
116 }
117 
118 template <class Type>
119 unsigned int KQueue<Type>::size() const
120 {
121  if(tail >= head)
122  return(tail - head);
123  return (end - head) + (tail - entries);
124 }
125 
126 template <class Type>
128 {
129  assert(head != tail);
130  return *head;
131 }
132 
133 template <class Type>
135 {
136  assert(head != tail);
137  Type *old_head = head;
138  head++;
139  if(head == end)
140  head = entries;
141  return *old_head;
142 }
143 
144 template <class Type>
146 {
147  if(head == entries)
148  head = end - 1;
149  else
150  head--;
151  assert(head != tail);
152  *head = e;
153 }
154 
155 template <class Type>
157 {
158  *tail = e;
159  tail++;
160  if(tail == end)
161  tail = entries;
162  assert(head != tail);
163 }
164 
165 } // namespace bliss
void push_back(Type e)
Definition: kqueue.hh:156
Definition: abstractgraph.cc:35
A simple implementation of queues with fixed maximum capacity.
Definition: kqueue.hh:31
KQueue()
Definition: kqueue.hh:76
void push_front(Type e)
Definition: kqueue.hh:145
Type pop_front()
Definition: kqueue.hh:134
void init(const unsigned int N)
Definition: kqueue.hh:95
unsigned int size() const
Definition: kqueue.hh:119
void clear()
Definition: kqueue.hh:106
bool is_empty() const
Definition: kqueue.hh:113
Type front() const
Definition: kqueue.hh:127