> > 2 weeks ago, you ( DFS ) showed me
> > your "readable and maintainable" code:
> >
> > int isleapyear(int yr) {
> > if(((yr%4==0) && (yr%100!=0)) || (yr%400==0))
> > {return 0;}
> > return -1;
> > } // oackac$fve$
1...@dont-email.me
> >
> > Then I showed you my "kooky and oddly formatted" version of it:
> >
> > #define isLeapYear(y) ( !( y%400 ) || y%100 && !( y%4 ) )
>
> Who are you talking to?
Mr. DFS, see above; the thread ( oldest first ):
JeffRelf Jeff-R...@Mar.30--1.54A.Seattle.2017
PeterKöhlmann obii4i$m36$
1...@dont-email.me
MrChrisV
njtpdc5354qqii10m...@4ax.com
MrDFS obj166$7rj$
3...@dont-email.me
JeffRelf Jeff-R...@Mar.30--1.07P.Seattle.2017
Juha_Nieminen obsp3m$11mu$
1...@adenine.netfront.net
> Consider what happens if you call it eg. like isLeapYear(a+b).
> Even when you fix the macro, there's still the problem of calling it
> like isLeapYear(someReallyExpensiveFunctionThatTakesAMinuteToReturn()).
Thanks for dreaming up scary scenarios however,
like guns and knives, macros are useful too, not just dangerous.
No _serious programming language lacks "evil" macros.
Here's some more "scary" macros for you...
MicroSoft's WinMerge uses this this algorithm:
" Dynamic Programming | Set 4 (Longest Common Subsequence) "
http://www.geeksforgeeks.org/dynamic-programming-set-4-longest-common-subsequence/
My code, below, produces the same results as WinMerge:
// ScreenShot:
http://Jeff-Relf.Me/Diff.PNG
// Help/Settings:
http://Jeff-Relf.Me/X.HTM
// _FileCmp(), below, records thousands of matching lines ( text ),
// LeftOlder vs RightNewer, using the "LongestCommonSequence"
// ( shortest diffs ) algorithm.
//
// BB is a pointer to the start of
// a dynamic array of ( contiguous ) pointers ( lines );
// PP points to the end of the array.
//
// BB and PP are from the LeftOlder file.
// _BB and _PP are from the RightNewer file.
_FileCmp( LnA BB, LnA _BB, LnA PP, LnA _PP, LnT &Ln ) {
int Rows = PP - BB + 1, Cols = _PP - _BB + 1 ; u64 Row = Rows, Col = Cols ;
// Allocate a " LeftOlderLines * RightNewerLines " table of 32 bit integers
// to store all LongestCommonSequence Lengths.
pInt _Table = (pInt)MallocTmp( Rows * Cols * szInt );
{ LoopRow( Rows ) { LoopCol( Cols ) {
if ( !Row || !Col ) { Table( Row, Col ) = 0 ; continue ; }
if ( aMatch ) Table( Row, Col ) = Table( Row - 1, Col - 1 ) + 1 ;
else Table( Row, Col ) = ER( Table( Row, Col - 1 ), Table( Row - 1, Col ) ); } } }
// " Ln " is a dynamic array of thousands of ( contiguous ) 64 bit pointers,
// repurosed to store two 32 bit intergers ( Row and Col ) in a pointer.
// If both files are the same, with X Lines, it'll store X RowCol pairs.
//
// The first " StoreRowColumn ", below, stores the end of the files;
// the last is first, and vice versa; it's reversed.
Zero( Ln ), StoreRowColumn;
while ( Row > 0 && Col > 0 )
if ( aMatch ) StoreRowColumn ;
else Table( Row, Col - 1 ) > Table( Row - 1, Col ) ? Col-- : Row-- ; }
Near Globals:
// Access " _Table ", a One Dimensional array, as if it were Two Dimensional.
#define Table( i, j ) _Table[ ( j ) * Rows + i ]
// F[-1] is the ( 16 bit ) length of the leading whitespace.
#define aMatch ( F = BB[ Row - 1 ], _F = _BB[ Col - 1 ], F[-1] == _F[-1] && Eq( F, _F ) )
// Store the matching lines as line numbers from LeftOlder and RightNewer
#define StoreRowColumn ( Inc( Ln ) = LnP( Row - 1 << 32 | Col - 1 ), Row--, Col-- )
LnP F, _F ;
Far Globals:
#define Zero( X ) memset( & X, 0, sizeof X )
#define Eq !strCmp
#define LoopRows( N ) int Row = -1, eRow = ( N ) - 1 ; while ( ++Row <= eRow )
#define LoopCols( N ) int Col = -1, eCol = ( N ) - 1 ; while ( ++Col <= eCol )
typedef wchar_t wchar ; typedef wchar *LnP ; typedef LnP *LnA ;
typedef int *pInt ; typedef void *Void_P ; typedef unsigned __int64 u64 ;
const int szInt = sizeof( int );
struct LnT { LnA BB, PP, maxPP ; }; LnT Ln ;
// " Inc() ", below, dynamically allocates thousands of ( contiguous ) pointers.
// When diff is done, " Temp_Heap " is simply destroyed.
template < typename TyT, typename TyA, typename TyP >
TyP & Inc( TyT & Xx, int N ) { int I_PP, I_PP_New, I_maxPP, rv, Sz, Temp_Heap = N >= 0 ; if ( !Temp_Heap ) N = 1 ;
I_PP = !Xx.PP ? -1 : Xx.PP - Xx.BB, I_maxPP = !Xx.maxPP ? 0 : Xx.maxPP - Xx.BB, I_PP_New = I_PP + N ; if( I_maxPP < 0 ) exit(1);
if ( I_PP_New < I_maxPP ) { OK: return Xx.PP = Xx.BB + I_PP_New, *Xx.PP ; }
rv = ER( 1024, 3 * I_PP_New ), Sz = 16 + rv * szPtr ;
Xx.maxPP = Xx.BB = TyA( Temp_Heap ? ReAllocTmp( Xx.BB, ++I_PP * szPtr, Sz ) : realloc( Xx.BB, Sz ) ), Xx.maxPP += rv ;
if( !Xx.BB ) exit(1); goto OK ; }
LnP &Inc( LnT &Ln ) { return Inc< LnT, LnA, LnP>( Ln, 1 ) ; }
LnP ReAllocTmp( Void_P B⁰, int Sz⁰, int Sz ) { Void_P B = B⁰ ; if ( Sz > 0 && Sz > Sz⁰ ) B = MallocTmp( Sz );
if ( B⁰ && Sz⁰ > 0 ) memmove( B, B⁰, Sz⁰ ); return LnP( B ); }