// @brief Return the set of Update links that point to this bag
const UpdateSet *updates(void) const;
+public:
+ // @brief Replace all the occurence of a bag (except those in Input/Output) with another bag
+ //
+ // NOTE reaplceWith(b) works correctly only when b is neither Input nor Output
+ void replaceWith(Bag *b);
+
private:
std::unique_ptr<BagInfo> _info;
};
}
}
}
+
+//
+// This test demonstrates how to use 'replaceWith' method
+//
+TEST(IR, bag_replaceWith)
+{
+ auto m = coco::Module::create();
+
+ auto bag_1 = m->entity()->bag()->create(1);
+ auto bag_2 = m->entity()->bag()->create(1);
+
+ auto obj = m->entity()->object()->create(nncc::core::ADT::feature::Shape{1, 1, 1});
+ obj->bag(bag_1);
+
+ auto shuffle_1 = m->entity()->instr()->create<coco::Shuffle>();
+ shuffle_1->into(bag_1);
+
+ auto shuffle_2 = m->entity()->instr()->create<coco::Shuffle>();
+ shuffle_2->from(bag_1);
+
+ ASSERT_EQ(obj->bag(), bag_1);
+ ASSERT_EQ(shuffle_1->into(), bag_1);
+ ASSERT_EQ(shuffle_2->from(), bag_1);
+
+ bag_1->replaceWith(bag_2);
+
+ ASSERT_EQ(obj->bag(), bag_2);
+ ASSERT_EQ(shuffle_1->into(), bag_2);
+ ASSERT_EQ(shuffle_2->from(), bag_2);
+}
const ReadSet *Bag::reads(void) const { return _info->reads(); }
const UpdateSet *Bag::updates(void) const { return _info->updates(); }
+void Bag::replaceWith(Bag *b)
+{
+ assert(!isInput() && !isOutput());
+
+ // Replace all the occurence inside Dep
+ while (!(deps()->empty()))
+ {
+ auto dep = *(deps()->begin());
+ assert(dep->bag() == this);
+ dep->bag(b);
+ }
+
+ // Replace all the occurence inside Read
+ while (!(reads()->empty()))
+ {
+ auto read = *(reads()->begin());
+ assert(read->bag() == this);
+ read->bag(b);
+ }
+
+ // Replace all the occurence insider Update
+ while (!(updates()->empty()))
+ {
+ auto update = *(updates()->begin());
+ assert(update->bag() == this);
+ update->bag(b);
+ }
+
+ assert(deps()->empty());
+ assert(reads()->empty());
+ assert(updates()->empty());
+}
+
ObjectSet dependent_objects(const Bag *b)
{
ObjectSet res;