vecFlt.h 19.1 KB
Newer Older
Alan Mishchenko committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**CFile****************************************************************

  FileName    [vecFlt.h]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Resizable arrays.]

  Synopsis    [Resizable arrays of floats.]

  Author      [Aaron P. Hurst]
  
  Affiliation [UC Berkeley]

  Date        [Ver. 1.0. Started - June 20, 2005.]

  Revision    [$Id: vecInt.h,v 1.00 2005/06/20 00:00:00 alanmi Exp $]

***********************************************************************/
 
21 22
#ifndef ABC__misc__vec__vecFlt_h
#define ABC__misc__vec__vecFlt_h
Alan Mishchenko committed
23

24

Alan Mishchenko committed
25 26 27 28 29 30
////////////////////////////////////////////////////////////////////////
///                          INCLUDES                                ///
////////////////////////////////////////////////////////////////////////

#include <stdio.h>

31 32 33
ABC_NAMESPACE_HEADER_START


Alan Mishchenko committed
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
////////////////////////////////////////////////////////////////////////
///                         PARAMETERS                               ///
////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////
///                         BASIC TYPES                              ///
////////////////////////////////////////////////////////////////////////

typedef struct Vec_Flt_t_       Vec_Flt_t;
struct Vec_Flt_t_ 
{
    int              nCap;
    int              nSize;
    float *          pArray;
};

////////////////////////////////////////////////////////////////////////
///                      MACRO DEFINITIONS                           ///
////////////////////////////////////////////////////////////////////////

#define Vec_FltForEachEntry( vVec, Entry, i )                                               \
    for ( i = 0; (i < Vec_FltSize(vVec)) && (((Entry) = Vec_FltEntry(vVec, i)), 1); i++ )
#define Vec_FltForEachEntryStart( vVec, Entry, i, Start )                                   \
    for ( i = Start; (i < Vec_FltSize(vVec)) && (((Entry) = Vec_FltEntry(vVec, i)), 1); i++ )
#define Vec_FltForEachEntryStartStop( vVec, Entry, i, Start, Stop )                         \
    for ( i = Start; (i < Stop) && (((Entry) = Vec_FltEntry(vVec, i)), 1); i++ )
#define Vec_FltForEachEntryReverse( vVec, pEntry, i )                                       \
    for ( i = Vec_FltSize(vVec) - 1; (i >= 0) && (((pEntry) = Vec_FltEntry(vVec, i)), 1); i-- )

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

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

  Synopsis    [Allocates a vector with the given capacity.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Flt_t * Vec_FltAlloc( int nCap )
{
    Vec_Flt_t * p;
Alan Mishchenko committed
81
    p = ABC_ALLOC( Vec_Flt_t, 1 );
Alan Mishchenko committed
82 83 84 85
    if ( nCap > 0 && nCap < 16 )
        nCap = 16;
    p->nSize  = 0;
    p->nCap   = nCap;
Alan Mishchenko committed
86
    p->pArray = p->nCap? ABC_ALLOC( float, p->nCap ) : NULL;
Alan Mishchenko committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    return p;
}

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

  Synopsis    [Allocates a vector with the given size and cleans it.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Flt_t * Vec_FltStart( int nSize )
{
    Vec_Flt_t * p;
    p = Vec_FltAlloc( nSize );
    p->nSize = nSize;
    memset( p->pArray, 0, sizeof(float) * nSize );
107 108 109 110 111 112 113 114
    return p;
}
static inline Vec_Flt_t * Vec_FltStartFull( int nSize )
{
    Vec_Flt_t * p;
    p = Vec_FltAlloc( nSize );
    p->nSize = nSize;
    memset( p->pArray, 0xFF, sizeof(float) * nSize );
Alan Mishchenko committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    return p;
}

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

  Synopsis    [Creates the vector from a float array of the given size.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Flt_t * Vec_FltAllocArray( float * pArray, int nSize )
{
    Vec_Flt_t * p;
Alan Mishchenko committed
132
    p = ABC_ALLOC( Vec_Flt_t, 1 );
Alan Mishchenko committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    p->nSize  = nSize;
    p->nCap   = nSize;
    p->pArray = pArray;
    return p;
}

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

  Synopsis    [Creates the vector from a float array of the given size.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Flt_t * Vec_FltAllocArrayCopy( float * pArray, int nSize )
{
    Vec_Flt_t * p;
Alan Mishchenko committed
153
    p = ABC_ALLOC( Vec_Flt_t, 1 );
Alan Mishchenko committed
154 155
    p->nSize  = nSize;
    p->nCap   = nSize;
Alan Mishchenko committed
156
    p->pArray = ABC_ALLOC( float, nSize );
Alan Mishchenko committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
    memcpy( p->pArray, pArray, sizeof(float) * nSize );
    return p;
}

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

  Synopsis    [Duplicates the float array.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Flt_t * Vec_FltDup( Vec_Flt_t * pVec )
{
    Vec_Flt_t * p;
Alan Mishchenko committed
175
    p = ABC_ALLOC( Vec_Flt_t, 1 );
Alan Mishchenko committed
176 177
    p->nSize  = pVec->nSize;
    p->nCap   = pVec->nCap;
Alan Mishchenko committed
178
    p->pArray = p->nCap? ABC_ALLOC( float, p->nCap ) : NULL;
Alan Mishchenko committed
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    memcpy( p->pArray, pVec->pArray, sizeof(float) * pVec->nSize );
    return p;
}

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

  Synopsis    [Transfers the array into another vector.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Flt_t * Vec_FltDupArray( Vec_Flt_t * pVec )
{
    Vec_Flt_t * p;
Alan Mishchenko committed
197
    p = ABC_ALLOC( Vec_Flt_t, 1 );
Alan Mishchenko committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    p->nSize  = pVec->nSize;
    p->nCap   = pVec->nCap;
    p->pArray = pVec->pArray;
    pVec->nSize  = 0;
    pVec->nCap   = 0;
    pVec->pArray = NULL;
    return p;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_FltFree( Vec_Flt_t * p )
{
Alan Mishchenko committed
220 221
    ABC_FREE( p->pArray );
    ABC_FREE( p );
Alan Mishchenko committed
222 223 224 225 226 227 228 229 230 231 232 233 234
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
static inline void Vec_FltFreeP( Vec_Flt_t ** p )
{
    if ( *p == NULL )
        return;
    ABC_FREE( (*p)->pArray );
    ABC_FREE( (*p) );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
static inline float * Vec_FltReleaseArray( Vec_Flt_t * p )
{
    float * pArray = p->pArray;
    p->nCap = 0;
    p->nSize = 0;
    p->pArray = NULL;
    return pArray;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline float * Vec_FltArray( Vec_Flt_t * p )
{
    return p->pArray;
}
278 279 280 281
static inline float ** Vec_FltArrayP( Vec_Flt_t * p )
{
    return &p->pArray;
}
Alan Mishchenko committed
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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_FltSize( Vec_Flt_t * p )
{
    return p->nSize;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
static inline int Vec_FltCap( Vec_Flt_t * p )
{
    return p->nCap;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static inline double Vec_FltMemory( Vec_Flt_t * p )
{
    return !p ? 0.0 : 1.0 * sizeof(float) * p->nCap + sizeof(Vec_Flt_t);
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
342 343 344 345 346
static inline float Vec_FltEntry( Vec_Flt_t * p, int i )
{
    assert( i >= 0 && i < p->nSize );
    return p->pArray[i];
}
347 348 349 350 351
static inline float * Vec_FltEntryP( Vec_Flt_t * p, int i )
{
    assert( i >= 0 && i < p->nSize );
    return p->pArray + i;
}
Alan Mishchenko committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_FltWriteEntry( Vec_Flt_t * p, int i, float Entry )
{
    assert( i >= 0 && i < p->nSize );
    p->pArray[i] = Entry;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_FltAddToEntry( Vec_Flt_t * p, int i, float Addition )
{
    assert( i >= 0 && i < p->nSize );
    p->pArray[i] += Addition;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412

***********************************************************************/
static inline void Vec_FltUpdateEntry( Vec_Flt_t * p, int i, float Value )
{
    if ( Vec_FltEntry( p, i ) < Value )
        Vec_FltWriteEntry( p, i, Value );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []
Alan Mishchenko committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434

***********************************************************************/
static inline float Vec_FltEntryLast( Vec_Flt_t * p )
{
    return p->pArray[p->nSize-1];
}

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

  Synopsis    [Resizes the vector to the given capacity.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_FltGrow( Vec_Flt_t * p, int nCapMin )
{
    if ( p->nCap >= nCapMin )
        return;
Alan Mishchenko committed
435
    p->pArray = ABC_REALLOC( float, p->pArray, nCapMin ); 
Alan Mishchenko committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
    p->nCap   = nCapMin;
}

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

  Synopsis    [Fills the vector with given number of entries.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_FltFill( Vec_Flt_t * p, int nSize, float Entry )
{
    int i;
    Vec_FltGrow( p, nSize );
    for ( i = 0; i < nSize; i++ )
        p->pArray[i] = Entry;
    p->nSize = nSize;
}

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

  Synopsis    [Fills the vector with given number of entries.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
470
static inline void Vec_FltFillExtra( Vec_Flt_t * p, int nSize, float Fill )
Alan Mishchenko committed
471 472
{
    int i;
473
    if ( nSize <= p->nSize )
Alan Mishchenko committed
474
        return;
475 476 477 478
    if ( nSize > 2 * p->nCap )
        Vec_FltGrow( p, nSize );
    else if ( nSize > p->nCap )
        Vec_FltGrow( p, 2 * p->nCap );
Alan Mishchenko committed
479
    for ( i = p->nSize; i < nSize; i++ )
480
        p->pArray[i] = Fill;
Alan Mishchenko committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
    p->nSize = nSize;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_FltShrink( Vec_Flt_t * p, int nSizeNew )
{
    assert( p->nSize >= nSizeNew );
    p->nSize = nSizeNew;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_FltClear( Vec_Flt_t * p )
{
    p->nSize = 0;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_FltPush( Vec_Flt_t * p, float Entry )
{
    if ( p->nSize == p->nCap )
    {
        if ( p->nCap < 16 )
            Vec_FltGrow( p, 16 );
        else
            Vec_FltGrow( p, 2 * p->nCap );
    }
    p->pArray[p->nSize++] = Entry;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_FltPushOrder( Vec_Flt_t * p, float Entry )
{
    int i;
    if ( p->nSize == p->nCap )
    {
        if ( p->nCap < 16 )
            Vec_FltGrow( p, 16 );
        else
            Vec_FltGrow( p, 2 * p->nCap );
    }
    p->nSize++;
    for ( i = p->nSize-2; i >= 0; i-- )
        if ( p->pArray[i] > Entry )
            p->pArray[i+1] = p->pArray[i];
        else
            break;
    p->pArray[i+1] = Entry;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_FltPushUnique( Vec_Flt_t * p, float Entry )
{
    int i;
    for ( i = 0; i < p->nSize; i++ )
        if ( p->pArray[i] == Entry )
            return 1;
    Vec_FltPush( p, Entry );
    return 0;
}

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

  Synopsis    [Returns the last entry and removes it from the list.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline float Vec_FltPop( Vec_Flt_t * p )
{
    assert( p->nSize > 0 );
    return p->pArray[--p->nSize];
}

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

  Synopsis    [Find entry.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_FltFind( Vec_Flt_t * p, float Entry )
{
    int i;
    for ( i = 0; i < p->nSize; i++ )
        if ( p->pArray[i] == Entry )
            return i;
    return -1;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_FltRemove( Vec_Flt_t * p, float Entry )
{
    int i;
    for ( i = 0; i < p->nSize; i++ )
        if ( p->pArray[i] == Entry )
            break;
    if ( i == p->nSize )
        return 0;
    assert( i < p->nSize );
    for ( i++; i < p->nSize; i++ )
        p->pArray[i-1] = p->pArray[i];
    p->nSize--;
    return 1;
}

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

656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
  Synopsis    [Find entry.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline float Vec_FltFindMax( Vec_Flt_t * p )
{
    int i;
    float Best;
    if ( p->nSize == 0 )
        return 0;
    Best = p->pArray[0];
    for ( i = 1; i < p->nSize; i++ )
        if ( Best < p->pArray[i] )
            Best = p->pArray[i];
    return Best;
}
static inline float Vec_FltFindMin( Vec_Flt_t * p )
{
    int i;
    float Best;
    if ( p->nSize == 0 )
        return 0;
    Best = p->pArray[0];
    for ( i = 1; i < p->nSize; i++ )
        if ( Best > p->pArray[i] )
            Best = p->pArray[i];
    return Best;
}

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

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
  Synopsis    [Checks if two vectors are equal.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_FltEqual( Vec_Flt_t * p1, Vec_Flt_t * p2 ) 
{
    int i;
    if ( p1->nSize != p2->nSize )
        return 0;
    for ( i = 0; i < p1->nSize; i++ )
        if ( p1->pArray[i] != p2->pArray[i] )
            return 0;
    return 1;
}

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

714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_FltPrint( Vec_Flt_t * vVec )
{
    int i; float Entry;
    printf( "Vector has %d entries: {", Vec_FltSize(vVec) );
    Vec_FltForEachEntry( vVec, Entry, i )
        printf( " %f", Entry );
    printf( " }\n" );
}

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

Alan Mishchenko committed
734 735 736 737 738 739 740 741 742
  Synopsis    [Comparison procedure for two floats.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
743
static int Vec_FltSortCompare1( float * pp1, float * pp2 )
Alan Mishchenko committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
{
    // for some reason commenting out lines (as shown) led to crashing of the release version
    if ( *pp1 < *pp2 )
        return -1;
    if ( *pp1 > *pp2 ) //
        return 1;
    return 0; //
}

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

  Synopsis    [Comparison procedure for two floats.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
764
static int Vec_FltSortCompare2( float * pp1, float * pp2 )
Alan Mishchenko committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
{
    // for some reason commenting out lines (as shown) led to crashing of the release version
    if ( *pp1 > *pp2 )
        return -1;
    if ( *pp1 < *pp2 ) //
        return 1;
    return 0; //
}

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

  Synopsis    [Sorting the entries by their value.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_FltSort( Vec_Flt_t * p, int fReverse )
{
    if ( fReverse ) 
        qsort( (void *)p->pArray, p->nSize, sizeof(float), 
                (int (*)(const void *, const void *)) Vec_FltSortCompare2 );
    else
        qsort( (void *)p->pArray, p->nSize, sizeof(float), 
                (int (*)(const void *, const void *)) Vec_FltSortCompare1 );
}

////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////

799 800 801 802


ABC_NAMESPACE_HEADER_END

Alan Mishchenko committed
803 804
#endif