Commit cca2f866 authored by Benoit Perrot's avatar Benoit Perrot
Browse files

2005-09-03 Benoît Perrot <benoit@lrde.epita.fr>

	* src/misc/unique_string.hh, src/misc/unique_string.cc: 
	Introduce a constructor from a const char*. Factor string
	insertion in pool.
	Suggested by Akim Demaille: Use a set of strings instead of a set
	of pointers as internal pool. Compose an iterator instead of a
	pointer.
	* src/parse/asm-scan.ll.gen.py: 
	Suggested by Akim Demaille: Don't be affraid to side-affect
	yytext.
parent 7e46b563
2005-09-03 Benot Perrot <benoit@lrde.epita.fr>
* src/misc/unique_string.hh, src/misc/unique_string.cc:
Introduce a constructor from a const char*. Factor string
insertion in pool.
Suggested by Akim Demaille: Use a set of strings instead of a set
of pointers as internal pool. Compose an iterator instead of a
pointer.
* src/parse/asm-scan.ll.gen.py:
Suggested by Akim Demaille: Don't be affraid to side-affect
yytext.
2005-09-01 Benot Perrot <benoit@lrde.epita.fr>
Shame on me.
......@@ -11,7 +23,7 @@
* src/misc/unique_string.hh, src/misc/unique_string.cc (create):
Remove. Move its implementation to unique_string's constructor,
hence make unique_string aggregate a pointer-to-string instead of a
hence make unique_string compose a pointer-to-string instead of a
reference.
* src/misc/test-unique_string.cc, src/vm/virtual_machine.cc,
* src/shell/shell.cc, src/parse/asm-parse.yy.gen.py,
......
......@@ -25,17 +25,13 @@ namespace misc
unique_string::string_to_unique_type unique_string::pool_;
unique_string::unique_string(const std::string &str)
unique_string::string_to_unique_type::const_iterator
unique_string::insert_in_pool(const std::string &str)
{
unique_string::string_to_unique_type::iterator it = pool_.find(&str);
if (it == pool_.end() || (*it)->compare(str))
{
std::string *s = new std::string(str);
it = pool_.insert(it, s);
}
str_ = *it;
assertion(str_);
string_to_unique_type::const_iterator it(pool_.find(str));
if (it == pool_.end() || it->compare(str))
it = pool_.insert(it, str);
return it;
}
} // namespace misc
......@@ -35,34 +35,30 @@ namespace misc
class unique_string
{
private:
struct string_ptr_less:
public std::binary_function<const std::string*, const std::string*, bool>
{
bool operator()(const std::string* s1, const std::string* s2) const
{
return *s1 < *s2;
}
};
typedef std::set<const std::string*,
string_ptr_less> string_to_unique_type;
typedef std::set<std::string> string_to_unique_type;
public:
unique_string(const std::string &str);
unique_string(const std::string &str):
it_(insert_in_pool(str))
{}
unique_string(const char *str):
it_(insert_in_pool(std::string(str)))
{}
// Accessors
public:
const std::string &get() const
{
return *str_;
return *it_;
}
operator const std::string &() const
{
return *str_;
return *it_;
}
const std::string & operator* () const
{
return *str_;
return *it_;
}
static unsigned pool_size()
......@@ -74,25 +70,28 @@ namespace misc
public:
bool operator==(const unique_string &rhs) const
{
return str_ == rhs.str_;
return it_ == rhs.it_;
}
bool operator!=(const unique_string &rhs) const
{
return str_ != rhs.str_;
return it_ != rhs.it_;
}
bool operator<(const unique_string &rhs) const
{
if (str_ == rhs.str_)
if (it_ == rhs.it_)
return false;
return *str_ < *(rhs.str_);
return *it_ < *rhs.it_;
}
private:
static string_to_unique_type::const_iterator insert_in_pool(const std::string &str);
private:
static string_to_unique_type pool_;
private:
const std::string *str_;
const string_to_unique_type::const_iterator it_;
};
inline std::ostream &
......
......@@ -149,9 +149,8 @@ print """
return LABEL;
}
{id}\":\" {
std::string label(yytext);
label.resize(yyleng - 1);
yylval->id = new misc::unique_string(label);
yytext[yyleng - 1] = 0; // Remove last character (side-effects on yytext are allowed)
yylval->id = new misc::unique_string(yytext);
return LABEL_DEF;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment