WIP: to make the ID type usable as hashtable key

This commit is contained in:
Fischlurch 2009-09-24 19:38:11 +02:00
parent c9f3a345dc
commit f278d4521c
7 changed files with 91 additions and 10 deletions

View file

@ -32,6 +32,7 @@
** - automatically maintain a symbol table at runtime to support this
** - provide some magic (macros) allowing to build distinct types based on symbols.
**
** @see symbol-impl.cpp
** @see configrules.hpp
** @see query.hpp
*/

View file

@ -46,6 +46,7 @@ liblumiera_la_SOURCES = \
$(liblumiera_la_srcdir)/test/testoption.cpp \
$(liblumiera_la_srcdir)/test/test-helper.cpp \
$(liblumiera_la_srcdir)/test/suite.cpp \
$(liblumiera_la_srcdir)/symbol-impl.cpp \
$(liblumiera_la_srcdir)/cmdline.cpp \
$(liblumiera_la_srcdir)/logging.cpp

View file

@ -25,7 +25,7 @@
/**
* @file
* Portable and safe wrapers around some clib functions and some tools
* Portable and safe wrappers around some clib functions and some tools
*/
LUMIERA_ERROR_DECLARE(NO_MEMORY);

78
src/lib/symbol-impl.cpp Normal file
View file

@ -0,0 +1,78 @@
/*
Symbol(impl) - helpers for working with literal string IDs
Copyright (C) Lumiera.org
2009, Hermann Vosseler <Ichthyostega@web.de>
This program 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.
This program 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., 675 Mass Ave, Cambridge, MA 02139, USA.
* *****************************************************/
/** @file symbol-impl.hpp
** Collection of helpers for working with the lumiera::Symbol.
**
** @todo currently as of 9/09 this is more of a placeholder.
** And maybe a location for collecting small bits of implementation,
** which could be usable later for real Symbol and Literal datatypes.
**
** lumiera::Symbol
** control::CommandRegistry for usage example of the hash function.
**
*/
#include "include/symbol.hpp"
#include "lib/safeclib.h"
#include <boost/functional/hash.hpp>
#include <cstddef>
using std::size_t;
using boost::hash_combine;
namespace lumiera {
/** equality on Symbol values is defined
* based on the content, not the address. */
inline bool
operator== (Symbol sy1, Symbol sy2)
{
return lumiera_strncmp (sy1,sy2, STRING_MAX_RELEVANT);
}
/** generating a hash value, e.g. for hashtables.
This function is intended to be picked up by ADL,
and should be usable both with \c std::tr1 and
\c <boost/functional/hash.hpp> . It is implemented
similar as the boost::hash specialisation for std::string */
size_t hash_value (Symbol sym)
{
size_t hash=0;
const char *pos = sym;
size_t maxpos = STRING_MAX_RELEVANT;
for ( ; pos && --maxpos; ++pos)
hash_combine(hash, *pos);
return hash;
}
} // namespace lumiera

View file

@ -33,6 +33,7 @@
#include <tr1/memory>
#include <tr1/unordered_map>
// #include <boost/functional/hash.hpp> /////////TODO which boost include to use here??
#include <boost/utility.hpp>

View file

@ -77,13 +77,13 @@ namespace control {
: public lib::Sync<>
, noncopyable
{
TypedAllocationManager allocator_;
// using a hashtable to implement the index
typedef std::tr1::unordered_map<Symbol, Command> CmdIndex;
typedef std::map<void*, Symbol> ReverseIndex;
CmdIndex index_;
ReverseIndex ridx_;
TypedAllocationManager allocator_;
public:

View file

@ -198,16 +198,16 @@ namespace control {
inline bool
operator== (Command const& c1, Command const& c2)
{
return (!c1 && !c2)
|| ( c1 && c2 && (&c1.impl() == &c2.impl()));
}
{
return (!c1 && !c2)
|| ( c1 && c2 && (&c1.impl() == &c2.impl()));
}
inline bool
operator!= (Command const& c1, Command const& c2)
{
return ! (c1 == c2);
}
{
return ! (c1 == c2);
}