Why did I think STL's map wasn't efficient? It has logarithmic
complexity (maintains a tree internally) and is faster than hashing for
small containers. It's the more portable solution and should be what I
turn to by default.
This commit is contained in:
Kartik K. Agaram 2015-04-24 17:09:17 -07:00
parent 544a1f1e17
commit 2fb94e3c4c
14 changed files with 41 additions and 46 deletions

View File

@ -3,10 +3,10 @@ parse/0: instruction: 2
parse/0: ingredient: {name: "2", value: 0, type: 0, properties: ["2": "literal"]}
parse/0: ingredient: {name: "2", value: 0, type: 0, properties: ["2": "literal"]}
parse/0: product: {name: "3", value: 0, type: 1, properties: ["3": "integer"]}
after-brace/0: recipe f
after-brace/0: add ...
after-brace/0: recipe main
after-brace/0: f ...
after-brace/0: recipe f
after-brace/0: add ...
run/0: instruction main/0
run/0: instruction f/0
run/0: ingredient 0 is 2

View File

@ -39,11 +39,16 @@ parse/0: instruction: 28
parse/0: ingredient: {name: "y", value: 0, type: 1, properties: ["y": "integer", "space": "1"]}
name/0: recipe increment-counter is surrounded by init-counter
new/0: location -> 1
new/0: location -> 1
name/0: assign x 1
name/0: assign y 2
new/0: space -> 0
name/0: assign y 1
new/0: location -> 1
after-brace/0: recipe main
after-brace/0: new ...
after-brace/0: init-counter ...
after-brace/0: increment-counter ...
after-brace/0: increment-counter ...
after-brace/0: recipe init-counter
after-brace/0: new ...
after-brace/0: copy ...
@ -55,11 +60,6 @@ after-brace/0: next-ingredient ...
after-brace/0: add ...
after-brace/0: copy ...
after-brace/0: reply ...
after-brace/0: recipe main
after-brace/0: new ...
after-brace/0: init-counter ...
after-brace/0: increment-counter ...
after-brace/0: increment-counter ...
run/0: instruction main/0
mem/0: new alloc: 1000
mem/0: array size is 30

View File

@ -7,11 +7,11 @@ parse/0: product: {name: "12", value: 0, type: 1, properties: ["12": "integer"
parse/0: instruction: 25
parse/0: product: {name: "13", value: 0, type: 1, properties: ["13": "integer"]}
parse/0: product: {name: "1", value: 0, type: 3, properties: ["1": "boolean"]}
after-brace/0: recipe main
after-brace/0: f ...
after-brace/0: recipe f
after-brace/0: ingredient ...
after-brace/0: next-ingredient ...
after-brace/0: recipe main
after-brace/0: f ...
run/0: instruction main/0
run/0: instruction f/0
run/0: product 0 is 2

View File

@ -6,11 +6,11 @@ parse/0: instruction: 2
parse/0: ingredient: {name: "1", value: 0, type: 0, properties: ["1": "literal"]}
parse/0: ingredient: {name: "12", value: 0, type: 1, properties: ["12": "integer"]}
parse/0: product: {name: "13", value: 0, type: 1, properties: ["13": "integer"]}
after-brace/0: recipe main
after-brace/0: f ...
after-brace/0: recipe f
after-brace/0: next-ingredient ...
after-brace/0: add ...
after-brace/0: recipe main
after-brace/0: f ...
run/0: instruction main/0
run/0: instruction f/0
run/0: product 0 is 2

View File

@ -2,10 +2,10 @@ parse/0: instruction: 1001
parse/0: instruction: 25
parse/0: product: {name: "_", value: 0, type: 0, properties: ["_": "dummy"]}
parse/0: product: {name: "12", value: 0, type: 1, properties: ["12": "integer"]}
after-brace/0: recipe f
after-brace/0: next-ingredient ...
after-brace/0: recipe main
after-brace/0: f ...
after-brace/0: recipe f
after-brace/0: next-ingredient ...
run/0: instruction main/0
run/0: instruction f/0
mem/0: storing 0 in location 12

View File

@ -11,12 +11,12 @@ parse/0: product: {name: "13", value: 0, type: 1, properties: ["13": "integer"
parse/0: instruction: 28
parse/0: ingredient: {name: "12", value: 0, type: 1, properties: ["12": "integer"]}
parse/0: ingredient: {name: "13", value: 0, type: 1, properties: ["13": "integer"]}
after-brace/0: recipe main
after-brace/0: f ...
after-brace/0: recipe f
after-brace/0: next-ingredient ...
after-brace/0: add ...
after-brace/0: reply ...
after-brace/0: recipe main
after-brace/0: f ...
run/0: instruction main/0
run/0: instruction f/0
run/0: product 0 is 2

View File

@ -8,12 +8,12 @@ parse/0: ingredient: {name: "35", value: 0, type: 0, properties: ["35": "liter
parse/0: product: {name: "13", value: 0, type: 1, properties: ["13": "integer"]}
parse/0: instruction: 28
parse/0: ingredient: {name: "12", value: 0, type: 6, properties: ["12": "point"]}
after-brace/0: recipe main
after-brace/0: f ...
after-brace/0: recipe f
after-brace/0: next-ingredient ...
after-brace/0: copy ...
after-brace/0: reply ...
after-brace/0: recipe main
after-brace/0: f ...
run/0: instruction main/0
run/0: instruction f/0
run/0: product 0 is 2

View File

@ -14,14 +14,14 @@ parse/0: product: {name: "4", value: 0, type: 1, properties: ["4": "integer"]}
parse/0: instruction: 1
parse/0: ingredient: {name: "34", value: 0, type: 0, properties: ["34": "literal"]}
parse/0: product: {name: "5", value: 0, type: 1, properties: ["5": "integer"]}
after-brace/0: recipe f
after-brace/0: copy ...
after-brace/0: copy ...
after-brace/0: recipe main
after-brace/0: f ...
after-brace/0: copy ...
after-brace/0: copy ...
after-brace/0: copy ...
after-brace/0: recipe f
after-brace/0: copy ...
after-brace/0: copy ...
run/0: instruction main/0
run/0: instruction f/0
run/0: ingredient 0 is 34

View File

@ -9,13 +9,13 @@ parse/0: instruction: 26
parse/0: instruction: 25
parse/0: product: {name: "13", value: 0, type: 1, properties: ["13": "integer"]}
parse/0: product: {name: "2", value: 0, type: 3, properties: ["2": "boolean"]}
after-brace/0: recipe main
after-brace/0: f ...
after-brace/0: recipe f
after-brace/0: next-ingredient ...
after-brace/0: next-ingredient ...
after-brace/0: rewind-ingredients ...
after-brace/0: next-ingredient ...
after-brace/0: recipe main
after-brace/0: f ...
run/0: instruction main/0
run/0: instruction f/0
run/0: product 0 is 2

View File

@ -93,7 +93,7 @@ Hide_warnings = false;
:(before "End Tracing")
struct trace_stream {
vector<pair<string, pair<int, string> > > past_lines; // [(layer label, frame, line)]
unordered_map<string, int> frame;
map<string, int> frame;
// accumulator for current line
ostringstream* curr_stream;
string curr_layer;
@ -458,11 +458,10 @@ using std::vector;
using std::list;
#include<utility>
using std::pair;
#include<tr1/unordered_map>
using std::tr1::unordered_map;
#include<tr1/unordered_set>
using std::tr1::unordered_set;
#include<map>
using std::map;
#include<set>
using std::set;
#include<algorithm>
#include<iostream>

View File

@ -2,8 +2,8 @@
// A program is a book of 'recipes' (functions)
typedef int recipe_number;
:(before "End Globals")
unordered_map<string, recipe_number> Recipe_number;
unordered_map<recipe_number, recipe> Recipe;
map<string, recipe_number> Recipe_number;
map<recipe_number, recipe> Recipe;
int Next_recipe_number = 1;
:(before "End Types")
@ -56,7 +56,7 @@ struct property {
:(before "End Globals")
// Locations refer to a common 'memory'. Each location can store a number.
unordered_map<int, int> Memory;
map<int, int> Memory;
:(before "End Setup")
Memory.clear();
@ -71,8 +71,8 @@ Memory.clear();
// seamless experience to help understand arbitrary mu programs.
typedef int type_number;
:(before "End Globals")
unordered_map<string, type_number> Type_number;
unordered_map<type_number, type_info> Type;
map<string, type_number> Type_number;
map<type_number, type_info> Type;
int Next_type_number = 1;
:(code)
void setup_types() {

View File

@ -17,7 +17,7 @@ vector<transform_fn> Transform;
void transform_all() {
//? cout << "AAA transform_all\n"; //? 1
for (size_t t = 0; t < Transform.size(); ++t) {
for (unordered_map<recipe_number, recipe>::iterator p = Recipe.begin(); p != Recipe.end(); ++p) {
for (map<recipe_number, recipe>::iterator p = Recipe.begin(); p != Recipe.end(); ++p) {
recipe& r = p->second;
if (r.steps.empty()) continue;
if (r.transformed_until != t-1) continue;
@ -30,7 +30,7 @@ void transform_all() {
void parse_int_reagents() {
//? cout << "parse_int_reagents\n"; //? 1
for (unordered_map<recipe_number, recipe>::iterator p = Recipe.begin(); p != Recipe.end(); ++p) {
for (map<recipe_number, recipe>::iterator p = Recipe.begin(); p != Recipe.end(); ++p) {
recipe& r = p->second;
if (r.steps.empty()) continue;
for (size_t index = 0; index < r.steps.size(); ++index) {

View File

@ -21,7 +21,7 @@ recipe main [
Transform.push_back(transform_names);
:(before "End Globals")
unordered_map<recipe_number, unordered_map<string, int> > Name;
map<recipe_number, map<string, int> > Name;
:(after "Clear Other State For recently_added_recipes")
for (size_t i = 0; i < recently_added_recipes.size(); ++i) {
Name.erase(recently_added_recipes[i]);
@ -29,7 +29,7 @@ for (size_t i = 0; i < recently_added_recipes.size(); ++i) {
:(code)
void transform_names(const recipe_number r) {
unordered_map<string, int>& names = Name[r];
map<string, int>& names = Name[r];
int curr_idx = 1;
//? cout << "Recipe " << r << '\n'; //? 2
//? cout << Recipe[r].steps.size(); //? 1
@ -77,7 +77,7 @@ void transform_names(const recipe_number r) {
}
}
bool already_transformed(const reagent& r, const unordered_map<string, int>& names) {
bool already_transformed(const reagent& r, const map<string, int>& names) {
return names.find(r.name) != names.end();
}

View File

@ -35,7 +35,7 @@ recipe increment-counter [
//: surrounding space of each recipe. This must happen before transform_names.
:(before "End Globals")
unordered_map<recipe_number, recipe_number> Surrounding_space;
map<recipe_number, recipe_number> Surrounding_space;
:(after "int main")
Transform.push_back(collect_surrounding_spaces);
@ -130,8 +130,8 @@ recipe_number lookup_surrounding_recipe(const recipe_number r, size_t n) {
}
//: weaken use-before-set warnings just a tad
:(replace{} "bool already_transformed(const reagent& r, const unordered_map<string, int>& names)")
bool already_transformed(const reagent& r, const unordered_map<string, int>& names) {
:(replace{} "bool already_transformed(const reagent& r, const map<string, int>& names)")
bool already_transformed(const reagent& r, const map<string, int>& names) {
if (has_property(r, "space")) {
vector<string> p = property(r, "space");
assert(p.size() == 1);
@ -139,7 +139,3 @@ bool already_transformed(const reagent& r, const unordered_map<string, int>& nam
}
return names.find(r.name) != names.end();
}
:(before "End Includes")
#include<set>
using std::set;