Update procedures
[shapes.git] / source / graphtypes_impl.h
blobcebdd84d40a64467445b01297a503c5d7cea3815
1 /* This file is part of Shapes.
3 * Shapes is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * any later version.
8 * Shapes is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright 2013, 2014 Henrik Tidefelt
19 template< class T >
20 Shapes::Kernel::Node::ListRef
21 Shapes::Kernel::Node::prepend_edges( const T & edges, bool directed, const ListRef & rest, const RefCountPtr< const Lang::Graph > & graph, const Kernel::EdgePredicate * filter )
23 Kernel::Node::ListRef result = rest;
24 if( filter == NULL ){
25 for( typename T::const_iterator e = edges.begin( ); e != edges.end( ); ++e ){
26 result = Helpers::SingleList_cons( static_cast< Lang::Value * >( new Lang::Edge( graph, *e, directed ) ), result );
28 }else{
29 for( typename T::const_iterator e = edges.begin( ); e != edges.end( ); ++e ){
30 if( (*filter)( **e ) ){
31 result = Helpers::SingleList_cons( static_cast< Lang::Value * >( new Lang::Edge( graph, *e, directed ) ), result );
35 return result;
38 template< class T >
39 Shapes::Kernel::Node::ListRef
40 Shapes::Kernel::Node::prepend_edges_neighbor( Kernel::Node * neighbor, const T & edges, bool directed, const ListRef & rest, const RefCountPtr< const Lang::Graph > & graph, const Kernel::EdgePredicate * filter )
42 /* Out of all four arguments to the Kernel::Edge constructor, only one of the first two and the third will be used. */
43 Kernel::Edge edgeLow( neighbor, neighbor, 0, true );
44 Kernel::Edge edgeHigh( neighbor, neighbor, std::numeric_limits< size_t >::max( ), true );
45 Kernel::Node::ListRef result = rest;
46 typename T::const_iterator eBegin = edges.lower_bound( & edgeLow );
47 typename T::const_iterator eEnd = edges.upper_bound( & edgeHigh );
48 if( filter == NULL ){
49 for( typename T::const_iterator e = eBegin; e != eEnd; ++e ){
50 result = Helpers::SingleList_cons( static_cast< Lang::Value * >( new Lang::Edge( graph, *e, directed ) ), result );
52 }else{
53 for( typename T::const_iterator e = eBegin; e != eEnd; ++e ){
54 if( (*filter)( **e ) ){
55 result = Helpers::SingleList_cons( static_cast< Lang::Value * >( new Lang::Edge( graph, *e, directed ) ), result );
59 return result;
63 template< class T >
64 Shapes::Kernel::EdgeLabelPredicate< T >::EdgeLabelPredicate( const RefCountPtr< std::vector< Kernel::ValueRef > > & edgeLabels, const T & label )
65 : edgeLabels_( edgeLabels ), label_( label )
67 if( edgeLabels_ == NullPtr< std::vector< Kernel::ValueRef > >( ) )
68 always_false_ = true;
71 template< class T >
72 Shapes::Kernel::EdgeLabelPredicate< T >::~EdgeLabelPredicate( )
73 { }
75 template< class T >
76 bool
77 Shapes::Kernel::EdgeLabelPredicate< T >::operator () ( const Edge & e ) const
79 if( always_false_ )
80 return false;
81 const T * ptr = dynamic_cast< const T * >( (*edgeLabels_)[ e.index( ) ].getPtr( ) );
82 if( ptr == NULL )
83 return false;
84 return *ptr == label_;