Grid 0.7.0
Lattice_comparison.h
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/lattice/Lattice_comparison.h
6
7 Copyright (C) 2015
8
9Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
10Author: Peter Boyle <paboyle@ph.ed.ac.uk>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 See the full license in the file "LICENSE" in the top level distribution directory
27*************************************************************************************/
28/* END LEGAL */
29#ifndef GRID_LATTICE_COMPARISON_H
30#define GRID_LATTICE_COMPARISON_H
31
33
35// relational operators
36//
37// Support <,>,<=,>=,==,!=
38//
39//Query supporting bitwise &, |, ^, !
40//Query supporting logical &&, ||,
42
44
46// compare lattice to lattice
48
49template<class vfunctor,class lobj,class robj>
50inline Lattice<vPredicate> LLComparison(vfunctor op,const Lattice<lobj> &lhs,const Lattice<robj> &rhs)
51{
52 Lattice<vPredicate> ret(rhs.Grid());
53 autoView( lhs_v, lhs, CpuRead);
54 autoView( rhs_v, rhs, CpuRead);
55 autoView( ret_v, ret, CpuWrite);
56 thread_for( ss, rhs_v.size(), {
57 ret_v[ss]=op(lhs_v[ss],rhs_v[ss]);
58 });
59 return ret;
60}
61
62// compare lattice to scalar
64template<class vfunctor,class lobj,class robj>
65inline Lattice<vPredicate> LSComparison(vfunctor op,const Lattice<lobj> &lhs,const robj &rhs)
66{
67 Lattice<vPredicate> ret(lhs.Grid());
68 autoView( lhs_v, lhs, CpuRead);
69 autoView( ret_v, ret, CpuWrite);
70 thread_for( ss, lhs_v.size(), {
71 ret_v[ss]=op(lhs_v[ss],rhs);
72 });
73 return ret;
74}
75
76// compare scalar to lattice
78template<class vfunctor,class lobj,class robj>
79inline Lattice<vPredicate> SLComparison(vfunctor op,const lobj &lhs,const Lattice<robj> &rhs)
80{
81 Lattice<vPredicate> ret(rhs.Grid());
82 autoView( rhs_v, rhs, CpuRead);
83 autoView( ret_v, ret, CpuWrite);
84 thread_for( ss, rhs_v.size(), {
85 ret_v[ss]=op(lhs,rhs_v[ss]);
86 });
87 return ret;
88}
89
91// Map to functors
93// Less than
94template<class lobj,class robj>
95inline Lattice<vPredicate> operator < (const Lattice<lobj> & lhs, const Lattice<robj> & rhs) {
96 return LLComparison(vlt<lobj,robj>(),lhs,rhs);
97}
98template<class lobj,class robj>
99inline Lattice<vPredicate> operator < (const Lattice<lobj> & lhs, const robj & rhs) {
100 return LSComparison(vlt<lobj,robj>(),lhs,rhs);
101}
102template<class lobj,class robj>
103inline Lattice<vPredicate> operator < (const lobj & lhs, const Lattice<robj> & rhs) {
104 return SLComparison(vlt<lobj,robj>(),lhs,rhs);
105}
106
107// Less than equal
108template<class lobj,class robj>
109inline Lattice<vPredicate> operator <= (const Lattice<lobj> & lhs, const Lattice<robj> & rhs) {
110 return LLComparison(vle<lobj,robj>(),lhs,rhs);
111}
112template<class lobj,class robj>
113inline Lattice<vPredicate> operator <= (const Lattice<lobj> & lhs, const robj & rhs) {
114 return LSComparison(vle<lobj,robj>(),lhs,rhs);
115}
116template<class lobj,class robj>
117inline Lattice<vPredicate> operator <= (const lobj & lhs, const Lattice<robj> & rhs) {
118 return SLComparison(vle<lobj,robj>(),lhs,rhs);
119}
120
121// Greater than
122template<class lobj,class robj>
124 return LLComparison(vgt<lobj,robj>(),lhs,rhs);
125}
126template<class lobj,class robj>
127inline Lattice<vPredicate> operator > (const Lattice<lobj> & lhs, const robj & rhs) {
128 return LSComparison(vgt<lobj,robj>(),lhs,rhs);
129}
130template<class lobj,class robj>
131inline Lattice<vPredicate> operator > (const lobj & lhs, const Lattice<robj> & rhs) {
132 return SLComparison(vgt<lobj,robj>(),lhs,rhs);
133}
134
135
136// Greater than equal
137template<class lobj,class robj>
139 return LLComparison(vge<lobj,robj>(),lhs,rhs);
140}
141template<class lobj,class robj>
142inline Lattice<vPredicate> operator >= (const Lattice<lobj> & lhs, const robj & rhs) {
143 return LSComparison(vge<lobj,robj>(),lhs,rhs);
144}
145template<class lobj,class robj>
146inline Lattice<vPredicate> operator >= (const lobj & lhs, const Lattice<robj> & rhs) {
147 return SLComparison(vge<lobj,robj>(),lhs,rhs);
148}
149
150// equal
151template<class lobj,class robj>
153 return LLComparison(veq<lobj,robj>(),lhs,rhs);
154}
155template<class lobj,class robj>
156inline Lattice<vPredicate> operator == (const Lattice<lobj> & lhs, const robj & rhs) {
157 return LSComparison(veq<lobj,robj>(),lhs,rhs);
158}
159template<class lobj,class robj>
160inline Lattice<vPredicate> operator == (const lobj & lhs, const Lattice<robj> & rhs) {
161 return SLComparison(veq<lobj,robj>(),lhs,rhs);
162}
163
164
165// not equal
166template<class lobj,class robj>
168 return LLComparison(vne<lobj,robj>(),lhs,rhs);
169}
170template<class lobj,class robj>
171inline Lattice<vPredicate> operator != (const Lattice<lobj> & lhs, const robj & rhs) {
172 return LSComparison(vne<lobj,robj>(),lhs,rhs);
173}
174template<class lobj,class robj>
175inline Lattice<vPredicate> operator != (const lobj & lhs, const Lattice<robj> & rhs) {
176 return SLComparison(vne<lobj,robj>(),lhs,rhs);
177}
179#endif
Lattice< vPredicate > operator==(const Lattice< lobj > &lhs, const Lattice< robj > &rhs)
Lattice< vPredicate > LSComparison(vfunctor op, const Lattice< lobj > &lhs, const robj &rhs)
Lattice< vPredicate > operator!=(const Lattice< lobj > &lhs, const Lattice< robj > &rhs)
Lattice< vPredicate > operator>(const Lattice< lobj > &lhs, const Lattice< robj > &rhs)
Lattice< vPredicate > operator<=(const Lattice< lobj > &lhs, const Lattice< robj > &rhs)
Lattice< vPredicate > LLComparison(vfunctor op, const Lattice< lobj > &lhs, const Lattice< robj > &rhs)
iScalar< vInteger > vPredicate
Lattice< vPredicate > operator<(const Lattice< lobj > &lhs, const Lattice< robj > &rhs)
Lattice< vPredicate > SLComparison(vfunctor op, const lobj &lhs, const Lattice< robj > &rhs)
Lattice< vPredicate > operator>=(const Lattice< lobj > &lhs, const Lattice< robj > &rhs)
#define autoView(l_v, l, mode)
@ CpuRead
@ CpuWrite
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
#define thread_for(i, num,...)
Definition Threads.h:60
GridBase * Grid(void) const