SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
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 #ifndef Minisat_Queue_h
22 #define Minisat_Queue_h
23 
24 #include "Vec.h"
25 
26 namespace Minisat
27 {
28  //=================================================================================================
29 
30  template<class T>
31  class Queue
32  {
34  int first;
35  int end;
36 
37  public:
38  typedef T Key;
39 
40  Queue():
41  buf( 1 ),
42  first( 0 ),
43  end( 0 )
44  {}
45 
46  void clear( bool dealloc = false )
47  {
48  buf.clear( dealloc );
49  buf.growTo( 1 );
50  first = end = 0;
51  }
52 
53  int size() const
54  {
55  return (end >= first) ? end - first : end - first + buf.size();
56  }
57 
58  const T& operator []( int index ) const
59  {
60  assert( index >= 0 );
61  assert( index < size() );
62  return buf[(first + index) % buf.size()];
63  }
64 
65  T& operator []( int index )
66  {
67  assert( index >= 0 );
68  assert( index < size() );
69  return buf[(first + index) % buf.size()];
70  }
71 
72  T peek() const
73  {
74  assert( first != end );
75  return buf[first];
76  }
77 
78  void pop()
79  {
80  assert( first != end );
81  first++;
82  if( first == buf.size() )
83  first = 0;
84  }
85 
86  void insert( T elem )
87  { // INVARIANT: buf[end] is always unused
88  buf[end++] = elem;
89  if( end == buf.size() )
90  end = 0;
91  if( first == end )
92  { // Resize:
93  vec<T> tmp( (buf.size() * 3 + 1) >> 1 );
94  //**/printf("queue alloc: %d elems (%.1f MB)\n", tmp.size(), tmp.size() * sizeof(T) / 1000000.0);
95  int i = 0;
96  for( int j = first; j < buf.size(); j++ )
97  tmp[i++] = buf[j];
98  for( int j = 0; j < end; j++ )
99  tmp[i++] = buf[j];
100  first = 0;
101  end = buf.size();
102  tmp.moveTo( buf );
103  }
104  }
105  };
106 
107  //=================================================================================================
108 }
109 
110 #endif
vec< T > buf
Definition: Queue.h:33
void clear(bool dealloc=false)
Definition: Queue.h:46
T peek() const
Definition: Queue.h:72
int size() const
Definition: Queue.h:53
void insert(T elem)
Definition: Queue.h:86
const T & operator[](int index) const
Definition: Queue.h:58
void pop()
Definition: Queue.h:78
int first
Definition: Queue.h:34
void moveTo(vec< T > &dest)
Definition: Vec.h:208
Definition: Alg.h:27