COMBINATORIAL_BLAS  1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Compare.h
Go to the documentation of this file.
1 /****************************************************************/
2 /* Parallel Combinatorial BLAS Library (for Graph Computations) */
3 /* version 1.2 -------------------------------------------------*/
4 /* date: 10/06/2011 --------------------------------------------*/
5 /* authors: Aydin Buluc (abuluc@lbl.gov), Adam Lugowski --------*/
6 /****************************************************************/
7 /*
8 Copyright (c) 2011, Aydin Buluc
9 
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16 
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19 
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 THE SOFTWARE.
27 */
28 
29 #ifndef _COMPARE_H_
30 #define _COMPARE_H_
31 
32 #include <cmath>
33 #include "SpDefs.h"
34 #include "CombBLAS.h"
35 
37 template <class T>
39  public binary_function< T, T, bool >
40  {
41  ErrorTolerantEqual(const T & myepsilon):epsilon(myepsilon) {};
42  inline bool operator() (const T & a, const T & b) const
43  {
44  // According to the IEEE 754 standard, negative zero and positive zero should
45  // compare as equal with the usual (numerical) comparison operators, like the == operators of C++
46 
47  if(a == b) // covers the "division by zero" case as well: max(a,b) can't be zero if it fails
48  return true; // covered the integral numbers case
49 
50  return ( std::abs(a - b) < epsilon || (std::abs(a - b) / max(std::abs(a), std::abs(b))) < epsilon ) ;
51  }
53  };
54 
55 template < typename T >
56 struct absdiff : binary_function<T, T, T>
57 {
58  T operator () ( T const &arg1, T const &arg2 ) const
59  {
60  using std::abs;
61  return abs( arg1 - arg2 );
62  }
63 };
64 
65 
66 template<class IT, class NT>
67 struct TupleEqual:
68  public binary_function< tuple<IT, IT, NT>, tuple<IT, IT, NT>, bool >
69  {
70  inline bool operator()(const tuple<IT, IT, NT> & lhs, const tuple<IT, IT, NT> & rhs) const
71  {
72  return ( (get<0>(lhs) == get<0>(rhs)) && (get<1>(lhs) == get<1>(rhs)) );
73  }
74  };
75 
76 
82 template <class IT, class NT>
83 struct ColLexiCompare: // struct instead of class so that operator() is public
84  public binary_function< tuple<IT, IT, NT>, tuple<IT, IT, NT>, bool > // (par1, par2, return_type)
85  {
86  inline bool operator()(const tuple<IT, IT, NT> & lhs, const tuple<IT, IT, NT> & rhs) const
87  {
88  if(get<1>(lhs) == get<1>(rhs))
89  {
90  return get<0>(lhs) < get<0>(rhs);
91  }
92  else
93  {
94  return get<1>(lhs) < get<1>(rhs);
95  }
96  }
97  };
98 
99 template <class IT, class NT>
100 struct RowLexiCompare: // struct instead of class so that operator() is public
101  public binary_function< tuple<IT, IT, NT>, tuple<IT, IT, NT>, bool > // (par1, par2, return_type)
102  {
103  inline bool operator()(const tuple<IT, IT, NT> & lhs, const tuple<IT, IT, NT> & rhs) const
104  {
105  if(get<0>(lhs) == get<0>(rhs))
106  {
107  return get<1>(lhs) < get<1>(rhs);
108  }
109  else
110  {
111  return get<0>(lhs) < get<0>(rhs);
112  }
113  }
114  };
115 
116 
117 // Non-lexicographical, just compares columns
118 template <class IT, class NT>
119 struct ColCompare: // struct instead of class so that operator() is public
120  public binary_function< tuple<IT, IT, NT>, tuple<IT, IT, NT>, bool > // (par1, par2, return_type)
121  {
122  inline bool operator()(const tuple<IT, IT, NT> & lhs, const tuple<IT, IT, NT> & rhs) const
123  {
124  return get<1>(lhs) < get<1>(rhs);
125  }
126  };
127 
128 // Non-lexicographical, just compares columns
129 template <class IT, class NT>
130 struct RowCompare: // struct instead of class so that operator() is public
131  public binary_function< tuple<IT, IT, NT>, tuple<IT, IT, NT>, bool > // (par1, par2, return_type)
132  {
133  inline bool operator()(const tuple<IT, IT, NT> & lhs, const tuple<IT, IT, NT> & rhs) const
134  {
135  return get<0>(lhs) < get<0>(rhs);
136  }
137  };
138 
139 template <class IT, class NT>
140 struct ColLexiCompareWithID: // struct instead of class so that operator() is public
141  public binary_function< pair< tuple<IT, IT, NT> , int > , pair< tuple<IT, IT, NT> , int>, bool > // (par1, par2, return_type)
142  {
143  inline bool operator()(const pair< tuple<IT, IT, NT> , int > & lhs, const pair< tuple<IT, IT, NT> , int > & rhs) const
144  {
145  if(get<1>(lhs.first) == get<1>(rhs.first))
146  {
147  return get<0>(lhs.first) < get<0>(rhs.first);
148  }
149  else
150  {
151  return get<1>(lhs.first) < get<1>(rhs.first);
152  }
153  }
154  };
155 
156 
157 
158 #endif