~gabriel1984sibiu/octave/octave

« back to all changes in this revision

Viewing changes to libinterp/octave-value/ov-lazy-idx.cc

  • Committer: Grevutiu Gabriel
  • Date: 2014-01-02 13:05:54 UTC
  • Revision ID: gabriel1984sibiu@gmail.com-20140102130554-3r7ivdjln1ni6kcg
New version (3.8.0) from upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Copyright (C) 2010-2013 VZLU Prague
 
4
 
 
5
This file is part of Octave.
 
6
 
 
7
Octave is free software; you can redistribute it and/or modify it
 
8
under the terms of the GNU General Public License as published by the
 
9
Free Software Foundation; either version 3 of the License, or (at your
 
10
option) any later version.
 
11
 
 
12
Octave is distributed in the hope that it will be useful, but WITHOUT
 
13
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
14
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
15
for more details.
 
16
 
 
17
You should have received a copy of the GNU General Public License
 
18
along with Octave; see the file COPYING.  If not, see
 
19
<http://www.gnu.org/licenses/>.
 
20
 
 
21
*/
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
#include <config.h>
 
25
#endif
 
26
 
 
27
#include "ov-lazy-idx.h"
 
28
#include "ops.h"
 
29
#include "ov-scalar.h"
 
30
#include "ls-oct-ascii.h"
 
31
#include "ls-oct-binary.h"
 
32
 
 
33
DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_lazy_index, "lazy_index", "double");
 
34
 
 
35
static octave_base_value *
 
36
default_numeric_conversion_function (const octave_base_value& a)
 
37
{
 
38
  CAST_CONV_ARG (const octave_lazy_index&);
 
39
 
 
40
  return v.full_value ().clone ();
 
41
}
 
42
 
 
43
octave_base_value::type_conv_info
 
44
octave_lazy_index::numeric_conversion_function (void) const
 
45
{
 
46
  return octave_base_value::type_conv_info (default_numeric_conversion_function,
 
47
                                            octave_matrix::static_type_id ());
 
48
}
 
49
 
 
50
octave_base_value *
 
51
octave_lazy_index::try_narrowing_conversion (void)
 
52
{
 
53
  octave_base_value *retval = 0;
 
54
 
 
55
  switch (index.length (0))
 
56
    {
 
57
    case 1:
 
58
      retval = new octave_scalar (static_cast<double> (index(0) + 1));
 
59
      break;
 
60
 
 
61
    case 0:
 
62
      retval = new octave_matrix (NDArray (index.orig_dimensions ()));
 
63
      break;
 
64
 
 
65
    default:
 
66
      break;
 
67
    }
 
68
 
 
69
  return retval;
 
70
}
 
71
 
 
72
octave_value
 
73
octave_lazy_index::reshape (const dim_vector& new_dims) const
 
74
{
 
75
  return idx_vector (index.as_array ().reshape (new_dims),
 
76
                     index.extent (0));
 
77
}
 
78
 
 
79
octave_value
 
80
octave_lazy_index::permute (const Array<int>& vec, bool inv) const
 
81
{
 
82
  // If the conversion has already been made, forward the operation.
 
83
  if (value.is_defined ())
 
84
    return value.permute (vec, inv);
 
85
  else
 
86
    return idx_vector (index.as_array ().permute (vec, inv),
 
87
                       index.extent (0));
 
88
}
 
89
 
 
90
octave_value
 
91
octave_lazy_index::squeeze (void) const
 
92
{
 
93
  return idx_vector (index.as_array ().squeeze (),
 
94
                     index.extent (0));
 
95
}
 
96
 
 
97
octave_value
 
98
octave_lazy_index::sort (octave_idx_type dim, sortmode mode) const
 
99
{
 
100
  const dim_vector odims = index.orig_dimensions ();
 
101
  // index_vector can employ a more efficient sorting algorithm.
 
102
  if (mode == ASCENDING && odims.length () == 2
 
103
      && (dim >= 0 && dim <= 1) && odims (1-dim) == 1)
 
104
    return index_vector ().sorted ();
 
105
  else
 
106
    return idx_vector (index.as_array ().sort (dim, mode),
 
107
                       index.extent (0));
 
108
}
 
109
 
 
110
octave_value
 
111
octave_lazy_index::sort (Array<octave_idx_type> &sidx, octave_idx_type dim,
 
112
                         sortmode mode) const
 
113
{
 
114
  const dim_vector odims = index.orig_dimensions ();
 
115
  // index_vector can employ a more efficient sorting algorithm.
 
116
  if (mode == ASCENDING && odims.length () == 2
 
117
      && (dim >= 0 && dim <= 1) && odims (1-dim) == 1)
 
118
    return index_vector ().sorted (sidx);
 
119
  else
 
120
    return idx_vector (index.as_array ().sort (sidx, dim, mode),
 
121
                       index.extent (0));
 
122
}
 
123
 
 
124
sortmode
 
125
octave_lazy_index::is_sorted (sortmode mode) const
 
126
{
 
127
  if (index.is_range ())
 
128
    {
 
129
      // Avoid the array conversion.
 
130
      octave_idx_type inc = index.increment ();
 
131
      if (inc == 0)
 
132
        return (mode == UNSORTED ? ASCENDING : mode);
 
133
      else if (inc > 0)
 
134
        return (mode == DESCENDING ? UNSORTED : ASCENDING);
 
135
      else
 
136
        return (mode == ASCENDING ? UNSORTED : DESCENDING);
 
137
    }
 
138
  else
 
139
    return index.as_array ().is_sorted (mode);
 
140
}
 
141
 
 
142
Array<octave_idx_type>
 
143
octave_lazy_index::sort_rows_idx (sortmode mode) const
 
144
{
 
145
  return index.as_array ().sort_rows_idx (mode);
 
146
}
 
147
 
 
148
sortmode
 
149
octave_lazy_index::is_sorted_rows (sortmode mode) const
 
150
{
 
151
  return index.as_array ().is_sorted_rows (mode);
 
152
}
 
153
 
 
154
static const std::string value_save_tag ("index_value");
 
155
 
 
156
bool octave_lazy_index::save_ascii (std::ostream& os)
 
157
{
 
158
  return save_ascii_data (os, make_value (), value_save_tag, false, 0);
 
159
}
 
160
 
 
161
bool octave_lazy_index::load_ascii (std::istream& is)
 
162
{
 
163
  bool dummy;
 
164
 
 
165
  std::string nm = read_ascii_data (is, std::string (), dummy, value, 0);
 
166
 
 
167
  if (nm != value_save_tag)
 
168
    error ("lazy_index: corrupted data on load");
 
169
  else
 
170
    index = value.index_vector ();
 
171
 
 
172
  return ! error_state;
 
173
}
 
174
 
 
175
 
 
176
bool octave_lazy_index::save_binary (std::ostream& os, bool& save_as_floats)
 
177
{
 
178
  return save_binary_data (os, make_value (), value_save_tag,
 
179
                           std::string (), false, save_as_floats);
 
180
}
 
181
 
 
182
bool octave_lazy_index::load_binary (std::istream& is, bool swap,
 
183
                                     oct_mach_info::float_format fmt)
 
184
{
 
185
  bool dummy;
 
186
  std::string doc;
 
187
 
 
188
  std::string nm = read_binary_data (is, swap, fmt, std::string (),
 
189
                                     dummy, value, doc);
 
190
 
 
191
  if (nm != value_save_tag)
 
192
    error ("lazy_index: corrupted data on load");
 
193
  else
 
194
    index = value.index_vector ();
 
195
 
 
196
  return ! error_state;
 
197
}