Various bugfixes and additions
This commit is contained in:
parent
14c3182200
commit
44876f0926
24 changed files with 889 additions and 187 deletions
|
|
@ -26,9 +26,9 @@ typedef UniqueNodeStats* (*NodeStatFunc)(Sym&);
|
|||
|
||||
UniqueNodeStats* (*Sym::factory)(const Sym&) = 0;
|
||||
|
||||
void (*Sym::extra_dtor)(token_t) = 0;
|
||||
|
||||
SymMap Sym::dag(100000); // reserve space for so many nodes
|
||||
std::vector<unsigned> Sym::token_count;
|
||||
|
||||
|
||||
size_t get_size(const SymVec& vec) {
|
||||
size_t sz = 0;
|
||||
|
|
@ -56,10 +56,17 @@ Sym::Sym(token_t tok, const SymVec& args_) : node(dag.end())
|
|||
if (__unchecked_refcount() == 0) { // new node, set some stats
|
||||
node->second.size = 1 + get_size(args_);
|
||||
node->second.depth = 1 + get_depth(args_);
|
||||
|
||||
// token count
|
||||
if (tok >= token_count.size()) {
|
||||
token_count.resize(tok+1);
|
||||
}
|
||||
|
||||
incref();
|
||||
node->first.fixate();
|
||||
// call the factory function if available
|
||||
if (factory) node->second.uniqueNodeStats = factory(*this);
|
||||
|
||||
}
|
||||
else incref();
|
||||
}
|
||||
|
|
@ -74,6 +81,12 @@ Sym::Sym(token_t tok, const Sym& a) : node(dag.end()) {
|
|||
if (__unchecked_refcount() == 0) { // new node, set some stats
|
||||
node->second.size = 1 + get_size(args_);
|
||||
node->second.depth = 1 + get_depth(args_);
|
||||
|
||||
// token count
|
||||
if (tok >= token_count.size()) {
|
||||
token_count.resize(tok+1);
|
||||
}
|
||||
|
||||
incref();
|
||||
node->first.fixate();
|
||||
// call the factory function if available
|
||||
|
|
@ -90,10 +103,17 @@ Sym::Sym(token_t tok) : node(dag.end()) {
|
|||
if (__unchecked_refcount() == 0) { // new node, set some stats
|
||||
node->second.size = 1;
|
||||
node->second.depth = 1;
|
||||
|
||||
// token count
|
||||
if (tok >= token_count.size()) {
|
||||
token_count.resize(tok+1);
|
||||
}
|
||||
|
||||
incref();
|
||||
|
||||
// call the factory function if available
|
||||
if (factory) node->second.uniqueNodeStats = factory(*this);
|
||||
|
||||
}
|
||||
else incref();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ struct UniqueNodeStats { virtual ~UniqueNodeStats(){} };
|
|||
#include "SymImpl.h"
|
||||
#include "token.h"
|
||||
|
||||
#if __GNUC__ == 444
|
||||
#if __GNUC__ == 4
|
||||
#define USE_TR1 1
|
||||
#else
|
||||
#define USE_TR1 0
|
||||
|
|
@ -92,7 +92,7 @@ class Sym
|
|||
|
||||
/* Support for traversing trees */
|
||||
unsigned arity() const { return node->first.arity(); }
|
||||
token_t token() const { assert(!empty()); return node->first.token; }
|
||||
token_t token() const { return node->first.token; }
|
||||
|
||||
const SymVec& args() const { return node->first.vec(); }
|
||||
|
||||
|
|
@ -109,9 +109,9 @@ class Sym
|
|||
* it can for instance be used to create ERC's and what not. */
|
||||
static void set_factory_function(UniqueNodeStats* (*f)(const Sym&)) { factory=f; }
|
||||
static void clear_factory_function() { factory = 0; }
|
||||
|
||||
static void set_extra_dtor( void (*extra)(token_t) ) { extra_dtor = extra; }
|
||||
|
||||
|
||||
static const std::vector<unsigned>& token_refcount() { return token_count; }
|
||||
|
||||
unsigned address() const { return reinterpret_cast<unsigned>(&*node); }
|
||||
|
||||
private :
|
||||
|
|
@ -122,15 +122,17 @@ class Sym
|
|||
unsigned __unchecked_refcount() const { return node->second.refcount; }
|
||||
|
||||
void incref() {
|
||||
if (!empty())
|
||||
if (!empty()) {
|
||||
++(node->second.refcount);
|
||||
++token_count[token()];
|
||||
}
|
||||
}
|
||||
void decref() {
|
||||
if (!empty() && --(node->second.refcount) == 0) {
|
||||
if (extra_dtor) {
|
||||
extra_dtor(token());
|
||||
if (!empty()) {
|
||||
--token_count[token()];
|
||||
if (--(node->second.refcount) == 0) {
|
||||
dag.erase(node);
|
||||
}
|
||||
dag.erase(node);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -140,11 +142,11 @@ class Sym
|
|||
// A static hash_map that contains all live nodes..
|
||||
static SymMap dag;
|
||||
|
||||
static std::vector<unsigned> token_count;
|
||||
|
||||
// Factory function for creating extra node stats, default will be 0
|
||||
static UniqueNodeStats* (*factory)(const Sym&);
|
||||
|
||||
static void (*extra_dtor)(token_t);
|
||||
|
||||
};
|
||||
|
||||
/* Utility hash functor for syms */
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@
|
|||
|
||||
class Sym;
|
||||
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
|
||||
#if __GNUC__ > 4
|
||||
#include <ext/pool_allocator.h>
|
||||
typedef std::vector<Sym, __gnu_cxx::__pool_alloc<Sym> > std::vector<Sym>;
|
||||
//typedef std::vector<Sym> SymVec;
|
||||
|
|
@ -60,10 +60,6 @@ class SymKey
|
|||
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:
|
||||
detail::SymArgs args;
|
||||
public:
|
||||
bool operator==(const SymKey& other) const;
|
||||
|
||||
struct Hash
|
||||
|
|
@ -74,13 +70,14 @@ class SymKey
|
|||
unsigned arity() const { return args.len(); }
|
||||
const std::vector<Sym>& vec() const { return args.vec(); }
|
||||
|
||||
token_t token; // identifies the function
|
||||
|
||||
// fixates (i.e. claims memory) for the embedded vector of Syms
|
||||
void fixate() const { args.fixate(); }
|
||||
|
||||
int get_hash_code() const { return hash_code; }
|
||||
|
||||
detail::SymArgs args;
|
||||
token_t token; // identifies the function
|
||||
|
||||
private:
|
||||
int calc_hash() const;
|
||||
int hash_code;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue