Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_indexNode.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
73# ifndef TPL_INDEXNODE_H
74# define TPL_INDEXNODE_H
75
76# include <tpl_dynSetTree.H>
77# include <tpl_graph.H>
78# include <ah-errors.H>
79
80namespace Aleph
81{
82
83 template <class GT>
84 struct Dft_Node_Cmp
85 {
86 bool
87 operator () (typename GT::Node * p1, typename GT::Node * p2) const
88 {
89 return p1->get_info() < p2->get_info();
90 }
91 };
92
125 template <class GT, class Compare = Dft_Node_Cmp<GT>,
126 template <class /* Key */, class /* Compare */> class Tree = Treap,
127 class SN = Dft_Show_Node<GT> >
129 {
130 typedef typename GT::Node GT_Node;
131 typedef typename GT::Node_Type GT_Node_Type;
132
134
135 GT & g;
137
138 public:
139
147 {
148 index.put(p);
149 return p;
150 }
151
160 {
162
163 try
164 {
166 return ret_val;
167 }
168 catch (...)
169 {
171 throw;
172 }
173 }
174
177 {
178 GT_Node * ret_val = g.insert_node(std::forward<GT_Node_Type>(info));
179
180 try
181 {
183 return ret_val;
184 }
185 catch (...)
186 {
188 throw;
189 }
190 }
191
202 {
203 GT_Node ** ret_val = index.search(p);
204
205 if (ret_val == nullptr)
206 return nullptr;
207
208 return *ret_val;
209 }
210
221 {
223 return search(&dummy_node);
224 }
225
227 void remove(GT_Node * p)
228 {
229 index.remove(p);
230 }
231
239 {
240 index.find(p); // Throws an exception if p is not in the index
241 index.remove(p);
242 g.remove_node(p);
243 }
244
247 {
248 index.empty();
249 }
250
253 {
254 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
255 {
256 GT_Node * p = it.get_curr_ne();
257 if (search(p) != p)
258 insert(p);
259 }
260 }
261
264 {
265 clear_index();
267 }
268
269 private:
270
271 void init()
272 {
273 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
274 insert(it.get_curr_ne());
275 }
276
277 public:
278
286 {
287 init();
288 }
289
291 size_t size() const { return index.size(); }
292 };
293
294}
295
296# endif
Exception handling system with formatted messages for Aleph-w.
Dynamic set backed by balanced binary search trees with automatic memory management.
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Builds an index of nodes for fast search and retrieval.
void remove_from_graph(GT_Node *p)
Removes the node p from the graph and the index.
GT::Node_Type GT_Node_Type
GT_Node * search(const GT_Node_Type &info)
Searches for a node based on the info.
void remove(GT_Node *p)
Removes the node at address p from the index.
void clear_index()
Clears the index; all nodes are removed from it.
GT_Node * insert_in_graph(GT_Node_Type &&info)
GT_Node * search(GT_Node *p)
Searches for a node based on the content of p.
IndexNode(GT &__g, SN __sn=SN())
Creates an index from the nodes inserted in the graph.
GT_Node * insert(GT_Node *p)
Inserts the node p into the index.
void build_index()
Inserts all nodes from the graph into the index.
GT_Node * insert_in_graph(const GT_Node_Type &info)
Creates a new node with generic content info, inserts it into the graph, and then into the index.
void clear_graph()
Removes all nodes from the graph and the index.
size_t size() const
Returns the number of items contained in the index.
DynSetTree< GT_Node *, Tree, Compare > index
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
virtual void remove_node(Node *node) noexcept
Remove a node from the graph and free its memory.
Definition tpl_graph.H:543
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:436
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1206
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:494
void clear_graph(GT &g) noexcept
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:3549
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Default node comparison class for Nodes_Index.
bool operator()(typename GT::Node *p1, typename GT::Node *p2) const
Comparison operator.
Default filter for the graph nodes.
Definition tpl_graph.H:1192
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:611
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.