www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why does std.container.BinaryHeap use RefCounted?

reply "Tobias Pankrath" <tobias pankrath.net> writes:
Original: 
http://forum.dlang.org/thread/uapqmdbtadpwnciwfdwn forum.dlang.org
Reposting here, because I got no answer in D.learn:

std.container.BinrayHeap
(http://dlang.org/library/std/container/BinaryHeap.html)
implements a binary heap on top of a) a given random access range
or b) another container that supports random access.

Regardless of it's underlying data structure type, it wraps its
store in RefCounted, but I don't see why this is necessary.

In case b) the container itself uses reference counting, so
holding a simple reference to a container should be enough.

In case a) the given range might use ref counting under the hood,
see b). Or not, than it is probably relying on the GC, and no
reference count is needed.
Dec 15 2014
parent Shammah Chancellor <email domain.com> writes:
On 2014-12-16 07:57:32 +0000, Tobias Pankrath said:

 Original: http://forum.dlang.org/thread/uapqmdbtadpwnciwfdwn forum.dlang.org
 Reposting here, because I got no answer in D.learn:
 
 std.container.BinrayHeap
 (http://dlang.org/library/std/container/BinaryHeap.html)
 implements a binary heap on top of a) a given random access range
 or b) another container that supports random access.
 
 Regardless of it's underlying data structure type, it wraps its
 store in RefCounted, but I don't see why this is necessary.
 
 In case b) the container itself uses reference counting, so
 holding a simple reference to a container should be enough.
 
 In case a) the given range might use ref counting under the hood,
 see b). Or not, than it is probably relying on the GC, and no
 reference count is needed.
I ran into the same issue with one of the other containers the other day. It was causing me a substantial amount of confusion has it was automatically deallocating things I put into it for reasons I did not care to understand at the time. -Shammah
Dec 16 2014