3
Copyright (C) 2010-2013 VZLU Prague
5
This file is part of Octave.
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.
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
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/>.
27
#include "ov-lazy-idx.h"
29
#include "ov-scalar.h"
30
#include "ls-oct-ascii.h"
31
#include "ls-oct-binary.h"
33
DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_lazy_index, "lazy_index", "double");
35
static octave_base_value *
36
default_numeric_conversion_function (const octave_base_value& a)
38
CAST_CONV_ARG (const octave_lazy_index&);
40
return v.full_value ().clone ();
43
octave_base_value::type_conv_info
44
octave_lazy_index::numeric_conversion_function (void) const
46
return octave_base_value::type_conv_info (default_numeric_conversion_function,
47
octave_matrix::static_type_id ());
51
octave_lazy_index::try_narrowing_conversion (void)
53
octave_base_value *retval = 0;
55
switch (index.length (0))
58
retval = new octave_scalar (static_cast<double> (index(0) + 1));
62
retval = new octave_matrix (NDArray (index.orig_dimensions ()));
73
octave_lazy_index::reshape (const dim_vector& new_dims) const
75
return idx_vector (index.as_array ().reshape (new_dims),
80
octave_lazy_index::permute (const Array<int>& vec, bool inv) const
82
// If the conversion has already been made, forward the operation.
83
if (value.is_defined ())
84
return value.permute (vec, inv);
86
return idx_vector (index.as_array ().permute (vec, inv),
91
octave_lazy_index::squeeze (void) const
93
return idx_vector (index.as_array ().squeeze (),
98
octave_lazy_index::sort (octave_idx_type dim, sortmode mode) const
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 ();
106
return idx_vector (index.as_array ().sort (dim, mode),
111
octave_lazy_index::sort (Array<octave_idx_type> &sidx, octave_idx_type dim,
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);
120
return idx_vector (index.as_array ().sort (sidx, dim, mode),
125
octave_lazy_index::is_sorted (sortmode mode) const
127
if (index.is_range ())
129
// Avoid the array conversion.
130
octave_idx_type inc = index.increment ();
132
return (mode == UNSORTED ? ASCENDING : mode);
134
return (mode == DESCENDING ? UNSORTED : ASCENDING);
136
return (mode == ASCENDING ? UNSORTED : DESCENDING);
139
return index.as_array ().is_sorted (mode);
142
Array<octave_idx_type>
143
octave_lazy_index::sort_rows_idx (sortmode mode) const
145
return index.as_array ().sort_rows_idx (mode);
149
octave_lazy_index::is_sorted_rows (sortmode mode) const
151
return index.as_array ().is_sorted_rows (mode);
154
static const std::string value_save_tag ("index_value");
156
bool octave_lazy_index::save_ascii (std::ostream& os)
158
return save_ascii_data (os, make_value (), value_save_tag, false, 0);
161
bool octave_lazy_index::load_ascii (std::istream& is)
165
std::string nm = read_ascii_data (is, std::string (), dummy, value, 0);
167
if (nm != value_save_tag)
168
error ("lazy_index: corrupted data on load");
170
index = value.index_vector ();
172
return ! error_state;
176
bool octave_lazy_index::save_binary (std::ostream& os, bool& save_as_floats)
178
return save_binary_data (os, make_value (), value_save_tag,
179
std::string (), false, save_as_floats);
182
bool octave_lazy_index::load_binary (std::istream& is, bool swap,
183
oct_mach_info::float_format fmt)
188
std::string nm = read_binary_data (is, swap, fmt, std::string (),
191
if (nm != value_save_tag)
192
error ("lazy_index: corrupted data on load");
194
index = value.index_vector ();
196
return ! error_state;