~ubuntu-branches/ubuntu/wily/octave-miscellaneous/wily

« back to all changes in this revision

Viewing changes to src/cell2cell.cc

  • Committer: Package Import Robot
  • Author(s): Sébastien Villemot
  • Date: 2012-10-17 13:40:55 UTC
  • mfrom: (1.1.6)
  • mto: (5.1.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: package-import@ubuntu.com-20121017134055-e8lrxjd3qgcd3kmt
Tags: upstream-1.2.0
Import upstream version 1.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (C) 2010 Olaf Till <olaf.till@uni-jena.de>
 
2
//
 
3
// This program is free software; you can redistribute it and/or modify it under
 
4
// the terms of the GNU General Public License as published by the Free Software
 
5
// Foundation; either version 3 of the License, or (at your option) any later
 
6
// version.
 
7
//
 
8
// This program is distributed in the hope that it will be useful, but WITHOUT
 
9
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
10
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 
11
// details.
 
12
//
 
13
// You should have received a copy of the GNU General Public License along with
 
14
// this program; if not, see <http://www.gnu.org/licenses/>.
 
15
 
 
16
#include <octave/oct.h>
 
17
 
 
18
DEFUN_DLD (cell2cell, args, , 
 
19
  "-*- texinfo -*-\n\
 
20
@deftypefn {Loadable Function} {} cell2cell (@var{c}, @var{dim})\n\
 
21
Return a one-dimensional cell array, extending along dimension @var{dim},\n\
 
22
which contains the slices of cell array @var{c} vertical to dimension\n\
 
23
@var{dim}.\n\
 
24
@end deftypefn")
 
25
{
 
26
  std::string fname ("cell2cell");
 
27
 
 
28
  if (args.length () != 2)
 
29
    {
 
30
      print_usage ();
 
31
      return octave_value_list ();
 
32
    }
 
33
 
 
34
  Cell c = args(0).cell_value ();
 
35
  if (error_state)
 
36
    {
 
37
      error ("%s: first argument must be a cell array", fname.c_str ());
 
38
      return octave_value_list ();
 
39
    }
 
40
 
 
41
  octave_idx_type dim = args(1).int_value ();
 
42
  if (error_state)
 
43
    {
 
44
      error ("%s: second argument must be an integer",
 
45
             fname.c_str ());
 
46
      return octave_value_list ();
 
47
    }
 
48
 
 
49
  octave_idx_type i, j;
 
50
 
 
51
  dim_vector cdims (c.dims ());
 
52
 
 
53
  octave_idx_type n_cdims = cdims.length ();
 
54
 
 
55
  dim_vector rdims, tdims (cdims);
 
56
 
 
57
  octave_idx_type nr = 1;
 
58
 
 
59
  if (n_cdims >= dim && cdims(dim - 1) > 1)
 
60
    {
 
61
      if (dim == 1)
 
62
        {
 
63
          rdims.resize (2);
 
64
 
 
65
          rdims(1) = 1;
 
66
        }
 
67
      else
 
68
        rdims.resize (dim);
 
69
 
 
70
      for (i = 0; i < dim - 1; i++)
 
71
        rdims(i) = 1;
 
72
 
 
73
      rdims(dim - 1) = (nr = cdims(dim - 1));
 
74
 
 
75
      tdims(dim - 1) = 1;
 
76
 
 
77
      tdims.chop_trailing_singletons ();
 
78
    }
 
79
  else
 
80
    {
 
81
      rdims.resize (2);
 
82
 
 
83
      rdims(0) = 1;
 
84
      rdims(1) = 1;
 
85
    }
 
86
 
 
87
  Cell retval (rdims);
 
88
 
 
89
  octave_idx_type nt = tdims.numel ();
 
90
 
 
91
  octave_idx_type base = 0, origin, cursor;
 
92
 
 
93
  octave_idx_type skip_count = 1; // when to skip
 
94
 
 
95
  octave_idx_type min_tp = n_cdims < dim - 1 ? n_cdims : dim - 1;
 
96
 
 
97
  for (i = 0; i < min_tp; i++)
 
98
    skip_count *= cdims(i);
 
99
 
 
100
  octave_idx_type skip = skip_count; // how much to skip
 
101
 
 
102
  if (n_cdims >= dim)
 
103
    skip *= cdims(dim - 1);
 
104
 
 
105
  for (i = 0; i < nr; i++)
 
106
    {
 
107
      Cell t (tdims);
 
108
 
 
109
      origin = base;
 
110
 
 
111
      cursor = 0;
 
112
 
 
113
      for (j = 0; j < nt; j++)
 
114
        {
 
115
          t(j) = c(origin + cursor++);
 
116
 
 
117
          if (cursor == skip_count)
 
118
            {
 
119
              cursor = 0;
 
120
 
 
121
              origin += skip;
 
122
            }
 
123
        }
 
124
 
 
125
      retval(i) = t;
 
126
 
 
127
      base += skip_count;
 
128
    }
 
129
 
 
130
  return octave_value (retval);
 
131
}