From c2ed17faa6aa9f8420128d2713c5b557ab333466 Mon Sep 17 00:00:00 2001 From: olpc user Date: Fri, 22 Nov 2019 17:55:18 -0800 Subject: use refs for temporaries, which means pulling them out of containers --- starts/meaning-vm/concept.cpp | 12 ++++++------ starts/meaning-vm/concept.hpp | 12 ++++++------ starts/meaning-vm/helpers.cpp | 31 +++++++++++++++---------------- starts/meaning-vm/helpers.hpp | 2 +- starts/meaning-vm/main.cpp | 4 ++-- starts/meaning-vm/memorystore.cpp | 14 +++++++------- 6 files changed, 37 insertions(+), 38 deletions(-) (limited to 'starts') diff --git a/starts/meaning-vm/concept.cpp b/starts/meaning-vm/concept.cpp index 825ab5a..c46b058 100644 --- a/starts/meaning-vm/concept.cpp +++ b/starts/meaning-vm/concept.cpp @@ -7,7 +7,7 @@ ref concept::id() bool concept::linked(ref type) { - return links.count(type) > 0; + return links.count(type.ptr) > 0; } bool concept::linked(ref type, ref target) @@ -22,7 +22,7 @@ bool concept::linked(ref type, ref target) ref concept::get(ref type) { - auto result = links.equal_range(type); + auto result = links.equal_range(type.ptr); if (result.first == result.second) { throw std::out_of_range("no such concept link to get: " + type.name()); } @@ -33,7 +33,7 @@ concept::array concept::getAll(ref type) { array ret; for ( - auto range = links.equal_range(type); + auto range = links.equal_range(type.ptr); range.first != range.second; ++ range.first ) { @@ -44,14 +44,14 @@ concept::array concept::getAll(ref type) void concept::link(ref type, ref target) { - links.insert({type, target}); + links.insert({type.ptr, target.ptr}); } void concept::unlink(ref type, ref target) { - auto ls = links.equal_range(type); + auto ls = links.equal_range(type.ptr); for (auto l = ls.first; l != ls.second; ++ l) { - if (l->second == target) { + if (l->second == target.ptr) { links.erase(l); return; } diff --git a/starts/meaning-vm/concept.hpp b/starts/meaning-vm/concept.hpp index 7c8fd6d..4d73770 100644 --- a/starts/meaning-vm/concept.hpp +++ b/starts/meaning-vm/concept.hpp @@ -14,9 +14,9 @@ struct ref ref(concept *p) : ptr(p) { if (p == 0) throw std::logic_error("null reference"); } ref(ref const &) = default; concept* operator->() { return ptr; } + bool operator==(ref const & that) const { return this->ptr == that.ptr; } - // for use by containers - bool operator<(ref const & other) const { return ptr < other.ptr; } + bool operator<(ref const &) const { throw std::logic_error("ref has redefined syntax sugar: do not use in containers"); } // for helpers ref(std::string const &); @@ -26,8 +26,8 @@ struct ref value & name() const; operator const char *() const; - concept operator=(ref other); // helper constructs new concept with this as link - ref operator[](concept links); // helper sets all links from passed concept + ref operator=(ref other); // helper constructs new concept with this as link + ref operator[](ref links); // helper sets all links from passed concept bool isa(ref what) const; bool isan(ref what) const; @@ -46,7 +46,7 @@ struct vref operator ref() { return ptr; } // for use by containers - bool operator<(ref const & other) const { return ptr < other.ptr; } + //bool operator<(ref const & other) const { return ptr < other.ptr; } value * ptr; }; @@ -54,7 +54,7 @@ struct vref struct concept { // a concept is made of concept-typed links to other concepts - std::multimap links; + std::multimap links; using array = std::vector; ref id(); diff --git a/starts/meaning-vm/helpers.cpp b/starts/meaning-vm/helpers.cpp index 1d6d498..8c8618d 100644 --- a/starts/meaning-vm/helpers.cpp +++ b/starts/meaning-vm/helpers.cpp @@ -9,7 +9,7 @@ ref operator-(ref a, ref b) return ref(a.name() + "-" + b.name()); } -concept ref::operator=(ref other) +ref ref::operator=(ref other) { // if this is not anonymous, and other is, then we are naming it /*declrefs(anonymous); @@ -17,28 +17,28 @@ concept ref::operator=(ref other) return; }*/ // if this is link-type, make new concept [not checked, might want to assume] - concept ret; - ret.link(*this, other); + ref ret = alloc(); + ret->link(*this, other); return ret; } -ref ref::operator[](concept links) { - ptr->links.insert(links.links.begin(), links.links.end()); +ref ref::operator[](ref links) { + ptr->links.insert(links->links.begin(), links->links.end()); + dealloc(links); return *this; } -concept operator,(concept a, concept b) +ref operator,(ref a, ref b) { - concept ret; - ret.links.insert(a.links.begin(), a.links.end()); - ret.links.insert(b.links.begin(), b.links.end()); - return ret; + a->links.insert(b->links.begin(), b->links.end()); + dealloc(b); + return a; } // concept names are for bootstrapping convenience, // to make hardcoding structures easier. // hence there is just one single list of them -std::unordered_map,ref,std::hash,std::equal_to> conceptsByName; +std::unordered_map,std::equal_to> conceptsByName; struct name_t : public ref { @@ -49,19 +49,18 @@ name_t::name_t() { auto nam = valloc(std::string("name")); ptr->link(::name, nam); - conceptsByName.emplace(nam, *this); + conceptsByName.emplace(nam, ptr); } ref::ref(std::string const & s) { - value str(s); - auto res = conceptsByName.find(&str); + auto res = conceptsByName.find(s); if (res != conceptsByName.end()) { - ptr = res->second.ptr; + ptr = res->second; } else { ref con = alloc(); auto nam = valloc(s); - conceptsByName.emplace(nam, con); + conceptsByName.emplace(nam, con.ptr); con->link(::name, nam); ptr = con.ptr; } diff --git a/starts/meaning-vm/helpers.hpp b/starts/meaning-vm/helpers.hpp index d151849..103e826 100644 --- a/starts/meaning-vm/helpers.hpp +++ b/starts/meaning-vm/helpers.hpp @@ -48,7 +48,7 @@ void __helper_init_ref_names(std::string names, T &... refrefs) ref __VA_ARGS__; \ __helper_init_ref_names(#__VA_ARGS__, __VA_ARGS__) -concept operator,(concept a, concept b); +ref operator,(ref a, ref b); ref operator-(ref a, ref b); ref a(ref what); diff --git a/starts/meaning-vm/main.cpp b/starts/meaning-vm/main.cpp index b8d0e02..f8e4ec0 100644 --- a/starts/meaning-vm/main.cpp +++ b/starts/meaning-vm/main.cpp @@ -10,10 +10,10 @@ void dumpconcept(ref r) declrefs(dumped, name); for (auto & l : r->links) { - if (l.first == name) { + if (ref(l.first) == name) { continue; } - cout << r << " " << l.first << " " << l.second << endl; + cout << r << " " << ref(l.first) << " " << ref(l.second) << endl; } if (!r->linked(dumped)) { r[dumped = true]; diff --git a/starts/meaning-vm/memorystore.cpp b/starts/meaning-vm/memorystore.cpp index 0d636ee..4178a4b 100644 --- a/starts/meaning-vm/memorystore.cpp +++ b/starts/meaning-vm/memorystore.cpp @@ -5,24 +5,24 @@ using namespace std; -std::vector concepts; +std::vector concepts; ref alloc(concept * moved) { ref r = moved ? moved : new concept(); - concepts.push_back(r); + concepts.push_back(r.ptr); return r; } bool referenced(ref r) { - for (auto & r2 : concepts) { + for (ref r2 : concepts) { if (r2 == r) { continue; } for (auto & l : r2->links) { - if (l.first == r) { + if (ref(l.first) == r) { return true; } - if (l.second == r) { + if (ref(l.second) == r) { return true; } } @@ -39,9 +39,9 @@ void dealloc(ref r) { it != concepts.end(); ++ it) { - if (*it == r) { + if (ref(*it) == r) { concepts.erase(it); - delete r; + delete r.ptr; return; } } -- cgit v1.2.3