-
Notifications
You must be signed in to change notification settings - Fork 1
/
Helper.c
124 lines (96 loc) · 3.04 KB
/
Helper.c
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
113
114
115
116
117
118
119
120
121
122
123
124
#include "Helper.h"
Instance MyHelper::Tokenize(
const char* str,
const vector<NumericAttr>& featureVec )
{
unsigned int numFeatures = featureVec.size();
Instance instance;
instance.featureAttrArray =
(double*) calloc( numFeatures, sizeof( double ) );
unsigned int iter = 0;
while (str[iter] != '\0')
{
unsigned int startIndex = iter;
while (IsLetter( str[iter] ) ||
str[iter] == '\?' || str[iter] == '_')
iter++;
// Found a token
if (iter > startIndex)
{
unsigned int tokenLen = iter - startIndex;
// Compare the token with every feature name
// Might use a hashmap (with key: name, value: index)
// to speed up
for (unsigned int feaIndex = 0;
feaIndex < numFeatures; feaIndex++)
{
const char* feaName = featureVec[feaIndex].name;
unsigned index = 0;
while (index < tokenLen && feaName[index] != '\0'
&& (feaName[index] == str[startIndex + index] ||
feaName[index] == str[startIndex + index] + 32))
index++;
if (index == tokenLen && feaName[index] == '\0')
instance.featureAttrArray[feaIndex]++;
}
}
if (str[iter] != '\0') iter++;
}
return instance;
}
bool MyHelper::IsLetter( const char c )
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
bool MyHelper::StrEqualCaseSen( const char* str1, const char* str2 )
{
unsigned short i = 0;
while (str1[i] != '\0' && str1[i] == str2[i]) i++;
return (str1[i] == '\0' && str2[i] == '\0') ? true : false;
}
bool MyHelper::StrEqualCaseInsen( const char* str1, const char* str2 )
{
unsigned short i = 0;
while (str1[i] != '\0' &&
(str1[i] == str2[i] ||
(IsLetter( str1[i] ) && IsLetter( str2[i] ) &&
abs( str1[i] - str2[i] ) == 32))) i++;
return (str1[i] == '\0' && str2[i] == '\0') ? true : false;
}
unsigned int MyHelper::GetStrLength( const char* str )
{
unsigned int len = 0;
while (str[len++] != '\0');
return len;
}
unsigned int MyHelper::getIndexOfMax(
const unsigned int* uintArray,
const unsigned int length )
{
return max_element( uintArray, uintArray + length ) - uintArray;
}
unsigned int MyHelper::removeDuplicates(
double* sortedArr,
unsigned int length )
{
if (sortedArr == nullptr) return 0;
unsigned int uniqueId = 1;
unsigned int iter = 1;
while (iter < length)
{
if (sortedArr[iter - 1] != sortedArr[iter])
sortedArr[uniqueId++] = sortedArr[iter];
iter++;
}
return uniqueId;
}
void MyHelper::CheckMPIErr( int errorCode, int mpiNodeId )
{
if (errorCode != MPI_SUCCESS)
{
char errorString[MPI_ERROR_MESSAGE_BUFF_SIZE];
int strLen;
MPI_Error_string( errorCode, errorString, &strLen );
fprintf( stderr, "%3d: %s\n", mpiNodeId, errorString );
}
}