comparison src/ov-struct.cc @ 10691:e0ba186b242b

Fisfield: Accept cell arrays as name argument.
author David Grundberg <davidg@cs.umu.se>
date Wed, 09 Jun 2010 15:14:31 +0200
parents 9c4daf174387
children 604e13a89c7f
comparison
equal deleted inserted replaced
10690:35adf2a71f3f 10691:e0ba186b242b
894 */ 894 */
895 895
896 DEFUN (isfield, args, , 896 DEFUN (isfield, args, ,
897 "-*- texinfo -*-\n\ 897 "-*- texinfo -*-\n\
898 @deftypefn {Built-in Function} {} isfield (@var{expr}, @var{name})\n\ 898 @deftypefn {Built-in Function} {} isfield (@var{expr}, @var{name})\n\
899 Return true if the expression @var{expr} is a structure and it includes an\n\ 899 Return true if the expression @var{expr} is a structure and it\n\
900 element named @var{name}. The first argument must be a structure and\n\ 900 includes an element named @var{name}. If @var{name} is a cell\n\
901 the second must be a string.\n\ 901 array, a logical array of equal dimension is returned.\n\
902 @end deftypefn") 902 @end deftypefn")
903 { 903 {
904 octave_value retval; 904 octave_value retval;
905 905
906 int nargin = args.length (); 906 int nargin = args.length ();
907 907
908 if (nargin == 2) 908 if (nargin == 2)
909 { 909 {
910 retval = false; 910 retval = false;
911 911
912 // FIXME -- should this work for all types that can do 912 if (args(0).is_map ())
913 // structure reference operations? 913 {
914
915 if (args(0).is_map () && args(1).is_string ())
916 {
917 std::string key = args(1).string_value ();
918
919 Octave_map m = args(0).map_value (); 914 Octave_map m = args(0).map_value ();
920 915
921 retval = m.contains (key) != 0; 916 // FIXME -- should this work for all types that can do
917 // structure reference operations?
918
919 if (args(1).is_string ())
920 {
921 std::string key = args(1).string_value ();
922
923 retval = m.contains (key) != 0;
924 }
925 else if (args(1).is_cell ())
926 {
927 Cell c = args(1).cell_value ();
928 boolMatrix bm (c.dims ());
929 octave_idx_type n = bm.numel ();
930
931 for (octave_idx_type i = 0; i < n; i++)
932 {
933 if (c(i).is_string ())
934 {
935 std::string key = c(i).string_value ();
936
937 bm(i) = m.contains (key) != 0;
938 }
939 else
940 bm(i) = false;
941 }
942
943 retval = bm;
944 }
922 } 945 }
923 } 946 }
924 else 947 else
925 print_usage (); 948 print_usage ();
926 949