Inexor
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cube_queue.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 
8 template <class T, int SIZE> struct queue
9 {
10  int head, tail, len;
11  T data[SIZE];
12 
13  queue() { clear(); }
14 
15  void clear() { head = tail = len = 0; }
16 
17  int length() const { return len; }
18  bool empty() const { return !len; }
19  bool full() const { return len == SIZE; }
20 
21  bool inrange(size_t i) const { return i<size_t(len); }
22  bool inrange(int i) const { return i>=0 && i<len; }
23 
24  T &added() { return data[tail > 0 ? tail-1 : SIZE-1]; }
25  T &added(int offset) { return data[tail-offset > 0 ? tail-offset-1 : tail-offset-1 + SIZE]; }
26  T &adding() { return data[tail]; }
27  T &adding(int offset) { return data[tail+offset >= SIZE ? tail+offset - SIZE : tail+offset]; }
28  T &add()
29  {
30  T &t = data[tail];
31  tail++;
32  if(tail >= SIZE) tail -= SIZE;
33  if(len < SIZE) len++;
34  return t;
35  }
36  T &add(const T &e) { return add() = e; }
37 
38  T &pop()
39  {
40  tail--;
41  if(tail < 0) tail += SIZE;
42  len--;
43  return data[tail];
44  }
45 
46  T &removing() { return data[head]; }
47  T &removing(int offset) { return data[head+offset >= SIZE ? head+offset - SIZE : head+offset]; }
48  T &remove()
49  {
50  T &t = data[head];
51  head++;
52  if(head >= SIZE) head -= SIZE;
53  len--;
54  return t;
55  }
56 
57  T &operator[](int offset) { return removing(offset); }
58  const T &operator[](int offset) const { return removing(offset); }
59 };
60 
61 template <class T, int SIZE> struct reversequeue : queue<T, SIZE>
62 {
63  T &operator[](int offset) { return queue<T, SIZE>::added(offset); }
64  const T &operator[](int offset) const { return queue<T, SIZE>::added(offset); }
65 };
T & removing()
Definition: cube_queue.hpp:46
T & adding()
Definition: cube_queue.hpp:26
Definition: cube_queue.hpp:61
T & operator[](int offset)
Definition: cube_queue.hpp:63
const T & operator[](int offset) const
Definition: cube_queue.hpp:64
T & add()
Definition: cube_queue.hpp:28
const T & operator[](int offset) const
Definition: cube_queue.hpp:58
bool inrange(int i) const
Definition: cube_queue.hpp:22
T & pop()
Definition: cube_queue.hpp:38
Legacy implementation of queues.
Definition: cube_queue.hpp:8
queue()
Definition: cube_queue.hpp:13
int length() const
Definition: cube_queue.hpp:17
int len
Definition: cube_queue.hpp:10
T & adding(int offset)
Definition: cube_queue.hpp:27
bool empty() const
Definition: cube_queue.hpp:18
bool full() const
Definition: cube_queue.hpp:19
T & operator[](int offset)
Definition: cube_queue.hpp:57
T & added(int offset)
Definition: cube_queue.hpp:25
bool inrange(size_t i) const
Definition: cube_queue.hpp:21
T & add(const T &e)
Definition: cube_queue.hpp:36
void t(T x, const char *cmp)
Definition: utilTest.cpp:52
int head
Definition: cube_queue.hpp:10
GLintptr offset
Definition: glexts.hpp:354
int tail
Definition: cube_queue.hpp:10
T & removing(int offset)
Definition: cube_queue.hpp:47
T data[SIZE]
Definition: cube_queue.hpp:11
T & added()
Definition: cube_queue.hpp:24
void clear()
Definition: cube_queue.hpp:15