Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnIndexQueue.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnIndexQueue.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 **
11 *****************************************************************************/
12 
13 #pragma once
14 
15 #include <vector>
16 
17 using namespace std;
18 
19 // drwnIndexQueue -----------------------------------------------------------
37 
39  protected:
40  static const int TERMINAL = -1;
41 
42  int _head, _tail;
43  vector<int> _rev;
44  vector<int> _fwd;
45 
46  public:
48  drwnIndexQueue() : _head(TERMINAL), _tail(TERMINAL) { /* do nothing */ }
50  drwnIndexQueue(size_t n) : _head(TERMINAL), _tail(TERMINAL),
51  _rev(n, TERMINAL), _fwd(n, TERMINAL) { /* do nothing */ }
53  ~drwnIndexQueue() { /* do nothing */ }
54 
56  inline bool empty() const { return (_head == TERMINAL); }
57 
59  inline void clear() {
60  _head = _tail = TERMINAL;
61  fill(_rev.begin(), _rev.end(), TERMINAL);
62  fill(_fwd.begin(), _fwd.end(), TERMINAL);
63  }
64 
66  inline void resize(size_t n) {
67  _rev.resize(n); _fwd.resize(n); clear();
68  }
69 
71  inline int front() const { return _head; }
73  inline int back() const { return _tail; }
74 
76  inline bool is_queued(int u) const {
77  return (_rev[u] != TERMINAL);
78  }
79 
81  inline void push_back(int u) {
82  if (is_queued(u)) return;
83 
84  if (_tail == TERMINAL) {
85  _head = _tail = _fwd[u] = _rev[u] = u;
86  } else {
87  _fwd[u] = _head;
88  _rev[u] = _tail;
89  _fwd[_tail] = u;
90  _rev[_head] = u;
91  _tail = u;
92  }
93  }
94 
96  inline void push_front(int u) {
97  if (is_queued(u)) return;
98 
99  if (_head == TERMINAL) {
100  _head = _tail = _fwd[u] = _rev[u] = u;
101  } else {
102  _fwd[u] = _head;
103  _rev[u] = _tail;
104  _fwd[_tail] = u;
105  _rev[_head] = u;
106  _head = u;
107  }
108  }
109 
111  inline void pop_front() {
112  DRWN_ASSERT(_head != TERMINAL);
113  _rev[_head] = TERMINAL;
114  if (_head == _tail) {
115  _head = _tail = TERMINAL;
116  } else {
117  _head = _fwd[_head];
118  _rev[_head] = _tail;
119  _fwd[_tail] = _head;
120  }
121  }
122 
124  inline void erase(int u) {
125  if (!is_queued(u)) return;
126 
127  if (_head == _tail) {
128  _head = _tail = TERMINAL;
129  } else {
130  _fwd[_rev[u]] = _fwd[u];
131  _rev[_fwd[u]] = _rev[u];
132  if (_head == u) _head = _fwd[u];
133  if (_tail == u) _tail = _rev[u];
134  }
135 
136  _rev[u] = TERMINAL;
137  }
138 };
139 
141 std::string toString(const drwnIndexQueue& q);
void push_front(int u)
add u to the front of the queue
Definition: drwnIndexQueue.h:96
void resize(size_t n)
clears and resizes the queue
Definition: drwnIndexQueue.h:66
void erase(int u)
remove u from the queue or do nothing if not in the queue
Definition: drwnIndexQueue.h:124
int front() const
return the element at the front of the queue (or -1 if empty)
Definition: drwnIndexQueue.h:71
int back() const
return the element at the back of the queue (or -1 if empty)
Definition: drwnIndexQueue.h:73
void clear()
clears the queue
Definition: drwnIndexQueue.h:59
bool empty() const
returns true if the queue is empty
Definition: drwnIndexQueue.h:56
drwnIndexQueue(size_t n)
constructor takes the maximum number of indexes
Definition: drwnIndexQueue.h:50
std::string toString(const T &v)
Templated function to make conversion from simple data types like int and double to strings easy for ...
Definition: drwnStrUtils.h:134
drwnIndexQueue()
default constructor of an empty queue
Definition: drwnIndexQueue.h:48
void pop_front()
remove the elelemt from the head of the queue
Definition: drwnIndexQueue.h:111
~drwnIndexQueue()
destructor
Definition: drwnIndexQueue.h:53
bool is_queued(int u) const
returns true if u is in the queue
Definition: drwnIndexQueue.h:76
Provides a queue datastructure on a fixed number of indexes. At most one copy of each index can appea...
Definition: drwnIndexQueue.h:38
void push_back(int u)
add u to the back of the queue
Definition: drwnIndexQueue.h:81