diff options
author | olpc user <olpc@xo-5d-f7-86.localdomain> | 2019-11-24 13:22:55 -0800 |
---|---|---|
committer | olpc user <olpc@xo-5d-f7-86.localdomain> | 2019-11-24 13:22:55 -0800 |
commit | 9a14918abec434d8463e07635daef380ad630649 (patch) | |
tree | fa1a5200da88ad6065092b05dc063a8a80df8faa /starts/meaning-vm/level-0/concept.cpp | |
parent | 50be8bb8697b23ff469de142eaebe9666c2a9537 (diff) | |
download | standingwithresilience-9a14918abec434d8463e07635daef380ad630649.tar.gz standingwithresilience-9a14918abec434d8463e07635daef380ad630649.zip |
breaking into levels
Diffstat (limited to 'starts/meaning-vm/level-0/concept.cpp')
-rw-r--r-- | starts/meaning-vm/level-0/concept.cpp | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/starts/meaning-vm/level-0/concept.cpp b/starts/meaning-vm/level-0/concept.cpp new file mode 100644 index 0000000..579cca8 --- /dev/null +++ b/starts/meaning-vm/level-0/concept.cpp @@ -0,0 +1,94 @@ +#include "concept.hpp" +#include "errors.hpp" + +using namespace intellect::level0; + +#define selfref const_cast<concept*>(&self) + +ref concept::id() +{ + return this; +} + +void concept::link(ref const & type, ref const & target) +{ + links.insert({type.ptr, target.ptr}); +} + +void concept::unlink(ref const & type, ref const & target) +{ + auto ls = links.equal_range(type); + for (auto l = ls.first; l != ls.second; ++ l) { + if (l->second == target) { + links.erase(l); + return; + } + } + throw no_such_link_type_target(selfref, type, target); +} + +void concept::unlink(ref const & type) +{ + auto ls = links.equal_range(type); + if (ls.first == ls.second) { + throw no_such_link_type(selfref, type); + } + auto mid = ls.first; + ++ mid; + if (mid != ls.second) { + throw link_type_not_unique(selfref, type); + } + links.erase(ls.first); +} + +bool concept::linked(ref const & type) const +{ + return links.count(type.ptr) > 0; +} + +bool concept::linked(ref const & type, ref const & target) const +{ + for (ref const & t : getAll(type)) { + if (t == target) { + return true; + } + } + return false; +} + +concept::array concept::getAll(ref const & type) const +{ + array ret; + for ( + auto range = links.equal_range(type); + range.first != range.second; + ++ range.first + ) { + ret.emplace_back(range.first->second); + } + return ret; +} + +ref concept::get(ref const & type) const +{ + auto result = links.equal_range(type.ptr); + if (result.first == result.second) { + throw no_such_link_type(selfref, type); + } + auto test = result.first; + ++ test; + if (test != result.second) { + throw link_type_not_unique(selfref, type); + } + return result.first->second; +} + +void concept::set(ref const & type, ref const & target) +{ + if (linked(type)) { + throw link_type_not_unique(selfref, type); + } + link(type, target); +} + + |