Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions src/Data/Observers/link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
******************************************************************************/

#include "link.hpp"
#include "hashset.hpp"
#include "iterator.hpp"
#include "modification.hpp"
#include "observers.hpp"
Expand All @@ -20,10 +21,15 @@

using namespace moebius;

hashmap<string, list<observer>> id_resolve;
hashmap<observer, list<string>> pointer_resolve;
hashmap<tree, list<soft_link>> vertex_occurrences;
hashmap<string, int> type_count (0);
static int
hash (soft_link l) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数哪里用到了?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hashset<soft_link> 需要提供一个 hash函数

return hash (l->t);
}

hashmap<string, list<observer>> id_resolve;
hashmap<observer, list<string>> pointer_resolve;
hashmap<tree, hashset<soft_link>> vertex_occurrences;
hashmap<string, int> type_count (0);

static hashset<string> visited_table;

Expand Down Expand Up @@ -57,15 +63,22 @@ unregister_pointer (string id, observer which) {

void
register_vertex (tree v, soft_link ln) {
list<soft_link>& l= vertex_occurrences (v);
l = list<soft_link> (ln, l);
if (vertex_occurrences->contains (v)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么这里要判断v是否是vertex_occurrences的键?如果hashmap不包含对应的键,会原位构造对应的参数,然后返回引用。如果hashset没有默认初始化函数,可以在初始化vertex_occurrences的时候传个初始对象进去

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

用原来的写法,编辑器直接卡死了。所以用了这种朴素的写法。

hashset<soft_link>& l= vertex_occurrences (v);
l->insert (ln);
}
else {
hashset<soft_link> l= hashset<soft_link> ();
l->insert (ln);
vertex_occurrences (v)= l;
}
}

void
unregister_vertex (tree v, soft_link ln) {
list<soft_link>& l= vertex_occurrences (v);
l = remove (l, ln);
if (is_nil (l)) vertex_occurrences->reset (v);
hashset<soft_link>& l= vertex_occurrences (v);
l->remove (ln);
if (N (l) == 0) vertex_occurrences->reset (v);
}

void
Expand Down Expand Up @@ -162,9 +175,13 @@ get_trees (string id) {
}

list<tree>
as_tree_list (list<soft_link> l) {
if (is_nil (l)) return list<tree> ();
else return list<tree> (l->item->t, as_tree_list (l->next));
as_tree_list (hashset<soft_link> l) {
iterator<soft_link> it= iterate (l);
list<tree> r;
while (it->busy ()) {
r= list<tree> (it->next ()->t, r);
}
return r;
}

list<tree>
Expand Down