blc_array.cpp 7.29 KB
Newer Older
Arnaud Blanchard's avatar
Arnaud Blanchard committed
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
/* 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 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. */

//
//  Created by Arnaud Blanchard on 17/06/2014.
//
//

#include "blc_array.h"
#include "blc_text.h"

#include <signal.h>

blc_array::blc_array():dims(NULL), dims_nb(0){};

void blc_array::vdef_array(uint32_t type, uint32_t format, int dims_nb, int length, va_list arguments){
    blc_dim *dim;
    

        this->dims_nb=dims_nb;
        this->type=type;
        this->format=format;
        dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
        
        size=get_type_size();
        
        FOR_EACH(dim, dims, dims_nb)
        {
            dim->step=size;
            size*=length;
            dim->length=length;
            length=va_arg(arguments, int);
        }
}

void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, int length, ...){
    va_list arguments;
    
    va_start(arguments, length);
    vdef_array(type, format, dims_nb, length, arguments);
    va_end(arguments);
    
}

void blc_array::def_array(uint32_t type, uint32_t format, int dims_nb, blc_dim *dims){

     this->type=type;
     this->format=format;
     this->dims_nb=dims_nb;

     size=get_type_size();
     this->dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
     memcpy(this->dims, dims, sizeof(blc_dim)*dims_nb);

     size =dims[dims_nb-1].length*dims[dims_nb-1].step;
}

71
72
73
74
75
76
void blc_array::def_array(uint32_t type, uint32_t format, char const *dims_string){
    this->type=type;
    this->format=format;
    sscan_dims(dims_string);
}

Arnaud Blanchard's avatar
Arnaud Blanchard committed
77
void blc_array::destroy(){
78
79
    free();
    ::free(dims);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
}

void blc_array::add_dim(int length, int step){
    blc_dim dim;
    
    dim.length=length;
    dim.step=step;
    
    APPEND_ITEM(&dims, &dims_nb, &dim);
    size=step*length; //Check if it always true ??
}

void blc_array::add_dim(int length){
    if (dims_nb==0) add_dim(length, get_type_size());
    else add_dim(length, dims[dims_nb-1].length*dims[dims_nb-1].step);
}

void blc_array::set_dims(int dims_nb, int length, ...){
    blc_dim *dim;
    va_list args;

101
    ::free(dims);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
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

    this->dims_nb=dims_nb;
    dims=MANY_ALLOCATIONS(dims_nb, blc_dim);
    
    size=get_type_size();
    va_start(args, length);
    FOR_EACH(dim, dims,  dims_nb)
    {
        size*=length;
        dim->step=1;
        dim->length=length;
        length=va_arg(args, int);
    }
    va_end(args);
}

int blc_array::get_type_size(){
    switch (type){
        case 'INT8': case 'UIN8': case 'TEXT': return 1;
        case 'UI16':case 'IN16':  return 2;
        case 'FL32':case 'UI32':case 'IN32': return 4;
        case 'FL64': case'UI64':case 'IN64': return 8;
        default: EXIT_ON_ARRAY_ERROR(this, "The type is unknown.");
    }
    return -1;
}

int blc_array::sprint_dims(char *string, int string_size){
    int i, width=0;
    
    if (dims_nb==0)  width+=snprintf(string, string_size,  "0");
    else {
Arnaud Blanchard's avatar
Arnaud Blanchard committed
134
135
        width+=sprintf(string, "%lu", dims[0].length);
        for(i=1; i<dims_nb; i++) width+=sprintf(string+width, "x%lu", dims[i].length);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
136
137
138
139
140
141
142
143
144
145
146
    }
    if (width >= string_size) EXIT_ON_ERROR("The reserved size %d is too small to store the %d dims.",size, dims_nb);
    return width;
}


int blc_array::fprint_dims(FILE *file)const {
    int i, width=0;
    
    if (dims_nb==0)  width+=fprintf(file, "0");
    else {
Arnaud Blanchard's avatar
Arnaud Blanchard committed
147
148
        width+=fprintf(file, "%lu", dims[0].length);
        for(i=1; i<dims_nb; i++) width+=fprintf(file, "x%lu", dims[i].length);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
    }
    return width;
}


void blc_array::fscan_dims(FILE *file){
    blc_dim *dim;
    int length;
    
    size=get_type_size();
    
    FSCANF(1, file, "%d", &length);
    dims=NULL;
    dims_nb=0;
    if (length != 0){
        do{
           dim=APPEND_ALLOCATION(&dims, &dims_nb, blc_dim);
           dim->length=length;
           dim->step=size;
           size*=dim->length;
        }while(fscanf(file, "x%d", &length)==1);
    }
}

void blc_array::sscan_dims(char const *string){
    blc_dim *dim;
    int pos;
    int length;

    size=get_type_size();
    
    SSCANF(1, string, "%d%n", &length, &pos);
    
    dims_nb=0;
    if (length != 0){
        do{
            string+=pos;
            dim=APPEND_ALLOCATION(&dims, &dims_nb, blc_dim);
            dim->length=length;
            dim->step=size;
            size*=dim->length;
Arnaud Blanchard's avatar
Arnaud Blanchard committed
190
        }while(sscanf(string, "x%lu%n", &dim->length, &pos)==1);
Arnaud Blanchard's avatar
Arnaud Blanchard committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
    }
}

//usefull ??
size_t blc_array::get_minimum_size(){
    blc_dim *dim;
    size_t size = get_type_size();
    
    FOR_EACH(dim, dims, dims_nb) {
        if (dim->length==0) EXIT_ON_ERROR("Length must not be 0 on dim '%d'");
        size*=dim->length;
    }
    return size;
}

void blc_array::fprint_debug (FILE *file) const{
    uint32_t type_str, format_str;

    fprintf(file, "\nblc_array:\n type:%.4s, format:%.4s, dims_nb:%d, size:%ld \n", UINT32_TO_STRING(type_str, type), UINT32_TO_STRING(format_str, format),  dims_nb, size);
    fprint_dims(file);
    fprintf(file, "\n");
    if (data==NULL) fprintf(file, "data is null\n");
}


/* 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_destroy(blc_array *array){
  array->destroy();
}
}