From fc46219ca74e64868a5383e1bf54da33ca1be8a6 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Sun, 15 Oct 2017 11:32:38 +0300 Subject: [PATCH] Support printing tree in 'parents' format --- doc/printers.md | 8 ++- doc/tree.md | 2 +- generic_graph.h | 6 +- impl/generic_graph_inl.h | 32 +++++----- impl/tree_inl.h | 53 ++++++++++++++++- jngen.h | 124 +++++++++++++++++++++++++++++---------- repr.h | 24 ++++++-- tests/Makefile | 2 +- tests/tree.cpp | 25 ++++++++ tree.h | 9 ++- 10 files changed, 216 insertions(+), 69 deletions(-) diff --git a/doc/printers.md b/doc/printers.md index 3120d03..c1e237b 100644 --- a/doc/printers.md +++ b/doc/printers.md @@ -64,11 +64,13 @@ Note that Jngen does not interact with stl-defined operators. That mean that wri * Action: print number of edges of a graph. * Default: unset. #### printEdges(bool value = true) -* Action: when printing a tree, print a list of edges. Opposite to *printParents*. +* Action: when printing a tree, print a list of edges. * Default: set. -#### printParents(bool value = true) +#### printParents(int value = -1) * Action: when printing a tree, print a parent of each vertex. Opposite to *printEdges*. -* Default: unset. +* Arguments: *value* stands for the root of the tree. If *value* is *0* or greater, then the parent of each vertex is printed, having root's parent as + *-1* (*0* if *add1()* is present). *value = -1* is a special value: in this case tree is rooted at *0* and its parent is not printed (printing *n-1* values in total). +* Note: this option and *printEdges* cancel each other. #### endl(bool value = true) * Action: separate elements of the array with line breaks instead of spaces. * Default: unset. diff --git a/doc/tree.md b/doc/tree.md index 2193e56..a81f39d 100644 --- a/doc/tree.md +++ b/doc/tree.md @@ -48,7 +48,7 @@ Note that all generators return trees with sorted edges to make tests more human * randomly swap egdes' endpoints. #### Array parents(int root) const -* Returns: array of size *n*, where *i*-th element is a parent of vertex *i* if the tree is rooted at *root*. Parent of *root* is *root* itself. +* Returns: array of size *n*, where *i*-th element is a parent of vertex *i* if the tree is rooted at *root*. Parent of *root* is *-1*. #### Tree link(int vInThis, const Tree& other, int vInOther) * Returns: a tree made of _*this_ and *other*, with an extra edge between two vertices with ids *vInThis* and *vInOther*, respectively. diff --git a/generic_graph.h b/generic_graph.h index 66c437c..40b85d0 100644 --- a/generic_graph.h +++ b/generic_graph.h @@ -25,8 +25,8 @@ class GenericGraph { virtual void addEdge(int u, int v, const Weight& w = Weight{}); virtual bool isConnected() const { return dsu_.isConnected(); } - virtual int vertexLabel(int v) const { return vertexLabel_[v]; } - virtual int vertexByLabel(int v) const { return vertexByLabel_[v]; } + virtual int vertexLabel(int v) const { return vertexLabel_.at(v); } + virtual int vertexByLabel(int v) const { return vertexByLabel_.at(v); } // v: label // return: array