*** empty log message ***
[wmaker-crm.git] / src / Q.h
blob54bd037d5f314367d98050562742e7d3edb6e4dd
1 /*
2 * Q.h - support for ForAll, ThereExists, Count and Sum (i.e. quantifiers).
4 * Portability: this file uses the GNU C Statement Expression extension and
5 * so requires GCC/C++ with extensions enabled (this is checked by the
6 * header file).
7 *
8 * Copyright (c) 1997 Phil Maker
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * Id: Q.h,v 1.1.1.1 1997/11/23 11:45:50 pjm Exp
36 #ifndef _Q_h_
37 #define _Q_h_ 1
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
43 #ifndef WITHOUT_NANA
45 #ifndef __GNUC__ /* not compiling with GCC/G++ extensions */
46 error
47 This file requires the GNU C/C++ "statement expression" extension;
48 so use GCC/G++ with extensions enabled. If you have not got GCC then
49 misery follows though we may be able to do something about it
50 in the future.
51 #endif
54 * A(i,c,n,a) - true iff a is true for all values generated by for(i;c;n)
56 * Note: local variables can be introduced in this statement even in C.
59 #define A(i,c,n,a) /* ForAll */ \
60 ({ \
61 int _A_result = 1; \
62 i; \
63 while(c) { \
64 if(!(a)) { \
65 _A_result = 0; \
66 break; \
67 } \
68 n; \
69 } \
70 _A_result; \
73 /*
74 * E(i,c,n,a) - true iff exists any true a for values generated by for(i;c;n)
77 #define E(i,c,n,a) /* Exists */ \
78 ({ \
79 int _E_result = 0; \
80 i; \
81 while(c) { \
82 if(a) { \
83 _E_result = 1; \
84 break; \
85 } \
86 n; \
87 } \
88 _E_result; \
91 /*
92 * C(i,c,n,a) - count the number of times a is true over the values
93 * generated by for(i;c;n)
96 #define C(i,c,n,a) /* Count */ \
97 ({ \
98 long _C_result = 0; \
99 i; \
100 while(c) { \
101 if(a) { \
102 _C_result++; \
104 n; \
106 _C_result; \
110 * E1(i,c,n,a) - exists a single value generated by for(i;c;n)
111 * suchthat i is true.
114 #define E1(i,c,n,a) (C(i,c,n,a) == 1) /* There Exists 1 */
117 * S(i,c,n,v) - sum of v over the values generated by for(i;c;n)
120 #define S(i,c,n,v) \
121 ({ \
122 i; \
123 typeof(v) _S_result = 0; \
124 while(c) { \
125 _S_result += (v); \
126 n; \
128 _S_result; \
132 * P(i,c,n,v) - product of v over the values generated by for(i;c;n)
135 #define P(i,c,n,v) \
136 ({ \
137 i; \
138 typeof(v) _P_result = 1; \
139 while(c) { \
140 _P_result *= (v); \
141 n; \
143 _P_result; \
146 #else /* defined(WITHOUT_NANA) */
149 * we don't produce any empty stubs for Q.h when compiling without nana
150 * since calls to A(...), etc should only occur in stubbed out code such
151 * as I(...).
154 #endif /* !defined(WITHOUT_NANA) */
155 #ifdef __cplusplus
157 #endif
159 #endif /* _Q_h_ */