23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
-
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
-
-
+
+
-
-
-
+
+
+
-
-
+
+
-
-
-
+
+
+
-
-
-
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
-
-
-
-
-
-
+
+
+
+
+
-
-
-
+
+
+
-
-
-
-
+
+
+
+
-
+
-
-
+
+
-
-
-
+
+
+
-
+
-
+
+
|
return m_numVars < rhs.m_numVars;
}
};
class MatchSolution
{
public:
size_t complexity() const { return m_complexity; }
size_t numVars() const;
public:
size_t complexity() const { return m_complexity; }
size_t numVars() const;
MatchScore score() const
{
MatchScore score() const { return MatchScore{ m_complexity, m_pVars ? m_pVars->size() : 0 }; }
template< typename T >
const T* getVar( StringId name ) const
{
if( name == "_"_sid )
return nullptr;
return MatchScore{ m_complexity, m_pVars ? m_pVars->size() : 0 };
}
template< typename T > const T* getVar( StringId name ) const
{
if( name == "_"_sid )
return nullptr;
if( !m_pVars )
return nullptr;
if( !m_pVars )
return nullptr;
auto it = m_pVars->find( name );
if( it == m_pVars->end() )
return nullptr;
auto it = m_pVars->find( name );
if( it == m_pVars->end() )
return nullptr;
return any_cast< T >( &it->second );
}
return any_cast< T >( &it->second );
}
// Tries to set the variable. If it already exists but have a different
// value, returns false.
template< typename T >
// Tries to set the variable. If it already exists but have a different
// value, returns false.
template< typename T > bool setVar( StringId name, T&& val )
bool setVar( StringId name, T&& val )
{
if( name == "_"_sid )
return true;
{
if( name == "_"_sid )
return true;
if( m_pVars )
{
auto it = m_pVars->find( name );
if( it != m_pVars->end() )
{
using TT = remove_cvref_t< T >;
if( const auto* pExistingVal = any_cast< TT >( &it->second ) )
{
if constexpr( is_same_v< TT, VecLength > )
{
if( !AreVecLengthsCompatible( *pExistingVal, val ) )
return false;
if( m_pVars )
{
auto it = m_pVars->find( name );
if( it != m_pVars->end() )
{
using TT = remove_cvref_t< T >;
if( const auto* pExistingVal = any_cast< TT >( &it->second ) )
{
if constexpr( is_same_v< TT, VecLength > )
{
if( !AreVecLengthsCompatible( *pExistingVal, val ) )
return false;
it->second = VecLength(
max( pExistingVal->minLength(), val.minLength() ),
pExistingVal->isVariable() && val.isVariable()
it->second =
VecLength( max( pExistingVal->minLength(), val.minLength() ),
pExistingVal->isVariable() && val.isVariable() );
);
return true;
}
else
return *pExistingVal == val;
}
return true;
}
else
return *pExistingVal == val;
}
return false;
}
}
return false;
}
}
setupVars();
m_pVars->emplace( name, forward< T >( val ) );
return true;
}
setupVars();
m_pVars->emplace( name, forward< T >( val ) );
return true;
}
void addComplexity( uint32_t n ) { m_complexity += n; }
void addComplexity( uint32_t n ) { m_complexity += n; }
private:
void setupVars();
private:
void setupVars();
ptr< unordered_map< StringId, any > > m_pVars;
uint32_t m_complexity = 0; // Each matched structural element of the pattern (vector) adds 1,
// each matched literal element of the pattern adds 2
ptr< unordered_map< StringId, any > > m_pVars;
uint32_t m_complexity = 0; // Each matched structural element of the pattern (vector)
// adds 1, each matched literal element of the pattern adds 2
};
template< typename U >
static Generator< pair< MatchSolution, const U& > > Match( const Term& expression, const Trie< U >& patterns );
static Generator< pair< MatchSolution, const U& > > Match(
}
const Term& expression, const Trie< U >& patterns );
} // namespace goose::eir
#endif
|