Commit 94eac981 authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Add graph for mem and blc_array

parent c71697ce
/* Basic Library for C/C++ (blclib)
Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2014)
Author: A. Blanchard
This software is a computer program whose purpose is to simulate neural networks and control robots or simulations.
This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
......@@ -31,80 +31,71 @@
#define EXIT_ON_ARRAY_ERROR(array, ...) blc_array_fatal_error(array, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__)
typedef struct blc_dim {
size_t length;
size_t step;
size_t length;
size_t step;
} blc_dim;
typedef struct blc_array
#ifdef __cplusplus
:blc_mem {
blc_array();
/**def and does not allocate*/
void vdef_array(uint32_t type, uint32_t format, int dims_nb, int length0, va_list arguments);
void def_array(uint32_t type, uint32_t format, int dims_nb, int length0, ...);
void def_array(uint32_t type, uint32_t format, int dims_nb, blc_dim *dims);
void def_array(uint32_t type, uint32_t format, char const *dims_string);
/*the name should be changed*/
void init(char const *properties);
void destroy();
void add_dim(int length, int step);
void add_dim(int length);
void set_dims(int dims_nb, int length, ...);
int sprint_dims(char *string, int size);
int fprint_dims(FILE *file) const;
void fprint_debug(FILE *file) const;
void fscan_dims(FILE *file);
void sscan_properties(char const *string);
void fscan_properties(FILE *file);
void sprint_properties(char *buffer, size_t buffer_size);
void fprint_properties(FILE *file);
:blc_mem {
blc_array();
~blc_array();
/**def and does not allocate*/
void vdef_array(uint32_t type, uint32_t format, int dims_nb, int length0, va_list arguments);
void def_array(uint32_t type, uint32_t format, int dims_nb, int length0, ...);
void def_array(uint32_t type, uint32_t format, int dims_nb, blc_dim *dims);
void def_array(uint32_t type, uint32_t format, char const *dims_string);
/*the name should be changed*/
void init(char const *properties);
void add_dim(int length, int step);
void add_dim(int length);
void set_dims(int dims_nb, int length, ...);
int sprint_dims(char *string, int size);
int fprint_dims(FILE *file) const;
void fprint_debug(FILE *file) const;
void fscan_dims(FILE *file);
void sscan_properties(char const *string);
void fscan_properties(FILE *file);
void sprint_properties(char *buffer, size_t buffer_size);
void fprint_properties(FILE *file);
///Scan and interpret the string as the description of the dimensions of a **blc_channel**. **Return** how many chars have been interpreted.
int sscan_dims(char const* string);
int get_type_size();
size_t get_minimum_size();
int sscan_dims(char const* string);
int get_type_size();
size_t get_minimum_size();
void def_with_blc_file(char const *filename);
void init_with_blc_file(char const *filename);
void update_with_blc_file(char const *filename);
void save_blc_file(char const *filename);
void fprint_tsv(FILE *file);
void save_tsv_file(char const *filename);
void fprint_surface_uchars(FILE *file);
#else
{
blc_mem mem;
{
blc_mem mem;
#endif
blc_dim * dims;
int dims_nb;
uint32_t type, format;
} blc_array;
START_EXTERN_C
void blc_array_fatal_error(blc_array const *array, const char *name_of_file, const char* name_of_function, int numero_of_line, const char *message, ...);
void blc_array_destroy(blc_array *narray);
END_EXTERN_C
///@}
blc_dim * dims;
int dims_nb;
uint32_t type, format;
} blc_array;
START_EXTERN_C
void blc_array_fatal_error(blc_array const *array, const char *name_of_file, const char* name_of_function, int numero_of_line, const char *message, ...);
void blc_array_destroy(blc_array *array);
END_EXTERN_C
///@}
#endif
/* Basic Library for C/C++ (blclib)
Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2014)
Author: A. Blanchard
This software is a computer program whose purpose is to simulate neural networks and control robots or simulations.
This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
 users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
 In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
 that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
 users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
 In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
 that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured
 and, more generally, to use and operate it in the same conditions as regards security.
 The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms. */
 and, more generally, to use and operate it in the same conditions as regards security.
 The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms. */
/**
@date Apr 28, 2014
@author Arnaud Blanchard
@defgroup blc_mem blc_mem
@{
@date Apr 28, 2014
@author Arnaud Blanchard
@defgroup blc_mem blc_mem
@{
@brief Structure with a data pointer and a size. This kind of functionnalities are more complete with the Standard Template Library (STL) or boost, but it is much simpler here.
@code{.c}
typedef struct blc_mem {
......@@ -37,7 +37,7 @@
size_t size;
}
@endcode
*/
*/
#ifndef BLC_MEM_H
#define BLC_MEM_H
......@@ -46,47 +46,52 @@
#include <stdio.h>
typedef struct blc_mem {
//We can use only one of this datatpe at a time
union{
void *data;
char *chars;
uchar *uchars;
int16_t *ints16;
uint16_t *uints16;
int32_t *ints32;
uint32_t *uints32;
float *floats;
double *doubles;
};
size_t size;
//We can use only one of this datatpe at a time
union{
void *data;
char *chars;
uchar *uchars;
int16_t *ints16;
uint16_t *uints16;
int32_t *ints32;
uint32_t *uints32;
float *floats;
double *doubles;
};
size_t size;
#ifdef __cplusplus
blc_mem();
/** Create and allocate memory of size size */
blc_mem(size_t size);
/** Create and allocate memory of size size */
blc_mem(size_t size);
/// Free the data
~blc_mem();
/**really allocates the data of size previously set*/
void allocate();
/** Change the size of memory. The content is not preserved. Use size = 0 to free the memory. */
void allocate(size_t size);
/// Allocate more memory if needed but does not free memory
void allocate_min(size_t size);
/** Change the size of memory. But keep the content.*/
void reallocate(size_t size);
/** Increases the memory of size and adds the new content.*/
void append(char const *new_data, size_t new_data_size);
void allocate();
/** Change the size of memory. The content is not preserved. Use size = 0 to free the memory. */
void allocate(size_t size);
/// Allocate more memory if needed but does not free memory
void allocate_min(size_t size);
/** Change the size of memory. But keep the content.*/
void reallocate(size_t size);
/** Increases the memory of size and adds the new content.*/
void append(char const *new_data, size_t new_data_size);
/** Like append but automatically detect the size with the null char*/
void append_text(char const *new_text);
/** Replace the memory with the new data and size*/
void replace(char const *new_data, size_t size);
/** Reset all the value to 'value'. */
void reset(int value=0);
void free();
/** Replace the memory with the new data and size*/
void replace(char const *new_data, size_t size);
/** Reset all the value to 'value'. */
void reset(int value=0);
void fprint_graph_uchars(FILE *file, char const *title, int height, int max, int min, char const* abscissa_name=NULL, char const* ordinate_name = NULL);
#endif
}blc_mem;
START_EXTERN_C
///Useful only on plain C. C++ initializes in the constructor
#define BLC_MEM_INITIALIZER {.data=NULL, .size=0}
/// Allocate or reallocate memory as needed but lose the content.
void blc_mem_allocate(blc_mem *mem, size_t size);
......@@ -96,7 +101,10 @@ void blc_mem_reallocate(blc_mem *mem, size_t size);
void blc_mem_replace(blc_mem *mem, char const* data, size_t size);
/// Increase the size and append the data at the end.
void blc_mem_append(blc_mem *mem, char const* data, size_t size);
END_EXTERN_C
/// Draw graphs based and blc_mems data
void blc_mems_fprint_graph_uchars(blc_mem *const *mems, int mems_nb, FILE *file, char const *const* title, int height, int max, int min, char const* abscissa_name, char const* ordinate_name );
END_EXTERN_C
///@}
#endif
......
......@@ -190,6 +190,10 @@ void fprint_tsv_floats(FILE *file, float *values, int values_nb);
///Read tab separated values in the files and set values with the result.
void fscan_tsv_floats(FILE *file, float *values, int values_nb);
///Print a color scale usefull for fprint_graph
void blc_print_color_scale();
int fprint_fourcc(FILE *file, uint32_t value);
char* sprint_fourcc(char *string, uint32_t value);
......
......@@ -25,7 +25,13 @@
#include <signal.h>
#include <stdio.h> //fopen
blc_array::blc_array():dims(NULL), dims_nb(0){};
blc_array::blc_array():dims(NULL), dims_nb(0){}
blc_array::~blc_array(){
FREE(dims);
}
void blc_array::init(char const *properties){
sscan_properties(properties);
......@@ -79,10 +85,7 @@ void blc_array::def_array(uint32_t type, uint32_t format, char const *dims_strin
sscan_dims(dims_string);
}
void blc_array::destroy(){
free();
::free(dims);
}
void blc_array::add_dim(int length, int step){
blc_dim dim;
......@@ -419,13 +422,12 @@ void blc_array::fprint_tsv(FILE *file){
}
else dim--;
if (dim==dims_nb-2 )fprintf(file, "\n");
else fprintf(file, "\t");
}
}
fprintf(file, "\n");
::free(tmp_dims);
FREE(tmp_dims);
}
void blc_array::save_tsv_file(char const *filename){
......@@ -438,30 +440,55 @@ void blc_array::save_tsv_file(char const *filename){
if (strcmp(ext, "tsv")!=0) EXIT_ON_ERROR("'%s' has not tsv extension but '%s'", filename, ext);
SYSTEM_ERROR_CHECK(file=fopen(filename, "w"), NULL, "Opening filename '%s' for writing.", filename);
this->fprint_tsv(file);
fclose(file);
}
/* C wrapper */
extern "C" {
// Envoie un message d'erreur avec name_of_file, name_of_function, number_of_line et affiche le message formate avec les parametres variables. Puis exit le programme avec le parametre EXIT_FAILURE. To be used with EXIT_ON_ERROR.
void blc_array_fatal_error(blc_array const *array, const char *name_of_file, const char* name_of_function, int numero_of_line, const char *message, ...){
va_list arguments;
va_start(arguments, message);
fprintf(stderr, "\n%s: %s \t %s \t %i :\nError: ", blc_program_name, name_of_file, name_of_function, numero_of_line);
color_vfprintf(BLC_BRIGHT_RED, stderr, message, arguments);
va_end(arguments);
array->fprint_debug(stderr);
fprintf(stderr, "\n\n");
fflush(stderr);
raise(SIGABRT);
exit(EXIT_FAILURE);
}
void blc_array::fprint_surface_uchars(FILE *file){
uchar value;
int color_id;
int i, j, width, height;
void blc_array_destroy(blc_array *array){
array->destroy();
width= dims[0].length;
height=dims[1].length;
FOR(j, height)
{
FOR(i, width)
{
value = uchars[j*dims[1].step+i*dims[0].step]*100/256;
if ( value < 20) color_id = BLC_BRIGHT_BLUE;
else if ( value < 35 ) color_id = BLC_BLUE;
else if ( value < 45 ) color_id = BLC_GREY;
else if ( value < 55 ) color_id = BLC_WHITE;
else if ( value < 65 ) color_id = BLC_BRIGHT_WHITE;
else if ( value < 80 ) color_id = BLC_RED;
else color_id = BLC_BRIGHT_RED;
color_fprintf(color_id, file, "%2d ", value);
/// else fprintf(file, "%2d ", value);
}
fprintf(file, "\n");
}
}
/* C wrapper */
START_EXTERN_C
// Envoie un message d'erreur avec name_of_file, name_of_function, number_of_line et affiche le message formate avec les parametres variables. Puis exit le programme avec le parametre EXIT_FAILURE. To be used with EXIT_ON_ERROR.
void blc_array_fatal_error(blc_array const *array, const char *name_of_file, const char* name_of_function, int numero_of_line, const char *message, ...){
va_list arguments;
va_start(arguments, message);
fprintf(stderr, "\n%s: %s \t %s \t %i :\nError: ", blc_program_name, name_of_file, name_of_function, numero_of_line);
color_vfprintf(BLC_BRIGHT_RED, stderr, message, arguments);
va_end(arguments);
array->fprint_debug(stderr);
fprintf(stderr, "\n\n");
fflush(stderr);
raise(SIGABRT);
exit(EXIT_FAILURE);
}
void blc_array_destroy(blc_array *array){
array->~blc_array();
}
END_EXTERN_C
......@@ -23,32 +23,31 @@
blc_mem::blc_mem():data(NULL), size(0){};
blc_mem::blc_mem(size_t size)
: data(NULL), size(size)
{
: data(NULL), size(size){
data = MANY_ALLOCATIONS(size, char);
}
void blc_mem::allocate()
{
blc_mem::~blc_mem(){
FREE(data);
size=0;
}
void blc_mem::allocate(){
if (data) EXIT_ON_ERROR("mem already allocated");
else data = MANY_ALLOCATIONS(size, char);
}
void blc_mem::allocate(size_t new_size)
{
void blc_mem::allocate(size_t new_size){
if (new_size != size || data == NULL) // Changement de taille ou l'allocation n'a encore jamais été faite
{
::free(data);
free(data);
data = MANY_ALLOCATIONS(new_size, char);
size = new_size;
}
}
void blc_mem::allocate_min(size_t new_size)
{
void blc_mem::allocate_min(size_t new_size){
if (new_size > size){
::free(data);
data = MANY_ALLOCATIONS(new_size, char);
......@@ -56,8 +55,7 @@ void blc_mem::allocate_min(size_t new_size)
}
}
void blc_mem::reallocate(size_t new_size)
{
void blc_mem::reallocate(size_t new_size){
MANY_REALLOCATIONS(&chars, new_size);
size = new_size;
}
......@@ -80,37 +78,99 @@ void blc_mem::append_text(char const *new_text)
append(new_text, strlen(new_text));
}
void blc_mem::reset(int value){
memset(data, value, size);
}
void blc_mem::free(){
::free(data); //:: not calling itself but system call
data=NULL;
size=0;
void blc_mem::fprint_graph_uchars(FILE *file, char const *title, int height, int max, int min, char const* abscissa_name, char const* ordinate_name ){
blc_mem *this_mem=this; //This does not support &
blc_mems_fprint_graph_uchars(&this_mem, 1, file, &title, height, max, min, abscissa_name, ordinate_name);
}
/* C wrapper */
extern "C" {
void blc_mem_allocate(blc_mem *mem, size_t size)
{
mem->allocate(size);
}
START_EXTERN_C
void blc_mem_allocate(blc_mem *mem, size_t size){
mem->allocate(size);
}
void blc_mem_reallocate(blc_mem *mem, size_t new_size){
mem->reallocate(new_size);
}
void blc_mem_replace(blc_mem *mem, const char *data, size_t size){
mem->replace(data, size);
}
void blc_mem_append(blc_mem *mem, const char *data, size_t size){
mem->append(data, size);
}
void blc_mems_fprint_graph_uchars(blc_mem *const*mems, int mems_nb, FILE *file, char const * const *titles, int height, int max, int min, char const* abscissa_name, char const* ordinate_name ){
void blc_mem_reallocate(blc_mem *mem, size_t new_size)
{
mem->reallocate(new_size);
unsigned char value;
size_t i, total_size = 0;
int range, ordinate_name_length, ordinate_name_position;
int j, threhold, threhold1, graph_id;
char c;
char vertical_arrow[] = "^ ";
ordinate_name_position = - strlen(vertical_arrow);
if (ordinate_name != NULL) ordinate_name_length = strlen(ordinate_name);
else ordinate_name_length = 0;
height-=3;
range = max - min + 1;
fprintf(file, "\n");
FOR(graph_id, mems_nb){
for (i = fprintf(file, "100%%(%d) [%s] ", max, titles[graph_id]); i < mems[graph_id]->size * 3; ++i) fputc('-', file);
total_size+=mems[graph_id]->size;
fputc('|', file);
}
fprintf(file, "\n");
void blc_mem_replace(blc_mem *mem, const char *data, size_t size)
{
mem->replace(data, size);
FOR_INV(j, height){
threhold = j * range / height;
threhold1 = (j + 1) * range / height;
if (ordinate_name_position < 0)
{
c = vertical_arrow[strlen(vertical_arrow) + ordinate_name_position];
ordinate_name_position++;
}
else if (ordinate_name_position != ordinate_name_length)
{
c = ordinate_name[ordinate_name_position];
ordinate_name_position++;
}
else c = '|';
FOR(graph_id, mems_nb)
{
fputc(c, file);
FOR(i, mems[graph_id]->size)
{
value = mems[graph_id]->chars[i];
if (value - min > threhold1) fprintf(file, " []");
else if (value - min >= threhold) {
fprintf(file, " %.2d", 100 * (value - min) / range);
}
else fprintf(file, " ");
}
}
fprintf(file, "\n");
}
void blc_mem_append(blc_mem *mem, const char *data, size_t size)
{
mem->append(data, size);
FOR(graph_id, mems_nb){
for (i = fprintf(file, "0%%(%d) %s ", min, abscissa_name); i < mems[graph_id]->size * 3; ++i)
fputc('-', file);
fputc('>', file);
}
fprintf(file, "\n");
}
END_EXTERN_C
......@@ -274,4 +274,10 @@ void fscan_tsv_floats(FILE *file, float *values, int values_nb)
if (fscanf(file, "\n")!=0) EXIT_ON_ERROR("fscanf");
}
void blc_print_color_scale(){
int i;
FOR(i, BLC_BAR_COLORS_NB-1) color_printf(blc_bar_colors[i],"%d < ", i*256/BLC_BAR_COLORS_NB);
color_printf(blc_bar_colors[i],"%d\n", i*256/BLC_BAR_COLORS_NB);
}
END_EXTERN_C
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment