简体   繁体   English

有效地从 unordered_set 中删除 unique_ptr

[英]Efficiently erase a unique_ptr from an unordered_set

I am storing the ownership of some objects inside an unordered_set , using unique_ptr s.我使用unique_ptr将某些对象的所有权存储在unordered_set But I don't know a good way to erase one of them from the set, when the time comes.但是我不知道有什么好方法可以在时机成熟时从集合中删除其中一个。

Code looks something like this:代码如下所示:

typedef unique_ptr<MyType> MyPtr;

unordered_set<MyPtr> owner;

MyPtr p = make_unique<MyType>("foo")
MyType *pRaw = p.get();
owner.insert(std::move(p));

// Later ...

// I want to do something like this (cannot be written as-is, of course):
// owner.erase(pRaw);

Is there a way to do this?有没有办法做到这一点? I can, of course, iterate the entire set with begin() and end() , but the whole point of putting them in the set is to make these lookups efficient.当然,我可以使用begin()end()迭代整个集合,但将它们放入集合中的全部意义在于使这些查找有效。

Some things I have thought of already:我已经想到了一些事情:

  • Use shared_ptr .使用shared_ptr This is the wrong abstraction for my case.对于我的案例,这是错误的抽象。 Ownership is unique.所有权是独一无二的。
  • Use raw pointers, and forget about unique_ptr.使用原始指针,而忘记 unique_ptr。 This abandons all the advantages that unique_ptr provides.这放弃了unique_ptr提供的所有优点。
  • Find the bucket with unordered_set::begin(key) .使用unordered_set::begin(key)查找存储桶。 As far as I know, there is no way for me to create a key that will match the unique_ptr I want to delete.据我所知,我无法创建一个与我要删除的unique_ptr匹配的键。 But I'm happy to be proven wrong (:但我很高兴被证明是错误的 (:

(In truth, I solved this using eastl::unordered_set , with its find_as function for custom keys) (事实上​​,我使用eastl::unordered_set解决了这个问题,它的find_as函数用于自定义键)

This is a tough case.这是一个棘手的案例。 erase has an overload that takes a const key_type& parameter, so we can try to create a "stale" unique_ptr to get the hash value of the element to be erased: erase有一个带有const key_type&参数的重载,因此我们可以尝试创建一个“陈旧”的unique_ptr来获取要擦除的元素的哈希值:

template <typename T>
auto erase(std::unordered_set<std::unique_ptr<T>>& set, T* ptr)
{
    std::unique_ptr<T> stale_ptr{ptr};
    auto ret = set.erase(stale_ptr);
    stale_ptr.release();
    return ret;
}

( live demo ) 现场演示


This version, however, is not exception safe in general, because release will not be called if set.erase throws an exception.这个版本,但是,不是一般的异常安全的,因为release如果将不会被调用set.erase抛出异常。 This is not a problem in this case, since std::equal_to<std::unique_ptr<T>>::operator() never throws exception.在这种情况下这不是问题,因为std::equal_to<std::unique_ptr<T>>::operator()从不抛出异常。 In the general case, we can abuse unique_ptr (!) to enforce exception safety by ensuring that release is called regardless of whether the function is exited normally or exceptionally:在一般情况下,我们可以滥用unique_ptr (!) 通过确保无论函数是正常退出还是异常退出都调用release来强制执行异常安全:

template <typename T>
auto erase(std::unordered_set<std::unique_ptr<T>>& set, T* ptr)
{
    std::unique_ptr<T> stale_ptr{ptr};

    auto release = [](std::unique_ptr<T>* p) { p->release(); };
    std::unique_ptr<std::unique_ptr<T>, decltype(release)> release_helper{&stale_ptr, release};

    return set.erase(stale_ptr);
}

( live demo ) 现场演示

In C++20, std::unordered_set::find can use equivalent key with transparent hash and KeyEqual, then you might do something similar to:在 C++20 中, std::unordered_set::find可以使用具有透明散列和 KeyEqual 的等效键,然后您可以执行类似于:

struct MyHash
{
    using is_transparent = void;

    auto operator()(MyType* p) const { return std::hash<MyType*>{}(p); }
    auto operator()(const MyPtr& p) const { return std::hash<MyType*>{}(p.get()); }
};

struct MyEqual
{
    using is_transparent = void;

    template <typename LHS, typename RHS>
    auto operator()(const LHS& lhs, const RHS& rhs) const
    {
        return AsPtr(lhs) == AsPtr(rhs);
    }
private:
    static const MyType* AsPtr(const MyType* p) { return p; }
    static const MyType* AsPtr(const MyPtr& p) { return p.get(); }

};

int main()
{
    std::unordered_set<MyPtr, MyHash, MyEqual> owner;

    MyPtr p = std::make_unique<MyType>();
    MyType *pRaw = p.get();
    owner.insert(std::move(p));

    auto it = owner.find(pRaw);
    if (it != owner.end()) {
        owner.erase(it);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM