-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
147 lines (119 loc) · 3.83 KB
/
Copy pathutils.cpp
File metadata and controls
147 lines (119 loc) · 3.83 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
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
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <random>
#include "utils.h"
int weighted_sample(const std::vector<double>& prob, std::mt19937& gen) {
std::discrete_distribution<int> dist(prob.begin(), prob.end());
return dist(gen);
}
std::vector<int> read1D(std::string filename) {
// Open the file for reading
std::ifstream fin(filename);
// Create an empty vector
std::vector<int> v;
int size;
fin >> size;
v.resize(size);
// Read the contents of the file and store them in the
// vector
int c;
int pos {0};
while (fin >> c) {
v[pos] = c;
++pos;
}
// Close the file
fin.close();
return v;
}
std::vector<std::vector<int>> read2D(std::string filename) {
// Open the file for reading
std::ifstream fin(filename);
// Create an empty vector of vectors
std::vector<std::vector<int>> v;
int num_rows;
// First line of the file will contain number of rows
fin >> num_rows;
v.resize(num_rows);
int row_sizes[num_rows];
// Next num_rows lines will contain number of elements in each row
// Note that this is a "ragged" 2D array -- rows have diff. # of elements
int row_size;
for (int i = 0; i < num_rows; i++) {
fin >> row_size;
v[i].resize(row_size);
row_sizes[i] = row_size;
}
fin.get(); //read one token to account for trailing newline
std::string row;
int c;
int pos = 0;
while (std::getline(fin, row)) {
if (!row.empty()) {
std::istringstream row_split(row);
for (int j = 0; j < row_sizes[pos]; j++) {
row_split >> c;
v[pos][j] = c;
}
}
pos++;
}
// Close the file
fin.close();
return v;
}
void write1D(std::string filename, std::vector<double> v) {
std::ofstream outputFile;
outputFile.open(filename);
//1D vector Files begin with number of elements in the vector
outputFile << v.size() << std::endl;
for (int i = 0; i < v.size(); i++) {
outputFile << v[i] << " "; //Elements separated by spaces
}
outputFile << std::endl;
outputFile.close();
}
void write2D(std::string filename, std::vector<std::vector<double>> v) {
std::ofstream outputFile;
outputFile.open(filename);
//2D vector files begin with number of rows
outputFile << v.size() << std::endl;
//2D vectors files then contain number of elements in each row
for (int i = 0; i < v.size(); i++) {
outputFile << v[i].size() << std::endl;
}
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].size(); j++) {
outputFile << v[i][j] << " "; //Row elements separated by spaces
}
outputFile << std::endl;; //Rows separated by newlines
}
outputFile.close();
}
void write3D(std::string filename, std::vector<std::vector<std::vector<double>>> v) {
std::ofstream outputFile;
outputFile.open(filename);
//2D vector files begin with number of rows
outputFile << v.size() << std::endl;
//2D vectors files then contain number of elements in each row
for (int i = 0; i < v.size(); i++) {
outputFile << v[i].size() << std::endl;
}
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].size(); j++) {
outputFile << v[i][j].size() << std::endl;
}
}
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < v[i].size(); j++) {
for (int k = 0; k < v[i][j].size(); k++) {
outputFile << v[i][j][k] << " "; //Row elements separated by spaces
}
outputFile << std::endl; //Rows separated by newlines
}
outputFile << std::endl << std::endl; //Rows separated by newlines
}
outputFile.close();
}