Solved some issues and prepared the library for using unordered_map instead of hash_map (C++ TR1). Can only use this when g++-4.0.3 is out
This commit is contained in:
parent
f128fcd86f
commit
bac6644915
11 changed files with 149 additions and 33 deletions
|
|
@ -46,7 +46,7 @@ size_t get_depth(const SymVec& vec) {
|
|||
return dp;
|
||||
}
|
||||
|
||||
Sym::Sym(token_t tok, const SymVec& args_)
|
||||
Sym::Sym(token_t tok, const SymVec& args_) : node(dag.end())
|
||||
{
|
||||
detail::SymKey key(tok, detail::SymArgs(args_));
|
||||
detail::SymValue val;
|
||||
|
|
@ -64,7 +64,7 @@ Sym::Sym(token_t tok, const SymVec& args_)
|
|||
else incref();
|
||||
}
|
||||
|
||||
Sym::Sym(token_t tok, const Sym& a) {
|
||||
Sym::Sym(token_t tok, const Sym& a) : node(dag.end()) {
|
||||
SymVec args_; args_.push_back(a);
|
||||
detail::SymKey key(tok, detail::SymArgs(args_));
|
||||
detail::SymValue val;
|
||||
|
|
@ -82,7 +82,7 @@ Sym::Sym(token_t tok, const Sym& a) {
|
|||
else incref();
|
||||
}
|
||||
|
||||
Sym::Sym(token_t tok) {
|
||||
Sym::Sym(token_t tok) : node(dag.end()) {
|
||||
detail::SymKey key(tok);
|
||||
detail::SymValue val;
|
||||
node = dag.insert(pair<const detail::SymKey, detail::SymValue>(key, val)).first;
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@
|
|||
|
||||
#if __GNUC__ >= 3
|
||||
#include <backward/hash_map.h>
|
||||
#else
|
||||
#elif __GNUC__ < 3
|
||||
#include <hash_map.h>
|
||||
using std::hash_map;
|
||||
#endif
|
||||
|
|
@ -33,15 +33,23 @@ struct UniqueNodeStats { virtual ~UniqueNodeStats(){} };
|
|||
#include "SymImpl.h"
|
||||
#include "token.h"
|
||||
|
||||
typedef hash_map<detail::SymKey, detail::SymValue, detail::SymKey::Hash> SymMap;
|
||||
typedef SymMap::iterator SymIterator;
|
||||
class Sym;
|
||||
#if __GNUC__ == 444
|
||||
#define USE_TR1 1
|
||||
#else
|
||||
#define USE_TR1 0
|
||||
#endif
|
||||
|
||||
typedef std::vector<Sym> SymVec;
|
||||
#if USE_TR1
|
||||
#include <tr1/unordered_map>
|
||||
typedef std::tr1::unordered_map<detail::SymKey, detail::SymValue, detail::SymKey::Hash> SymMap;
|
||||
#else
|
||||
typedef hash_map<detail::SymKey, detail::SymValue, detail::SymKey::Hash> SymMap;
|
||||
#endif
|
||||
|
||||
typedef SymMap::iterator SymIterator;
|
||||
|
||||
/* Sym is the tree, for which all the nodes are stored in a hash table.
|
||||
* This makes checking for equality O(1) */
|
||||
|
||||
class Sym
|
||||
{
|
||||
public:
|
||||
|
|
@ -67,7 +75,7 @@ class Sym
|
|||
/* Unique Stats are user defined */
|
||||
UniqueNodeStats* extra_stats() const { return empty()? 0 : node->second.uniqueNodeStats; }
|
||||
|
||||
int hashcode() const { detail::SymKey::Hash hash; return hash(node->first); }
|
||||
int hashcode() const { return node->first.get_hash_code(); } //detail::SymKey::Hash hash; return hash(node->first); }
|
||||
|
||||
// Friends, need to touch the node
|
||||
friend struct detail::SymKey::Hash;
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ namespace detail {
|
|||
|
||||
class SymArgsImpl {
|
||||
public:
|
||||
vector<Sym> owned_args;
|
||||
std::vector<Sym> owned_args;
|
||||
};
|
||||
|
||||
size_t SymArgs::len() const {
|
||||
|
|
@ -76,7 +76,7 @@ int SymKey::calc_hash() const {
|
|||
unsigned long hash = unsigned(token);
|
||||
hash *= PRIMET;
|
||||
|
||||
const SymVec& v = args.vec();
|
||||
const std::vector<Sym>& v = args.vec();
|
||||
for (unsigned i = 0; i < v.size(); ++i) {
|
||||
hash += ( (v[i].address() >> 3) * primes[i%nprimes]) % HASHMOD;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,6 +22,16 @@
|
|||
#include "token.h"
|
||||
|
||||
class Sym;
|
||||
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
|
||||
#include <ext/pool_allocator.h>
|
||||
typedef std::vector<Sym, __gnu_cxx::__pool_alloc<Sym> > std::vector<Sym>;
|
||||
//typedef std::vector<Sym> SymVec;
|
||||
#else
|
||||
typedef std::vector<Sym> SymVec;
|
||||
#endif
|
||||
|
||||
|
||||
namespace detail {
|
||||
|
||||
class SymArgsImpl;
|
||||
|
|
@ -47,8 +57,8 @@ class SymArgs {
|
|||
class SymKey
|
||||
{
|
||||
public:
|
||||
SymKey(token_t _token) : args(), token(_token) {}
|
||||
SymKey(token_t _token, const detail::SymArgs& _args) : args(_args), token(_token) {}
|
||||
SymKey(token_t _token) : args(), token(_token), hash_code(calc_hash()) {}
|
||||
SymKey(token_t _token, const detail::SymArgs& _args) : args(_args), token(_token), hash_code(calc_hash()) {}
|
||||
|
||||
|
||||
private:
|
||||
|
|
@ -69,8 +79,11 @@ class SymKey
|
|||
// fixates (i.e. claims memory) for the embedded vector of Syms
|
||||
void fixate() const { args.fixate(); }
|
||||
|
||||
int get_hash_code() const { return hash_code; }
|
||||
|
||||
private:
|
||||
int calc_hash() const;
|
||||
int hash_code;
|
||||
};
|
||||
|
||||
struct SymValue
|
||||
|
|
|
|||
Reference in a new issue