Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Queue.h
Go to the documentation of this file.
1 /*****************************************************************************************[Queue.h]
2 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007-2010, Niklas Sorensson
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6 associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute,
8 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 **************************************************************************************************/
20 
21 #pragma once
22 
24 
25 namespace Minisat {
26 namespace Internal {
27 
28 //=================================================================================================
29 
30 template<class T>
31 class Queue {
33  int first;
34  int end;
35 
36 public:
37  using Key = T;
38 
39  Queue() : buf(1), first(0), end(0) {}
40 
41  void clear (bool dealloc = false) { buf.clear(dealloc); buf.growTo(1); first = end = 0; }
42  int size () const { return (end >= first) ? end - first : end - first + buf.size(); }
43 
44  const T& operator [] (int index) const { assert(index >= 0); assert(index < size()); return buf[(first + index) % buf.size()]; }
45  T& operator [] (int index) { assert(index >= 0); assert(index < size()); return buf[(first + index) % buf.size()]; }
46 
47  T peek () const { assert(first != end); return buf[first]; }
48  void pop () { assert(first != end); first++; if (first == buf.size()) first = 0; }
49  void insert(T elem) { // INVARIANT: buf[end] is always unused
50  buf[end++] = elem;
51  if (end == buf.size()) end = 0;
52  if (first == end){ // Resize:
53  vec<T> tmp((buf.size()*3 + 1) >> 1);
54 #if 0
55  **/printf("queue alloc: %d elems (%.1f MB)\n", tmp.size(), tmp.size() * sizeof(T) / 1000000.0);
56 #endif
57  int i = 0;
58  for (int j = first; j < buf.size(); j++) tmp[i++] = buf[j];
59  for (int j = 0 ; j < end ; j++) tmp[i++] = buf[j];
60  first = 0;
61  end = buf.size();
62  tmp.moveTo(buf);
63  }
64  }
65 };
66 
67 
68 //=================================================================================================
69 } // namespace Internal
70 } // namespace Minisat
Minisat::Internal::Queue< CRef >::Key
CRef Key
Definition: Queue.h:37
Vec.h
Minisat::Internal::Queue::clear
void clear(bool dealloc=false)
Definition: Queue.h:41
Minisat::Internal::Queue::peek
T peek() const
Definition: Queue.h:47
Minisat::Internal::vec::moveTo
void moveTo(vec< T > &dest)
Definition: Vec.h:91
Minisat::Internal::Queue::end
int end
Definition: Queue.h:34
Minisat::Internal::Queue::buf
vec< T > buf
Definition: Queue.h:32
Minisat::Internal::Queue
Definition: Queue.h:31
Minisat::Internal::vec::size
int size(void) const
Definition: Vec.h:63
Minisat::Internal::Queue::Queue
Queue()
Definition: Queue.h:39
Minisat::Internal::Queue::insert
void insert(T elem)
Definition: Queue.h:49
Minisat
Definition: Minisat.h:53
Minisat::Internal::Queue::operator[]
const T & operator[](int index) const
Definition: Queue.h:44
Minisat::Internal::vec
Definition: Vec.h:38
Minisat::Internal::Queue::size
int size() const
Definition: Queue.h:42
Minisat::Internal::Queue::pop
void pop()
Definition: Queue.h:48
Minisat::Internal::Queue::first
int first
Definition: Queue.h:33