vecStr.h 15.7 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 21 22 23
/**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 $]

***********************************************************************/
 
#ifndef __VEC_STR_H__
#define __VEC_STR_H__

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 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
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     []

***********************************************************************/
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
328
    assert( p->nSize > 0 );
Alan Mishchenko committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
    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;
Alan Mishchenko committed
347
    p->pArray = ABC_REALLOC( char, p->pArray, 2 * nCapMin ); 
Alan Mishchenko committed
348
    p->nCap   = 2 * nCapMin;
Alan Mishchenko committed
349 350 351 352 353 354 355 356 357 358 359 360 361
}

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

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

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
362
static inline void Vec_StrFill( Vec_Str_t * p, int nSize, char Fill )
Alan Mishchenko committed
363 364 365 366 367
{
    int i;
    Vec_StrGrow( p, nSize );
    p->nSize = nSize;
    for ( i = 0; i < p->nSize; i++ )
Alan Mishchenko committed
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
        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;
385
    if ( nSize <= p->nSize )
Alan Mishchenko committed
386
        return;
387 388 389 390
    if ( nSize > 2 * p->nCap )
        Vec_StrGrow( p, nSize );
    else if ( nSize > p->nCap )
        Vec_StrGrow( p, 2 * p->nCap );
Alan Mishchenko committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
    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
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 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
}

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

486
/**Function*************************************************************
Alan Mishchenko committed
487

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

490 491
  Description []
               
Alan Mishchenko committed
492 493 494 495
  SideEffects []

  SeeAlso     []

496 497
***********************************************************************/
static inline char Vec_StrPop( Vec_Str_t * p )
Alan Mishchenko committed
498
{
499 500 501
    assert( p->nSize > 0 );
    return p->pArray[--p->nSize];
}
Alan Mishchenko committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
static inline void Vec_StrPrintNum( Vec_Str_t * p, int Num )
{
516 517 518
    int i;
    char Digits[16];
    if ( Num == 0 )
Alan Mishchenko committed
519
    {
520
        Vec_StrPush( p, '0' );
Alan Mishchenko committed
521 522
        return;
    }
523
    if ( Num < 0 )
Alan Mishchenko committed
524
    {
525 526
        Vec_StrPush( p, '-' );
        Num = -Num;
Alan Mishchenko committed
527
    }
528 529 530 531
    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
532 533 534 535 536 537 538 539 540 541 542 543 544
}

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

  Synopsis    []

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
545
static inline void Vec_StrPrintStr( Vec_Str_t * p, const char * pStr )
Alan Mishchenko committed
546 547 548 549 550 551
{
    int i, Length = strlen(pStr);
    for ( i = 0; i < Length; i++ )
        Vec_StrPush( p, pStr[i] );
}

Alan Mishchenko committed
552 553
/**Function*************************************************************

Alan Mishchenko committed
554 555 556 557 558 559 560 561 562
  Synopsis    [Appends the string to the char vector.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
563
static inline void Vec_StrAppend( Vec_Str_t * p, const char * pString )
Alan Mishchenko committed
564
{
565
    Vec_StrPrintStr( p, pString );
Alan Mishchenko committed
566 567 568 569
}

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

Alan Mishchenko committed
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
  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
592 593 594 595 596 597 598 599 600
  Synopsis    [Comparison procedure for two clauses.]

  Description []
               
  SideEffects []

  SeeAlso     []

***********************************************************************/
Alan Mishchenko committed
601
static int Vec_StrSortCompare1( char * pp1, char * pp2 )
Alan Mishchenko committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
{
    // 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
622
static int Vec_StrSortCompare2( char * pp1, char * pp2 )
Alan Mishchenko committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
{
    // 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
646
        qsort( (void *)p->pArray, p->nSize, sizeof(char), 
Alan Mishchenko committed
647 648
                (int (*)(const void *, const void *)) Vec_StrSortCompare2 );
    else
Alan Mishchenko committed
649
        qsort( (void *)p->pArray, p->nSize, sizeof(char), 
Alan Mishchenko committed
650 651 652
                (int (*)(const void *, const void *)) Vec_StrSortCompare1 );
}

653 654 655 656


ABC_NAMESPACE_HEADER_END

Alan Mishchenko committed
657 658
#endif

Alan Mishchenko committed
659 660 661 662
////////////////////////////////////////////////////////////////////////
///                       END OF FILE                                ///
////////////////////////////////////////////////////////////////////////