vecStr.h 25.8 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    [vecStr.h]

  SystemName  [ABC: Logic synthesis and verification system.]

  PackageName [Resizable arrays.]

  Synopsis    [Resizable arrays of characters.]

  Author      [Alan Mishchenko]
  
  Affiliation [UC Berkeley]

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

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

***********************************************************************/
 
21 22
#ifndef ABC__misc__vec__vecStr_h
#define ABC__misc__vec__vecStr_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
////////////////////////////////////////////////////////////////////////
///                         PARAMETERS                               ///
////////////////////////////////////////////////////////////////////////

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

typedef struct Vec_Str_t_       Vec_Str_t;
struct Vec_Str_t_ 
{
    int              nCap;
Alan Mishchenko committed
46
    int              nSize;
Alan Mishchenko committed
47 48 49 50
    char *           pArray;
};

////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
51
///                      MACRO DEFINITIONS                           ///
Alan Mishchenko committed
52 53
////////////////////////////////////////////////////////////////////////

Alan Mishchenko committed
54 55 56
#define Vec_StrForEachEntry( vVec, Entry, i )                                               \
    for ( i = 0; (i < Vec_StrSize(vVec)) && (((Entry) = Vec_StrEntry(vVec, i)), 1); i++ )   

Alan Mishchenko committed
57
////////////////////////////////////////////////////////////////////////
Alan Mishchenko committed
58
///                     FUNCTION DEFINITIONS                         ///
Alan Mishchenko committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
////////////////////////////////////////////////////////////////////////

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

  Synopsis    [Allocates a vector with the given capacity.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Str_t * Vec_StrAlloc( int nCap )
{
    Vec_Str_t * p;
Alan Mishchenko committed
75
    p = ABC_ALLOC( Vec_Str_t, 1 );
Alan Mishchenko committed
76 77 78 79
    if ( nCap > 0 && nCap < 16 )
        nCap = 16;
    p->nSize  = 0;
    p->nCap   = nCap;
Alan Mishchenko committed
80
    p->pArray = p->nCap? ABC_ALLOC( char, p->nCap ) : NULL;
Alan Mishchenko committed
81 82
    return p;
}
83 84 85 86 87 88 89 90 91 92
static inline Vec_Str_t * Vec_StrAllocExact( int nCap )
{
    Vec_Str_t * p;
    assert( nCap >= 0 );
    p = ABC_ALLOC( Vec_Str_t, 1 );
    p->nSize  = 0;
    p->nCap   = nCap;
    p->pArray = p->nCap? ABC_ALLOC( char, p->nCap ) : NULL;
    return p;
}
Alan Mishchenko committed
93 94 95

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

Alan Mishchenko committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  Synopsis    [Allocates a vector with the given size and cleans it.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Str_t * Vec_StrStart( int nSize )
{
    Vec_Str_t * p;
    p = Vec_StrAlloc( nSize );
    p->nSize = nSize;
    memset( p->pArray, 0, sizeof(char) * nSize );
    return p;
}

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

Alan Mishchenko committed
116 117 118 119 120 121 122 123 124 125 126 127
  Synopsis    [Creates the vector from an integer array of the given size.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Str_t * Vec_StrAllocArray( char * pArray, int nSize )
{
    Vec_Str_t * p;
Alan Mishchenko committed
128
    p = ABC_ALLOC( Vec_Str_t, 1 );
Alan Mishchenko committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    p->nSize  = nSize;
    p->nCap   = nSize;
    p->pArray = pArray;
    return p;
}

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

  Synopsis    [Creates the vector from an integer array of the given size.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Str_t * Vec_StrAllocArrayCopy( char * pArray, int nSize )
{
    Vec_Str_t * p;
Alan Mishchenko committed
149
    p = ABC_ALLOC( Vec_Str_t, 1 );
Alan Mishchenko committed
150 151
    p->nSize  = nSize;
    p->nCap   = nSize;
Alan Mishchenko committed
152
    p->pArray = ABC_ALLOC( char, nSize );
Alan Mishchenko committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    memcpy( p->pArray, pArray, sizeof(char) * nSize );
    return p;
}

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

  Synopsis    [Duplicates the integer array.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Str_t * Vec_StrDup( Vec_Str_t * pVec )
{
    Vec_Str_t * p;
Alan Mishchenko committed
171
    p = ABC_ALLOC( Vec_Str_t, 1 );
Alan Mishchenko committed
172 173
    p->nSize  = pVec->nSize;
    p->nCap   = pVec->nCap;
Alan Mishchenko committed
174
    p->pArray = p->nCap? ABC_ALLOC( char, p->nCap ) : NULL;
Alan Mishchenko committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    memcpy( p->pArray, pVec->pArray, sizeof(char) * pVec->nSize );
    return p;
}

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

  Synopsis    [Transfers the array into another vector.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline Vec_Str_t * Vec_StrDupArray( Vec_Str_t * pVec )
{
    Vec_Str_t * p;
Alan Mishchenko committed
193
    p = ABC_ALLOC( Vec_Str_t, 1 );
Alan Mishchenko committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    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     []

***********************************************************************/
Alan Mishchenko committed
214 215 216 217 218 219 220 221 222 223 224 225
static inline void Vec_StrZero( Vec_Str_t * p )
{
    p->pArray = NULL;
    p->nSize = 0;
    p->nCap = 0;
}
static inline void Vec_StrErase( Vec_Str_t * p )
{
    ABC_FREE( p->pArray );
    p->nSize = 0;
    p->nCap = 0;
}
Alan Mishchenko committed
226 227
static inline void Vec_StrFree( Vec_Str_t * p )
{
Alan Mishchenko committed
228 229
    ABC_FREE( p->pArray );
    ABC_FREE( p );
Alan Mishchenko committed
230 231 232 233 234 235 236 237 238 239 240 241 242
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
static inline void Vec_StrFreeP( Vec_Str_t ** p )
{
    if ( *p == NULL )
        return;
    ABC_FREE( (*p)->pArray );
    ABC_FREE( (*p) );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
static inline char * Vec_StrReleaseArray( Vec_Str_t * p )
{
    char * pArray = p->pArray;
    p->nCap = 0;
    p->nSize = 0;
    p->pArray = NULL;
    return pArray;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline char * Vec_StrArray( Vec_Str_t * p )
{
    return p->pArray;
}
286 287 288 289
static inline char * Vec_StrLimit( Vec_Str_t * p )
{
    return p->pArray + p->nSize;
}
Alan Mishchenko committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_StrSize( Vec_Str_t * p )
{
    return p->nSize;
}
306 307 308 309
static inline void Vec_StrSetSize( Vec_Str_t * p, int nSize )
{
    p->nSize = nSize;
}
Alan Mishchenko committed
310 311 312 313 314 315 316 317 318 319 320 321

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
static inline int Vec_StrCap( Vec_Str_t * p )
{
    return p->nCap;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
static inline double Vec_StrMemory( Vec_Str_t * p )
{
    return !p ? 0.0 : 1.0 * sizeof(char) * p->nCap + sizeof(Vec_Str_t);
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
354 355 356 357 358
static inline char Vec_StrEntry( Vec_Str_t * p, int i )
{
    assert( i >= 0 && i < p->nSize );
    return p->pArray[i];
}
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline char * Vec_StrEntryP( Vec_Str_t * p, int i )
{
    assert( i >= 0 && i < p->nSize );
    return p->pArray + i;
}
Alan Mishchenko committed
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline char Vec_StrEntryLast( Vec_Str_t * p )
{
Alan Mishchenko committed
407
    assert( p->nSize > 0 );
Alan Mishchenko committed
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
    return p->pArray[p->nSize-1];
}

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

  Synopsis    [Resizes the vector to the given capacity.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_StrGrow( Vec_Str_t * p, int nCapMin )
{
    if ( p->nCap >= nCapMin )
        return;
426 427
    p->pArray = ABC_REALLOC( char, p->pArray, nCapMin ); 
    p->nCap   = nCapMin;
Alan Mishchenko committed
428 429 430 431 432 433 434 435 436 437 438 439 440
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
441
static inline void Vec_StrFill( Vec_Str_t * p, int nSize, char Fill )
Alan Mishchenko committed
442 443 444 445 446
{
    int i;
    Vec_StrGrow( p, nSize );
    p->nSize = nSize;
    for ( i = 0; i < p->nSize; i++ )
Alan Mishchenko committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
        p->pArray[i] = Fill;
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_StrFillExtra( Vec_Str_t * p, int nSize, char Fill )
{
    int i;
464
    if ( nSize <= p->nSize )
Alan Mishchenko committed
465
        return;
466 467 468 469
    if ( nSize > 2 * p->nCap )
        Vec_StrGrow( p, nSize );
    else if ( nSize > p->nCap )
        Vec_StrGrow( p, 2 * p->nCap );
Alan Mishchenko committed
470 471 472 473 474 475 476 477 478 479 480 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
    for ( i = p->nSize; i < nSize; i++ )
        p->pArray[i] = Fill;
    p->nSize = nSize;
}

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

  Synopsis    [Returns the entry even if the place not exist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline char Vec_StrGetEntry( Vec_Str_t * p, int i )
{
    Vec_StrFillExtra( p, i + 1, 0 );
    return Vec_StrEntry( p, i );
}

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

  Synopsis    [Inserts the entry even if the place does not exist.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_StrSetEntry( Vec_Str_t * p, int i, char Entry )
{
    Vec_StrFillExtra( p, i + 1, 0 );
    Vec_StrWriteEntry( p, i, Entry );
Alan Mishchenko committed
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
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

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

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_StrPush( Vec_Str_t * p, char Entry )
{
    if ( p->nSize == p->nCap )
    {
        if ( p->nCap < 16 )
            Vec_StrGrow( p, 16 );
        else
            Vec_StrGrow( p, 2 * p->nCap );
    }
    p->pArray[p->nSize++] = Entry;
}
564 565 566 567 568 569 570
static inline void Vec_StrPushBuffer( Vec_Str_t * p, char * pBuffer, int nSize )
{
    if ( p->nSize + nSize > p->nCap )
        Vec_StrGrow( p, 2 * (p->nSize + nSize) );
    memcpy( p->pArray + p->nSize, pBuffer, nSize );
    p->nSize += nSize;
}
Alan Mishchenko committed
571

572
/**Function*************************************************************
Alan Mishchenko committed
573

574
  Synopsis    [Returns the last entry and removes it from the list.]
Alan Mishchenko committed
575

576 577
  Description []
               
Alan Mishchenko committed
578 579 580 581
  SideEffects []

  SeeAlso     []

582 583
***********************************************************************/
static inline char Vec_StrPop( Vec_Str_t * p )
Alan Mishchenko committed
584
{
585 586 587
    assert( p->nSize > 0 );
    return p->pArray[--p->nSize];
}
Alan Mishchenko committed
588 589 590 591 592 593 594 595 596 597 598 599

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
600
static inline void Vec_StrIntPrint( Vec_Str_t * p )
601 602 603 604
{
    int i;
    printf( "Vector has %d entries: {", Vec_StrSize(p) );
    for ( i = 0; i < Vec_StrSize(p); i++ )
605
        printf( " %d", (int)Vec_StrEntry(p, i) );
606 607 608 609 610 611 612 613 614 615 616 617 618 619
    printf( " }\n" );
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
620 621
static inline void Vec_StrPrintNum( Vec_Str_t * p, int Num )
{
622 623 624
    int i;
    char Digits[16];
    if ( Num == 0 )
Alan Mishchenko committed
625
    {
626
        Vec_StrPush( p, '0' );
Alan Mishchenko committed
627 628
        return;
    }
629
    if ( Num < 0 )
Alan Mishchenko committed
630
    {
631 632
        Vec_StrPush( p, '-' );
        Num = -Num;
Alan Mishchenko committed
633
    }
634
    for ( i = 0; Num; Num /= 10,  i++ )
635
        Digits[i] = Num % 10;
636
    for ( i--; i >= 0; i-- )
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
        Vec_StrPush( p, (char)('0' + Digits[i]) );
}
static inline void Vec_StrPrintNumStar( Vec_Str_t * p, int Num, int nDigits )
{
    int i;
    char Digits[16] = {0};
    if ( Num == 0 )
    {
        for ( i = 0; i < nDigits; i++ )
            Vec_StrPush( p, '0' );
        return;
    }
    if ( Num < 0 )
    {
        Vec_StrPush( p, '-' );
        Num = -Num;
        nDigits--;
    }
    for ( i = 0; Num; Num /= 10,  i++ )
        Digits[i] = Num % 10;
    for ( i = Abc_MaxInt(i, nDigits)-1; i >= 0; i-- )
        Vec_StrPush( p, (char)('0' + Digits[i]) );
Alan Mishchenko committed
659 660 661 662 663 664 665 666 667 668 669 670 671
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
672
static inline void Vec_StrPrintStr( Vec_Str_t * p, const char * pStr )
Alan Mishchenko committed
673 674 675 676 677 678
{
    int i, Length = strlen(pStr);
    for ( i = 0; i < Length; i++ )
        Vec_StrPush( p, pStr[i] );
}

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
#ifdef WIN32
#define vsnprintf _vsnprintf
#endif

static inline char * Vec_StrPrintF( Vec_Str_t * p, const char * format, ... )
{
    int nAdded, nSize = 1000; 
    va_list args;  va_start( args, format );
    Vec_StrGrow( p, Vec_StrSize(p) + nSize );
    nAdded = vsnprintf( Vec_StrLimit(p), nSize, format, args );
    if ( nAdded > nSize )
    {
        Vec_StrGrow( p, Vec_StrSize(p) + nAdded + nSize );
        nSize = vsnprintf( Vec_StrLimit(p), nAdded, format, args );
        assert( nSize == nAdded );
    }
    p->nSize += nAdded;
    va_end( args );
    return Vec_StrLimit(p) - nAdded;
}

Alan Mishchenko committed
712 713
/**Function*************************************************************

Alan Mishchenko committed
714 715 716 717 718 719 720 721 722
  Synopsis    [Appends the string to the char vector.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
723
static inline void Vec_StrAppend( Vec_Str_t * p, const char * pString )
Alan Mishchenko committed
724
{
725
    Vec_StrPrintStr( p, pString );
Alan Mishchenko committed
726
}
727 728 729 730 731 732
static inline void Vec_StrCopy( Vec_Str_t * p, const char * pString )
{
    Vec_StrClear( p );
    Vec_StrAppend( p, pString );
    Vec_StrPush( p, '\0' );
}
Alan Mishchenko committed
733 734 735

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

Alan Mishchenko committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
  Synopsis    [Reverses the order of entries.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_StrReverseOrder( Vec_Str_t * p )
{
    int i, Temp;
    for ( i = 0; i < p->nSize/2; i++ )
    {
        Temp = p->pArray[i];
        p->pArray[i] = p->pArray[p->nSize-1-i];
        p->pArray[p->nSize-1-i] = Temp;
    }
}

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

Alan Mishchenko committed
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_StrSum( Vec_Str_t * p ) 
{
    int i, Counter = 0;
    for ( i = 0; i < p->nSize; i++ )
        Counter += (int)p->pArray[i];
    return Counter;
}

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

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_StrCountEntry( Vec_Str_t * p, char Entry ) 
{
    int i, Counter = 0;
    for ( i = 0; i < p->nSize; i++ )
        Counter += (p->pArray[i] == Entry);
    return Counter;
}
static inline int Vec_StrCountLarger( Vec_Str_t * p, char Entry ) 
{
    int i, Counter = 0;
    for ( i = 0; i < p->nSize; i++ )
        Counter += (p->pArray[i] > Entry);
    return Counter;
}
static inline int Vec_StrCountSmaller( Vec_Str_t * p, char Entry ) 
{
    int i, Counter = 0;
    for ( i = 0; i < p->nSize; i++ )
        Counter += (p->pArray[i] < Entry);
    return Counter;
}

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

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_StrCountEntryLit( Vec_Str_t * p, char Entry ) 
{
    int i, Counter = 0;
    for ( i = 0; i < p->nSize; i++ )
        Counter += (Abc_Lit2Var((int)p->pArray[i]) == Entry);
    return Counter;
}
static inline int Vec_StrCountLargerLit( Vec_Str_t * p, char Entry ) 
{
    int i, Counter = 0;
    for ( i = 0; i < p->nSize; i++ )
        Counter += (Abc_Lit2Var((int)p->pArray[i]) > Entry);
    return Counter;
}
static inline int Vec_StrCountSmallerLit( Vec_Str_t * p, char Entry ) 
{
    int i, Counter = 0;
    for ( i = 0; i < p->nSize; i++ )
        Counter += (Abc_Lit2Var((int)p->pArray[i]) < Entry);
    return Counter;
}

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

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
  Synopsis    [Compares two strings.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_StrEqual( Vec_Str_t * p1, Vec_Str_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*************************************************************

Alan Mishchenko committed
865 866 867 868 869 870 871 872 873
  Synopsis    [Comparison procedure for two clauses.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
874
static int Vec_StrSortCompare1( char * pp1, char * pp2 )
Alan Mishchenko committed
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
{
    // 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 clauses.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
895
static int Vec_StrSortCompare2( char * pp1, char * pp2 )
Alan Mishchenko committed
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918
{
    // 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 integer value.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_StrSort( Vec_Str_t * p, int fReverse )
{
    if ( fReverse ) 
Alan Mishchenko committed
919
        qsort( (void *)p->pArray, p->nSize, sizeof(char), 
Alan Mishchenko committed
920 921
                (int (*)(const void *, const void *)) Vec_StrSortCompare2 );
    else
Alan Mishchenko committed
922
        qsort( (void *)p->pArray, p->nSize, sizeof(char), 
Alan Mishchenko committed
923 924 925
                (int (*)(const void *, const void *)) Vec_StrSortCompare1 );
}

926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
/**Function*************************************************************

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_StrCompareVec( Vec_Str_t * p1, Vec_Str_t * p2 )
{
    if ( p1 == NULL || p2 == NULL )
        return (p1 != NULL) - (p2 != NULL);
    if ( Vec_StrSize(p1) != Vec_StrSize(p2) )
        return Vec_StrSize(p1) - Vec_StrSize(p2);
    return memcmp( Vec_StrArray(p1), Vec_StrArray(p2), Vec_StrSize(p1) );
}

946

947 948 949 950 951 952 953 954 955 956 957 958 959 960
/**Function*************************************************************

  Synopsis    [Binary I/O for numbers (int/float/etc) and strings (char *).]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_StrPutI_ne( Vec_Str_t * vOut, int Val )
{
    int i;
961 962
//    for ( i = 0; i < 4; i++ )
    for ( i = 3; i >= 0; i-- )
963 964 965 966 967 968
        Vec_StrPush( vOut, (char)(Val >> (8*i)) );
}
static inline int Vec_StrGetI_ne( Vec_Str_t * vOut, int * pPos )
{
    int i;
    int Val = 0;
969 970
//    for ( i = 0; i < 4; i++ )
    for ( i = 3; i >= 0; i-- )
Alan Mishchenko committed
971
        Val |= ((int)(unsigned char)Vec_StrEntry(vOut, (*pPos)++) << (8*i));
972 973 974 975 976 977 978 979 980 981 982 983 984 985
    return Val;
}

static inline void Vec_StrPutI( Vec_Str_t * vOut, int Val )
{
    for ( ; Val >= 0x80; Val >>= 7 )
        Vec_StrPush( vOut, (unsigned char)(Val | 0x80) );
    Vec_StrPush( vOut, (unsigned char)Val );
}
static inline int Vec_StrGetI( Vec_Str_t * vOut, int * pPos )
{
    unsigned char ch;
    int i = 0, Val = 0;
    while ( (ch = Vec_StrEntry(vOut, (*pPos)++)) & 0x80 )
Alan Mishchenko committed
986
        Val |= ((ch & 0x7f) << (7 * i++));
987 988 989 990 991 992 993 994 995 996 997 998 999 1000
    return Val | (ch << (7 * i));
}

static inline void Vec_StrPutW( Vec_Str_t * vOut, word Val )
{
    int i;
    for ( i = 0; i < 8; i++ )
        Vec_StrPush( vOut, (char)(Val >> (8*i)) );
}
static inline word Vec_StrGetW( Vec_Str_t * vOut, int * pPos )
{
    int i;
    word Val = 0;
    for ( i = 0; i < 8; i++ )
Alan Mishchenko committed
1001
        Val |= ((word)(unsigned char)Vec_StrEntry(vOut, (*pPos)++) << (8*i));
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
    return Val;
}

static inline void Vec_StrPutF( Vec_Str_t * vOut, float Val )
{
    union { float num; unsigned char data[4]; } tmp;
    tmp.num = Val;
    Vec_StrPush( vOut, tmp.data[0] );
    Vec_StrPush( vOut, tmp.data[1] );
    Vec_StrPush( vOut, tmp.data[2] );
    Vec_StrPush( vOut, tmp.data[3] );
}
static inline float Vec_StrGetF( Vec_Str_t * vOut, int * pPos )
{
    union { float num; unsigned char data[4]; } tmp;
    tmp.data[0] = Vec_StrEntry( vOut, (*pPos)++ );
    tmp.data[1] = Vec_StrEntry( vOut, (*pPos)++ );
    tmp.data[2] = Vec_StrEntry( vOut, (*pPos)++ );
    tmp.data[3] = Vec_StrEntry( vOut, (*pPos)++ );
    return tmp.num;
}

static inline void Vec_StrPutD( Vec_Str_t * vOut, double Val )
{
    union { double num; unsigned char data[8]; } tmp;
    int i, Lim = sizeof(double);
    tmp.num = Val;
    for ( i = 0; i < Lim; i++ )
        Vec_StrPush( vOut, tmp.data[i] );
}
static inline double Vec_StrGetD( Vec_Str_t * vOut, int * pPos )
{
    union { double num; unsigned char data[8]; } tmp;
    int i, Lim = sizeof(double);
    for ( i = 0; i < Lim; i++ )
        tmp.data[i] = Vec_StrEntry( vOut, (*pPos)++ );
    return tmp.num;
}

static inline void Vec_StrPutS( Vec_Str_t * vOut, char * pStr )
{
    while ( *pStr )
        Vec_StrPush( vOut, *pStr++ );
    Vec_StrPush( vOut, (char)0 );
}
static inline char * Vec_StrGetS( Vec_Str_t * vOut, int * pPos )
{
    char * pStr = Vec_StrEntryP( vOut, *pPos );
    while ( Vec_StrEntry(vOut, (*pPos)++) );
    return Abc_UtilStrsav(pStr);
}

static inline void Vec_StrPutC( Vec_Str_t * vOut, char c )
{
    Vec_StrPush( vOut, c );
}
static inline char Vec_StrGetC( Vec_Str_t * vOut, int * pPos )
{
    return Vec_StrEntry(vOut, (*pPos)++);
}


1064 1065 1066

ABC_NAMESPACE_HEADER_END

Alan Mishchenko committed
1067 1068
#endif

Alan Mishchenko committed
1069 1070 1071 1072
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////