Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Const + simplification for jedule::Container::get_child_position.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 8 Jan 2020 09:35:02 +0000 (10:35 +0100)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Wed, 8 Jan 2020 09:44:19 +0000 (10:44 +0100)
include/simgrid/jedule/jedule_platform.hpp
src/instr/jedule/jedule_platform.cpp

index 4d328cf..9079e9e 100644 (file)
@@ -31,7 +31,7 @@ public:
   const char* get_cname() const { return name.c_str(); }
   void set_parent(Container* parent) { parent_ = parent; }
   bool has_children() { return not children_.empty(); }
-  int get_child_position(Container* child);
+  int get_child_position(const Container* child) const;
   unsigned int get_id_by_name(const char* name) { return name2id.at(name); }
 
   void add_child(Container* child);
index 53e657a..c53f3dd 100644 (file)
@@ -65,39 +65,28 @@ void Container::create_hierarchy(const_sg_netzone_t from_as)
   }
 }
 
-int Container::get_child_position(Container* child)
+int Container::get_child_position(const Container* child) const
 {
-  unsigned int i = 0;
-  int child_nb   = -1;
-
-  for (auto const& c : children_) {
-    if (c.get() == child) {
-      child_nb = i;
-      break;
-    }
-    i++;
-  }
-  return child_nb;
+  auto it = std::find_if(begin(children_), end(children_),
+                         [&child](const std::unique_ptr<Container>& c) { return c.get() == child; });
+  return it == end(children_) ? -1 : std::distance(begin(children_), it);
 }
 
 std::vector<int> Container::get_hierarchy()
 {
-  if (parent_ != nullptr) {
-    if (not parent_->has_children()) {
-      // we are in the last level
-      return parent_->get_hierarchy();
-    } else {
-      int child_nb = parent_->get_child_position(this);
-
-      xbt_assert( child_nb > - 1);
-      std::vector<int> heir_list = parent_->get_hierarchy();
-      heir_list.insert(heir_list.begin(), child_nb);
-      return heir_list;
-    }
-  } else {
+  if (parent_ == nullptr) {
     int top_level = 0;
     std::vector<int> heir_list = {top_level};
     return heir_list;
+  } else if (parent_->has_children()) {
+    int child_nb = parent_->get_child_position(this);
+    xbt_assert(child_nb > -1);
+    std::vector<int> heir_list = parent_->get_hierarchy();
+    heir_list.insert(heir_list.begin(), child_nb);
+    return heir_list;
+  } else {
+    // we are in the last level
+    return parent_->get_hierarchy();
   }
 }