comparison src/ov.cc @ 6153:e60688a1ea07

[project @ 2006-11-13 17:30:54 by jwe]
author jwe
date Mon, 13 Nov 2006 17:30:55 +0000
parents acb4a1e0b311
children 045038e0108a
comparison
equal deleted inserted replaced
6152:2eb0723b4fad 6153:e60688a1ea07
1878 print_usage (); 1878 print_usage ();
1879 1879
1880 return retval; 1880 return retval;
1881 } 1881 }
1882 1882
1883 static void
1884 decode_subscripts (const char* name, const octave_value& arg,
1885 std::string& type_string,
1886 std::list<octave_value_list>& idx)
1887 {
1888 Octave_map m = arg.map_value ();
1889
1890 if (! error_state
1891 && m.length () == 2 && m.contains ("type") && m.contains ("subs"))
1892 {
1893 Cell& type = m.contents ("type");
1894 Cell& subs = m.contents ("subs");
1895
1896 type_string = std::string (type.length(), '\0');
1897
1898 for (int k = 0; k < type.length (); k++)
1899 {
1900 std::string item = type(k).string_value ();
1901
1902 if (! error_state)
1903 {
1904 if (item == "{}")
1905 type_string[k] = '{';
1906 else if (item == "()")
1907 type_string[k] = '(';
1908 else if (item == ".")
1909 type_string[k] = '.';
1910 else
1911 {
1912 error("%s: invalid indexing type `%s'", name, item.c_str ());
1913 return;
1914 }
1915 }
1916 else
1917 {
1918 error ("%s: expecting type(%d) to be a character string",
1919 name, k+1);
1920 return;
1921 }
1922
1923 octave_value_list idx_item;
1924
1925 if (subs(k).is_string ())
1926 idx_item(0) = subs(k);
1927 else if (subs(k).is_cell ())
1928 {
1929 Cell subs_cell = subs(k).cell_value ();
1930
1931 for (int n = 0; n < subs_cell.length (); n++)
1932 {
1933 if (subs_cell(n).is_string ()
1934 && subs_cell(n).string_value () == ":")
1935 idx_item(n) = octave_value(octave_value::magic_colon_t);
1936 else
1937 idx_item(n) = subs_cell(n);
1938 }
1939 }
1940 else
1941 {
1942 error ("%s: expecting subs(%d) to be a character string or cell array",
1943 name, k+1);
1944 return;
1945 }
1946
1947 idx.push_back (idx_item);
1948 }
1949 }
1950 else
1951 error ("%s: second argument must be a structure with fields `type' and `subs'", name);
1952 }
1953
1954 DEFUN (subsref, args, nargout,
1955 "-*- texinfo -*-\n\
1956 @deftypefn {Built-in Function} {} subsref (@var{val}, @var{idx})\n\
1957 Perform the subscripted element selection operation according to\n\
1958 the subscript specified by @var{idx}.\n\
1959 \n\
1960 The subscript @var{idx} is expected to be a structure array with\n\
1961 fields @samp{type} and @samp{subs}. Valid values for @samp{type}\n\
1962 are @samp{\"()\"}, @samp{\"@{@}\", and @samp{\".\"}.\n\
1963 The @samp{subs} field may be either @samp{\":\"} or a cell array\n\
1964 of index values.\n\
1965 @end deftypefn")
1966 {
1967 octave_value_list retval;
1968
1969 if (args.length () == 2)
1970 {
1971 std::string type;
1972 std::list<octave_value_list> idx;
1973
1974 decode_subscripts ("subsref", args(1), type, idx);
1975
1976 if (! error_state)
1977 retval = args(0).subsref (type, idx, nargout);
1978 }
1979 else
1980 print_usage ();
1981
1982 return retval;
1983 }
1984
1985 DEFUN (subsasgn, args, ,
1986 "-*- texinfo -*-\n\
1987 @deftypefn {Built-in Function} {} subsasgn (@var{val}, @var{idx}, @var{rhs})\n\
1988 Perform the subscripted assignment operation according to\n\
1989 the subscript specified by @var{idx}.\n\
1990 \n\
1991 The subscript @var{idx} is expected to be a structure array with\n\
1992 fields @samp{type} and @samp{subs}. Valid values for @samp{type}\n\
1993 are @samp{\"()\"}, @samp{\"@{@}\", and @samp{\".\"}.\n\
1994 The @samp{subs} field may be either @samp{\":\"} or a cell array\n\
1995 of index values.\n\
1996 @end deftypefn")
1997 {
1998 octave_value retval;
1999
2000 if (args.length () == 3)
2001 {
2002 std::string type;
2003 std::list<octave_value_list> idx;
2004
2005 decode_subscripts ("subsasgn", args(1), type, idx);
2006
2007 if (! error_state)
2008 retval = args(0).subsasgn (type, idx, args(2));
2009 }
2010 else
2011 print_usage ();
2012
2013 return retval;
2014 }
2015
1883 /* 2016 /*
1884 ;;; Local Variables: *** 2017 ;;; Local Variables: ***
1885 ;;; mode: C++ *** 2018 ;;; mode: C++ ***
1886 ;;; End: *** 2019 ;;; End: ***
1887 */ 2020 */