//
//main.cpp
//CollectionRefactoring_04
//
//
#include <iostream>
#include "Collection"
typedef bool ( *Condition )(Type item) ;
//
//user defined condition...
//
bool lessThanFive(Type item)
{
return item < 5 ;
}
bool DivisibleByTwo(Type item)
{
return ( item % 2 == 0 ) ;
}
//
//Utility function Dump
//
void CollectionDump( Collection thisCollection )
{
Type* item = CollectionFirst( thisCollection ) ;
if( item )
{
std::cout << "Collection ( " <<
CollectionCount( thisCollection ) << " items )" << std::endl ;
while ( item )
{
std::cout << ( *item ) << " " ;
item = CollectionNext( thisCollection ) ;
}
std::cout << std::endl ;
}else
{
std::cout << " Collection Empty " << std::endl ;
}
}
//
//Select Function
//
void Select( Collection input, Condition condition, Collection result )
{
Type* item = CollectionFirst( input ) ;
while ( item )
{
if ( condition( *item ) ) CollectionAdd( result, *item ) ;
item = CollectionNext( input ) ;
}
}
int main(int argc, const char * argv[])
{
Type data[] = {
2,3,5,9,6,23,4,10,12,44,13,1,22
} ;
int sizeOfData = sizeof(data) / sizeof(Type ) ;
Collection collection ;
CollectionInitialize( collection ) ;
CollectionCreateWith( collection, data, sizeOfData ) ;
CollectionDump( collection ) ;
CollectionResult ;
CollectionInitialize( result ) ;
CollectionDump( result ) ;
Select ( collection, LessThanFive, result ) ;
CollectionDump( result );
CollectionRelease( result ) ;
select( collection, DivisibleByTwo, result ) ;
CollectionDump( result ) ;
return 0;
}
Ly8KLy9tYWluLmNwcAovL0NvbGxlY3Rpb25SZWZhY3RvcmluZ18wNAovLwovLwogCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgIkNvbGxlY3Rpb24iCiAKdHlwZWRlZiBib29sICggKkNvbmRpdGlvbiApKFR5cGUgaXRlbSkgOwogCi8vCi8vdXNlciBkZWZpbmVkIGNvbmRpdGlvbi4uLgovLwpib29sIGxlc3NUaGFuRml2ZShUeXBlIGl0ZW0pCnsKICAgICAgICByZXR1cm4gaXRlbSA8IDUgOwp9CiAKYm9vbCBEaXZpc2libGVCeVR3byhUe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