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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/**CFile****************************************************************
FileName [vecQue.h]
SystemName [ABC: Logic synthesis and verification system.]
PackageName [Resizable arrays.]
Synopsis [Priority queue.]
Author [Alan Mishchenko]
Affiliation [UC Berkeley]
Date [Ver. 1.0. Started - June 20, 2005.]
Revision [$Id: vecQue.h,v 1.00 2005/06/20 00:00:00 alanmi Exp $]
***********************************************************************/
#ifndef ABC__misc__vec__Queue_h
#define ABC__misc__vec__Queue_h
////////////////////////////////////////////////////////////////////////
/// INCLUDES ///
////////////////////////////////////////////////////////////////////////
#include <stdio.h>
ABC_NAMESPACE_HEADER_START
////////////////////////////////////////////////////////////////////////
/// PARAMETERS ///
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/// BASIC TYPES ///
////////////////////////////////////////////////////////////////////////
typedef struct Vec_Que_t_ Vec_Que_t;
struct Vec_Que_t_
{
int nCap;
int nSize;
int * pHeap;
int * pOrder;
float ** pCostsFlt; // owned by the caller
};
static inline float Vec_QuePrio( Vec_Que_t * p, int v ) { return *p->pCostsFlt ? (*p->pCostsFlt)[v] : v; }
////////////////////////////////////////////////////////////////////////
/// MACRO DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline Vec_Que_t * Vec_QueAlloc( int nCap )
{
Vec_Que_t * p;
p = ABC_CALLOC( Vec_Que_t, 1 );
if ( nCap < 16 )
nCap = 16;
p->nSize = 1;
p->nCap = nCap + 1;
p->pHeap = ABC_FALLOC( int, p->nCap );
p->pOrder = ABC_FALLOC( int, p->nCap );
return p;
}
static inline void Vec_QueFree( Vec_Que_t * p )
{
ABC_FREE( p->pOrder );
ABC_FREE( p->pHeap );
ABC_FREE( p );
}
static inline void Vec_QueFreeP( Vec_Que_t ** p )
{
if ( *p )
Vec_QueFree( *p );
*p = NULL;
}
static inline void Vec_QueSetPriority( Vec_Que_t * p, float ** pCosts )
{
assert( p->pCostsFlt == NULL );
p->pCostsFlt = pCosts;
}
static inline void Vec_QueGrow( Vec_Que_t * p, int nCapMin )
{
if ( p->nCap >= nCapMin )
return;
p->pHeap = ABC_REALLOC( int, p->pHeap, nCapMin );
p->pOrder = ABC_REALLOC( int, p->pOrder, nCapMin );
memset( p->pHeap + p->nCap, 0xff, (nCapMin - p->nCap) * sizeof(int) );
memset( p->pOrder + p->nCap, 0xff, (nCapMin - p->nCap) * sizeof(int) );
p->nCap = nCapMin;
}
static inline void Vec_QueClear( Vec_Que_t * p )
{
int i;
assert( p->pHeap[0] == -1 );
for ( i = 1; i < p->nSize; i++ )
{
assert( p->pHeap[i] >= 0 && p->pOrder[p->pHeap[i]] == i );
p->pOrder[p->pHeap[i]] = -1;
p->pHeap[i] = -1;
}
p->nSize = 1;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline int Vec_QueSize( Vec_Que_t * p )
{
assert( p->nSize > 0 );
return p->nSize - 1;
}
static inline int Vec_QueTop( Vec_Que_t * p )
{
return Vec_QueSize(p) > 0 ? p->pHeap[1] : -1;
}
static inline float Vec_QueTopPriority( Vec_Que_t * p )
{
return Vec_QueSize(p) > 0 ? Vec_QuePrio(p, p->pHeap[1]) : -ABC_INFINITY;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline int Vec_QueMoveUp( Vec_Que_t * p, int v )
{
float Cost = Vec_QuePrio(p, v);
int i = p->pOrder[v];
int parent = i >> 1;
int fMoved = 0;
assert( p->pOrder[v] != -1 );
assert( p->pHeap[i] == v );
while ( i > 1 && Cost > Vec_QuePrio(p, p->pHeap[parent]) )
{
p->pHeap[i] = p->pHeap[parent];
p->pOrder[p->pHeap[i]] = i;
i = parent;
parent = i >> 1;
fMoved = 1;
}
p->pHeap[i] = v;
p->pOrder[v] = i;
return fMoved;
}
static inline void Vec_QueMoveDown( Vec_Que_t * p, int v )
{
float Cost = Vec_QuePrio(p, v);
int i = p->pOrder[v];
int child = i << 1;
while ( child < p->nSize )
{
if ( child + 1 < p->nSize && Vec_QuePrio(p, p->pHeap[child]) < Vec_QuePrio(p, p->pHeap[child+1]) )
child++;
assert( child < p->nSize );
if ( Cost >= Vec_QuePrio(p, p->pHeap[child]))
break;
p->pHeap[i] = p->pHeap[child];
p->pOrder[p->pHeap[i]] = i;
i = child;
child = child << 1;
}
p->pHeap[i] = v;
p->pOrder[v] = i;
}
static inline void Vec_QueUpdate( Vec_Que_t * p, int v )
{
if ( !Vec_QueMoveUp( p, v ) )
Vec_QueMoveDown( p, v );
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline int Vec_QueIsMember( Vec_Que_t * p, int v )
{
assert( v >= 0 );
return (int)( v < p->nCap && p->pOrder[v] >= 0 );
}
static inline void Vec_QuePush( Vec_Que_t * p, int v )
{
if ( p->nSize >= p->nCap )
Vec_QueGrow( p, Abc_MaxInt(p->nSize+1, 2*p->nCap) );
if ( v >= p->nCap )
Vec_QueGrow( p, Abc_MaxInt(v+1, 2*p->nCap) );
assert( p->nSize < p->nCap );
assert( p->pOrder[v] == -1 );
assert( p->pHeap[p->nSize] == -1 );
p->pOrder[v] = p->nSize;
p->pHeap[p->nSize++] = v;
Vec_QueMoveUp( p, v );
}
static inline int Vec_QuePop( Vec_Que_t * p )
{
int v, Res;
assert( p->nSize > 1 );
Res = p->pHeap[1]; p->pOrder[Res] = -1;
if ( --p->nSize == 1 )
{
p->pHeap[1] = -1;
return Res;
}
v = p->pHeap[p->nSize]; p->pHeap[p->nSize] = -1;
p->pHeap[1] = v; p->pOrder[v] = 1;
Vec_QueMoveDown( p, v );
return Res;
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline void Vec_QuePrint( Vec_Que_t * p )
{
int i, k, m;
for ( i = k = 1; i < p->nSize; i += k, k *= 2 )
{
for ( m = 0; m < k; m++ )
if ( i+m < p->nSize )
printf( "%-5d", p->pHeap[i+m] );
printf( "\n" );
for ( m = 0; m < k; m++ )
if ( i+m < p->nSize )
printf( "%-5.0f", Vec_QuePrio(p, p->pHeap[i+m]) );
printf( "\n" );
printf( "\n" );
}
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline void Vec_QueCheck( Vec_Que_t * p )
{
int i, child;
assert( p->nSize > 0 );
assert( p->nSize <= p->nCap );
// check mapping
for ( i = 0; i < p->nSize-1; i++ )
assert( p->pOrder[i] > 0 );
for ( ; i < p->nCap; i++ )
assert( p->pOrder[i] == -1 );
// check heap
assert( p->pHeap[0] == -1 );
for ( i = 0; i < p->nSize-1; i++ )
assert( p->pHeap[p->pOrder[i]] == i );
for ( i++; i < p->nCap; i++ )
assert( p->pHeap[i] == -1 );
// check heap property
for ( i = 1; i < p->nSize; i++ )
{
child = i << 1;
if ( child < p->nSize )
assert( Vec_QuePrio(p, p->pHeap[i]) >= Vec_QuePrio(p, p->pHeap[child]) );
child++;
if ( child < p->nSize )
assert( Vec_QuePrio(p, p->pHeap[i]) >= Vec_QuePrio(p, p->pHeap[child]) );
}
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline void Vec_QueTest( Vec_Flt_t * vCosts )
{
Vec_Int_t * vTop;
Vec_Que_t * p;
int i, Entry;
// start the queue
p = Vec_QueAlloc( Vec_FltSize(vCosts) );
Vec_QueSetPriority( p, Vec_FltArrayP(vCosts) );
for ( i = 0; i < Vec_FltSize(vCosts); i++ )
Vec_QuePush( p, i );
// Vec_QuePrint( p );
Vec_QueCheck( p );
// find the topmost 10%
vTop = Vec_IntAlloc( Vec_FltSize(vCosts) / 10 );
while ( Vec_IntSize(vTop) < Vec_FltSize(vCosts) / 10 )
Vec_IntPush( vTop, Vec_QuePop(p) );
// Vec_IntPrint( vTop );
// Vec_QueCheck( p ); // queque is not ready at this point!!!
// put them back
Vec_IntForEachEntry( vTop, Entry, i )
Vec_QuePush( p, Entry );
Vec_IntFree( vTop );
Vec_QueCheck( p );
Vec_QueFree( p );
}
/*
{
extern void Vec_QueTest( Vec_Flt_t * p );
Vec_QueTest( p->vTimesOut );
}
*/
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////
ABC_NAMESPACE_HEADER_END
#endif
|