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