35 #include "../WAssert.h"
36 #include "../WLogger.h"
37 #include "../WStringUtils.h"
38 #include "../exceptions/WOutOfBounds.h"
39 #include "WDendrogram.h"
68 WAssert( n > 0,
"A Dendrogram for an empty set of objects was created which makes no sence, if your really need it implement it!" );
78 throw WOutOfBounds(
"Accessed an empty WDendrogam via a call to a memeber function: " + caller +
" which needs access to elements." );
87 std::stringstream errMsg;
88 errMsg <<
"Bug: n=" <<
m_heights.size() <<
" many leafs can lead maximal to 2n-1 many nodes in a tree but this was violated now!" << std::endl;
105 std::stringstream result;
106 std::map< size_t, std::vector< size_t > > childsOfInnerNodes;
107 std::map< size_t, std::vector< size_t > > preds;
108 std::vector< size_t > level( 2 *
m_heights.size() + 1, 0 );
118 for(
size_t i = 0; i <
m_heights.size() + 1; ++i )
120 result <<
"(0, (" << i <<
",))" << std::endl;
121 childsOfInnerNodes[
m_parents[i] ].push_back( i );
132 size_t left = *( preds[ i ].begin() );
133 size_t right = *( preds[ i ].rbegin() );
134 level[i] = std::max( level[left], level[right] ) + 1;
138 childsOfInnerNodes[
m_parents[i] ].reserve( childsOfInnerNodes[
m_parents[i] ].size() + childsOfInnerNodes[i].size() );
139 childsOfInnerNodes[
m_parents[i] ].insert( childsOfInnerNodes[
m_parents[i] ].end(), childsOfInnerNodes[i].begin(),
140 childsOfInnerNodes[i].end() );
142 result <<
"(" << level[i] <<
", (";
143 size_t numElements = childsOfInnerNodes[i].size();
144 for( std::vector< size_t >::const_iterator cit = childsOfInnerNodes[i].begin(); cit != childsOfInnerNodes[i].end(); ++cit )
146 if( numElements == 1 )
148 result << *cit <<
"), ";
152 result << *cit <<
", ";
160 result <<
"(" << left <<
", " << right <<
"), " <<
m_heights[ i -
m_heights.size() - 1 ] <<
")" << std::endl;
void checkAndThrowExceptionIfUsedUninitialized(std::string caller) const
Checks if this instance is initialized.
std::vector< double > m_heights
Stores only for the inner nodes their heights.
std::vector< size_t > m_parents
Stores the parents of leafs as well as of inner nodes.
size_t merge(size_t i, size_t j, double height)
Merges two elements (either inner nodes or leafs) given via the indices i and j.
void reset(size_t n)
Resets the whole dendrogram to the number of elements it should be used for.
static std::shared_ptr< WPrototyped > m_prototype
The prototype as singleton.
static std::shared_ptr< WPrototyped > getPrototype()
Returns a prototype instantiated with the true type of the deriving class.
const std::vector< size_t > & getParents() const
Returns const reference to the internal parents array.
std::string toString() const
Transform this dendrogram into a string, where each leaf or inner node is mapped to a special string.
const std::vector< double > & getHeights() const
Const reference to the heights array.
WDendrogram()
Default constructs an empty dendrogram.
Indicates invalid element access of a container.
Class building the interface for classes that might be transferred using WModuleConnector.