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/hypergraph.hh | 65 +++++++++++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 33 deletions(-) (limited to 'fast/hypergraph.hh') 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 +#include +#include #include #include +#include -#include +#include "msgpack-c/include/msgpack.hpp" using namespace std; typedef double score_t; typedef double weight_t; -typedef size_t id_t; namespace Hg { - class Node; class Edge { public: - id_t head; - vector tails; + Node* head; + vector tails; score_t score; + //Grammar::Rule rule; FIXME vector f; + unsigned int arity; unsigned int mark; - //Grammar::Rule rule; FIXME - unsigned int arity_; - unsigned int arity(); bool is_marked(); - string s(); + friend std::ostream& operator<<(std::ostream& os, const Edge& s); - MSGPACK_DEFINE(head, tails, score, f, mark, arity_); + size_t head_id_; + vector tails_ids_; // node ids + MSGPACK_DEFINE(head_id_, tails_ids_, score, f, arity); }; - class Node { public: - unsigned int id; - string symbol; - unsigned int left; - unsigned int right; - score_t score; - vector outgoing; - vector incoming; + size_t id; + string symbol; + unsigned short left; + unsigned short right; + score_t score; + vector incoming; + vector outgoing; + unsigned int mark; - string s(); + bool is_marked(); + friend std::ostream& operator<<(std::ostream& os, const Node& n); - MSGPACK_DEFINE(id, symbol, left, right, score, outgoing, incoming); + vector incoming_ids_; // edge ids + vector outgoing_ids_; // edge ids + MSGPACK_DEFINE(id, symbol, left, right, score, incoming_ids_, outgoing_ids_); }; - - class Hypergraph { public: - vector nodes; - vector edges; - unsigned int arity_; + list nodes; + vector edges; + unordered_map nodes_by_id; + unsigned int arity; - unsigned int arity(); void reset(); - string s(); - string json_s(); - - MSGPACK_DEFINE(nodes, edges, arity_, nodes_by_id); + void add_node(Node* n) { nodes.push_back(n); nodes_by_id[n->id] = n; } }; -vector topological_sort(vector& nodes); -void viterbi(vector& nodes, map nodes_by_id, Node* root); +void topological_sort(list& nodes, list::iterator root); +void viterbi(Hypergraph& hg); } // namespace -- cgit v1.2.3