ov-lazy-idx.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2010-2012 VZLU Prague
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include "ov-lazy-idx.h"
00028 #include "ops.h"
00029 #include "ov-scalar.h"
00030 #include "ls-oct-ascii.h"
00031 #include "ls-oct-binary.h"
00032 
00033 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_lazy_index, "lazy_index", "double");
00034 
00035 static octave_base_value *
00036 default_numeric_conversion_function (const octave_base_value& a)
00037 {
00038   CAST_CONV_ARG (const octave_lazy_index&);
00039 
00040   return v.full_value ().clone ();
00041 }
00042 
00043 octave_base_value::type_conv_info
00044 octave_lazy_index::numeric_conversion_function (void) const
00045 {
00046   return octave_base_value::type_conv_info (default_numeric_conversion_function,
00047                                             octave_matrix::static_type_id ());
00048 }
00049 
00050 octave_base_value *
00051 octave_lazy_index::try_narrowing_conversion (void)
00052 {
00053   octave_base_value *retval = 0;
00054 
00055   switch (index.length (0))
00056     {
00057     case 1:
00058       retval = new octave_scalar (static_cast<double> (index(0) + 1));
00059       break;
00060 
00061     case 0:
00062       retval = new octave_matrix (NDArray (index.orig_dimensions ()));
00063       break;
00064 
00065     default:
00066       break;
00067     }
00068 
00069   return retval;
00070 }
00071 
00072 octave_value
00073 octave_lazy_index::reshape (const dim_vector& new_dims) const
00074 {
00075   return idx_vector (index.as_array ().reshape (new_dims),
00076                      index.extent (0));
00077 }
00078 
00079 octave_value
00080 octave_lazy_index::permute (const Array<int>& vec, bool inv) const
00081 {
00082   // If the conversion has already been made, forward the operation.
00083   if (value.is_defined ())
00084     return value.permute (vec, inv);
00085   else
00086     return idx_vector (index.as_array ().permute (vec, inv),
00087                        index.extent (0));
00088 }
00089 
00090 octave_value
00091 octave_lazy_index::squeeze (void) const
00092 {
00093   return idx_vector (index.as_array ().squeeze (),
00094                      index.extent (0));
00095 }
00096 
00097 octave_value
00098 octave_lazy_index::sort (octave_idx_type dim, sortmode mode) const
00099 {
00100   const dim_vector odims = index.orig_dimensions ();
00101   // index_vector can employ a more efficient sorting algorithm.
00102   if (mode == ASCENDING && odims.length () == 2
00103       && (dim >= 0 && dim <= 1) && odims (1-dim) == 1)
00104     return index_vector ().sorted ();
00105   else
00106     return idx_vector (index.as_array ().sort (dim, mode),
00107                        index.extent (0));
00108 }
00109 
00110 octave_value
00111 octave_lazy_index::sort (Array<octave_idx_type> &sidx, octave_idx_type dim,
00112                          sortmode mode) const
00113 {
00114   const dim_vector odims = index.orig_dimensions ();
00115   // index_vector can employ a more efficient sorting algorithm.
00116   if (mode == ASCENDING && odims.length () == 2
00117       && (dim >= 0 && dim <= 1) && odims (1-dim) == 1)
00118     return index_vector ().sorted (sidx);
00119   else
00120     return idx_vector (index.as_array ().sort (sidx, dim, mode),
00121                        index.extent (0));
00122 }
00123 
00124 sortmode
00125 octave_lazy_index::is_sorted (sortmode mode) const
00126 {
00127   if (index.is_range ())
00128     {
00129       // Avoid the array conversion.
00130       octave_idx_type inc = index.increment ();
00131       if (inc == 0)
00132         return (mode == UNSORTED ? ASCENDING : mode);
00133       else if (inc > 0)
00134         return (mode == DESCENDING ? UNSORTED : ASCENDING);
00135       else
00136         return (mode == ASCENDING ? UNSORTED : DESCENDING);
00137     }
00138   else
00139     return index.as_array ().is_sorted (mode);
00140 }
00141 
00142 Array<octave_idx_type>
00143 octave_lazy_index::sort_rows_idx (sortmode mode) const
00144 {
00145   return index.as_array ().sort_rows_idx (mode);
00146 }
00147 
00148 sortmode
00149 octave_lazy_index::is_sorted_rows (sortmode mode) const
00150 {
00151   return index.as_array ().is_sorted_rows (mode);
00152 }
00153 
00154 static const std::string value_save_tag ("index_value");
00155 
00156 bool octave_lazy_index::save_ascii (std::ostream& os)
00157 {
00158   return save_ascii_data (os, make_value (), value_save_tag, false, 0);
00159 }
00160 
00161 bool octave_lazy_index::load_ascii (std::istream& is)
00162 {
00163   bool dummy;
00164 
00165   std::string nm = read_ascii_data (is, std::string (), dummy, value, 0);
00166 
00167   if (nm != value_save_tag)
00168     error ("lazy_index: corrupted data on load");
00169   else
00170     index = value.index_vector ();
00171 
00172   return ! error_state;
00173 }
00174 
00175 
00176 bool octave_lazy_index::save_binary (std::ostream& os, bool& save_as_floats)
00177 {
00178   return save_binary_data (os, make_value (), value_save_tag,
00179                            std::string (), false, save_as_floats);
00180 }
00181 
00182 bool octave_lazy_index::load_binary (std::istream& is, bool swap,
00183                                      oct_mach_info::float_format fmt)
00184 {
00185   bool dummy;
00186   std::string doc;
00187 
00188   std::string nm = read_binary_data (is, swap, fmt, std::string (),
00189                                      dummy, value, doc);
00190 
00191   if (nm != value_save_tag)
00192     error ("lazy_index: corrupted data on load");
00193   else
00194     index = value.index_vector ();
00195 
00196   return ! error_state;
00197 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines