/
labels_dendrogram.cpp
112 lines (74 loc) · 2.92 KB
/
labels_dendrogram.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Copyright (C) Tal Galili
This file is part of dendextendRcpp.
dendextend is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
dendextend is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
A copy of the GNU General Public License is available at
http://www.r-project.org/Licenses/
*/
#include <Rcpp.h>
using namespace Rcpp;
#include "is_functions.h"
// Extract the label attr from an R object
// Only works for a character label (will crash in case of an integer value)
std::string get_label(RObject x){
std::string label = "<EMPTY>";
if (x.hasAttribute("label")) {
label = as<std::string>(x.attr( "label" )) ;
}
return label; // either TRUE or FALSE. But often, if it exists - it is TRUE.
}
void push_back_labels( List data, std::vector<std::string>& the_labels){
// The Rcout are commented in order to not through too many comments to the user...
// Rcout << "List with " << data.size() << " elements\n";
for( int i=0; i<data.size(); i++){
if( is_list( data[i] ) ){
// recurse
// Rcout << "Recursing into list\n";
push_back_labels( data[i], the_labels ) ;
} else if( is_leaf( data[i] ) ){
// Rcout << "Looking at leaf\n";
// we want to collect them. we can use the NumericVector class
// wince we know this is a numeric vector.
std::string x_label = get_label(data[i]);
the_labels.push_back(x_label);
} // else do nothing
}
}
// [[Rcpp::export]]
std::vector<std::string> Rcpp_labels_dendrogram(List x){
std::vector<std::string> the_labels;
// // For some reason - this doesn't work - so I fixed it in the R code...
// // if the tree is a leaf - return its label:
// if( is_leaf(x) ){
// std::string x_label = get_label(x);
// the_labels.push_back(x_label);
// } else {
// // else: run through all of it:
// push_back_labels(x, the_labels) ;
// }
push_back_labels(x, the_labels) ;
return(the_labels) ;
}
// [[Rcpp::export]]
int Rcpp_count_leaves(List data){
// The Rcout are commented in order to not through too many comments to the user...
// Rcout << "List with " << data.size() << " elements\n";
int number_of_leaves=0;
for( int i=0; i<data.size(); i++){
if( is_list( data[i] ) ){
// recurse
// Rcout << "Recursing into list\n";
number_of_leaves += Rcpp_count_leaves( data[i]) ;
} else if( is_leaf( data[i] ) ){
number_of_leaves++;
} // else do nothing
}
return(number_of_leaves);
}