abc/src/misc/vec/vecStr.h

847 lines
20 KiB
C
Raw Normal View History

2005-07-29 17:01:00 +02:00
/**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 $]
***********************************************************************/
2012-01-21 13:30:10 +01:00
#ifndef ABC__misc__vec__vecStr_h
#define ABC__misc__vec__vecStr_h
2005-07-29 17:01:00 +02:00
2010-11-01 09:35:04 +01:00
2005-07-29 17:01:00 +02:00
////////////////////////////////////////////////////////////////////////
/// INCLUDES ///
////////////////////////////////////////////////////////////////////////
#include <stdio.h>
2010-11-01 09:35:04 +01:00
ABC_NAMESPACE_HEADER_START
2005-07-29 17:01:00 +02:00
////////////////////////////////////////////////////////////////////////
/// PARAMETERS ///
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/// BASIC TYPES ///
////////////////////////////////////////////////////////////////////////
typedef struct Vec_Str_t_ Vec_Str_t;
struct Vec_Str_t_
{
int nCap;
2005-08-27 17:01:00 +02:00
int nSize;
2005-07-29 17:01:00 +02:00
char * pArray;
};
////////////////////////////////////////////////////////////////////////
2008-01-31 05:01:00 +01:00
/// MACRO DEFINITIONS ///
2005-07-29 17:01:00 +02:00
////////////////////////////////////////////////////////////////////////
2005-08-07 17:01:00 +02:00
#define Vec_StrForEachEntry( vVec, Entry, i ) \
for ( i = 0; (i < Vec_StrSize(vVec)) && (((Entry) = Vec_StrEntry(vVec, i)), 1); i++ )
2005-07-29 17:01:00 +02:00
////////////////////////////////////////////////////////////////////////
2008-01-31 05:01:00 +01:00
/// FUNCTION DEFINITIONS ///
2005-07-29 17:01:00 +02:00
////////////////////////////////////////////////////////////////////////
/**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;
2009-02-15 17:01:00 +01:00
p = ABC_ALLOC( Vec_Str_t, 1 );
2005-07-29 17:01:00 +02:00
if ( nCap > 0 && nCap < 16 )
nCap = 16;
p->nSize = 0;
p->nCap = nCap;
2009-02-15 17:01:00 +01:00
p->pArray = p->nCap? ABC_ALLOC( char, p->nCap ) : NULL;
2005-07-29 17:01:00 +02:00
return p;
}
2005-09-04 17:01:00 +02:00
/**Function*************************************************************
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;
}
2005-07-29 17:01:00 +02:00
/**Function*************************************************************
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;
2009-02-15 17:01:00 +01:00
p = ABC_ALLOC( Vec_Str_t, 1 );
2005-07-29 17:01:00 +02:00
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;
2009-02-15 17:01:00 +01:00
p = ABC_ALLOC( Vec_Str_t, 1 );
2005-07-29 17:01:00 +02:00
p->nSize = nSize;
p->nCap = nSize;
2009-02-15 17:01:00 +01:00
p->pArray = ABC_ALLOC( char, nSize );
2005-07-29 17:01:00 +02:00
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;
2009-02-15 17:01:00 +01:00
p = ABC_ALLOC( Vec_Str_t, 1 );
2005-07-29 17:01:00 +02:00
p->nSize = pVec->nSize;
p->nCap = pVec->nCap;
2009-02-15 17:01:00 +01:00
p->pArray = p->nCap? ABC_ALLOC( char, p->nCap ) : NULL;
2005-07-29 17:01:00 +02:00
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;
2009-02-15 17:01:00 +01:00
p = ABC_ALLOC( Vec_Str_t, 1 );
2005-07-29 17:01:00 +02:00
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 )
{
2009-02-15 17:01:00 +01:00
ABC_FREE( p->pArray );
ABC_FREE( p );
2005-07-29 17:01:00 +02:00
}
2010-11-01 09:35:04 +01:00
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline void Vec_StrFreeP( Vec_Str_t ** p )
{
if ( *p == NULL )
return;
ABC_FREE( (*p)->pArray );
ABC_FREE( (*p) );
}
2005-07-29 17:01:00 +02:00
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
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;
}
2012-06-22 19:30:22 +02:00
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline int Vec_StrCap( Vec_Str_t * p )
{
return p->nCap;
}
2012-07-29 21:34:32 +02:00
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline double Vec_StrMemory( Vec_Str_t * p )
{
return !p ? 0.0 : 1.0 * sizeof(char) * p->nCap + sizeof(Vec_Str_t);
}
2005-07-29 17:01:00 +02:00
/**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 char * Vec_StrEntryP( Vec_Str_t * p, int i )
{
assert( i >= 0 && i < p->nSize );
return p->pArray + i;
}
2005-07-29 17:01:00 +02:00
/**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 )
{
2008-01-31 05:01:00 +01:00
assert( p->nSize > 0 );
2005-07-29 17:01:00 +02:00
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;
2012-07-21 00:54:08 +02:00
p->pArray = ABC_REALLOC( char, p->pArray, nCapMin );
p->nCap = nCapMin;
2005-07-29 17:01:00 +02:00
}
/**Function*************************************************************
Synopsis [Fills the vector with given number of entries.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
2008-10-29 16:01:00 +01:00
static inline void Vec_StrFill( Vec_Str_t * p, int nSize, char Fill )
2005-07-29 17:01:00 +02:00
{
int i;
Vec_StrGrow( p, nSize );
p->nSize = nSize;
for ( i = 0; i < p->nSize; i++ )
2008-10-29 16:01:00 +01:00
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;
2010-11-01 09:35:04 +01:00
if ( nSize <= p->nSize )
2008-10-29 16:01:00 +01:00
return;
2010-11-01 09:35:04 +01:00
if ( nSize > 2 * p->nCap )
Vec_StrGrow( p, nSize );
else if ( nSize > p->nCap )
Vec_StrGrow( p, 2 * p->nCap );
2008-10-29 16:01:00 +01:00
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 );
2005-07-29 17:01:00 +02:00
}
/**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;
}
2010-11-01 09:35:04 +01:00
/**Function*************************************************************
2008-01-31 05:01:00 +01:00
2010-11-01 09:35:04 +01:00
Synopsis [Returns the last entry and removes it from the list.]
2008-01-31 05:01:00 +01:00
2010-11-01 09:35:04 +01:00
Description []
2008-01-31 05:01:00 +01:00
SideEffects []
SeeAlso []
2010-11-01 09:35:04 +01:00
***********************************************************************/
static inline char Vec_StrPop( Vec_Str_t * p )
2008-01-31 05:01:00 +01:00
{
2010-11-01 09:35:04 +01:00
assert( p->nSize > 0 );
return p->pArray[--p->nSize];
}
2008-01-31 05:01:00 +01:00
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
static inline void Vec_StrPrintNum( Vec_Str_t * p, int Num )
{
2010-11-01 09:35:04 +01:00
int i;
char Digits[16];
if ( Num == 0 )
2008-01-31 05:01:00 +01:00
{
2010-11-01 09:35:04 +01:00
Vec_StrPush( p, '0' );
2008-01-31 05:01:00 +01:00
return;
}
2010-11-01 09:35:04 +01:00
if ( Num < 0 )
2008-01-31 05:01:00 +01:00
{
2010-11-01 09:35:04 +01:00
Vec_StrPush( p, '-' );
Num = -Num;
2008-01-31 05:01:00 +01:00
}
2010-11-01 09:35:04 +01:00
for ( i = 0; Num; Num /= 10, i++ )
Digits[i] = (char)('0' + Num % 10);
for ( i--; i >= 0; i-- )
Vec_StrPush( p, Digits[i] );
2008-01-31 05:01:00 +01:00
}
/**Function*************************************************************
Synopsis []
Description []
SideEffects []
SeeAlso []
***********************************************************************/
2010-11-01 09:35:04 +01:00
static inline void Vec_StrPrintStr( Vec_Str_t * p, const char * pStr )
2008-01-31 05:01:00 +01:00
{
int i, Length = strlen(pStr);
for ( i = 0; i < Length; i++ )
Vec_StrPush( p, pStr[i] );
}
2005-08-09 17:01:00 +02:00
/**Function*************************************************************
Synopsis [Appends the string to the char vector.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
2010-11-01 09:35:04 +01:00
static inline void Vec_StrAppend( Vec_Str_t * p, const char * pString )
2005-08-09 17:01:00 +02:00
{
2010-11-01 09:35:04 +01:00
Vec_StrPrintStr( p, pString );
2005-07-29 17:01:00 +02:00
}
/**Function*************************************************************
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;
}
}
2005-07-29 17:01:00 +02:00
/**Function*************************************************************
Synopsis [Comparison procedure for two clauses.]
Description []
SideEffects []
SeeAlso []
***********************************************************************/
2008-07-02 17:01:00 +02:00
static int Vec_StrSortCompare1( char * pp1, char * pp2 )
2005-07-29 17:01:00 +02:00
{
// 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 []
***********************************************************************/
2008-07-02 17:01:00 +02:00
static int Vec_StrSortCompare2( char * pp1, char * pp2 )
2005-07-29 17:01:00 +02:00
{
// 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 )
2008-01-31 05:01:00 +01:00
qsort( (void *)p->pArray, p->nSize, sizeof(char),
2005-07-29 17:01:00 +02:00
(int (*)(const void *, const void *)) Vec_StrSortCompare2 );
else
2008-01-31 05:01:00 +01:00
qsort( (void *)p->pArray, p->nSize, sizeof(char),
2005-07-29 17:01:00 +02:00
(int (*)(const void *, const void *)) Vec_StrSortCompare1 );
}
2012-02-11 09:22:05 +01:00
/**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) );
}
2010-11-01 09:35:04 +01:00
/**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;
for ( i = 0; i < 4; i++ )
Vec_StrPush( vOut, (char)(Val >> (8*i)) );
}
static inline int Vec_StrGetI_ne( Vec_Str_t * vOut, int * pPos )
{
int i;
int Val = 0;
for ( i = 0; i < 4; i++ )
Val |= (int)(unsigned char)Vec_StrEntry(vOut, (*pPos)++) << (8*i);
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 )
Val |= (ch & 0x7f) << (7 * i++);
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++ )
Val |= (word)(unsigned char)Vec_StrEntry(vOut, (*pPos)++) << (8*i);
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)++);
}
2010-11-01 09:35:04 +01:00
ABC_NAMESPACE_HEADER_END
2008-01-31 05:01:00 +01:00
#endif
2005-07-29 17:01:00 +02:00
////////////////////////////////////////////////////////////////////////
/// END OF FILE ///
////////////////////////////////////////////////////////////////////////