mCRL2
Loading...
Searching...
No Matches
number_postfix_generator.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//
11
12#ifndef MCRL2_UTILITIES_NUMBER_POSTFIX_GENERATOR_H
13#define MCRL2_UTILITIES_NUMBER_POSTFIX_GENERATOR_H
14
16#include <cassert>
17#include <cctype>
18#include <map>
19
20namespace mcrl2 {
21
22namespace utilities {
23
27{
28 protected:
30 mutable std::map<std::string, std::size_t> m_index;
31
33 std::string m_hint;
34
35 public:
36
38 number_postfix_generator(std::string hint = "FRESH_VAR")
39 : m_hint(hint)
40 {}
41
44 void add_identifier(const std::string& id)
45 {
46 std::string::size_type i = id.find_last_not_of("0123456789");
47 std::size_t new_index = 0;
48 std::string name;
49 if (i == std::string::npos || id.size() == i + 1) // string does not end with a number
50 {
51 name = id;
52 }
53 else
54 {
55 name = id.substr(0, i + 1);
56 std::string num = id.substr(i + 1);
57 new_index = atoi(num.c_str());
58 }
59 std::size_t old_index = m_index.find(name) == m_index.end() ? 0 : m_index[name];
60 m_index[name] = (std::max)(old_index, new_index); // Windows requires brackets around std::max.
61 }
62
67 template <typename Iter>
68 void add_identifiers(Iter first, Iter last)
69 {
70 for (Iter i = first; i != last; ++i)
71 {
73 }
74 }
75
81 template <typename Iter>
82 number_postfix_generator(Iter first, Iter last, std::string hint = "FRESH_VAR")
83 : m_hint(hint)
84 {
85 add_identifiers(first, last);
86 }
87
90 std::string operator()(std::string hint, bool add_to_context = true) const
91 {
92 // remove digits at the end of hint
93 if (std::isdigit(hint[hint.size() - 1]))
94 {
95 std::string::size_type i = hint.find_last_not_of("0123456789");
96 hint = hint.substr(0, i + 1);
97 }
98
99 auto j = m_index.find(hint);
100 if (j == m_index.end())
101 {
102 if (add_to_context)
103 {
104 m_index[hint] = 0;
105 }
106 return hint;
107 }
108 return hint + utilities::number2string(add_to_context ? ++(j->second) : j->second + 1);
109 }
110
113 std::string operator()() const
114 {
115 return (*this)(m_hint, true);
116 }
117
119 const std::string& hint() const
120 {
121 return m_hint;
122 }
123
125 std::string& hint()
126 {
127 return m_hint;
128 }
129
131 void clear()
132 {
133 m_index.clear();
134 }
135};
136
137} // namespace utilities
138
139} // namespace mcrl2
140
141#endif // MCRL2_UTILITIES_NUMBER_POSTFIX_GENERATOR_H
Identifier generator that generates names consisting of a prefix followed by a number....
number_postfix_generator(std::string hint="FRESH_VAR")
Constructor.
std::string operator()(std::string hint, bool add_to_context=true) const
Generates a fresh identifier that doesn't appear in the context.
void add_identifiers(Iter first, Iter last)
Adds the strings in the range [first, last) to the context.
number_postfix_generator(Iter first, Iter last, std::string hint="FRESH_VAR")
Constructor.
void clear()
Clear the context of the generator.
std::map< std::string, std::size_t > m_index
A map that maintains the highest index for each prefix.
void add_identifier(const std::string &id)
Adds the strings in the range [first, last) to the context.
const std::string & hint() const
Returns the default hint.
std::string & hint()
Returns the default hint.
std::string operator()() const
Generates a fresh identifier that doesn't appear in the context.
void number2string(std::size_t number, std::string &buffer, std::size_t start_position)
Convert a number to a string in the buffer starting at position start_position.
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72
String manipulation functions.