ALP User Documentation  0.8.preview
Algebraic Programming User Documentation
monoid.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_MONOID
28 #define _H_GRB_MONOID
29 
30 #ifdef _DEBUG
31 #include <cstdio>
32 #endif
33 
34 #include <cstddef> //size_t
35 #include <cstdlib> //posix_memalign, rand
36 #include <type_traits>
37 
38 #include <assert.h>
39 
40 #include <graphblas/identities.hpp>
41 #include <graphblas/ops.hpp>
43 
44 
45 namespace grb {
46 
53  template< class _OP, template< typename > class _ID >
54  class Monoid {
55 
56  static_assert( grb::is_operator< _OP >::value, "First template argument to Monoid must be a GraphBLAS operator" );
57 
58  static_assert( grb::is_associative< _OP >::value,
59  "Cannot form a monoid using the given operator since it is not "
60  "associative" );
61 
62  static_assert( std::is_same< typename _OP::D1, typename _OP::D3 >::value || std::is_same< typename _OP::D2, typename _OP::D3 >::value,
63  "Cannot form a monoid when the output domain does not match at least "
64  "one of its input domains" );
65 
66  public:
67 
69  typedef typename _OP::D1 D1;
70 
72  typedef typename _OP::D2 D2;
73 
75  typedef typename _OP::D3 D3;
76 
78  typedef _OP Operator;
79 
81  template< typename IdentityType >
82  using Identity = _ID< IdentityType >;
83 
84 
85  private:
86 
92  Operator op;
93 
94  public:
99  Monoid() : op() {}
100 
110  template< typename D >
111  constexpr D getIdentity() const {
112  return Identity< D >::value();
113  }
114 
121  return op;
122  }
123  };
124 
125  // type traits
126  template< class _OP, template< typename > class _ID >
127  struct is_monoid< Monoid< _OP, _ID > > {
129  static const constexpr bool value = true;
130  };
131 
132  template< class OP, template< typename > class ID >
133  struct has_immutable_nonzeroes< Monoid< OP, ID > > {
134  static const constexpr bool value = grb::is_monoid< Monoid< OP, ID > >::value &&
135  std::is_same< OP, typename grb::operators::logical_or< typename OP::D1, typename OP::D2, typename OP::D3 > >::value;
136  };
137 
138 } // namespace grb
139 
140 #endif
141 
Used to inspect whether a given operator or monoid is associative.
Definition: type_traits.hpp:202
Monoid()
Constructor that infers a default operator, given the operator type.
Definition: monoid.hpp:99
Used to inspect whether a given type is an ALP monoid.
Definition: type_traits.hpp:85
_OP Operator
The type of the underlying operator.
Definition: monoid.hpp:78
constexpr D getIdentity() const
Retrieves the identity corresponding to this monoid.
Definition: monoid.hpp:111
Provides a set of standard identities for use with ALP.
_ID1< IdentityType > Identity
The underlying identity.
Definition: monoid.hpp:82
_OP::D1 D1
The left-hand side input domain.
Definition: monoid.hpp:56
Operator getOperator() const
Retrieves the underlying operator.
Definition: monoid.hpp:120
Specifies the ALP algebraic type traits.
The ALP/GraphBLAS namespace.
Definition: graphblas.hpp:477
static const constexpr bool value
Whether T is an ALP monoid.
Definition: type_traits.hpp:92
_OP::D2 D2
The right-hand side input domain.
Definition: monoid.hpp:72
_OP::D3 D3
The output domain.
Definition: monoid.hpp:75
Used to inspect whether a given type is an ALP operator.
Definition: type_traits.hpp:104
static const constexpr bool value
Whether T a semiring where nonzeroes are immutable.
Definition: type_traits.hpp:286
Provides a set of standard binary operators.
A generalised monoid.
Definition: monoid.hpp:54