Commit 9312687f authored by Benoit Perrot's avatar Benoit Perrot
Browse files

Index: ChangeLog

from  Benoît Perrot  <benoit@lrde.epita.fr>

	Introduce unique_strings.
	
	* src/misc/unique_string.hh, src/misc/unique_string.cc:
	Declare and define the string uniquifier.
	* src/misc/test-unique_string.cc: Test it.
	* src/misc/Makefile.am: Update accordingly.
	* src/Makefile.am: Link with libmisc.a.
	* src/inst/section.hh, src/inst/label.hh: 
	Use misc::unique_string instead of std::string.
	* src/inst/label.cc: Empty it. 
parent 31407288
2005-01-14 Benot Perrot <benoit@lrde.epita.fr>
Introduce unique_strings.
* src/misc/unique_string.hh, src/misc/unique_string.cc:
Declare and define the string uniquifier.
* src/misc/test-unique_string.cc: Test it.
* src/misc/Makefile.am: Update accordingly.
* src/Makefile.am: Link with libmisc.a.
* src/inst/section.hh, src/inst/label.hh:
Use misc::unique_string instead of std::string.
* src/inst/label.cc: Empty it.
2005-01-08 Benot Perrot <benoit@lrde.epita.fr>
Migrate to bison2.0a.
......
......@@ -29,4 +29,5 @@ nolimips_LDADD = \
inst/libinst.a \
shell/libshell.a \
task/libtask.a \
misc/libmisc.a \
@READLINE_LIBS@
//
// This file is part of Nolimips, a MIPS simulator with unlimited registers
// Copyright (C) 2003, 2004 Benoit Perrot <benoit@lrde.epita.fr>
// Copyright (C) 2003, 2004, 2005 Benoit Perrot <benoit@lrde.epita.fr>
//
// Nolimips is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
......@@ -21,6 +21,4 @@
namespace inst
{
Label::string_set_type Label::set_;
} // namespace inst
//
// This file is part of Nolimips, a MIPS simulator with unlimited registers
// Copyright (C) 2003, 2004 Benoit Perrot <benoit@lrde.epita.fr>
// Copyright (C) 2003, 2004, 2005 Benoit Perrot <benoit@lrde.epita.fr>
//
// Nolimips is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
......@@ -23,56 +23,46 @@
# include <string>
# include <iostream>
# include "misc/unique_string.hh"
namespace inst
{
class Label
{
typedef std::set<std::string> string_set_type;
typedef string_set_type::size_type size_type;
public:
explicit Label (const std::string &s):
set_node_ (set_.insert (s).first)
{
}
public:
const std::string& get_name () const
{
return *set_node_;
}
Label(const std::string &s):
ustr_(misc::unique_string::create(s))
{}
public:
static size_type map_size ()
const std::string &get_name() const
{
return set_.size ();
return *ustr_;
}
public:
bool operator== (const Label &rhs) const
bool operator==(const Label &rhs) const
{
return set_node_ == rhs.set_node_;
return ustr_ == rhs.ustr_;
}
bool operator!= (const Label &rhs) const
bool operator!=(const Label &rhs) const
{
return !this->operator== (rhs);
return ! this->operator==(rhs);
}
bool operator< (const Label &rhs) const
bool operator<(const Label &rhs) const
{
return &this->get_name () < &rhs.get_name ();
return ustr_ < rhs.ustr_;
}
private:
static string_set_type set_;
const string_set_type::const_iterator set_node_;
const misc::unique_string ustr_;
};
inline std::ostream&
operator<< (std::ostream &ostr, const Label &l)
{
return ostr << l.get_name ();
return ostr << l.get_name();
}
} // namespace inst
......
//
// This file is part of Nolimips, a MIPS simulator with unlimited registers
// Copyright (C) 2003, 2004 Benoit Perrot <benoit@lrde.epita.fr>
// Copyright (C) 2003, 2004, 2005 Benoit Perrot <benoit@lrde.epita.fr>
//
// Nolimips is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
......@@ -23,6 +23,7 @@
# include <map>
# include "misc/contract.hh"
# include "misc/unique_string.hh"
# include "inst/label.hh"
......@@ -42,7 +43,7 @@ namespace inst
public:
/// Construct an abstract section `\a name'
Section(const std::string &name):
name_(name)
name_(misc::unique_string::create(name))
{}
virtual ~Section()
{}
......@@ -77,7 +78,7 @@ namespace inst
virtual void print(std::ostream& ostr) const = 0;
protected:
std::string name_;
const misc::unique_string &name_;
// FIXME: Might be static, to avoid multi-definition of labels.
offset_label_type labels_;
......@@ -91,7 +92,6 @@ namespace inst
return ostr;
}
} // namespace inst
#endif // !INST_SECTION_HH
AM_CPPFLAGS = -I $(top_srcdir)/src -I $(top_builddir)/src
noinst_LIBRARIES = libmisc.a
noinst_HEADERS = \
libmisc_a_CPPFLAGS = -I $(top_srcdir)/src -I $(top_builddir)/src
libmisc_a_SOURCES = \
contract.hh \
deref.hh \
escape.hh \
has.hh \
counted_ptr.hh \
select_const.hh \
unique_string.hh unique_string.cc \
readline.hh
check_PROGRAMS = test-unique_string
test_unique_string_CPPFLAGS = -I $(top_srcdir)/src -I $(top_builddir)/src
test_unique_string_SOURCES = test-unique_string.cc
test_unique_string_LDADD = libmisc.a
TESTS = $(check_PROGRAMS)
//
// test-unique_string.cc: test string uniquifier
// Copyright (C) 2005 Benoit Perrot <benoit@lrde.epita.fr>
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// It is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "misc/contract.hh"
#include "misc/unique_string.hh"
int
main()
{
const misc::unique_string &foo0 = misc::unique_string::create("foo");
const misc::unique_string &foo1 = misc::unique_string::create("foo");
const misc::unique_string &bar = misc::unique_string::create("bar");
assertion(misc::unique_string::pool_size() == 2);
assertion(*foo0 == "foo");
assertion(foo0 == foo1);
assertion(foo0 != bar);
assertion(bar < foo1);
return 0;
}
//
// unique_string.cc: string uniquifier
// Copyright (C) 2005 Benoit Perrot <benoit@lrde.epita.fr>
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// It is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#include "misc/unique_string.hh"
namespace misc
{
unique_string::string_to_unique_type unique_string::pool_;
const unique_string &
unique_string::create(const std::string &str)
{
unique_string::string_to_unique_type::iterator it = pool_.find(&str);
if (it == pool_.end() || it->first->compare(str))
{
std::string *s = new std::string(str);
it = pool_.insert(it, unique_string::string_to_unique_type::
value_type(s, unique_string(*s)));
}
return it->second;
}
} // namespace misc
//
// unique_string.hh: string uniquifier
// Copyright (C) 2005 Benoit Perrot <benoit@lrde.epita.fr>
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// It is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
#ifndef MISC_UNIQUE_STRING_HH
# define MISC_UNIQUE_STRING_HH
/** \file misc/unique_string.hh
\brief String uniquifier.
The idea of this class comes from TC's LRDE's Symbol class, but
the implementation is quite different. */
#include <string>
#include <map>
#include <iostream>
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::map<const std::string*, unique_string,
string_ptr_less> string_to_unique_type;
private:
unique_string(const std::string &str):
str_(str)
{}
public:
static const unique_string &create(const std::string &str);
// Accessors
public:
const std::string &get() const
{
return str_;
}
operator const std::string &() const
{
return str_;
}
const std::string & operator* () const
{
return str_;
}
static unsigned pool_size()
{
return pool_.size();
}
// Operators
public:
bool operator==(const unique_string &rhs) const
{
return &str_ == &rhs.str_;
}
bool operator!=(const unique_string &rhs) const
{
return &str_ != &rhs.str_;
}
bool operator<(const unique_string &rhs) const
{
if (&str_ == &rhs.str_)
return false;
return str_ < rhs.str_;
}
private:
static string_to_unique_type pool_;
private:
const std::string &str_;
};
inline std::ostream &
operator<<(std::ostream &ostr, const unique_string &u)
{
ostr << *u;
return ostr;
}
} // namespace misc
#endif // !MISC_UNIQUE_STRING_HH
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