Debian Codegraph @

matio-1.5.0/src/matvar_cell.c

raw
1 /*
2 * Copyright (C) 2012 Christopher C. Hulbert
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY CHRISTOPHER C. HULBERT ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL CHRISTOPHER C. HULBERT OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28 #include <stdlib.h>
29 #include <string.h>
30 #include "matio_private.h"
31
32 /** @brief Returns a pointer to the Cell array at a specific index
33 *
34 * Returns a pointer to the Cell Array Field at the given 1-relative index.
35 * MAT file must be a version 5 matlab file.
36 * @ingroup MAT
37 * @param matvar Pointer to the Cell Array MAT variable
38 * @param index linear index of cell to return
39 * @return Pointer to the Cell Array Field on success, NULL on error
40 */
41 matvar_t *
42 Mat_VarGetCell(matvar_t *matvar,int index)
43 {
44 int nmemb = 1, i;
45 matvar_t *cell = NULL;
46
47 if ( matvar == NULL )
48 return NULL;
49
50 for ( i = 0; i < matvar->rank; i++ )
51 nmemb *= matvar->dims[i];
52
53 if ( index < nmemb )
54 cell = *((matvar_t **)matvar->data + index);
55
56 return cell;
57 }
58
59 /** @brief Indexes a cell array
60 *
61 * Finds cells of a cell array given a start, stride, and edge for each.
62 * dimension. The cells are placed in a pointer array. The cells should not
63 * be freed, but the array of pointers should be. If copies are needed,
64 * use Mat_VarDuplicate on each cell.
65 *
66 * Note that this function is limited to structure arrays with a rank less than
67 * 10.
68 *
69 * @ingroup MAT
70 * @param matvar Cell Array matlab variable
71 * @param start vector of length rank with 0-relative starting coordinates for
72 * each diemnsion.
73 * @param stride vector of length rank with strides for each diemnsion.
74 * @param edge vector of length rank with the number of elements to read in
75 * each diemnsion.
76 * @returns an array of pointers to the cells
77 */
78 matvar_t **
79 Mat_VarGetCells(matvar_t *matvar,int *start,int *stride,int *edge)
80 {
81 int i, j, N, I = 0;
82 size_t idx[10] = {0,}, cnt[10] = {0,}, dimp[10] = {0,};
83 matvar_t **cells;
84
85 if ( (matvar == NULL) || (start == NULL) || (stride == NULL) ||
86 (edge == NULL) ) {
87 return NULL;
88 } else if ( matvar->rank > 9 ) {
89 return NULL;
90 }
91
92 dimp[0] = matvar->dims[0];
93 N = edge[0];
94 I = start[0];
95 idx[0] = start[0];
96 for ( i = 1; i < matvar->rank; i++ ) {
97 idx[i] = start[i];
98 dimp[i] = dimp[i-1]*matvar->dims[i];
99 N *= edge[i];
100 I += start[i]*dimp[i-1];
101 }
102 cells = malloc(N*sizeof(matvar_t *));
103 for ( i = 0; i < N; i+=edge[0] ) {
104 for ( j = 0; j < edge[0]; j++ ) {
105 cells[i+j] = *((matvar_t **)matvar->data + I);
106 I += stride[0];
107 }
108 idx[0] = start[0];
109 I = idx[0];
110 cnt[1]++;
111 idx[1] += stride[1];
112 for ( j = 1; j < matvar->rank; j++ ) {
113 if ( cnt[j] == edge[j] ) {
114 cnt[j] = 0;
115 idx[j] = start[j];
116 cnt[j+1]++;
117 idx[j+1] += stride[j+1];
118 }
119 I += idx[j]*dimp[j-1];
120 }
121 }
122 return cells;
123 }
124
125 /** @brief Indexes a cell array
126 *
127 * Finds cells of a cell array given a linear indexed start, stride, and edge.
128 * The cells are placed in a pointer array. The cells themself should not
129 * be freed as they are part of the original cell array, but the pointer array
130 * should be. If copies are needed, use Mat_VarDuplicate on each of the cells.
131 * MAT file version must be 5.
132 * @ingroup MAT
133 * @param matvar Cell Array matlab variable
134 * @param start starting index
135 * @param stride stride
136 * @param edge Number of cells to get
137 * @returns an array of pointers to the cells
138 */
139 matvar_t **
140 Mat_VarGetCellsLinear(matvar_t *matvar,int start,int stride,int edge)
141 {
142 int i, I = 0;
143 matvar_t **cells = NULL;
144
145 if ( matvar != NULL ) {
146 cells = malloc(edge*sizeof(matvar_t *));
147 I = start;
148 for ( i = 0; i < edge; i++ ) {
149 cells[i] = *((matvar_t **)matvar->data + I);
150 I += stride;
151 }
152 }
153 return cells;
154 }
155
156 /** @brief Sets the element of the cell array at the specific index
157 *
158 * Sets the element of the cell array at the given 0-relative index to @c cell.
159 * @ingroup MAT
160 * @param matvar Pointer to the cell array variable
161 * @param index 0-relative linear index of the cell to set
162 * @return Pointer to the previous cell element, or NULL if there was no
163 * previous cell element or error.
164 */
165 matvar_t *
166 Mat_VarSetCell(matvar_t *matvar,int index,matvar_t *cell)
167 {
168 int nmemb = 1, i;
169 matvar_t **cells, *old_cell = NULL;
170
171 if ( matvar == NULL || matvar->rank < 1 )
172 return NULL;
173
174 for ( i = 0; i < matvar->rank; i++ )
175 nmemb *= matvar->dims[i];
176
177 cells = matvar->data;
178 if ( index < nmemb ) {
179 old_cell = cells[index];
180 cells[index] = cell;
181 }
182
183 return old_cell;
184 }