mCRL2
Loading...
Searching...
No Matches
index_traits.h
Go to the documentation of this file.
1// Author(s): Wieger Wesselink
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9
10// By putting this outside the definition of MCRL2_ATERMPP_INDEX_TRAITS_H a failing header test is avoided.
12
13#ifndef MCRL2_ATERMPP_INDEX_TRAITS_H
14#define MCRL2_ATERMPP_INDEX_TRAITS_H
15#pragma once
16
19
20namespace atermpp {
21
22namespace detail {
23
24template <typename Variable, typename KeyType>
26{
28 return m;
29}
30
31template <typename Variable, typename KeyType>
32std::stack<std::size_t>& variable_map_free_numbers()
33{
34 static std::stack<std::size_t> s;
35 return s;
36}
37
38template <typename Variable, typename KeyType>
39std::mutex& variable_mutex()
40{
41 static std::mutex m;
42 return m;
43}
44
45
46template <typename Variable, typename KeyType>
48{
49 static std::size_t s;
50 return s;
51}
52
62template <typename Variable, typename KeyType, const int N>
64{
65
66public:
68 static inline
69 std::size_t index(const Variable& x)
70 {
71 const _aterm_int* i = reinterpret_cast<const _aterm_int*>(address(x[N]));
72 return i->value();
73 }
74
76 static inline
77 std::size_t max_index()
78 {
79 return variable_map_max_index<Variable, KeyType>();
80 }
81
84 static inline
85 std::size_t insert(const KeyType& x)
86 {
87 if constexpr (mcrl2::utilities::detail::GlobalThreadSafe) { variable_mutex<Variable, KeyType>().lock(); }
88
89 auto& m = variable_index_map<Variable, KeyType>();
90 auto i = m.find(x);
91 std::size_t value;
92 if (i == m.end())
93 {
94 auto& s = variable_map_free_numbers<Variable, KeyType>();
95 if (s.empty())
96 {
97 value = m.size();
98 variable_map_max_index<Variable, KeyType>() = value;
99 }
100 else
101 {
102 value = s.top();
103 s.pop();
104 }
105 m[x] = value;
106 }
107 else
108 {
109 value = i->second;
110 }
111 if constexpr (mcrl2::utilities::detail::GlobalThreadSafe) { variable_mutex<Variable, KeyType>().unlock(); }
112 return value;
113 }
114
117 static inline
118 void erase(const KeyType& x)
119 {
120 if constexpr (mcrl2::utilities::detail::GlobalThreadSafe) { variable_mutex<Variable, KeyType>().lock(); }
121
122 auto& m = variable_index_map<Variable, KeyType>();
123 auto& s = variable_map_free_numbers<Variable, KeyType>();
124 auto i = m.find(x);
125 assert(i != m.end());
126 s.push(i->second);
127 m.erase(i);
128
129 if constexpr (mcrl2::utilities::detail::GlobalThreadSafe) { variable_mutex<Variable, KeyType>().unlock(); }
130 }
131
134 static inline
135 std::size_t size()
136 {
137 auto& m = variable_index_map<Variable, KeyType>();
138 return m.size();
139 }
140};
141
142} // namespace detail
143
144} // namespace atermpp
145
146#endif // MCRL2_ATERMPP_INDEX_TRAITS_H
This file contains an implementation of the hash function to break circular header dependencies.
The underlying integer term that actually carries the integer data.
Definition aterm_int.h:22
std::size_t value() const noexcept
Definition aterm_int.h:30
A unordered_map class in which aterms can be stored.
_aterm * address(const unprotected_aterm_core &t)
Definition aterm_core.h:239
std::stack< std::size_t > & variable_map_free_numbers()
atermpp::unordered_map< KeyType, std::size_t > & variable_index_map()
std::mutex & variable_mutex()
std::size_t & variable_map_max_index()
The main namespace for the aterm++ library.
Definition algorithm.h:21
static constexpr bool GlobalThreadSafe
Enables thread safety for the whole toolset.
For several variable types in mCRL2 an implicit mapping of these variables to integers is available....
static std::size_t size()
Note: intended for internal use only! Provides the size of the variable index map.
static void erase(const KeyType &x)
Note: intended for internal use only! Removes the variable from the index map.
static std::size_t max_index()
Returns an upper bound for the largest index of a variable that is currently in use.
static std::size_t insert(const KeyType &x)
Note: intended for internal use only! Returns the index of the variable. If the variable was not alre...
static std::size_t index(const Variable &x)
Returns the index of the variable.