summaryrefslogtreecommitdiffstats
path: root/src/sat/satoko/utils/b_queue.h
blob: 926edf290d20047e3b63e76d4ed67fbc3b4f4418 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//===--- b_queue.h ----------------------------------------------------------===
//
//                     satoko: Satisfiability solver
//
// This file is distributed under the BSD 2-Clause License.
// See LICENSE for details.
//
//===------------------------------------------------------------------------===
#ifndef satoko__utils__b_queue_h
#define satoko__utils__b_queue_h

#include "mem.h"

#include "misc/util/abc_global.h"
ABC_NAMESPACE_HEADER_START

/* Bounded Queue */
typedef struct b_queue_t_ b_queue_t;
struct b_queue_t_ {
    unsigned size;
    unsigned cap;
    unsigned i_first;
    unsigned i_empty;
    unsigned long long sum;
    unsigned *data;
};

//===------------------------------------------------------------------------===
// Bounded Queue API
//===------------------------------------------------------------------------===
static inline b_queue_t *b_queue_alloc(unsigned cap)
{
    b_queue_t *p = satoko_calloc(b_queue_t, 1);
    p->cap = cap;
    p->data = satoko_calloc(unsigned, cap);
    return p;
}

static inline void b_queue_free(b_queue_t *p)
{
    satoko_free(p->data);
    satoko_free(p);
}

static inline void b_queue_push(b_queue_t *p, unsigned Value)
{
    if (p->size == p->cap) {
        assert(p->i_first == p->i_empty);
        p->sum -= p->data[p->i_first];
        p->i_first = (p->i_first + 1) % p->cap;
    } else
        p->size++;

    p->sum += Value;
    p->data[p->i_empty] = Value;
    if ((++p->i_empty) == p->cap) {
        p->i_empty = 0;
        p->i_first = 0;
    }
}

static inline unsigned b_queue_avg(b_queue_t *p)
{
    return (unsigned)(p->sum / ((unsigned long long) p->size));
}

static inline unsigned b_queue_is_valid(b_queue_t *p)
{
    return (p->cap == p->size);
}

static inline void b_queue_clean(b_queue_t *p)
{
    p->i_first = 0;
    p->i_empty = 0;
    p->size = 0;
    p->sum = 0;
}

ABC_NAMESPACE_HEADER_END
#endif /* satoko__utils__b_queue_h */