Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[ad9030]: src / cell2fields.cc Maximize Restore History

Download this file

cell2fields.cc    159 lines (116 with data), 3.8 kB

  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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright (C) 2010 Olaf Till
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <octave/oct.h>
#include <octave/ov-struct.h>
DEFUN_DLD (cell2fields, args, ,
"-*- texinfo -*-\n\
@deftypefn {Loadable Function} {} cell2fields (@var{c}, @var{fields}, @var{dim}, @var{s})\n\
Return structure-array @var{s} after setting the fields @var{fields} with the slices of cell-array @var{c} vertically to dimension @var{dim}. @var{s} must have matching dimensions or be empty.\n\
@end deftypefn")
{
std::string fname ("cell2fields");
if (args.length () != 4)
{
print_usage ();
return octave_value_list ();
}
Cell c = args(0).cell_value ();
if (error_state)
{
error ("%s: first argument must be a cell array", fname.c_str ());
return octave_value_list ();
}
Array<std::string> names = args(1).cellstr_value ();
if (error_state)
{
error ("%s: second argument must be a cell array of strings", fname.c_str ());
return octave_value_list ();
}
// do not reject if ndims == [0, 0]
dim_vector ndims = names.dims ();
if (ndims.length () > 2 || (ndims(0) > 1 && ndims(1) > 1))
{
error ("%s: second argument must be a one-dimensional cell array",
fname.c_str ());
return octave_value_list ();
}
octave_idx_type dim = args(2).int_value ();
if (error_state)
{
error ("%s: third argument must be an integer",
fname.c_str ());
return octave_value_list ();
}
octave_map s = args(3).map_value ();
if (error_state)
{
error ("%s: fourth argument must be a structure", fname.c_str ());
return octave_value_list ();
}
octave_idx_type i, j;
dim_vector cdims (c.dims ());
octave_idx_type n_cdims = cdims.length ();
dim_vector tdims (cdims);
octave_idx_type nr = 1;
if (n_cdims >= dim)
{
nr = cdims(dim - 1);
tdims(dim - 1) = 1;
tdims.chop_trailing_singletons ();
}
if (nr != names.numel ())
{
error ("%s: second argument has incorrect length", fname.c_str ());
return octave_value_list ();
}
octave_map retval;
if (s.keys (). length () == 0)
retval.resize (tdims);
else
{
if (s.dims () != tdims)
{
error ("%s: structure has incorrect dimensions", fname.c_str ());
return octave_value_list ();
}
retval = (s);
}
octave_idx_type nt = tdims.numel ();
octave_idx_type base = 0, origin, cursor;
octave_idx_type skip_count = 1; // when to skip
octave_idx_type min_tp = n_cdims < dim - 1 ? n_cdims : dim - 1;
for (i = 0; i < min_tp; i++)
skip_count *= cdims(i);
octave_idx_type skip = skip_count; // how much to skip
if (n_cdims >= dim)
skip *= cdims(dim - 1);
for (i = 0; i < nr; i++)
{
Cell t (tdims);
origin = base;
cursor = 0;
for (j = 0; j < nt; j++)
{
t(j) = c(origin + cursor++);
if (cursor == skip_count)
{
cursor = 0;
origin += skip;
}
}
retval.assign (names(i), t);
base += skip_count;
}
return octave_value (retval);
}