diff libinterp/interpfcn/symtab.cc @ 16070:e603ce23f20c

Fix and improve functions related to class precedence (bug #38290) * libinterp/octave-value/ov-class.cc(inferiorto): Use only one call to set_class_relationship() and properly use the returned value. Rewrite for clarity (fewer braces, shorter lines). (superiorto): Rewrite similarly to inferiorto() for clarity. * libinterp/interpfcn/symtab.cc(symbol_table::set_class_relationship): Reduce code redundancy using a call to is_superiorto(). Add a comment to make it clear that a new entry in the precedence table is created if sup_class is not already there. (symbol_table::is_superiorto): Rewrite more concisely. Add a comment. * test/classes/@CPrecedenceTester1, test/classes/@CPrecedenceTester2, test/classes/@CPrecedenceTester3: New classes for precedence tests. * test/classes/@Snork/tattack.m: New method for precedence tests. * test/classes/classes.tst: Add precedence tests.
author Julien Bect <julien.bect@supelec.fr>
date Tue, 29 Jan 2013 17:55:53 +0100
parents 049e8bbff782
children 94e95309710c
line wrap: on
line diff
--- a/libinterp/interpfcn/symtab.cc	Sat Feb 09 16:13:55 2013 -0500
+++ b/libinterp/interpfcn/symtab.cc	Tue Jan 29 17:55:53 2013 +0100
@@ -1013,20 +1013,12 @@
 symbol_table::set_class_relationship (const std::string& sup_class,
                                       const std::string& inf_class)
 {
-  class_precedence_table_const_iterator p
-    = class_precedence_table.find (inf_class);
-
-  if (p != class_precedence_table.end ())
-    {
-      const std::set<std::string>& inferior_classes = p->second;
+  if (is_superiorto (inf_class, sup_class))
+    return false;
 
-      std::set<std::string>::const_iterator q
-        = inferior_classes.find (sup_class);
-
-      if (q != inferior_classes.end ())
-        return false;
-    }
-
+  // If sup_class doesn't have an entry in the precedence table, 
+  // this will automatically create it, and associate to it a 
+  // singleton set {inf_class} of inferior classes.
   class_precedence_table[sup_class].insert (inf_class);
 
   return true;
@@ -1034,7 +1026,7 @@
 
 // Has class A been marked as superior to class B?  Also returns
 // TRUE if B has been marked as inferior to A, since we only keep
-// one table, and convert inferiort information to a superiorto
+// one table, and convert inferiorto information to a superiorto
 // relationship.  Two calls are required to determine whether there
 // is no relationship between two classes:
 //
@@ -1048,20 +1040,14 @@
 bool
 symbol_table::is_superiorto (const std::string& a, const std::string& b)
 {
-  bool retval = false;
-
-  class_precedence_table_const_iterator p = class_precedence_table.find (a);
+  class_precedence_table_const_iterator p = class_precedence_table.find (a);  
+  // If a has no entry in the precedence table, return false
+  if (p == class_precedence_table.end ())
+    return false;
 
-  if (p != class_precedence_table.end ())
-    {
-      const std::set<std::string>& inferior_classes = p->second;
-      std::set<std::string>::const_iterator q = inferior_classes.find (b);
-
-      if (q != inferior_classes.end ())
-        retval = true;
-    }
-
-  return retval;
+  const std::set<std::string>& inferior_classes = p->second;
+  std::set<std::string>::const_iterator q = inferior_classes.find (b);
+  return (q != inferior_classes.end ());
 }
 
 static std::string