(empty message)
[qanava.git] / src / la / laNode.cpp
blob64cc97b73f1ee849186ae76bac58bfeec3902c8f
1 /*
2 Qanava - Graph drawing library for QT
3 Copyright (C) 2005 Benoit AUTHEMAN
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 //-----------------------------------------------------------------------------
21 // This file is a part of the Qanava software.
23 // \file laNode.cpp
24 // \author Benoit Autheman (authem_b@epita.fr)
25 // \date 2004 February 15
26 //-----------------------------------------------------------------------------
29 // Qanava headers
30 #include "laNode.h"
31 #include "laGraph.h"
34 // Std headers
35 #include <algorithm>
36 #include <iterator>
39 // Boost headers
40 #ifndef DOXYSKIP
41 #include "boost/date_time/posix_time/posix_time.hpp"
42 #endif
45 namespace qan { // ::qan
46 namespace la { // ::qan::la
49 /* Node Constructor/Destructor *///--------------------------------------------
50 Node::Node( const std::string& label ) :
51 _id( 0 ),
52 _label( label ),
53 _type( -1 ),
54 _position( 2 ),
55 _dimension( 2 ),
56 _date( 0 )
58 setPosition( -1.f, -1.f );
59 setDimension( -1.f, -1.f );
62 Node::Node( const std::string& label, int type ) :
63 _id( 0 ),
64 _label( label ),
65 _type( type ),
66 _position( 2 ),
67 _dimension( 2 ),
68 _date( 0 )
70 setPosition( -1.f, -1.f );
71 setDimension( -1.f, -1.f );
73 //-----------------------------------------------------------------------------
77 /* Node Edges Management *///--------------------------------------------------
78 const Node::Nodes& Node::getOutNodes( ) const
80 return *new Nodes( );
83 Node::NodesSet* Node::getOutNodesSet( )
85 NodesSet* outNodeSet = new NodesSet( );
87 // Add all edge destination to the out nodes set
88 Edge::Edges& edges = getOutEdges( );
89 Edge::Edges::iterator edgeIter = edges.begin( );
90 for ( ; edgeIter != edges.end( ); edgeIter++ )
92 Edge* edge = *edgeIter;
93 outNodeSet->insert( &( edge->getDst( ) ) );
96 return outNodeSet;
99 Node::NodesSet* Node::getInNodesSet( )
101 NodesSet* inNodeSet = new NodesSet( );
103 // Add all edge destination to the out nodes set
104 Edge::Edges& edges = getInEdges( );
105 Edge::Edges::iterator edgeIter = edges.begin( );
106 for ( ; edgeIter != edges.end( ); edgeIter++ )
108 Edge* edge = *edgeIter;
109 inNodeSet->insert( &( edge->getSrc( ) ) );
112 return inNodeSet;
115 Node::NodesSet* Node::getAdjacentNodesSet( )
117 NodesSet* outNodes = getOutNodesSet( );
118 NodesSet* inNodes = getInNodesSet( );
120 NodesSet& adjacentNodes = *new NodesSet( );
121 std::set_union( outNodes->begin( ), outNodes->end( ),
122 inNodes->begin( ), inNodes->end( ),
123 std::insert_iterator< NodesSet >( adjacentNodes, adjacentNodes.begin( ) ) );
125 delete outNodes;
126 delete inNodes;
128 return &adjacentNodes;
131 Node::NodesSet* Node::getNonAdjacentNodesSet( la::Graph& graph )
133 NodesSet* adjacentNodes = getAdjacentNodesSet( );
134 NodesSet* graphNodes = graph.getNodesSet( );
136 NodesSet& nonAdjacentNodes = *new NodesSet( );
137 std::set_difference( graphNodes->begin( ), graphNodes->end( ),
138 adjacentNodes->begin( ), adjacentNodes->end( ),
139 std::insert_iterator< NodesSet >( nonAdjacentNodes, nonAdjacentNodes.begin( ) ) );
141 // Remove the node idself from to non adjacent nodes list
142 nonAdjacentNodes.erase( this );
144 delete adjacentNodes;
145 delete graphNodes;
147 return &nonAdjacentNodes;
149 //-----------------------------------------------------------------------------
153 /* Node Attributes Management *///---------------------------------------------
154 void Node::setDate( const std::string& date )
158 _date = new boost::posix_time::ptime( boost::posix_time::time_from_string( date ) );
159 } catch( std::exception& e )
161 if ( _date != 0 )
162 delete _date;
163 _date = 0;
166 //-----------------------------------------------------------------------------
169 } // ::qan::la
170 } // ::qan