cpp : more robust code and added more documentation
This commit is contained in:
58
cpp/data.cpp
58
cpp/data.cpp
@ -1,9 +1,7 @@
|
||||
#include "data.hpp"
|
||||
//#include "toolbox.hpp"
|
||||
//#include <cstring>
|
||||
|
||||
int print(const np::Shape& shape) noexcept {
|
||||
int num_written = 0;
|
||||
int32_t print(const np::Shape& shape) noexcept {
|
||||
int32_t num_written = 0;
|
||||
num_written += printf("(");
|
||||
if (shape.length > 1) {
|
||||
const size_t length = shape.length - 1;
|
||||
@ -17,7 +15,7 @@ int print(const np::Shape& shape) noexcept {
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int print(const np::Array<T>& array, const char* format) noexcept {
|
||||
int32_t print(const np::Array<T>& array, const char* const format) noexcept {
|
||||
//printf("[");
|
||||
//const size_t length = np::prod(array.shape);
|
||||
//for(size_t i = 0; i < length - 1; ++i)
|
||||
@ -27,10 +25,10 @@ int print(const np::Array<T>& array, const char* format) noexcept {
|
||||
//printf("%f]\n", array[length - 1]);
|
||||
|
||||
char format_space[BUFFER_SIZE] = { 0 };
|
||||
sprintf(format_space, "%s ", format);
|
||||
snprintf(format_space, BUFFER_SIZE,"%s ", format);
|
||||
char format_close[BUFFER_SIZE] = { 0 };
|
||||
sprintf(format_close, "%s]\n", format);
|
||||
int num_written = 0;
|
||||
snprintf(format_close, BUFFER_SIZE,"%s]\n", format);
|
||||
int32_t num_written = 0;
|
||||
|
||||
if (array.shape.length == 1) {
|
||||
const size_t max = array.shape[0] - 1;
|
||||
@ -53,16 +51,16 @@ int print(const np::Array<T>& array, const char* format) noexcept {
|
||||
return num_written;
|
||||
}
|
||||
|
||||
int print(const np::Array<uint8_t>& array) noexcept {
|
||||
int32_t print(const np::Array<uint8_t>& array) noexcept {
|
||||
return print(array, "%hu");
|
||||
}
|
||||
|
||||
int print(const np::Array<float64_t>& array) noexcept {
|
||||
int32_t print(const np::Array<float64_t>& array) noexcept {
|
||||
return print(array, "%f");
|
||||
}
|
||||
|
||||
int print_feat(const np::Array<uint8_t>& array, const np::Slice& slice) noexcept {
|
||||
int num_written = 0;
|
||||
int32_t print_feat(const np::Array<uint8_t>& array, const np::Slice& slice) noexcept {
|
||||
int32_t num_written = 0;
|
||||
num_written += printf("[");
|
||||
const size_t feat_size = np::prod(array.shape, 1);
|
||||
const size_t offset = slice.x * feat_size;
|
||||
@ -74,8 +72,8 @@ int print_feat(const np::Array<uint8_t>& array, const np::Slice& slice) noexcept
|
||||
return num_written;
|
||||
}
|
||||
|
||||
int print(const np::Array<uint8_t>& array, const np::Slice& slice) noexcept {
|
||||
int num_written = 0;
|
||||
int32_t print(const np::Array<uint8_t>& array, const np::Slice& slice) noexcept {
|
||||
int32_t num_written = 0;
|
||||
if (array.shape.length == 1) {
|
||||
const size_t max = slice.y - 1; //std::min(slice.y, array.shape[0] - 1);
|
||||
num_written += printf("[");
|
||||
@ -97,8 +95,8 @@ int print(const np::Array<uint8_t>& array, const np::Slice& slice) noexcept {
|
||||
return num_written;
|
||||
}
|
||||
|
||||
int print(const np::Array<uint32_t>& array, const np::Slice& slice) noexcept {
|
||||
int num_written = 0;
|
||||
int32_t print(const np::Array<uint32_t>& array, const np::Slice& slice) noexcept {
|
||||
int32_t num_written = 0;
|
||||
if (array.shape.length == 1) {
|
||||
const size_t max = slice.y - 1; //std::min(slice.y, array.shape[0] - 1);
|
||||
num_written += printf("[");
|
||||
@ -115,37 +113,37 @@ int print(const np::Array<uint32_t>& array, const np::Slice& slice) noexcept {
|
||||
num_written += printf("%5i ", array[k + i * array.shape[1] + j]);
|
||||
num_written += printf("]\n");
|
||||
}
|
||||
num_written += print("]");
|
||||
num_written += printf("]");
|
||||
}
|
||||
return num_written;
|
||||
}
|
||||
|
||||
int print(const np::Array<int32_t>& array, const np::Slice& slice) noexcept {
|
||||
int num_written = 0;
|
||||
int32_t print(const np::Array<int32_t>& array, const np::Slice& slice) noexcept {
|
||||
int32_t num_written = 0;
|
||||
num_written += printf("[");
|
||||
//size_t k = slice.x * array.shape[1] * array.shape[2] + slice.y * array.shape[2] + slice.z;
|
||||
size_t k = slice.x * array.shape[1];
|
||||
for (size_t i = k; i < k + (slice.y - slice.x); ++i) {
|
||||
num_written += printf("%5i ", array[i]);
|
||||
}
|
||||
num_written += print("]");
|
||||
num_written += printf("]");
|
||||
return num_written;
|
||||
}
|
||||
|
||||
int print(const np::Array<uint16_t>& array, const np::Slice& slice) noexcept {
|
||||
int num_written = 0;
|
||||
int32_t print(const np::Array<uint16_t>& array, const np::Slice& slice) noexcept {
|
||||
int32_t num_written = 0;
|
||||
num_written += printf("[");
|
||||
//size_t k = slice.x * array.shape[1] * array.shape[2] + slice.y * array.shape[2] + slice.z;
|
||||
size_t k = slice.x * array.shape[1];
|
||||
for (size_t i = k; i < k + (slice.y - slice.x); ++i) {
|
||||
num_written += printf("%5hu ", array[i]);
|
||||
}
|
||||
num_written += print("]");
|
||||
num_written += printf("]");
|
||||
return num_written;
|
||||
}
|
||||
|
||||
static inline np::Array<uint8_t> load_set(const char* set_name) {
|
||||
FILE* file = fopen(set_name, "rb");
|
||||
static inline np::Array<uint8_t> load_set(const char* const set_name) {
|
||||
FILE* const file = fopen(set_name, "rb");
|
||||
if (file == NULL) {
|
||||
print_error_file(set_name);
|
||||
throw;
|
||||
@ -156,7 +154,7 @@ static inline np::Array<uint8_t> load_set(const char* set_name) {
|
||||
fclose(file);
|
||||
throw;
|
||||
}
|
||||
size_t* dims = new size_t[3]();
|
||||
size_t* const dims = new size_t[3]();
|
||||
if (!sscanf(meta, "%lu %lu %lu", &dims[0], &dims[1], &dims[2])) {
|
||||
print_error_file(set_name);
|
||||
fclose(file);
|
||||
@ -167,7 +165,7 @@ static inline np::Array<uint8_t> load_set(const char* set_name) {
|
||||
|
||||
const size_t size = np::prod(a.shape);
|
||||
size_t i = 0, j = 0;
|
||||
int c;
|
||||
int32_t c;
|
||||
char buff[STRING_INT_SIZE] = { 0 };
|
||||
while ((c = fgetc(file)) != EOF && i < size) {
|
||||
if (c == ' ' || c == '\n') {
|
||||
@ -191,15 +189,15 @@ static inline np::Array<uint8_t> load_set(const char* set_name) {
|
||||
return a;
|
||||
}
|
||||
|
||||
std::array<np::Array<uint8_t>, 4> load_datasets() {
|
||||
std::array<np::Array<uint8_t>, 4> load_datasets(void) {
|
||||
return {
|
||||
load_set(DATA_DIR "/X_train.bin"), load_set(DATA_DIR "/y_train.bin"),
|
||||
load_set(DATA_DIR "/X_test.bin"), load_set(DATA_DIR "/y_test.bin")
|
||||
};
|
||||
}
|
||||
|
||||
void print_error_file(const char* file_dir) noexcept {
|
||||
const char* buff = strerror(errno);
|
||||
void print_error_file(const char* const file_dir) noexcept {
|
||||
const char* const buff = strerror(errno);
|
||||
fprintf(stderr, "Can't open %s, error code = %d : %s\n", file_dir, errno, buff);
|
||||
// delete buff;
|
||||
}
|
||||
|
Reference in New Issue
Block a user