| 1 | /* -------------------- */ | 
|---|
| 2 | /* --- matrix_dist.c -- */ | 
|---|
| 3 | /* -------------------- */ | 
|---|
| 4 |  | 
|---|
| 5 | // Copyright (c) 2015-2016 Lionel Lacassagne, All Rights Reserved | 
|---|
| 6 | // Laboratoire d'Informatique de Paris 6 - LIP6 | 
|---|
| 7 | // Universite Pierre et Marie Curie UPMC / CNRS | 
|---|
| 8 |  | 
|---|
| 9 |  | 
|---|
| 10 | #include <stdio.h> | 
|---|
| 11 | #include <stdbool.h> // C99 | 
|---|
| 12 | #include <stdlib.h> | 
|---|
| 13 | #include <string.h> | 
|---|
| 14 | #include <malloc.h> | 
|---|
| 15 |  | 
|---|
| 16 | #ifdef CLI | 
|---|
| 17 | #include "nrc_os_config.h" | 
|---|
| 18 | #include "nrc.h" | 
|---|
| 19 | #endif | 
|---|
| 20 |  | 
|---|
| 21 |  | 
|---|
| 22 | #include "mca_matrix_dist.h" | 
|---|
| 23 |  | 
|---|
| 24 | // ================================================================== | 
|---|
| 25 | // === fonctions allocation memoire ================================= | 
|---|
| 26 | // ================================================================== | 
|---|
| 27 |  | 
|---|
| 28 | // ------------------------------------------------------ | 
|---|
| 29 | uint32 ** dist_ui32matrix(int i0, int i1, int j0, int j1) | 
|---|
| 30 | // ------------------------------------------------------ | 
|---|
| 31 | { | 
|---|
| 32 | int i; | 
|---|
| 33 | int nrow = i1 - i0 + 1; | 
|---|
| 34 | int ncol = j1 - j0 + 1; | 
|---|
| 35 | uint32 ** m; | 
|---|
| 36 |  | 
|---|
| 37 | // allocate pointers to rows | 
|---|
| 38 | m = (uint32 **) malloc((nrow + 2) * sizeof(uint32 *)); | 
|---|
| 39 | if (!m) { | 
|---|
| 40 | nrerror("allocation failure 1 in dist_ui32matrix()"); | 
|---|
| 41 | } | 
|---|
| 42 | m -= i0; | 
|---|
| 43 | m += 1; | 
|---|
| 44 |  | 
|---|
| 45 | // allocate rows and set pointers to them | 
|---|
| 46 | m[i0] = (uint32 *) malloc((nrow * ncol + 1) * sizeof(uint32)); | 
|---|
| 47 | if (!m[i0]) { | 
|---|
| 48 | nrerror("allocation failure 2 in dist_ui32matrix()"); | 
|---|
| 49 | } | 
|---|
| 50 | m[i0] -= j0; | 
|---|
| 51 |  | 
|---|
| 52 | for (i = i0 + 1; i <= i1; i++) { | 
|---|
| 53 | m[i] = m[i - 1] + ncol; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | // make borders to point to first and last lines | 
|---|
| 57 | m[i0 - 1] = m[i0]; | 
|---|
| 58 | m[i1 + 1] = m[i1]; | 
|---|
| 59 |  | 
|---|
| 60 | return m; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | // ------------------------------------------------------------------- | 
|---|
| 65 | void free_dist_ui32matrix(uint32 ** m, int i0, int i1, int j0, int j1) | 
|---|
| 66 | // ------------------------------------------------------------------- | 
|---|
| 67 | { | 
|---|
| 68 | free((FREE_ARG) (m[i0] + j0)); | 
|---|
| 69 | free((FREE_ARG) (m + i0 - 1)); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|