1 /*
2  Copyright (c) 2008-2020, Benoit AUTHEMAN All rights reserved.
3 
4  Redistribution and use in source and binary forms, with or without
5  modification, are permitted provided that the following conditions are met:
6     * Redistributions of source code must retain the above copyright
7       notice, this list of conditions and the following disclaimer.
8     * Redistributions in binary form must reproduce the above copyright
9       notice, this list of conditions and the following disclaimer in the
10       documentation and/or other materials provided with the distribution.
11     * Neither the name of the author or Destrat.io nor the
12       names of its contributors may be used to endorse or promote products
13       derived from this software without specific prior written permission.
14 
15  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
19  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26 
27 //-----------------------------------------------------------------------------
28 // This file is a part of the GTpo software.
29 //
30 // \file	node.hpp
31 // \author	benoit@destrat.io
32 // \date	2016 01 22
33 //-----------------------------------------------------------------------------
34 
35 namespace gtpo { // ::gtpo
36 
37 /* node Edges Management *///-----------------------------------------------
38 template < class config_t >
add_out_edge(weak_edge_t outEdgePtr)39 auto node<config_t>::add_out_edge( weak_edge_t outEdgePtr ) -> void
40 {
41     assert_throw( !outEdgePtr.expired(), "gtpo::node<>::add_out_edge(): Error: out edge is expired." );
42     auto node = std::static_pointer_cast<typename config_t::final_node_t>(this->shared_from_this());
43     auto outEdge = outEdgePtr.lock();
44     auto outEdgeSrc = outEdge->get_src().lock();
45     if ( outEdge ) {
46         if ( !outEdgeSrc || outEdgeSrc != node )  // Out edge source should point to target node
47             outEdge->set_src( node );
48         config_t::template container_adapter< weak_edges_t >::insert( outEdgePtr, _out_edges );
49         if ( !outEdge->get_dst().expired() ) {
50             config_t::template container_adapter< weak_nodes_t >::insert( outEdge->get_dst(), _out_nodes );
51             this->notify_out_node_inserted( weak_node_t{node}, outEdge->get_dst(), weak_edge_t{outEdge} );
52         }
53     }
54 }
55 
56 template <class config_t>
add_in_edge(weak_edge_t inEdgePtr)57 auto node<config_t>::add_in_edge( weak_edge_t inEdgePtr ) -> void
58 {
59     assert_throw( !inEdgePtr.expired(), "gtpo::node<>::add_in_edge(): Error: in edge is expired." );
60     auto node = std::static_pointer_cast<typename config_t::final_node_t>(this->shared_from_this());
61     auto inEdge = inEdgePtr.lock( );
62     if ( inEdge ) {
63         auto inEdgeDst = inEdge->get_dst().lock();
64         if ( !inEdgeDst ||
65              inEdgeDst != node ) // In edge destination should point to target node
66             inEdge->set_dst( node );
67         config_t::template container_adapter< weak_edges_t >::insert( inEdgePtr, _in_edges );
68         if ( !inEdge->get_src().expired() ) {
69             config_t::template container_adapter< weak_nodes_t >::insert( inEdge->get_src(), _in_nodes );
70             this->notify_in_node_inserted( weak_node_t{node}, inEdge->get_src(), inEdgePtr );
71         }
72     }
73 }
74 
75 template < class config_t >
remove_out_edge(const weak_edge_t outEdge)76 auto node<config_t>::remove_out_edge( const weak_edge_t outEdge ) -> void
77 {
78     gtpo::assert_throw( !outEdge.expired(), "gtpo::node<>::remove_out_edge(): Error: Out edge has expired" );
79     auto outEdgePtr = outEdge.lock( );
80     auto outEdgeSrcPtr = outEdgePtr->get_src().lock();
81     weak_node_t node{ std::static_pointer_cast<typename config_t::final_node_t>(this->shared_from_this()) };
82     gtpo::assert_throw( outEdgeSrcPtr != nullptr &&    // Out edge src must be this node
83                         outEdgeSrcPtr.get() == this, "gtpo::node<>::remove_out_edge(): Error: Out edge source is expired or different from this node.");
84 
85     auto outEdgeDst = outEdgePtr->get_dst().lock();
86     if ( outEdgeDst != nullptr ) {
87         gtpo::assert_throw( outEdgeDst != nullptr, "gtpo::node<>::remove_out_edge(): Error: Out edge destination is expired." );
88         this->notify_out_node_removed( node, outEdgePtr->get_dst(), outEdge );
89     }
90     config_t::template container_adapter<weak_edges_t>::remove( outEdge, _out_edges );
91     config_t::template container_adapter<weak_nodes_t>::remove( outEdgePtr->get_dst(), _out_nodes );
92     if ( get_in_degree() == 0 ) {
93         graph_t* graph{ this->get_graph() };
94         if ( graph != nullptr )
95             graph->install_root_node( node );
96     }
97     this->notify_out_node_removed( node );
98 }
99 
100 template < class config_t >
remove_in_edge(const weak_edge_t inEdge)101 auto node<config_t>::remove_in_edge( const weak_edge_t inEdge ) -> void
102 {
103     gtpo::assert_throw( !inEdge.expired(), "gtpo::node<>::remove_in_edge(): Error: In edge has expired" );
104     auto nodePtr = std::static_pointer_cast<typename config_t::final_node_t>(this->shared_from_this());
105     auto inEdgePtr = inEdge.lock( );
106     auto inEdgeDstPtr = inEdgePtr->get_dst().lock();
107     gtpo::assert_throw( inEdgeDstPtr &&    // in edge dst must be this node
108                         inEdgeDstPtr == nodePtr, "gtpo::node<>::remove_in_edge(): Error: In edge destination is expired or different from this node.");
109 
110     auto inEdgeSrcPtr = inEdgePtr->get_src().lock();
111     gtpo::assert_throw( inEdgeSrcPtr != nullptr, "gtpo::node<>::remove_in_edge(): Error: In edge source is expired." );
112     this->notify_in_node_removed( weak_node_t{ nodePtr }, inEdgePtr->get_src(), inEdge );
113     config_t::template container_adapter< weak_edges_t >::remove( inEdge, _in_edges );
114     config_t::template container_adapter< weak_nodes_t >::remove( inEdgePtr->get_src(), _in_nodes );
115     if ( get_in_degree() == 0 ) {
116         graph_t* graph{ this->get_graph() };
117         if ( graph != nullptr )
118             graph->install_root_node( weak_node_t{ nodePtr } );
119     }
120     this->notify_in_node_removed( weak_node_t{nodePtr} );
121 }
122 //-----------------------------------------------------------------------------
123 
124 /* Group Nodes Management *///-------------------------------------------------
125 template < class config_t >
has_node(const weak_node_t & node) const126 auto node<config_t>::has_node( const weak_node_t& node ) const noexcept -> bool
127 {
128     if ( node.expired() )
129         return false;
130     shared_node_t group_node = node.lock();
131     if ( !group_node )
132         return false;
133     auto group_nodeIter = std::find_if( _nodes.begin(), _nodes.end(),
134                                         [=](const weak_node_t& group_node ){ return ( compare_weak_ptr<>( node, group_node ) ); } );
135     return group_nodeIter != _nodes.end();
136 }
137 //-----------------------------------------------------------------------------
138 
139 } // ::gtpo
140