From df8ed8821287fc8172cace28e09c2cac9823bb8a Mon Sep 17 00:00:00 2001
From: Patrick Simianer 
Date: Wed, 16 Jul 2014 20:56:49 +0200
Subject: in-place topological sort
---
 fast/.gitignore    |   3 +
 fast/Makefile      |   8 +--
 fast/hypergraph.cc | 147 ++++++++++++++++++++++---------------------------
 fast/hypergraph.hh |  65 +++++++++++-----------
 fast/main.cc       | 157 +++++++++++++++++++++++++++--------------------------
 5 files changed, 185 insertions(+), 195 deletions(-)
 create mode 100644 fast/.gitignore
(limited to 'fast')
diff --git a/fast/.gitignore b/fast/.gitignore
new file mode 100644
index 0000000..80d28d5
--- /dev/null
+++ b/fast/.gitignore
@@ -0,0 +1,3 @@
+fast_weaver
+hypergraph.o
+msgpack-c/
diff --git a/fast/Makefile b/fast/Makefile
index a453fd3..f09ab21 100644
--- a/fast/Makefile
+++ b/fast/Makefile
@@ -1,12 +1,12 @@
 all: hypergraph.o main.cc
-	clang -std=c++11 -lstdc++ main.cc hypergraph.o json/libjson.a -o fast_weaver
+	clang -std=c++11 -lstdc++ -lm hypergraph.o -I./msgpack-c/include/ main.cc -o fast_weaver
 
 hypergraph.o: hypergraph.cc hypergraph.hh grammar.o semiring.hh
-	clang -std=c++11 -lstdc++ -c hypergraph.cc -I./msgpack-c/include/ grammar.o ./msgpack-c/lib/libmsgpack.a
+	clang -std=c++11 -I./msgpack-c/include/ -c hypergraph.cc
 
 grammar.o: grammar.cc grammar.hh
-	clang -std=c++11 -lstdc++ -c grammar.cc
+	clang -std=c++11 -c grammar.cc
 
 clean:
-	rm fast_weaver hypergraph.o grammar.o
+	rm -f fast_weaver hypergraph.o grammar.o
 
diff --git a/fast/hypergraph.cc b/fast/hypergraph.cc
index 08b4d05..15b0a1f 100644
--- a/fast/hypergraph.cc
+++ b/fast/hypergraph.cc
@@ -5,126 +5,111 @@ namespace Hg {
 
 /*
  * Node
- * methods
  *
  */
-string
-Node::s()
+bool
+Node::is_marked()
+{
+  return mark >= incoming.size();
+}
+
+std::ostream&
+operator<<(std::ostream& os, const Node& n)
 {
-  ostringstream os;
   os << \
-    "Nodeid << \
-    ", symbol=" << this->symbol << \
-    ", span=(" << this->left << "," << this->right << ")" \
-    ", outgoing:" << this->outgoing.size() << \
-    ", incoming:" << this->incoming.size() << ">";
-  return os.str();
+    "Node";
 }
 
 /*
- * Hyperedge
- * methods
+ * Edge
  *
  */
-unsigned int
-Hyperedge::arity()
-{
-  return this->arity_;
-}
-
 bool
-Hyperedge::is_marked()
+Edge::is_marked()
 {
-  return (this->arity_ == this->mark);
+  return mark >= arity;
 }
 
-string
-Hyperedge::s()
+std::ostream&
+operator<<(std::ostream& os, const Edge& e)
 {
-  ostringstream os;
-  vector tails_ids;
   ostringstream _;
-  for_each(this->tails.begin(), this->tails.end(), [&_](Node* n) { _ << n->id; _ << ","; });
+  for (auto it = e.tails.begin(); it != e.tails.end(); ++it) {
+    _ << (*it)->id; if (*it != e.tails.back()) _ << ",";
+  }
   os << \
-    "Hyperedgehead->id << \
+    "Edgeid << \
+    "', tails=[" << _.str() << "]" \
+    ", score=" << e.score << \
     ", rule:'" << "TODO" << \
-    "', tails=" << _.str() << \
-    ", arity=" << this->arity() << \
-    ", score=" << this->score << \
     " , f=" << "TODO" << \
-    ", mark=" << this->mark << ">";
-  return os.str();
+    ", arity=" << e.arity << \
+    ", mark=" << e.mark << ">";
+  return os;
 }
 
-
 /*
  * Hypergraph
  * methods
  *
  */
-unsigned int
-Hypergraph::arity()
-{
-  // TODO
-  return 0;
-}
-
 void
 Hypergraph::reset()
 {
 }
 
-string
-Hypergraph::s()
-{
-  // TODO
-  return "";
-}
-
-string
-Hypergraph::json_s()
-{
-  // TODO
-  return "";
-}
-
 /*
  * functions
  *
  */
-vector
-topological_sort(vector& nodes)
+void
+topological_sort(list& nodes, list::iterator root)
 {
-  vector sorted;
-  vector tmp;
-  for_each(nodes.begin(), nodes.end(), [&tmp](Node* n) -> void { if (n->incoming.size()==0) tmp.push_back(n); });
-  while (!tmp.empty()) {
-    Node* n = tmp.front();
-    cout << n->id << endl;
-    tmp.erase(tmp.begin());
-    sorted.push_back(n);
-    for (auto it = n->outgoing.begin(); it !=  n->outgoing.end(); it++) { // edges
-      if ((*it)->is_marked()) {
-        continue;
-      }
-      (*it)->mark++;
-      bool add = true;
-      for (auto jt = (*it)->head->incoming.begin(); jt != (*it)->head->incoming.end(); jt++) {
-        if (!(*jt)->is_marked()) {
-          add = false;
-          break;
+  auto p = root;
+  (**p).mark = 0; // is_marked()==true
+  auto to = nodes.begin();
+  while (to != nodes.end()) {
+
+    for (auto it = nodes.begin(); it != nodes.end(); ++it) cout << (**it).id << " ";
+    cout << endl;
+
+    if ((**p).is_marked()) {
+      // explore edges
+      for (auto e = (**p).outgoing.begin(); e!=(**p).outgoing.end(); ++e) {
+        (**e).mark++;
+        if ((**e).is_marked()) {
+          (**e).head->mark++;
         }
       }
-      if (add==true)  {
-        tmp.push_back( (*it)->head );
-        cout << " " <<  (*it)->head->id<< endl;
-      }
+    }
+    if ((**p).is_marked()) {
+      nodes.splice(to, nodes, p);
+      to = next(p);
+      p = to;
+    } else {
+      ++p;
+      /*if (p == nodes.end()) {
+        for (auto e = (**to).outgoing.begin(); e!=(**to).outgoing.end(); ++e) {
+          // explore edges
+          (**e).mark++;
+          if ((**e).is_marked()) {
+            (**e).head->mark++;
+          }
+        }
+        to++;
+        p = to;
+      }*/
     }
   }
-  return sorted;
 }
 
-void
+/*void
 init(vector& nodes, ViterbiSemiring& semiring, Node* root)
 {
   for (auto it = nodes.begin(); it != nodes.end(); ++it)
@@ -139,7 +124,7 @@ viterbi(vector& nodes, map nodes_by_id, Node* ro
   ViterbiSemiring semiring;
 
   init(sorted, semiring, root);
-  
+
   for (auto n_it = sorted.begin(); n_it != sorted.end(); ++n_it) {
     for (auto e_it = (*n_it)->incoming.begin(); e_it != (*n_it)->incoming.end(); ++e_it) {
       cout << (*e_it)->s() << endl;
@@ -154,7 +139,7 @@ viterbi(vector& nodes, map nodes_by_id, Node* ro
   for (auto it = sorted.begin(); it != sorted.end(); ++it) {
     cout << (*it)->id << " " <<  (*it)->score << endl;
   }
-}
+}*/
 
 
 } // namespace
diff --git a/fast/hypergraph.hh b/fast/hypergraph.hh
index e5f91cb..68cca19 100644
--- a/fast/hypergraph.hh
+++ b/fast/hypergraph.hh
@@ -7,75 +7,74 @@
 #include 
 #include 
 #include 
-#include