Boost logo

Boost :

Subject: Re: [boost] [unordered] unordered_set::erase() complexity bug?
From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2010-03-15 17:24:49


On Mar 15, 2010, at 4:59 PM, Daniel James wrote:

> On 15 March 2010 20:44, Howard Hinnant <howard.hinnant_at_[hidden]> wrote:
>> On Mar 15, 2010, at 4:33 PM, Daniel James wrote:
>>>
>>>> where node_ptr is a nested typedef in the container to unique_ptr<__node, __node_deleter<__node_allocator>>. With this tool one can remove a node from a container, change its value, and insert it back into the container (or another which has an equal allocator). E.g.:
>>>
>>> You probably know better than me, but I don't think you can use
>>> unique_ptr like that with the new allocator requirements.
>>
>> Just for clarity's sake, the unique_ptr should hold a reference to the container's deleter, not a copy. So users of the node_ptr would need to watch out for lifetime issues (the node_ptr should not outlive the container which produced it). I consider that a small price to pay for the functionality/performance gained. But it /is/ inventive.
>
> Sorry, I should have been clearer, I was worried about the node
> pointer. I don't think you can use a '__node*' with the new allocator
> requirements. You can obviously get a pointer by from a reference, but
> there doesn't seem to be a way to get 'allocator_type::pointer' back
> later, since there's no longer a requirement for 'allocator::address'.

Ah! Excellent point. If I'm missing your point just let me know. But the way to handle this is to have your custom deleter make its node_pointer type the same as the node_allocator's node_pointer type. unique_ptr will then use it for the storage.

template <class _Alloc>
class __hash_node_destructor
{
    typedef _Alloc allocator_type;
    typedef allocator_traits<allocator_type> __alloc_traits;
public:
    typedef typename __alloc_traits::pointer pointer; // unique_ptr<T, __hash_node_destructor<A>> uses this for its pointer type
    ...
};

The container will want to rebind its user-supplied allocator into a node allocator and then instantiate its node_destructor with that. Now when the node_destructor calls __alloc_traits::deallocate, it is passing the same pointer type that the allocator expects.

It helps if you already have allocator_traits implemented for all of this.

hth,
Howard


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk