Cell.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1999-2012 John W. Eaton
00004 Copyright (C) 2009-2010 VZLU Prague
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #ifdef HAVE_CONFIG_H
00025 #include <config.h>
00026 #endif
00027 
00028 #include "idx-vector.h"
00029 
00030 #include "Cell.h"
00031 #include "error.h"
00032 #include "gripes.h"
00033 #include "oct-obj.h"
00034 
00035 Cell::Cell (const octave_value_list& ovl)
00036   : Array<octave_value> (ovl.cell_value ())
00037 {
00038 }
00039 
00040 Cell::Cell (const string_vector& sv, bool trim)
00041   : Array<octave_value> ()
00042 {
00043   octave_idx_type n = sv.length ();
00044 
00045   if (n > 0)
00046     {
00047       resize (dim_vector (n, 1));
00048 
00049       for (octave_idx_type i = 0; i < n; i++)
00050         {
00051           std::string s = sv[i];
00052 
00053           if (trim)
00054             {
00055               size_t pos = s.find_last_not_of (' ');
00056 
00057               s = (pos == std::string::npos) ? "" : s.substr (0, pos+1);
00058             }
00059 
00060           elem(i,0) = s;
00061         }
00062     }
00063 }
00064 
00065 Cell::Cell (const std::list<std::string>& lst)
00066   : Array<octave_value> ()
00067 {
00068   size_t n = lst.size ();
00069 
00070   if (n > 0)
00071     {
00072       resize (dim_vector (n, 1));
00073 
00074       octave_idx_type i = 0;
00075 
00076       for (std::list<std::string>::const_iterator it = lst.begin ();
00077            it != lst.end (); it++)
00078         {
00079           elem(i++,0) = *it;
00080         }
00081     }
00082 }
00083 
00084 Cell::Cell (const Array<std::string>& sa)
00085   : Array<octave_value> (sa.dims ())
00086 {
00087   octave_idx_type n = sa.numel ();
00088 
00089   octave_value *dst = fortran_vec ();
00090   const std::string *src = sa.data ();
00091 
00092   for (octave_idx_type i = 0; i < n; i++)
00093     dst[i] = src[i];
00094 }
00095 
00096 // Set size to DV, filling with [].  Then fill with as many elements of
00097 // SV as possible.
00098 
00099 Cell::Cell (const dim_vector& dv, const string_vector& sv, bool trim)
00100   : Array<octave_value> (dv, resize_fill_value ())
00101 {
00102   octave_idx_type n = sv.length ();
00103 
00104   if (n > 0)
00105     {
00106       octave_idx_type m = numel ();
00107 
00108       octave_idx_type len = n > m ? m : n;
00109 
00110       for (octave_idx_type i = 0; i < len; i++)
00111         {
00112           std::string s = sv[i];
00113 
00114           if (trim)
00115             {
00116               size_t pos = s.find_last_not_of (' ');
00117 
00118               s = (pos == std::string::npos) ? "" : s.substr (0, pos+1);
00119             }
00120 
00121           elem(i) = s;
00122         }
00123     }
00124 }
00125 
00126 bool
00127 Cell::is_cellstr (void) const
00128 {
00129   bool retval = true;
00130 
00131   octave_idx_type n = numel ();
00132 
00133   for (octave_idx_type i = 0; i < n; i++)
00134     {
00135       if (! elem(i).is_string ())
00136         {
00137           retval = false;
00138           break;
00139         }
00140     }
00141 
00142   return retval;
00143 }
00144 
00145 Array<std::string>
00146 Cell::cellstr_value (void) const
00147 {
00148   Array<std::string> retval (dims ());
00149 
00150   octave_idx_type n = numel ();
00151 
00152   for (octave_idx_type i = 0; i < n; i++)
00153     retval.xelem (i) = elem (i).string_value ();
00154 
00155   return retval;
00156 }
00157 
00158 Cell
00159 Cell::index (const octave_value_list& idx_arg, bool resize_ok) const
00160 {
00161   Cell retval;
00162 
00163   octave_idx_type n = idx_arg.length ();
00164 
00165   switch (n)
00166     {
00167     case 0:
00168       retval = *this;
00169       break;
00170 
00171     case 1:
00172       {
00173         idx_vector i = idx_arg(0).index_vector ();
00174 
00175         if (! error_state)
00176           retval = Array<octave_value>::index (i, resize_ok,
00177                                                resize_fill_value ());
00178       }
00179       break;
00180 
00181     case 2:
00182       {
00183         idx_vector i = idx_arg(0).index_vector ();
00184 
00185         if (! error_state)
00186           {
00187             idx_vector j = idx_arg(1).index_vector ();
00188 
00189             if (! error_state)
00190               retval = Array<octave_value>::index (i, j, resize_ok,
00191                                                     resize_fill_value ());
00192           }
00193       }
00194       break;
00195 
00196     default:
00197       {
00198         Array<idx_vector> iv (dim_vector (n, 1));
00199 
00200         for (octave_idx_type i = 0; i < n; i++)
00201           {
00202             iv(i) = idx_arg(i).index_vector ();
00203 
00204             if (error_state)
00205               break;
00206           }
00207 
00208         if (!error_state)
00209           retval = Array<octave_value>::index (iv, resize_ok,
00210                                                 resize_fill_value ());
00211       }
00212       break;
00213     }
00214 
00215   return retval;
00216 }
00217 
00218 void
00219 Cell::assign (const octave_value_list& idx_arg, const Cell& rhs,
00220               const octave_value& fill_val)
00221 
00222 {
00223   octave_idx_type len = idx_arg.length ();
00224 
00225   Array<idx_vector> ra_idx (dim_vector (len, 1));
00226 
00227   for (octave_idx_type i = 0; i < len; i++)
00228     ra_idx(i) = idx_arg(i).index_vector ();
00229 
00230   Array<octave_value>::assign (ra_idx, rhs, fill_val);
00231 }
00232 
00233 void
00234 Cell::delete_elements (const octave_value_list& idx_arg)
00235 
00236 {
00237   octave_idx_type len = idx_arg.length ();
00238 
00239   Array<idx_vector> ra_idx (dim_vector (len, 1));
00240 
00241   for (octave_idx_type i = 0; i < len; i++)
00242     ra_idx.xelem (i) = idx_arg(i).index_vector ();
00243 
00244   Array<octave_value>::delete_elements (ra_idx);
00245 }
00246 
00247 octave_idx_type
00248 Cell::nnz (void) const
00249 {
00250   gripe_wrong_type_arg ("nnz", "cell array");
00251   return -1;
00252 }
00253 
00254 Cell
00255 Cell::column (octave_idx_type i) const
00256 {
00257   Cell retval;
00258 
00259   if (ndims () < 3)
00260     {
00261       if (i < 0 || i >= cols ())
00262         error ("invalid column selection");
00263       else
00264         {
00265           octave_idx_type nr = rows ();
00266 
00267           retval.resize (dim_vector (nr, 1));
00268 
00269           for (octave_idx_type j = 0; j < nr; j++)
00270             retval.xelem (j) = elem (j, i);
00271         }
00272     }
00273   else
00274     error ("Cell::column: requires 2-d cell array");
00275 
00276   return retval;
00277 }
00278 
00279 Cell
00280 Cell::concat (const Cell& rb, const Array<octave_idx_type>& ra_idx)
00281 {
00282   return insert (rb, ra_idx);
00283 }
00284 
00285 Cell&
00286 Cell::insert (const Cell& a, octave_idx_type r, octave_idx_type c)
00287 {
00288   Array<octave_value>::insert (a, r, c);
00289   return *this;
00290 }
00291 
00292 Cell&
00293 Cell::insert (const Cell& a, const Array<octave_idx_type>& ra_idx)
00294 {
00295   Array<octave_value>::insert (a, ra_idx);
00296   return *this;
00297 }
00298 
00299 Cell
00300 Cell::map (ctype_mapper fcn) const
00301 {
00302   Cell retval (dims ());
00303   octave_value *r = retval.fortran_vec ();
00304 
00305   const octave_value *p = data ();
00306 
00307   for (octave_idx_type i = 0; i < numel (); i++)
00308     r[i] = ((p++)->*fcn) ();
00309 
00310   return retval;
00311 }
00312 
00313 Cell
00314 Cell::diag (octave_idx_type k) const
00315 {
00316   return Array<octave_value>::diag (k);
00317 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines