OpenWalnut  1.4.0
WTreeNode.cpp
00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #include <map>
00026 #include <string>
00027 #include <vector>
00028 
00029 #include "../../common/WStringUtils.h"
00030 #include "WTreeNode.h"
00031 
00032 WTreeNode::WTreeNode( size_t index, double level )
00033   : boost::enable_shared_from_this< WTreeNode >(),
00034     m_level( level ),
00035     m_index( index )
00036 {
00037 }
00038 
00039 WTreeNode::WTreeNode( const WDendrogram &dendrogram )
00040   : boost::enable_shared_from_this< WTreeNode >()
00041 {
00042     const std::vector< size_t >& nodes = dendrogram.getParents();
00043     const std::vector< double >& heights = dendrogram.getHeights();
00044 
00045     size_t n = heights.size();
00046 
00047     std::map< size_t, WTreeNode::SPtr > map;
00048 
00049     for( size_t index = 0; index < nodes.size(); index++ )
00050     {
00051         double height = 0.0;
00052 
00053         if( index > n )
00054         {
00055             height = heights[ index - n - 1 ];
00056         }
00057         map[ index ] = WTreeNode::SPtr( new WTreeNode( index, height ) );
00058     }
00059 
00060     for( size_t index = 0; index < nodes.size() - 1; index++ )
00061     {
00062         size_t parent = nodes[ index ];
00063         map[ parent ]->addChild( map[ index ] );
00064     }
00065 
00066     //Last node is the root node
00067     WTreeNode::SPtr root = map.at( map.size() - 1 );
00068 
00069     m_index = root->index();
00070     m_level = root->level();
00071     m_children = root->getChildren();
00072 }
00073 
00074 WTreeNode::~WTreeNode()
00075 {
00076 }
00077 
00078 double WTreeNode::level()
00079 {
00080     return m_level;
00081 }
00082 
00083 size_t WTreeNode::index()
00084 {
00085     return m_index;
00086 }
00087 
00088 void WTreeNode::addChild( WTreeNode::SPtr child )
00089 {
00090     m_children.push_back( child );
00091     child->m_parent = shared_from_this();
00092 }
00093 
00094 WTreeNode::SPtr WTreeNode::getParent()
00095 {
00096     return m_parent;
00097 }
00098 
00099 std::vector< WTreeNode::SPtr > WTreeNode::getChildren()
00100 {
00101     return m_children;
00102 }