forked from jMotif/jmotif-R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.cpp
More file actions
109 lines (102 loc) · 2.36 KB
/
string.cpp
File metadata and controls
109 lines (102 loc) · 2.36 KB
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
#include <RcppArmadillo.h>
using namespace Rcpp ;
//
#include <jmotif.h>
//
//' Get the ASCII letter by an index.
//'
//' @param idx the index.
//' @useDynLib jmotif
//' @export
//' @examples
//' # letter 'b'
//' idx_to_letter(2)
// [[Rcpp::export]]
char idx_to_letter(int idx) {
return LETTERS[idx-1];
}
//' Get the index for an ASCII letter.
//'
//' @param letter the letter.
//' @useDynLib jmotif
//' @export
//' @examples
//' # letter 'b' translates to 2
//' letter_to_idx('b')
// [[Rcpp::export]]
int letter_to_idx(char letter) {
return letter - 96;
}
//' Get an ASCII indexes sequence for a given character array.
//'
//' @param str the character array.
//' @useDynLib jmotif
//' @export
//' @examples
//' letters_to_idx(c('a','b','c','a'))
// [[Rcpp::export]]
IntegerVector letters_to_idx(CharacterVector str) {
IntegerVector res(str.length());
for(int i=0; i<str.length(); i++){
res[i] = letter_to_idx((str[i])[0]);
}
return res;
}
//' Compares two strings using natural letter ordering.
//'
//' @param a the string a.
//' @param b the string b.
//' @useDynLib jmotif
//' @export
//' @examples
//' is_equal_str("aaa", "bbb")
//' is_equal_str("ccc", "ccc")
// [[Rcpp::export]]
bool is_equal_str(CharacterVector a, CharacterVector b) {
std::string ca = Rcpp::as<std::string>(a);
std::string cb = Rcpp::as<std::string>(b);
// Rcout << ca << " and " << cb << "\n";
return (ca == cb);
}
//' Compares two strings using mindist.
//'
//' @param a the string a.
//' @param b the string b.
//' @useDynLib jmotif
//' @export
//' @examples
//' is_equal_str("aaa", "bbb") # true
//' is_equal_str("aaa", "ccc") # false
// [[Rcpp::export]]
bool is_equal_mindist(CharacterVector a, CharacterVector b) {
std::string ca = Rcpp::as<std::string>(a);
std::string cb = Rcpp::as<std::string>(b);
if(ca.length() != cb.length()){
return false;
}else{
for(unsigned i=0; i<ca.length(); i++){
if( abs(ca[i] - cb[i]) > 1 ){
return false;
}
}
}
return true;
}
/* bool _is_equal_mindist(std::string a, std::string b) {
if(a.length() != b.length()){
return false;
}else{
for(unsigned i=0; i<a.length(); i++){
if( abs(a[i] - b[i]) > 1 ){
return false;
}
}
}
return true;
}*/
int _count_spaces(std::string *s) {
int count = 0;
for (unsigned i = 0; i < s->size(); i++)
if (s->at(i) == ' ') count++;
return count;
}