COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
Compare.h
Go to the documentation of this file.
1/****************************************************************/
2/* Parallel Combinatorial BLAS Library (for Graph Computations) */
3/* version 1.6 -------------------------------------------------*/
4/* date: 6/15/2017 ---------------------------------------------*/
5/* authors: Ariful Azad, Aydin Buluc --------------------------*/
6/****************************************************************/
7/*
8 Copyright (c) 2010-2017, The Regents of the University of California
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 <type_traits>
34#include "SpDefs.h"
35#include "CombBLAS.h"
36
37namespace combblas {
38
39// third parameter of compare is about floating point-ness
40template <class T>
41inline bool compare(const T & a, const T & b, std::false_type) // not floating point
42{
43 return (a == b);
44}
45
46template <class T>
47inline bool compare(const T & a, const T & b, std::true_type) // floating point
48{
49 // According to the IEEE 754 standard, negative zero and positive zero should
50 // compare as equal with the usual (numerical) comparison operators, like the == operators of C++
51
52 if(a == b) return true; // covers the "division by zero" case as well: max(a,b) can't be zero if it fails
53 else return ( std::abs(a - b) < EPSILON || (std::abs(a - b) / std::max(std::abs(a), std::abs(b))) < EPSILON ) ; // Fine if either absolute or relative error is small
54}
55
56
57template <class T>
59 public std::binary_function< T, T, bool >
60 {
61 inline bool operator() (const T & a, const T & b) const
62 {
63 return compare(a,b, std::is_floating_point<T>());
64 }
65 };
66
67template < typename T >
68struct absdiff : std::binary_function<T, T, T>
69{
70 T operator () ( T const &arg1, T const &arg2 ) const
71 {
72 using std::abs;
73 return abs( arg1 - arg2 );
74 }
75};
76
77
78template<class IT, class NT>
80 public std::binary_function< std::tuple<IT, IT, NT>, std::tuple<IT, IT, NT>, bool >
81 {
82 inline bool operator()(const std::tuple<IT, IT, NT> & lhs, const std::tuple<IT, IT, NT> & rhs) const
83 {
84 return ( (std::get<0>(lhs) == std::get<0>(rhs)) && (std::get<1>(lhs) == std::get<1>(rhs)) );
85 }
86 };
87
88
94template <class IT, class NT>
95struct ColLexiCompare: // struct instead of class so that operator() is public
96 public std::binary_function< std::tuple<IT, IT, NT>, std::tuple<IT, IT, NT>, bool > // (par1, par2, return_type)
97 {
98 inline bool operator()(const std::tuple<IT, IT, NT> & lhs, const std::tuple<IT, IT, NT> & rhs) const
99 {
100 if(std::get<1>(lhs) == std::get<1>(rhs))
101 {
102 return std::get<0>(lhs) < std::get<0>(rhs);
103 }
104 else
105 {
106 return std::get<1>(lhs) < std::get<1>(rhs);
107 }
108 }
109 };
110
111template <class IT, class NT>
112struct RowLexiCompare: // struct instead of class so that operator() is public
113 public std::binary_function< std::tuple<IT, IT, NT>, std::tuple<IT, IT, NT>, bool > // (par1, par2, return_type)
114 {
115 inline bool operator()(const std::tuple<IT, IT, NT> & lhs, const std::tuple<IT, IT, NT> & rhs) const
116 {
117 if(std::get<0>(lhs) == std::get<0>(rhs))
118 {
119 return std::get<1>(lhs) < std::get<1>(rhs);
120 }
121 else
122 {
123 return std::get<0>(lhs) < std::get<0>(rhs);
124 }
125 }
126 };
127
128
129// Non-lexicographical, just compares columns
130template <class IT, class NT>
131struct ColCompare: // struct instead of class so that operator() is public
132 public std::binary_function< std::tuple<IT, IT, NT>, std::tuple<IT, IT, NT>, bool > // (par1, par2, return_type)
133 {
134 inline bool operator()(const std::tuple<IT, IT, NT> & lhs, const std::tuple<IT, IT, NT> & rhs) const
135 {
136 return std::get<1>(lhs) < std::get<1>(rhs);
137 }
138 };
139
140// Non-lexicographical, just compares columns
141template <class IT, class NT>
142struct RowCompare: // struct instead of class so that operator() is public
143 public std::binary_function< std::tuple<IT, IT, NT>, std::tuple<IT, IT, NT>, bool > // (par1, par2, return_type)
144 {
145 inline bool operator()(const std::tuple<IT, IT, NT> & lhs, const std::tuple<IT, IT, NT> & rhs) const
146 {
147 return std::get<0>(lhs) < std::get<0>(rhs);
148 }
149 };
150
151template <class IT, class NT>
152struct ColLexiCompareWithID: // struct instead of class so that operator() is public
153 public std::binary_function< std::pair< std::tuple<IT, IT, NT> , int > , std::pair< std::tuple<IT, IT, NT> , int>, bool > // (par1, par2, return_type)
154 {
155 inline bool operator()(const std::pair< std::tuple<IT, IT, NT> , int > & lhs, const std::pair< std::tuple<IT, IT, NT> , int > & rhs) const
156 {
157 if(std::get<1>(lhs.first) == std::get<1>(rhs.first))
158 {
159 return std::get<0>(lhs.first) < std::get<0>(rhs.first);
160 }
161 else
162 {
163 return std::get<1>(lhs.first) < std::get<1>(rhs.first);
164 }
165 }
166 };
167
168
169
170}
171
172#endif
#define EPSILON
Definition SpDefs.h:64
bool compare(const T &a, const T &b, std::false_type)
Definition Compare.h:41
bool operator()(const std::tuple< IT, IT, NT > &lhs, const std::tuple< IT, IT, NT > &rhs) const
Definition Compare.h:134
bool operator()(const std::pair< std::tuple< IT, IT, NT >, int > &lhs, const std::pair< std::tuple< IT, IT, NT >, int > &rhs) const
Definition Compare.h:155
bool operator()(const std::tuple< IT, IT, NT > &lhs, const std::tuple< IT, IT, NT > &rhs) const
Definition Compare.h:98
bool operator()(const T &a, const T &b) const
Definition Compare.h:61
bool operator()(const std::tuple< IT, IT, NT > &lhs, const std::tuple< IT, IT, NT > &rhs) const
Definition Compare.h:145
bool operator()(const std::tuple< IT, IT, NT > &lhs, const std::tuple< IT, IT, NT > &rhs) const
Definition Compare.h:115
bool operator()(const std::tuple< IT, IT, NT > &lhs, const std::tuple< IT, IT, NT > &rhs) const
Definition Compare.h:82
T operator()(T const &arg1, T const &arg2) const
Definition Compare.h:70