-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil.cc
155 lines (127 loc) · 3.12 KB
/
util.cc
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// util.cpp
// Copyright 2016-2016 University of Electronic Science and Technology of China
// (author : Xiaoming Qin)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file expect in compliance with the License.
#include "util.h"
#include <string>
float** create_array(const int row, const int col)
{
float **p;
p = new float*[row];
for (int i = 0; i <= row; ++i)
p[i] = new float[col];
return p;
}
float* create_array(const int col)
{
float *p;
p = new float[col];
return p;
}
int** create_int_array(const int row, const int col)
{
int **p;
p = new int*[row];
for (int i = 0; i <= row; ++i)
p[i] = new int[col];
return p;
}
int* create_int_array(const int col)
{
int *p;
p = new int[col];
return p;
}
// read file into a 2-d array
void read_file_into_2d_array(const string file_name, const int row, const int col, float** data)
{
std::ifstream file(file_name);
for (int i = 0; i < row; ++i)
{
std::string line;
std::getline(file, line);
if(!file.good())
break;
std::stringstream iss(line);
for (int j = 0; j < col; ++j)
{
std::string val;
std::getline(iss, val, ',');
if (!iss.good())
break;
std::stringstream convertor(val);
convertor >> data[i][j];
}
}
}
// read file into an 1-d array(float)
void read_file_into_1d_array(const string file_name, int col, float* data)
{
std::ifstream file(file_name);
std::string line;
std::getline(file, line);
if(!file.good())
return;
std::stringstream iss(line);
for (int j = 0; j < col; ++j)
{
std::string val;
std::getline(iss, val, ',');
if (!iss.good())
break;
std::stringstream convertor(val);
convertor >> data[j];
}
}
// read file into an 1-d array(int)
void read_file_into_1d_array(const string file_name, const int col, int* data)
{
std::ifstream file(file_name);
std::string line;
std::getline(file, line);
if(!file.good())
return;
std::stringstream iss(line);
for (int j = 0; j < col; ++j)
{
std::string val;
std::getline(iss, val, ',');
if (!iss.good())
break;
std::stringstream convertor(val);
convertor >> data[j];
}
}
// find maximum value and index
void find_max_value_index(float *p, float *q, float d, const int K, float &delta_val, int &psi_idx)
{
float tmp = 0.0;
int max_index = 0;
float max_val = p[0] * q[0] * d;
for (int i = 1; i != K; ++i )
{
tmp = p[i] * q[i] * d;
if ( max_val < tmp)
{
max_val = tmp;
max_index = i;
}
}
delta_val = max_val;
psi_idx = max_index;
}
int find_max_index(float *p, const int K)
{
float max_val = p[0];
int max_index = 0;
for (int i = 1; i != K; ++i )
{
if ( max_val < p[i])
{
max_val = p[i];
max_index = i;
}
}
return max_index;
}