summaryrefslogtreecommitdiff
path: root/src/queue.h
blob: 4785e67adae6abaff3ce0f273e5ff8b33ad3caae (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (c) 2007, Novell Inc.
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

/*
 * queue.h
 *
 */

#ifndef LIBSOLV_QUEUE_H
#define LIBSOLV_QUEUE_H

#include "pooltypes.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct _Queue {
  Id *elements;		/* pointer to elements */
  int count;		/* current number of elements in queue */
  Id *alloc;		/* this is whats actually allocated, elements > alloc if shifted */
  int left;		/* space left in alloc *after* elements+count */
} Queue;


extern void queue_alloc_one(Queue *q);		/* internal */
extern void queue_alloc_one_head(Queue *q);	/* internal */

/* clear queue */
static inline void
queue_empty(Queue *q)
{
  if (q->alloc)
    {
      q->left += (q->elements - q->alloc) + q->count;
      q->elements = q->alloc;
    }
  else
    q->left += q->count;
  q->count = 0;
}

static inline Id
queue_shift(Queue *q)
{
  if (!q->count)
    return 0;
  q->count--;
  return *q->elements++;
}

static inline Id
queue_pop(Queue *q)
{
  if (!q->count)
    return 0;
  q->left++;
  return q->elements[--q->count];
}

static inline void
queue_unshift(Queue *q, Id id)
{
  if (!q->alloc || q->alloc == q->elements)
    queue_alloc_one_head(q);
  *--q->elements = id;
  q->count++;
}

static inline void
queue_push(Queue *q, Id id)
{
  if (!q->left)
    queue_alloc_one(q);
  q->elements[q->count++] = id;
  q->left--;
}

static inline void
queue_pushunique(Queue *q, Id id)
{
  int i;
  for (i = q->count; i > 0; )
    if (q->elements[--i] == id)
      return;
  queue_push(q, id);
}

static inline void
queue_push2(Queue *q, Id id1, Id id2)
{
  queue_push(q, id1);
  queue_push(q, id2);
}

static inline void
queue_truncate(Queue *q, int n)
{
  if (q->count > n)
    {
      q->left += q->count - n;
      q->count = n;
    }
}

extern void queue_init(Queue *q);
extern void queue_init_buffer(Queue *q, Id *buf, int size);
extern void queue_init_clone(Queue *t, Queue *s);
extern void queue_free(Queue *q);

extern void queue_insert(Queue *q, int pos, Id id);
extern void queue_insert2(Queue *q, int pos, Id id1, Id id2);
extern void queue_insertn(Queue *q, int pos, int n, Id *elements);
extern void queue_delete(Queue *q, int pos);
extern void queue_delete2(Queue *q, int pos);
extern void queue_deleten(Queue *q, int pos, int n);
extern void queue_prealloc(Queue *q, int n);

#ifdef __cplusplus
}
#endif

#endif /* LIBSOLV_QUEUE_H */