forked from mindspore-Ecosystem/mindspore
!22980 Fix a bug for assign in OrderedSet/OrderedMap
Merge pull request !22980 from hewei/fix_ordered_set_map
This commit is contained in:
commit
c44c5b494b
|
@ -211,7 +211,7 @@ GraphDebugInfoPtr FuncGraph::debug_info() {
|
|||
|
||||
const AnfNodeSet &FuncGraph::nodes() const { return nodes_; }
|
||||
|
||||
void FuncGraph::CopyNodes(const FuncGraphPtr &source) { nodes_ = source->nodes(); }
|
||||
void FuncGraph::CopyNodes(const FuncGraphPtr &source) { nodes_.update(source->nodes()); }
|
||||
|
||||
void FuncGraph::ClearNodes() { nodes_.clear(); }
|
||||
|
||||
|
|
|
@ -78,9 +78,11 @@ class OrderedMap {
|
|||
}
|
||||
}
|
||||
|
||||
OrderedMap &operator=(const OrderedMap &os) {
|
||||
if (this != &os) {
|
||||
for (auto &item : os.sequential_data_) {
|
||||
OrderedMap &operator=(const OrderedMap &other) {
|
||||
if (this != &other) {
|
||||
clear();
|
||||
reserve(other.size());
|
||||
for (auto &item : other.sequential_data_) {
|
||||
(void)insert(pair_type(item.first, item.second));
|
||||
}
|
||||
}
|
||||
|
@ -88,9 +90,7 @@ class OrderedMap {
|
|||
}
|
||||
|
||||
void clear() {
|
||||
if (!map_data_.empty()) {
|
||||
map_data_.clear();
|
||||
}
|
||||
map_data_.clear();
|
||||
sequential_data_.clear();
|
||||
}
|
||||
|
||||
|
@ -99,10 +99,7 @@ class OrderedMap {
|
|||
std::swap(sequential_data_, rhs.sequential_data_);
|
||||
}
|
||||
|
||||
void reserve(size_type num_entries) {
|
||||
map_data_.reserve(num_entries);
|
||||
sequential_data_.reserve(num_entries);
|
||||
}
|
||||
void reserve(size_type num_entries) { map_data_.reserve(num_entries); }
|
||||
|
||||
std::pair<iterator, bool> add(const key_t &key) {
|
||||
iterator empty_itr;
|
||||
|
|
|
@ -73,16 +73,18 @@ class OrderedSet {
|
|||
}
|
||||
}
|
||||
|
||||
OrderedSet &operator=(const OrderedSet &os) {
|
||||
if (this != &os) {
|
||||
for (auto &item : os.ordered_data_) {
|
||||
OrderedSet &operator=(const OrderedSet &other) {
|
||||
if (this != &other) {
|
||||
clear();
|
||||
reserve(other.size());
|
||||
for (auto &item : other.ordered_data_) {
|
||||
add(item);
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
OrderedSet &operator=(OrderedSet &&os) = default;
|
||||
OrderedSet &operator=(OrderedSet &&other) = default;
|
||||
|
||||
// insert an element to the OrderedSet after the given position.
|
||||
std::pair<iterator, bool> insert(iterator pos, const element_type &e) {
|
||||
|
@ -142,12 +144,13 @@ class OrderedSet {
|
|||
|
||||
// Clear the elements
|
||||
void clear() {
|
||||
if (!mapped_data_.empty()) {
|
||||
mapped_data_.clear();
|
||||
}
|
||||
mapped_data_.clear();
|
||||
ordered_data_.clear();
|
||||
}
|
||||
|
||||
// Reserve memory for the number of entries.
|
||||
void reserve(size_t num_entries) { mapped_data_.reserve(num_entries); }
|
||||
|
||||
// Compare two orderedset, if the order is not equal shall return false
|
||||
bool operator==(const OrderedSet &other) const { return ordered_data_ == other.ordered_data_; }
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include <algorithm>
|
||||
|
||||
#include "utils/ordered_set.h"
|
||||
#include "utils/ordered_map.h"
|
||||
#include "common/common_test.h"
|
||||
|
||||
using std::cout;
|
||||
|
@ -88,7 +89,7 @@ TEST_F(TestOrderedSet, test_add_remove_first) {
|
|||
a.add(3);
|
||||
a.erase(1);
|
||||
auto first = a.pop();
|
||||
// 1 removed, 2 3 followd, 2 should be the poped one, remaining size = 1
|
||||
// 1 removed, 2 3 followed, 2 should be the popped one, remaining size = 1
|
||||
ASSERT_EQ(first, 2);
|
||||
ASSERT_EQ(a.size(), 1);
|
||||
}
|
||||
|
@ -147,4 +148,31 @@ TEST_F(TestOrderedSet, test_contains) {
|
|||
ASSERT_TRUE(!res.contains(e2));
|
||||
}
|
||||
|
||||
TEST_F(TestOrderedSet, test_assign) {
|
||||
OrderedSet<int> s;
|
||||
s.add(10);
|
||||
ASSERT_EQ(s.size(), 1);
|
||||
OrderedSet<int> s1;
|
||||
s1.add(20);
|
||||
s1.add(30);
|
||||
ASSERT_EQ(s1.size(), 2);
|
||||
s = s1;
|
||||
ASSERT_EQ(s.size(), 2);
|
||||
ASSERT_EQ(s, s1);
|
||||
}
|
||||
|
||||
TEST_F(TestOrderedSet, test_map_assign) {
|
||||
OrderedMap<int, int> m;
|
||||
m[10] = 10;
|
||||
ASSERT_EQ(m.size(), 1);
|
||||
OrderedMap<int, int> m1;
|
||||
m1[20] = 20;
|
||||
m1[30] = 30;
|
||||
ASSERT_EQ(m1.size(), 2);
|
||||
m = m1;
|
||||
ASSERT_EQ(m.size(), 2);
|
||||
ASSERT_EQ(m[20], m1[20]);
|
||||
ASSERT_EQ(m[30], m1[30]);
|
||||
}
|
||||
|
||||
} // namespace mindspore
|
||||
|
|
Loading…
Reference in New Issue