LAL: Linear Arrangement Library 24.10.00
A library focused on algorithms on linear arrangements of graphs.
Loading...
Searching...
No Matches
generic.hpp
1/*********************************************************************
2 *
3 * Linear Arrangement Library - A library that implements a collection
4 * algorithms for linear arrangments of graphs.
5 *
6 * Copyright (C) 2019 - 2024
7 *
8 * This file is part of Linear Arrangement Library. The full code is available
9 * at:
10 * https://github.com/LAL-project/linear-arrangement-library.git
11 *
12 * Linear Arrangement Library is free software: you can redistribute it
13 * and/or modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * Linear Arrangement Library is distributed in the hope that it will be
18 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with Linear Arrangement Library. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * Contact:
26 *
27 * LluĂ­s Alemany Puig (lluis.alemany.puig@upc.edu)
28 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
29 * CQL (Complexity and Quantitative Linguistics Lab)
30 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
31 * Webpage: https://cqllab.upc.edu/people/lalemany/
32 *
33 * Ramon Ferrer i Cancho (rferrericancho@cs.upc.edu)
34 * LQMC (Quantitative, Mathematical, and Computational Linguisitcs)
35 * CQL (Complexity and Quantitative Linguistics Lab)
36 * Office 220, Omega building
37 * Jordi Girona St 1-3, Campus Nord UPC, 08034 Barcelona. CATALONIA, SPAIN
38 * Webpage: https://cqllab.upc.edu/people/rferrericancho/
39 *
40 ********************************************************************/
41
42#pragma once
43
44// lal includes
45#include <lal/graphs/rooted_tree.hpp>
46#include <lal/linarr/chunking/chunk.hpp>
47#include <lal/linarr/chunking/chunk_sequence.hpp>
48
49namespace lal {
50namespace detail {
51
52
60template <class arrangement_t>
62public:
68 chunks_generic(const graphs::rooted_tree& rt, const arrangement_t& arr)
69 noexcept
70 : m_rt(rt), m_arr(arr),
71 m_n(rt.get_num_nodes())
72 {
73 }
74
76 virtual ~chunks_generic() noexcept { }
77
80 return m_sequence;
81 }
82 /*
84 linarr::chunk_sequence& get_chunk_sequence() noexcept {
85 return m_sequence;
86 }
87 */
90 return std::move(m_sequence);
91 }
92
93protected:
95 [[nodiscard]] linarr::chunk& last_chunk() noexcept {
96 return m_sequence[m_sequence.size() - 1];
97 }
98
100 [[nodiscard]] std::size_t node_to_chunk(const node u) const noexcept {
101 return m_sequence.get_chunk_index(u);
102 }
104 void set_chunk_index(const node u, const std::size_t i) noexcept {
105 m_sequence.set_chunk_index(u, i);
106 }
107
109 void set_parent_chunk(linarr::chunk& c) noexcept {
110#if defined DEBUG
111 bool head_found = false;
112#endif
113 for (node u : c.get_nodes()) {
114
115 // if the parent of 'u' (if it exists) is outside the chunk
116 // of 'u', then that parent is the parent of this chunk and 'u'
117 // is the root node of the chunk.
118 if (m_rt.get_in_degree(u) == 1) {
119 const node v = m_rt.get_parent_node(u);
120 if (node_to_chunk(v) != node_to_chunk(u)) {
121 c.set_root_node(u);
122 c.set_parent_node(v);
123#if defined DEBUG
124 head_found = true;
125#endif
126 }
127 }
128 else {
129 c.set_root_node(u);
130#if defined DEBUG
131 head_found = true;
132#endif
133 }
134 }
135
136#if defined DEBUG
137 assert(head_found);
138#endif
139 }
140
141protected:
145 const arrangement_t m_arr;
147 const uint64_t m_n;
148
151};
152
153} // -- namespace detail
154} // -- namespace lal
Basic algorithms existent in every definition of chunking.
Definition generic.hpp:61
const linarr::chunk_sequence & get_chunk_sequence() const noexcept
Returns a constant reference to the chunk sequence m_sequence.
Definition generic.hpp:79
const uint64_t m_n
Number of vertices of the tree.
Definition generic.hpp:147
const arrangement_t m_arr
Linear arrangement.
Definition generic.hpp:145
chunks_generic(const graphs::rooted_tree &rt, const arrangement_t &arr) noexcept
Constructor.
Definition generic.hpp:68
linarr::chunk_sequence m_sequence
The sequence of chunks obtained.
Definition generic.hpp:150
void set_chunk_index(const node u, const std::size_t i) noexcept
Sets the chunk index of node u to index i.
Definition generic.hpp:104
void set_parent_chunk(linarr::chunk &c) noexcept
Set the parent node of a chunks.
Definition generic.hpp:109
linarr::chunk & last_chunk() noexcept
Returns a reference to the last chunk in the sentence.
Definition generic.hpp:95
std::size_t node_to_chunk(const node u) const noexcept
Returns the chunk index of node u.
Definition generic.hpp:100
virtual ~chunks_generic() noexcept
Destructor.
Definition generic.hpp:76
linarr::chunk_sequence && retrieve_chunk_sequence() noexcept
Moves the chunk sequence m_sequence.
Definition generic.hpp:89
const graphs::rooted_tree & m_rt
Input rooted tree.
Definition generic.hpp:143
uint64_t get_in_degree(const node u) const noexcept
Returns the in-degree of a node.
Definition directed_graph.hpp:427
Rooted tree graph class.
Definition rooted_tree.hpp:109
node get_parent_node(const node u) const noexcept
Parent vertex of a node.
Definition rooted_tree.hpp:782
Chunk sequence of a syntactic dependency tree.
Definition chunk_sequence.hpp:107
std::size_t get_chunk_index(const node u) const noexcept
Returns the chunk index of node u.
Definition chunk_sequence.hpp:186
std::size_t size() const noexcept
Returns the number of chunks.
Definition chunk_sequence.hpp:181
Definition of a chunk.
Definition chunk.hpp:64
Main namespace of the library.
Definition basic_types.hpp:48
uint64_t node
Node type. See Node / Vertex page for further details.
Definition basic_types.hpp:51