diff options
author | Teddy <[email protected]> | 2013-08-12 08:47:24 +0800 |
---|---|---|
committer | Teddy <[email protected]> | 2013-08-12 08:47:24 +0800 |
commit | b27fd8f7572ee4774e1bdb635b0d9eda4339734a (patch) | |
tree | bfdb417705a4c8fb1b0d394568505a593f80616f | |
parent | 76977635e28e06192a486a9452e03bc7b8f612dc (diff) |
manual hash approach is even slower than STL mapfaster_fetch
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | TODO.rst | 5 | ||||
-rw-r--r-- | robust_test.scm | 21 | ||||
-rw-r--r-- | types.cpp | 14 | ||||
-rw-r--r-- | types.h | 6 |
5 files changed, 33 insertions, 15 deletions
@@ -1,4 +1,4 @@ -main: main.o parser.o builtin.o model.o eval.o exc.o consts.o types.o +main: main.o parser.o builtin.o model.o eval.o exc.o consts.o types.o hash.o g++ -o main $^ -pg -lgmp .cpp.o: @@ -1,7 +1,6 @@ -- OPT:Special Opt Repr -- OPT:Special Arg Checking -- Garbage Collection +- OPT:Special Arg Checking (Again) - Testing +- Garbage Collection - Rounding support - ext_repr optimization - Add macro support diff --git a/robust_test.scm b/robust_test.scm index 6dd4abf..4b68c4e 100644 --- a/robust_test.scm +++ b/robust_test.scm @@ -229,3 +229,24 @@ src (list x) x (cons x x) + +(define (f x) + (if (<= x 2) 1 (+ (f (- x 1)) (f (- x 2))))) +(f 1) +(f 2) +(f 3) +(f 4) +(f 5) + +(define (g n) + (define (f p1 p2 n) + (if (<= n 2) + p2 + (f p2 (+ p1 p2) (- n 1)))) + (f 1 1 n)) + +(define (all i n) + (if (= n i) + #f + (and (display (g i)) (all (+ i 1) n)))) +(all 1 100) @@ -2,6 +2,7 @@ #include "model.h" #include "exc.h" #include "consts.h" +#include "hash.h" #include <cmath> #include <cstdlib> @@ -12,6 +13,7 @@ const double EPS = 1e-16; const int PREC = 16; EmptyList *empty_list = new EmptyList(); +static VariableHash var_hash; Pair::Pair(EvalObj *_car, EvalObj *_cdr) : EvalObj(CLS_PAIR_OBJ), car(_car), cdr(_cdr), @@ -219,20 +221,16 @@ bool Environment::add_binding(SymObj *sym_obj, EvalObj *eval_obj, bool def) { if (!def) { for (Environment *ptr = this; ptr; ptr = ptr->prev_envt) - { - bool has_key = ptr->binding.count(name); - if (has_key) + if (var_hash.insert(ptr, name, eval_obj, false)) { - ptr->binding[name] = eval_obj; found = true; break; } - } return found; } else { - binding[name] = eval_obj; + var_hash.insert(this, name, eval_obj); return true; } } @@ -244,8 +242,8 @@ EvalObj *Environment::get_obj(EvalObj *obj) { string name(sym_obj->val); for (Environment *ptr = this; ptr; ptr = ptr->prev_envt) { - bool has_key = ptr->binding.count(name); - if (has_key) return ptr->binding[name]; + EvalObj *obj = var_hash.get(ptr, name); + if (obj) return obj; } // Object not found throw TokenError(name, RUN_ERR_UNBOUND_VAR); @@ -32,7 +32,7 @@ static const int NUM_LVL_INT = 3; typedef set<EvalObj*> EvalObjAddrHash; typedef vector<EvalObj*> EvalObjVec; -typedef map<string, EvalObj*> Str2EvalObj; +//typedef map<string, EvalObj*> Str2EvalObj; typedef EvalObj* (*BuiltinProc)(Pair *, const string &); class PairReprCons; @@ -326,8 +326,8 @@ class PromObj: public EvalObj {/*{{{*/ class Environment {/*{{{*/ private: Environment *prev_envt; /**< Pointer to the upper-level environment */ - Str2EvalObj binding; /**< Store all pairs of identifier and its - corresponding obj */ +// Str2EvalObj binding; /**< Store all pairs of identifier and its +// corresponding obj */ public: /** Create an runtime environment * @param prev_envt the outer environment |