ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
BoundedQueue.h
Go to the documentation of this file.
1/***********************************************************************************[BoundedQueue.h]
2 Glucose -- Copyright (c) 2009, Gilles Audemard, Laurent Simon
3 CRIL - Univ. Artois, France
4 LRI - Univ. Paris Sud, France
5
6Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7associated documentation files (the "Software"), to deal in the Software without restriction,
8including without limitation the rights to use, copy, modify, merge, publish, distribute,
9sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or
13substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
19OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20**************************************************************************************************/
21
22
23#ifndef BoundedQueue_h
24#define BoundedQueue_h
25
26#include "sat/glucose/Vec.h"
27
29
30//=================================================================================================
31
32namespace Gluco {
33
34template <class T>
35class bqueue {
36 vec<T> elems;
37 int first;
38 int last;
39 uint64_t sumofqueue;
40 int maxsize;
41 int queuesize; // Number of current elements (must be < maxsize !)
42 bool expComputed;
43 double exp,value;
44public:
45 bqueue(void) : first(0), last(0), sumofqueue(0), maxsize(0), queuesize(0),expComputed(false) { }
46
47 void initSize(int size) {growTo(size);exp = 2.0/(size+1);} // Init size of bounded size queue
48
49 void push(T x) {
50 expComputed = false;
51 if (queuesize==maxsize) {
52 assert(last==first); // The queue is full, next value to enter will replace oldest one
53 sumofqueue -= elems[last];
54 if ((++last) == maxsize) last = 0;
55 } else
56 queuesize++;
57 sumofqueue += x;
58 elems[first] = x;
59 if ((++first) == maxsize) {first = 0;last = 0;}
60 }
61
62 T peek() { assert(queuesize>0); return elems[last]; }
63 void pop() {sumofqueue-=elems[last]; queuesize--; if ((++last) == maxsize) last = 0;}
64
65 uint64_t getsum() const {return sumofqueue;}
66 unsigned int getavg() const {return (unsigned int)(sumofqueue/((uint64_t)queuesize));}
67 int maxSize() const {return maxsize;}
68 double getavgDouble() const {
69 double tmp = 0;
70 for(int i=0;i<elems.size();i++) {
71 tmp+=elems[i];
72 }
73 return tmp/elems.size();
74 }
75 int isvalid() const {return (queuesize==maxsize);}
76
77 void growTo(int size) {
78 elems.growTo(size);
79 first=0; maxsize=size; queuesize = 0;last = 0;
80 for(int i=0;i<size;i++) elems[i]=0;
81 }
82
83 double getAvgExp() {
84 if(expComputed) return value;
85 double a=exp;
86 value = elems[first];
87 for(int i = first;i<maxsize;i++) {
88 value+=a*((double)elems[i]);
89 a=a*exp;
90 }
91 for(int i = 0;i<last;i++) {
92 value+=a*((double)elems[i]);
93 a=a*exp;
94 }
95 value = value*(1-exp)/(1-a);
96 expComputed = true;
97 return value;
98
99
100 }
101 void fastclear() {first = 0; last = 0; queuesize=0; sumofqueue=0;} // to be called after restarts... Discard the queue
102
103 int size(void) { return queuesize; }
104
105 void clear(bool dealloc = false) { elems.clear(dealloc); first = 0; maxsize=0; queuesize=0;sumofqueue=0;}
106
107
108};
109}
110//=================================================================================================
111
113
114#endif
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
double getavgDouble() const
void growTo(int size)
int maxSize() const
void initSize(int size)
void clear(bool dealloc=false)
int size(void)
void push(T x)
int isvalid() const
double getAvgExp()
unsigned int getavg() const
uint64_t getsum() const
Definition Alg.h:28
#define false
Definition place_base.h:29
#define assert(ex)
Definition util_old.h:213