home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / octa21fs.zip / octave / octave-2.1.23 / src / oct-obj.cc < prev    next >
C/C++ Source or Header  |  2000-01-15  |  4KB  |  209 lines

  1. /*
  2.  
  3. Copyright (C) 1996, 1997 John W. Eaton
  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 2, or (at your option) any
  10. 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, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include "error.h"
  32. #include "oct-obj.h"
  33.  
  34. octave_allocator
  35. octave_value_list::allocator (sizeof (octave_value_list));
  36.  
  37. octave_value_list&
  38. octave_value_list::prepend (const octave_value& val)
  39. {
  40.   int n = length ();
  41.  
  42.   resize (n + 1);
  43.  
  44.   while (n > 0)
  45.     {
  46.       elem (n) = elem (n - 1);
  47.       n--;
  48.     }
  49.  
  50.   elem (0) = val;
  51.   
  52.   return *this;
  53. }
  54.  
  55. octave_value_list&
  56. octave_value_list::append (const octave_value& val)
  57. {
  58.   int n = length ();
  59.  
  60.   resize (n + 1);
  61.  
  62.   elem (n) = val;
  63.  
  64.   return *this;
  65. }
  66.  
  67. octave_value_list&
  68. octave_value_list::append (const octave_value_list& lst)
  69. {
  70.   int len = length ();
  71.   int lst_len = lst.length ();
  72.  
  73.   resize (len + lst_len);
  74.  
  75.   for (int i = 0; i < lst_len; i++)
  76.     elem (len + i) = lst (i);
  77.  
  78.   return *this;
  79. }
  80.  
  81. octave_value_list&
  82. octave_value_list::reverse (void)
  83. {
  84.   int n = length ();
  85.  
  86.   for (int i = 0; i < n / 2; i++)
  87.     {
  88.       octave_value tmp = elem (i);
  89.       elem (i) = elem (n - i - 1);
  90.       elem (n - i - 1) = tmp;
  91.     }
  92.  
  93.   return *this;
  94. }
  95.  
  96. octave_value_list
  97. octave_value_list::splice (int offset, int rep_length,
  98.                const octave_value_list& lst) const
  99.   octave_value_list retval;
  100.  
  101.   int len = length ();
  102.  
  103.   if (offset < 0 || offset >= len)
  104.     {
  105.       if (! (rep_length == 0 && offset == len))
  106.     {
  107.       error ("octave_value_list::splice: invalid OFFSET");
  108.       return retval;
  109.     }
  110.     }
  111.  
  112.   if (rep_length < 0 || rep_length + offset > len)
  113.     {
  114.       error ("octave_value_list::splice: invalid LENGTH");
  115.       return retval;
  116.     }
  117.  
  118.   int lst_len = lst.length ();
  119.  
  120.   int new_len = len - rep_length + lst_len;
  121.  
  122.   retval.resize (new_len);
  123.  
  124.   int k = 0;
  125.  
  126.   for (int i = 0; i < offset; i++)
  127.     retval(k++) = elem (i);
  128.  
  129.   for (int i = 0; i < lst_len; i++)
  130.     retval(k++) = lst(i);
  131.  
  132.   for (int i = offset + rep_length; i < len; i++)
  133.     retval(k++) = elem (i);
  134.  
  135.   return retval;
  136. }
  137.  
  138. octave_value_list
  139. octave_value_list::index (idx_vector& i) const
  140. {
  141.   return octave_value_list (data.index (i));
  142. }
  143.  
  144. bool
  145. octave_value_list::all_strings_p (void) const
  146. {
  147.   int n = length ();
  148.  
  149.   for (int i = 0; i < n; i++)
  150.     if (! elem(i).is_string ())
  151.       return 0;
  152.  
  153.   return 1;
  154. }
  155.  
  156. string_vector
  157. octave_value_list::make_argv (const string& fcn_name) const
  158. {
  159.   string_vector argv;
  160.  
  161.   if (all_strings_p ())
  162.     {
  163.       int len = length ();
  164.  
  165.       int total_nr = 0;
  166.  
  167.       for (int i = 0; i < len; i++)
  168.     {
  169.       // An empty string ("") has zero columns and zero rows (a
  170.       // change that was made for Matlab contemptibility.
  171.  
  172.       int n = elem(i).rows ();
  173.  
  174.       total_nr += n ? n : 1;
  175.     }
  176.  
  177.       argv.resize (total_nr+1);
  178.  
  179.       argv[0] = fcn_name;
  180.  
  181.       int k = 1;
  182.       for (int i = 0; i < len; i++)
  183.     {
  184.       int nr = elem(i).rows ();
  185.  
  186.       if (nr < 2)
  187.         argv[k++] = elem(i).string_value ();
  188.       else
  189.         {
  190.           string_vector tmp = elem(i).all_strings ();
  191.  
  192.           for (int j = 0; j < nr; j++)
  193.         argv[k++] = tmp[j];
  194.         }
  195.     }
  196.     }
  197.   else
  198.     error ("%s: expecting all arguments to be strings", fcn_name.c_str ());
  199.  
  200.   return argv;
  201. }
  202.  
  203. /*
  204. ;;; Local Variables: ***
  205. ;;; mode: C++ ***
  206. ;;; End: ***
  207. */
  208.