xsatHeap.h 8.14 KB
Newer Older
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
/**CFile****************************************************************

  FileName    [xsatHeap.h]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [xSAT - A SAT solver written in C.
               Read the license file for more info.]

  Synopsis    [Heap implementation.]

  Author      [Bruno Schmitt <boschmitt@inf.ufrgs.br>]

  Affiliation [UC Berkeley / UFRGS]

  Date        [Ver. 1.0. Started - November 10, 2016.]

  Revision    []

***********************************************************************/
#ifndef ABC__sat__xSAT__xsatHeap_h
#define ABC__sat__xSAT__xsatHeap_h

#include "misc/util/abc_global.h"
#include "misc/vec/vecInt.h"

ABC_NAMESPACE_HEADER_START

////////////////////////////////////////////////////////////////////////
///                    STRUCTURE DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////
typedef struct xSAT_Heap_t_ xSAT_Heap_t;
struct xSAT_Heap_t_
{
    Vec_Int_t * vActivity;
    Vec_Int_t * vIndices;
    Vec_Int_t * vHeap;
};

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
51
static inline int xSAT_HeapSize( xSAT_Heap_t * h )
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
{
    return Vec_IntSize( h->vHeap );
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
67
static inline int xSAT_HeapInHeap( xSAT_Heap_t * h, int Var )
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
{
    return ( Var < Vec_IntSize( h->vIndices ) ) && ( Vec_IntEntry( h->vIndices, Var ) >= 0 );
}

static inline int Left  ( int i ) { return 2 * i + 1; }
static inline int Right ( int i ) { return ( i + 1 ) * 2; }
static inline int Parent( int i ) { return ( i - 1 ) >> 1; }
static inline int Compare( xSAT_Heap_t * p, int x, int y )
{
    return ( unsigned )Vec_IntEntry( p->vActivity, x ) > ( unsigned )Vec_IntEntry( p->vActivity, y );
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void xSAT_HeapPercolateUp( xSAT_Heap_t * h, int i )
{
    int x = Vec_IntEntry( h->vHeap, i );
    int p = Parent( i );

    while ( i != 0 && Compare( h, x, Vec_IntEntry( h->vHeap, p ) ) )
    {
        Vec_IntWriteEntry( h->vHeap, i, Vec_IntEntry( h->vHeap, p ) );
        Vec_IntWriteEntry( h->vIndices, Vec_IntEntry( h->vHeap, p ), i );
        i = p;
        p = Parent(p);
    }
    Vec_IntWriteEntry( h->vHeap, i, x );
    Vec_IntWriteEntry( h->vIndices, x, i );
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void xSAT_HeapPercolateDown( xSAT_Heap_t * h, int i )
{
    int x = Vec_IntEntry( h->vHeap, i );

    while ( Left( i ) < Vec_IntSize( h->vHeap ) )
    {
        int child = Right( i ) < Vec_IntSize( h->vHeap ) &&
                    Compare( h, Vec_IntEntry( h->vHeap, Right( i ) ), Vec_IntEntry( h->vHeap, Left( i ) ) ) ?
                    Right( i ) : Left( i );

        if ( !Compare( h, Vec_IntEntry( h->vHeap, child ), x ) )
            break;

        Vec_IntWriteEntry( h->vHeap, i, Vec_IntEntry( h->vHeap, child ) );
        Vec_IntWriteEntry( h->vIndices, Vec_IntEntry( h->vHeap, i ), i );
        i = child;
    }
    Vec_IntWriteEntry( h->vHeap, i, x );
    Vec_IntWriteEntry( h->vIndices, x, i );
}

////////////////////////////////////////////////////////////////////////
///                     FUNCTION DEFINITIONS                         ///
////////////////////////////////////////////////////////////////////////

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline xSAT_Heap_t * xSAT_HeapAlloc( Vec_Int_t * vActivity )
{
    xSAT_Heap_t * p = ABC_ALLOC( xSAT_Heap_t, 1 );
    p->vActivity = vActivity;
    p->vIndices = Vec_IntAlloc( 0 );
    p->vHeap = Vec_IntAlloc( 0 );

    return p;
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void xSAT_HeapFree( xSAT_Heap_t * p )
{
    Vec_IntFree( p->vIndices );
    Vec_IntFree( p->vHeap );
    ABC_FREE( p );
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void xSAT_HeapIncrease( xSAT_Heap_t * h, int e )
{
    assert( xSAT_HeapInHeap( h, e ) );
    xSAT_HeapPercolateDown( h, Vec_IntEntry( h->vIndices, e ) );
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void xSAT_HeapDecrease( xSAT_Heap_t * p, int e )
{
    assert( xSAT_HeapInHeap( p, e ) );
    xSAT_HeapPercolateUp( p , Vec_IntEntry( p->vIndices, e ) );
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void xSAT_HeapInsert( xSAT_Heap_t * p, int n )
{
    Vec_IntFillExtra( p->vIndices, n + 1, -1);
    assert( !xSAT_HeapInHeap( p, n ) );

    Vec_IntWriteEntry( p->vIndices, n, Vec_IntSize( p->vHeap ) );
    Vec_IntPush( p->vHeap, n );
    xSAT_HeapPercolateUp( p, Vec_IntEntry( p->vIndices, n ) );
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void xSAT_HeapUpdate( xSAT_Heap_t * p, int i )
{
    if ( !xSAT_HeapInHeap( p, i ) )
        xSAT_HeapInsert( p, i );
    else
    {
        xSAT_HeapPercolateUp( p, Vec_IntEntry( p->vIndices, i ) );
        xSAT_HeapPercolateDown( p, Vec_IntEntry( p->vIndices, i ) );
    }
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void xSAT_HeapBuild( xSAT_Heap_t * p, Vec_Int_t * Vars )
{
    int i, Var;

    Vec_IntForEachEntry( p->vHeap, Var, i )
        Vec_IntWriteEntry( p->vIndices, Var, -1 );
    Vec_IntClear( p->vHeap );

    Vec_IntForEachEntry( Vars, Var, i )
    {
        Vec_IntWriteEntry( p->vIndices, Var, i );
        Vec_IntPush( p->vHeap, Var );
    }

    for ( ( i = Vec_IntSize( p->vHeap ) / 2 - 1 ); i >= 0; i-- )
        xSAT_HeapPercolateDown( p, i );
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void xSAT_HeapClear( xSAT_Heap_t * p )
{
    Vec_IntFill( p->vIndices, Vec_IntSize( p->vIndices ), -1 );
    Vec_IntClear( p->vHeap );
}

/**Function*************************************************************

  Synopsis    []

  Description []

  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int xSAT_HeapRemoveMin( xSAT_Heap_t * p )
{
    int x = Vec_IntEntry( p->vHeap, 0 );
    Vec_IntWriteEntry( p->vHeap, 0, Vec_IntEntryLast( p->vHeap ) );
    Vec_IntWriteEntry( p->vIndices, Vec_IntEntry( p->vHeap, 0), 0 );
    Vec_IntWriteEntry( p->vIndices, x, -1 );
    Vec_IntPop( p->vHeap );
    if ( Vec_IntSize( p->vHeap ) > 1 )
        xSAT_HeapPercolateDown( p, 0 );
    return x;
}

ABC_NAMESPACE_HEADER_END

#endif