[772] | 1 | /* ---------------- */ |
---|
| 2 | /* --- nrio2x.c --- */ |
---|
| 3 | /* ---------------- */ |
---|
| 4 | |
---|
| 5 | /* |
---|
| 6 | * Copyright (c) 2000-2014, Lionel Lacassagne, All rights reserved |
---|
| 7 | * Univ Paris Sud XI, CNRS |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | #include <stdio.h> |
---|
| 11 | #include <stddef.h> |
---|
| 12 | #include <stdlib.h> |
---|
| 13 | |
---|
| 14 | #include "mypredef.h" |
---|
| 15 | #include "nrtype.h" |
---|
| 16 | #include "nrdef.h" |
---|
| 17 | #include "nrmacro.h" |
---|
| 18 | #include "nrkernel.h" |
---|
| 19 | |
---|
| 20 | #include "nrio0.h" |
---|
| 21 | #include "nralloc1.h" |
---|
| 22 | #include "nralloc2.h" |
---|
| 23 | #include "nrio1.h" |
---|
| 24 | #include "nrio2.h" |
---|
| 25 | #include "nrio2x.h" |
---|
| 26 | |
---|
| 27 | /* ----------------------- */ |
---|
| 28 | /* --- write_trimatrix --- */ |
---|
| 29 | /* ----------------------- */ |
---|
| 30 | |
---|
[823] | 31 | #undef write_type_trimatrix |
---|
| 32 | #define write_type_trimatrix(t) \ |
---|
| 33 | void short_name(t,write_,trimatrix)(t ** m, int32_t i0, int32_t i1, int32_t j0, int32_t j1, int32_t step, char * format, char * filename) \ |
---|
| 34 | { \ |
---|
| 35 | FILE * f = fopen(filename, "wt"); \ |
---|
| 36 | if (f == NULL) { \ |
---|
| 37 | nrerror("Can't open file %s in %s", filename, __func__); \ |
---|
| 38 | } \ |
---|
| 39 | for (int32_t i = i0; i <= i1; i++) { \ |
---|
| 40 | for (int32_t j = j0; j <= j1; j++) { \ |
---|
| 41 | fprintf(f, format, m[i][j]); \ |
---|
| 42 | } \ |
---|
| 43 | fprintf(f, "\n"); \ |
---|
| 44 | j1 += step; \ |
---|
| 45 | } \ |
---|
| 46 | fprintf(f, "\n"); \ |
---|
| 47 | fclose(f); \ |
---|
[772] | 48 | } |
---|
| 49 | |
---|
[823] | 50 | write_type_trimatrix(int8_t); |
---|
| 51 | write_type_trimatrix(uint8_t); |
---|
| 52 | write_type_trimatrix(int16_t); |
---|
| 53 | write_type_trimatrix(uint16_t); |
---|
| 54 | write_type_trimatrix(int32_t); |
---|
| 55 | write_type_trimatrix(uint32_t); |
---|
| 56 | write_type_trimatrix(int64_t); |
---|
| 57 | write_type_trimatrix(uint64_t); |
---|
| 58 | write_type_trimatrix(float); |
---|
| 59 | write_type_trimatrix(double); |
---|
[772] | 60 | |
---|
| 61 | |
---|
| 62 | /* ---------------------------- */ |
---|
| 63 | /* --- write_matrix_endline --- */ |
---|
| 64 | /* ---------------------------- */ |
---|
| 65 | |
---|
[823] | 66 | #undef write_type_matrix_endline |
---|
| 67 | #define write_type_matrix_endline(t) \ |
---|
| 68 | void short_name(t,write_,matrix_endline)(t ** m, int32_t i0, int32_t i1, int32_t j0, int32_t j1, char * format, char * filename) \ |
---|
| 69 | { \ |
---|
| 70 | t * p = &m[i0][j0]; \ |
---|
| 71 | FILE * f = fopen(filename, "wt"); \ |
---|
| 72 | if (f == NULL) { \ |
---|
| 73 | nrerror("Can't open file %s in %s", filename, __func__); \ |
---|
| 74 | } \ |
---|
| 75 | for (int32_t i = i0; i <= i1; i++) { \ |
---|
| 76 | for (int32_t j = j0; j <= j1; j++) { \ |
---|
| 77 | fprintf(f, format, *p++); \ |
---|
| 78 | } \ |
---|
| 79 | fprintf(f, "\n"); \ |
---|
| 80 | } \ |
---|
| 81 | fprintf(f, "\n"); \ |
---|
| 82 | fclose(f); \ |
---|
[772] | 83 | } |
---|
| 84 | |
---|
[823] | 85 | write_type_matrix_endline(int8_t); |
---|
| 86 | write_type_matrix_endline(uint8_t); |
---|
| 87 | write_type_matrix_endline(int16_t); |
---|
| 88 | write_type_matrix_endline(uint16_t); |
---|
| 89 | write_type_matrix_endline(int32_t); |
---|
| 90 | write_type_matrix_endline(uint32_t); |
---|
| 91 | write_type_matrix_endline(int64_t); |
---|
| 92 | write_type_matrix_endline(uint64_t); |
---|
| 93 | write_type_matrix_endline(float); |
---|
| 94 | write_type_matrix_endline(double); |
---|
[772] | 95 | |
---|
| 96 | |
---|
| 97 | /* ------------------------------ */ |
---|
| 98 | /* --- write_imatrix_endline0 --- */ |
---|
| 99 | /* ------------------------------ */ |
---|
| 100 | |
---|
[823] | 101 | #undef write_type_matrix_endline0 |
---|
| 102 | #define write_type_matrix_endline0(t) \ |
---|
| 103 | void short_name(t,write_,matrix_endline0)(t ** m, int32_t i0, int32_t i1, int32_t j0, int32_t j1, char * format, char * filename) \ |
---|
| 104 | { \ |
---|
| 105 | FILE * f = fopen(filename, "wt"); \ |
---|
| 106 | if (f == NULL) { \ |
---|
| 107 | nrerror("Can't open file %s in %s", filename, __func__); \ |
---|
| 108 | } \ |
---|
| 109 | for (int32_t i = i0; i <= i1; i++) { \ |
---|
| 110 | int32_t n = (int32_t) m[i][j0]; \ |
---|
| 111 | for (int32_t j = j0; j <= j0 + n; j++) { \ |
---|
| 112 | fprintf(f, format, m[i][j]); \ |
---|
| 113 | } \ |
---|
| 114 | fprintf(f, "\n"); \ |
---|
| 115 | } \ |
---|
| 116 | fprintf(f, "\n"); \ |
---|
| 117 | fclose(f); \ |
---|
[772] | 118 | } |
---|
| 119 | |
---|
[823] | 120 | write_type_matrix_endline0(int8_t); |
---|
| 121 | write_type_matrix_endline0(uint8_t); |
---|
| 122 | write_type_matrix_endline0(int16_t); |
---|
| 123 | write_type_matrix_endline0(uint16_t); |
---|
| 124 | write_type_matrix_endline0(int32_t); |
---|
| 125 | write_type_matrix_endline0(uint32_t); |
---|
| 126 | write_type_matrix_endline0(int64_t); |
---|
| 127 | write_type_matrix_endline0(uint64_t); |
---|
| 128 | write_type_matrix_endline0(float); |
---|
| 129 | write_type_matrix_endline0(double); |
---|
[772] | 130 | |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | /* ------------------------------ */ |
---|
| 134 | /* --- write_imatrix_endline1 --- */ |
---|
| 135 | /* ------------------------------ */ |
---|
| 136 | |
---|
[823] | 137 | #undef write_type_matrix_endline1 |
---|
| 138 | #define write_type_matrix_endline1(t) \ |
---|
| 139 | void short_name(t,write_,matrix_endline1)(t ** m, int32_t i0, int32_t i1, int32_t j0, int32_t j1, char * format, char * filename) \ |
---|
| 140 | { \ |
---|
| 141 | FILE * f = fopen(filename, "wt"); \ |
---|
| 142 | if (f == NULL) { \ |
---|
| 143 | nrerror("Can't open file %s in %s", filename, __func__); \ |
---|
| 144 | } \ |
---|
| 145 | for (int32_t i = i0; i <= i1; i++) { \ |
---|
| 146 | int32_t n = (int32_t) m[i][j0 - 1]; \ |
---|
| 147 | for (int32_t j = j0; j <= j0 + n - 1; j++) { \ |
---|
| 148 | fprintf(f, format, m[i][j]); \ |
---|
| 149 | } \ |
---|
| 150 | fprintf(f, "\n"); \ |
---|
| 151 | } \ |
---|
| 152 | fprintf(f, "\n"); \ |
---|
| 153 | fclose(f); \ |
---|
[772] | 154 | } |
---|
| 155 | |
---|
[823] | 156 | write_type_matrix_endline1(int8_t); |
---|
| 157 | write_type_matrix_endline1(uint8_t); |
---|
| 158 | write_type_matrix_endline1(int16_t); |
---|
| 159 | write_type_matrix_endline1(uint16_t); |
---|
| 160 | write_type_matrix_endline1(int32_t); |
---|
| 161 | write_type_matrix_endline1(uint32_t); |
---|
| 162 | write_type_matrix_endline1(int64_t); |
---|
| 163 | write_type_matrix_endline1(uint64_t); |
---|
| 164 | write_type_matrix_endline1(float); |
---|
| 165 | write_type_matrix_endline1(double); |
---|
[772] | 166 | |
---|
| 167 | |
---|
| 168 | /* ------------------------------------- */ |
---|
| 169 | /* --- write_imatrix_number_endline0 --- */ |
---|
| 170 | /* ------------------------------------- */ |
---|
| 171 | |
---|
[823] | 172 | #undef write_type_matrix_number_endline0 |
---|
| 173 | #define write_type_matrix_number_endline0(t) \ |
---|
| 174 | void short_name(t,write_,matrix_number_endline0)(t ** m, int32_t i0, int32_t i1, int32_t j0, int32_t j1, char * format, char * filename) \ |
---|
| 175 | { \ |
---|
| 176 | FILE * f = fopen(filename, "wt"); \ |
---|
| 177 | if (f == NULL) { \ |
---|
| 178 | nrerror("Can't open file %s in %s", filename, __func__); \ |
---|
| 179 | } \ |
---|
| 180 | for (int32_t i = i0; i <= i1; i++) { \ |
---|
| 181 | fprintf(f, "[%3d] ", i); \ |
---|
| 182 | int32_t n = (int32_t) m[i][j0]; \ |
---|
| 183 | for (int32_t j = j0; j <= j0 + n; j++) { \ |
---|
| 184 | fprintf(f, format, m[i][j]); \ |
---|
| 185 | } \ |
---|
| 186 | fprintf(f, "\n"); \ |
---|
| 187 | } \ |
---|
| 188 | fprintf(f, "\n"); \ |
---|
| 189 | fclose(f); \ |
---|
[772] | 190 | } |
---|
| 191 | |
---|
[823] | 192 | write_type_matrix_number_endline0(int8_t); |
---|
| 193 | write_type_matrix_number_endline0(uint8_t); |
---|
| 194 | write_type_matrix_number_endline0(int16_t); |
---|
| 195 | write_type_matrix_number_endline0(uint16_t); |
---|
| 196 | write_type_matrix_number_endline0(int32_t); |
---|
| 197 | write_type_matrix_number_endline0(uint32_t); |
---|
| 198 | write_type_matrix_number_endline0(int64_t); |
---|
| 199 | write_type_matrix_number_endline0(uint64_t); |
---|
| 200 | write_type_matrix_number_endline0(float); |
---|
| 201 | write_type_matrix_number_endline0(double); |
---|
[772] | 202 | |
---|
| 203 | |
---|
| 204 | /* ------------------------------------- */ |
---|
| 205 | /* --- write_imatrix_number_endline1 --- */ |
---|
| 206 | /* ------------------------------------- */ |
---|
| 207 | |
---|
[823] | 208 | #undef write_type_matrix_number_endline1 |
---|
| 209 | #define write_type_matrix_number_endline1(t) \ |
---|
| 210 | void short_name(t,write_,matrix_number_endline1)(t ** m, int32_t i0, int32_t i1, int32_t j0, int32_t j1, char * format, char * filename) \ |
---|
| 211 | { \ |
---|
| 212 | FILE * f = fopen(filename, "wt"); \ |
---|
| 213 | if (f == NULL) { \ |
---|
| 214 | nrerror("Can't open file %s in %s", filename, __func__); \ |
---|
| 215 | } \ |
---|
| 216 | for (int32_t i = i0; i <= i1; i++) { \ |
---|
| 217 | fprintf(f, "[%3d] ", i); \ |
---|
| 218 | int32_t n = (int32_t) m[i][j0 - 1]; \ |
---|
| 219 | for (int32_t j = j0; j <= j0 + n - 1; j++) { \ |
---|
| 220 | fprintf(f, format, m[i][j]); \ |
---|
| 221 | } \ |
---|
| 222 | fprintf(f, "\n"); \ |
---|
| 223 | } \ |
---|
| 224 | fprintf(f, "\n"); \ |
---|
| 225 | fclose(f); \ |
---|
[772] | 226 | } |
---|
| 227 | |
---|
[823] | 228 | write_type_matrix_number_endline1(int8_t); |
---|
| 229 | write_type_matrix_number_endline1(uint8_t); |
---|
| 230 | write_type_matrix_number_endline1(int16_t); |
---|
| 231 | write_type_matrix_number_endline1(uint16_t); |
---|
| 232 | write_type_matrix_number_endline1(int32_t); |
---|
| 233 | write_type_matrix_number_endline1(uint32_t); |
---|
| 234 | write_type_matrix_number_endline1(int64_t); |
---|
| 235 | write_type_matrix_number_endline1(uint64_t); |
---|
| 236 | write_type_matrix_number_endline1(float); |
---|
| 237 | write_type_matrix_number_endline1(double); |
---|
[772] | 238 | |
---|
| 239 | |
---|
| 240 | /* ---------------------- */ |
---|
| 241 | /* -- fwrite_trimatrix -- */ |
---|
| 242 | /* ---------------------- */ |
---|
| 243 | |
---|
[823] | 244 | #undef fwrite_type_trimatrix |
---|
| 245 | #define fwrite_type_trimatrix(t) \ |
---|
| 246 | void short_name(t,fwrite_,trimatrix)(t ** m, int32_t i0, int32_t i1, int32_t j0, int32_t j1, int32_t step, char * filename) \ |
---|
| 247 | { \ |
---|
| 248 | int32_t ncol = j1 - j0 + 1; \ |
---|
| 249 | FILE * f = fopen(filename, "wb"); \ |
---|
| 250 | if (f == NULL) { \ |
---|
| 251 | nrerror("Can't open file %s in %s", filename, __func__); \ |
---|
| 252 | } \ |
---|
| 253 | for (int32_t i = i0; i <= i1; i++) { \ |
---|
| 254 | fwrite(m[i] + j0, sizeof(t), ncol, f); \ |
---|
| 255 | ncol += step; \ |
---|
| 256 | } \ |
---|
| 257 | fclose(f); \ |
---|
[772] | 258 | } |
---|
| 259 | |
---|
[823] | 260 | fwrite_type_trimatrix(int8_t); |
---|
| 261 | fwrite_type_trimatrix(uint8_t); |
---|
| 262 | fwrite_type_trimatrix(int16_t); |
---|
| 263 | fwrite_type_trimatrix(uint16_t); |
---|
| 264 | fwrite_type_trimatrix(int32_t); |
---|
| 265 | fwrite_type_trimatrix(uint32_t); |
---|
| 266 | fwrite_type_trimatrix(int64_t); |
---|
| 267 | fwrite_type_trimatrix(uint64_t); |
---|
| 268 | fwrite_type_trimatrix(float); |
---|
| 269 | fwrite_type_trimatrix(double); |
---|
[772] | 270 | |
---|
| 271 | |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | /* --------------------- */ |
---|
| 275 | /* -- fread_trimatrix -- */ |
---|
| 276 | /* --------------------- */ |
---|
| 277 | |
---|
[823] | 278 | #undef fread_type_trimatrix |
---|
| 279 | #define fread_type_trimatrix(t) \ |
---|
| 280 | void short_name(t,fread_,trimatrix)(char * filename, t ** m, int32_t i0, int32_t i1, int32_t j0, int32_t j1, int32_t step) \ |
---|
| 281 | { \ |
---|
| 282 | int32_t ncol = j1 - j0 + 1; \ |
---|
| 283 | FILE * f = fopen(filename, "rb"); \ |
---|
| 284 | if (f == NULL) { \ |
---|
| 285 | nrerror("Can't open file %s in %s", filename, __func__); \ |
---|
| 286 | } \ |
---|
| 287 | for (int32_t i = i0; i <= i1; i++) { \ |
---|
| 288 | fread(m[i] + j0, sizeof(t), ncol, f); \ |
---|
| 289 | ncol += step; \ |
---|
| 290 | } \ |
---|
| 291 | fclose(f); \ |
---|
[772] | 292 | } |
---|
| 293 | |
---|
[823] | 294 | fread_type_trimatrix(int8_t); |
---|
| 295 | fread_type_trimatrix(uint8_t); |
---|
| 296 | fread_type_trimatrix(int16_t); |
---|
| 297 | fread_type_trimatrix(uint16_t); |
---|
| 298 | fread_type_trimatrix(int32_t); |
---|
| 299 | fread_type_trimatrix(uint32_t); |
---|
| 300 | fread_type_trimatrix(int64_t); |
---|
| 301 | fread_type_trimatrix(uint64_t); |
---|
| 302 | fread_type_trimatrix(float); |
---|
| 303 | fread_type_trimatrix(double); |
---|
[772] | 304 | |
---|
| 305 | |
---|
[823] | 306 | // Local Variables: |
---|
| 307 | // tab-width: 4 |
---|
| 308 | // c-basic-offset: 4 |
---|
| 309 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 310 | // indent-tabs-mode: nil |
---|
| 311 | // End: |
---|
[772] | 312 | |
---|
[823] | 313 | // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
[772] | 314 | |
---|