fix(libmemdb): deep-copy semantics so indices point into the copy's own list
默认复制只拷贝容器,keys/time_touch 中的迭代器仍指向源对象的 list 节点, 源销毁或改动后副本悬垂(UB)。改为保留时间戳的深拷贝构造 + copy-and-swap 赋值。
This commit is contained in:
1 parent
b2ecb8e12b
commit
207137c0dd
1 file changed
+30
-1
@@ -5,6 +5,7 @@
|
|||||||
#include <set>
|
#include <set>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -72,7 +73,35 @@ public:
|
|||||||
|
|
||||||
explicit Memdb() = default;
|
explicit Memdb() = default;
|
||||||
explicit Memdb(int max_time) : MAX_TIME(std::max(0, max_time)) {}
|
explicit Memdb(int max_time) : MAX_TIME(std::max(0, max_time)) {}
|
||||||
Memdb& operator=(const Memdb& to_be_cp_memdb) = default;
|
|
||||||
|
// 深拷贝:重建索引,保证 keys/time_touch 的迭代器指向副本自身的 list
|
||||||
|
Memdb(const Memdb& other)
|
||||||
|
: __size(other.__size), MAX_TIME(other.MAX_TIME) {
|
||||||
|
this -> db = other.db;
|
||||||
|
for (auto it = this -> db.begin(); it != this -> db.end(); ++it) {
|
||||||
|
Memdb::index_keys new_key;
|
||||||
|
new_key.item = it;
|
||||||
|
this -> keys.emplace(it -> key, new_key);
|
||||||
|
|
||||||
|
Memdb::index_timestamp new_time_touch;
|
||||||
|
new_time_touch.item = it;
|
||||||
|
this -> time_touch.insert(new_time_touch);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Memdb& operator=(const Memdb& other) {
|
||||||
|
if (this == &other) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
Memdb tmp(other);
|
||||||
|
std::swap(this -> db, tmp.db);
|
||||||
|
std::swap(this -> keys, tmp.keys);
|
||||||
|
std::swap(this -> time_touch, tmp.time_touch);
|
||||||
|
std::swap(this -> __size, tmp.__size);
|
||||||
|
std::swap(this -> MAX_TIME, tmp.MAX_TIME);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
~Memdb() = default;
|
~Memdb() = default;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user