vecStr.h 16.9 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 83 84 85
    return p;
}

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

Alan Mishchenko committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  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
106 107 108 109 110 111 112 113 114 115 116 117
  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
118
    p = ABC_ALLOC( Vec_Str_t, 1 );
Alan Mishchenko committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    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
139
    p = ABC_ALLOC( Vec_Str_t, 1 );
Alan Mishchenko committed
140 141
    p->nSize  = nSize;
    p->nCap   = nSize;
Alan Mishchenko committed
142
    p->pArray = ABC_ALLOC( char, nSize );
Alan Mishchenko committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    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
161
    p = ABC_ALLOC( Vec_Str_t, 1 );
Alan Mishchenko committed
162 163
    p->nSize  = pVec->nSize;
    p->nCap   = pVec->nCap;
Alan Mishchenko committed
164
    p->pArray = p->nCap? ABC_ALLOC( char, p->nCap ) : NULL;
Alan Mishchenko committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    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
183
    p = ABC_ALLOC( Vec_Str_t, 1 );
Alan Mishchenko committed
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    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_StrFree( Vec_Str_t * p )
{
Alan Mishchenko committed
206 207
    ABC_FREE( p->pArray );
    ABC_FREE( p );
Alan Mishchenko committed
208 209 210 211 212 213 214 215 216 217 218 219 220
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
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
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
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;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline int Vec_StrSize( Vec_Str_t * p )
{
    return p->nSize;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
static inline int Vec_StrCap( Vec_Str_t * p )
{
    return p->nCap;
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
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
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
static inline char Vec_StrEntry( Vec_Str_t * p, int i )
{
    assert( i >= 0 && i < p->nSize );
    return p->pArray[i];
}

/**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
360
    assert( p->nSize > 0 );
Alan Mishchenko committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
    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;
379 380
    p->pArray = ABC_REALLOC( char, p->pArray, nCapMin ); 
    p->nCap   = nCapMin;
Alan Mishchenko committed
381 382 383 384 385 386 387 388 389 390 391 392 393
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
394
static inline void Vec_StrFill( Vec_Str_t * p, int nSize, char Fill )
Alan Mishchenko committed
395 396 397 398 399
{
    int i;
    Vec_StrGrow( p, nSize );
    p->nSize = nSize;
    for ( i = 0; i < p->nSize; i++ )
Alan Mishchenko committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
        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;
417
    if ( nSize <= p->nSize )
Alan Mishchenko committed
418
        return;
419 420 421 422
    if ( nSize > 2 * p->nCap )
        Vec_StrGrow( p, nSize );
    else if ( nSize > p->nCap )
        Vec_StrGrow( p, 2 * p->nCap );
Alan Mishchenko committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
    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
460 461 462 463 464 465 466 467 468 469 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 507 508 509 510 511 512 513 514 515 516 517
}

/**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;
}

518
/**Function*************************************************************
Alan Mishchenko committed
519

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

522 523
  Description []
               
Alan Mishchenko committed
524 525 526 527
  SideEffects []

  SeeAlso     []

528 529
***********************************************************************/
static inline char Vec_StrPop( Vec_Str_t * p )
Alan Mishchenko committed
530
{
531 532 533
    assert( p->nSize > 0 );
    return p->pArray[--p->nSize];
}
Alan Mishchenko committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_StrPrintNum( Vec_Str_t * p, int Num )
{
548 549 550
    int i;
    char Digits[16];
    if ( Num == 0 )
Alan Mishchenko committed
551
    {
552
        Vec_StrPush( p, '0' );
Alan Mishchenko committed
553 554
        return;
    }
555
    if ( Num < 0 )
Alan Mishchenko committed
556
    {
557 558
        Vec_StrPush( p, '-' );
        Num = -Num;
Alan Mishchenko committed
559
    }
560 561 562 563
    for ( i = 0; Num; Num /= 10,  i++ )
        Digits[i] = (char)('0' + Num % 10);
    for ( i--; i >= 0; i-- )
        Vec_StrPush( p, Digits[i] );
Alan Mishchenko committed
564 565 566 567 568 569 570 571 572 573 574 575 576
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
577
static inline void Vec_StrPrintStr( Vec_Str_t * p, const char * pStr )
Alan Mishchenko committed
578 579 580 581 582 583
{
    int i, Length = strlen(pStr);
    for ( i = 0; i < Length; i++ )
        Vec_StrPush( p, pStr[i] );
}

Alan Mishchenko committed
584 585
/**Function*************************************************************

Alan Mishchenko committed
586 587 588 589 590 591 592 593 594
  Synopsis    [Appends the string to the char vector.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
595
static inline void Vec_StrAppend( Vec_Str_t * p, const char * pString )
Alan Mishchenko committed
596
{
597
    Vec_StrPrintStr( p, pString );
Alan Mishchenko committed
598 599 600 601
}

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

Alan Mishchenko committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
  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
624 625 626 627 628 629 630 631 632
  Synopsis    [Comparison procedure for two clauses.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
633
static int Vec_StrSortCompare1( char * pp1, char * pp2 )
Alan Mishchenko committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
{
    // 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
654
static int Vec_StrSortCompare2( char * pp1, char * pp2 )
Alan Mishchenko committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
{
    // 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
678
        qsort( (void *)p->pArray, p->nSize, sizeof(char), 
Alan Mishchenko committed
679 680
                (int (*)(const void *, const void *)) Vec_StrSortCompare2 );
    else
Alan Mishchenko committed
681
        qsort( (void *)p->pArray, p->nSize, sizeof(char), 
Alan Mishchenko committed
682 683 684
                (int (*)(const void *, const void *)) Vec_StrSortCompare1 );
}

685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
/**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) );
}

705 706 707 708


ABC_NAMESPACE_HEADER_END

Alan Mishchenko committed
709 710
#endif

Alan Mishchenko committed
711 712 713 714
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////