ALP User Documentation  0.8.preview
Algebraic Programming User Documentation
identities.hpp
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2021 Huawei Technologies Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
27 #ifndef _H_GRB_IDENTITIES
28 #define _H_GRB_IDENTITIES
29 
30 #include <limits>
31 
32 
33 namespace grb {
34 
53  namespace identities {
54 
56  template< typename D >
57  class zero {
58  static_assert( std::is_convertible< int, D >::value, "Cannot form identity under the requested domain" );
59 
60  public:
65  static constexpr D value() {
66  return static_cast< D >( 0 );
67  }
68  };
69  template< typename K, typename V >
70  class zero< std::pair< K, V > > {
71  public:
72  static constexpr std::pair< K, V > value() {
73  return std::make_pair( zero< K >::value(), zero< V >::value() );
74  }
75  };
76 
78  template< typename D >
79  class one {
80  static_assert( std::is_convertible< int, D >::value, "Cannot form identity under the requested domain" );
81 
82  public:
87  static constexpr D value() {
88  return static_cast< D >( 1 );
89  }
90  };
91  template< typename K, typename V >
92  class one< std::pair< K, V > > {
93  public:
94  static constexpr std::pair< K, V > value() {
95  return std::make_pair( one< K >::value(), one< V >::value() );
96  }
97  };
98 
100  template< typename D >
101  class infinity {
102  static_assert( std::is_arithmetic< D >::value, "Cannot form identity under the requested domain" );
103 
104  public:
110  static constexpr D value() {
111  return std::numeric_limits< D >::has_infinity ? std::numeric_limits< D >::infinity() : std::numeric_limits< D >::max();
112  }
113  };
114  template< typename K, typename V >
115  class infinity< std::pair< K, V > > {
116  public:
117  static constexpr std::pair< K, V > value() {
118  return std::make_pair( infinity< K >::value(), infinity< V >::value() );
119  }
120  };
121 
123  template< typename D >
125  static_assert( std::is_arithmetic< D >::value, "Cannot form identity under the requested domain" );
126 
127  public:
133  static constexpr D value() {
134  return std::numeric_limits< D >::min() == 0 ? 0 : ( std::numeric_limits< D >::has_infinity ? -std::numeric_limits< D >::infinity() : std::numeric_limits< D >::min() );
135  }
136  };
137  template< typename K, typename V >
138  class negative_infinity< std::pair< K, V > > {
139  public:
140  static constexpr std::pair< K, V > value() {
141  return std::make_pair( negative_infinity< K >::value(), negative_infinity< V >::value() );
142  }
143  };
144 
150  template< typename D >
152  static_assert( std::is_convertible< bool, D >::value, "Cannot form identity under the requested domain" );
153 
154  public:
160  static const constexpr D value() {
161  return static_cast< D >( false );
162  }
163  };
164  template< typename K, typename V >
165  class logical_false< std::pair< K, V > > {
166  public:
167  static constexpr std::pair< K, V > value() {
168  return std::make_pair( logical_false< K >::value(), logical_false< V >::value() );
169  }
170  };
171 
177  template< typename D >
178  class logical_true {
179  static_assert( std::is_convertible< bool, D >::value, "Cannot form identity under the requested domain" );
180 
181  public:
187  static constexpr D value() {
188  return static_cast< D >( true );
189  }
190  };
191  template< typename K, typename V >
192  class logical_true< std::pair< K, V > > {
193  public:
194  static constexpr std::pair< K, V > value() {
195  return std::make_pair( logical_true< K >::value(), logical_true< V >::value() );
196  }
197  };
198 
199  } // namespace identities
200 } // namespace grb
201 
202 #endif
203 
Standard identity for numerical addition.
Definition: identities.hpp:57
static constexpr D value()
Definition: identities.hpp:187
Standard identity for numerical multiplication.
Definition: identities.hpp:79
Standard identity for the minimum operator.
Definition: identities.hpp:101
Standard identitity for the logical or operator.
Definition: identities.hpp:151
static const constexpr D value()
Definition: identities.hpp:160
Standard identity for the logical AND operator.
Definition: identities.hpp:178
The ALP/GraphBLAS namespace.
Definition: graphblas.hpp:477
static constexpr D value()
Definition: identities.hpp:65
static constexpr D value()
Definition: identities.hpp:87
static constexpr D value()
Definition: identities.hpp:110
static constexpr D value()
Definition: identities.hpp:133
Standard identity for the maximum operator.
Definition: identities.hpp:124