predicate logic in c++

I need help understanding the logic and what I might program to create the forAllForSome and the forSomeForAll functions. Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// psuedo code
/*
        Program the following functions:

	1. forAllForAll(int setX[], int sizeX, int setY[], int sizeY); AxAy P(x; y)
	2. forAllForSome(int setX[], int sizeX, int setY[], int sizeY); AxEy P(x; y)
	3. forSomeForAll(int setX[], int sizeX, int setY[], int sizeY); ExAy P(x; y)
	4. forSomeForSome(int setX[], int sizeX, int setY[], int sizeY); ExEy P(x; y)
	*/
// sees if the value is in x and the second value is in y

bool Predicate::forAllForAll(int setX[], int sizeX, int setY[], int sizeY)
{
    for (int i = 0; i < sizeX; i++) // checks if all values are true for all
		for (int j = 0; j < sizeY; j++)
		{
			if(isfalse(setX, setY)
				return false; // at least one false value
		}
		return true; // all values true
}



bool Predicate::forAllForSome(int setX[], int sizeX, int setY[], int sizeY)
{

}

bool Predicate::forSomeForAll(int setX[], int sizeX, int setY[], int sizeY)
{

}


bool Predicate::forSomeForSome(int setX[], int sizeX, int setY[], int sizeY)
{
    for (int i = 0; i < sizeY; i++) // checks if all values are true for all
		for (int j = 0; j < sizeX; j++)
		{
			if(isTrue(setX, setY)
				return true; // at least one true value
		}
		return false; // all values true
}
Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
template< typename ITERATOR_A, typename ITERATOR_B, typename UNARY_PREDICATE >
bool for_all_for_all( ITERATOR_A begin_a, ITERATOR_A end_a,
                       ITERATOR_B begin_b, ITERATOR_B end_b,
                       UNARY_PREDICATE unary_predicate )
{
    return std::all_of( begin_a, begin_b, unary_predicate ) &&
            std::all_of( begin_b, end_b, unary_predicate ) ;
}

template< typename ITERATOR_A, typename ITERATOR_B, typename UNARY_PREDICATE >
bool for_all_for_some( ITERATOR_A begin_a, ITERATOR_A end_a,
                        ITERATOR_B begin_b, ITERATOR_B end_b,
                        UNARY_PREDICATE unary_predicate )
{
    return std::all_of( begin_a, begin_b, unary_predicate ) &&
            std::any_of( begin_b, end_b, unary_predicate ) ;
}

template< typename ITERATOR_A, typename ITERATOR_B, typename UNARY_PREDICATE >
bool for_none_for_some( ITERATOR_A begin_a, ITERATOR_A end_a,
                         ITERATOR_B begin_b, ITERATOR_B end_b,
                         UNARY_PREDICATE unary_predicate )
{
    return std::none_of( begin_a, begin_b, unary_predicate ) &&
            std::any_of( begin_b, end_b, unary_predicate ) ;
}

// etc 


1
2
3
4
5
6
7
8
9
10
11
struct Predicate
{
    std::function< bool(int) > is_true ;

    explicit Predicate( std::function< bool(int) > p ) : is_true(p) {}

    bool forAllForSome( const int setX[], int sizeX, const int setY[], int sizeY ) const
    { return for_all_for_some( setX, setX+sizeX, setY, setY+sizeY, is_true ) ; }

    // etc
};


1
2
3
4
5
6
7
int main()
{
    Predicate ends_in_digit_7 { [] ( int v ) { return std::abs(v)%10 == 7 ; } } ;
    const int a[5] = { 7, 17, 27, 37, 47 } ;
    const int b[7] = { 6, 17, 29, 37, 42 } ;
    std::cout << std::boolalpha << ends_in_digit_7.forAllForSome( a, 5, b, 7 ) << '\n' ;
}

Thank you!
Topic archived. No new replies allowed.