The order is given by the comparator
.
var a = [3,4,1,2]
var h = new MinHeap[Int].default
h.add_all(a)
assert h.peek == 1
var b = h.take_all
assert b == [1, 2, 3, 4]
core :: MinHeap :: comparator=
The comparator used to order the Heapcore :: MinHeap :: defaultinit
serialization :: serialization_core $ MinHeap :: core_serialize_to
Actual serialization ofself
to serializer
serialization :: serialization_core $ MinHeap :: from_deserializer
Create an instance of this class from thedeserializer
core :: Collection :: CONCURRENT
Type of the concurrent variant of this collectionserialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Collection :: combinations
Allr
-length combinations on self (in same order) without repeated elements.
core :: Collection :: combinations_with_replacement
Allr
-length combination on self (in same order) with repeated elements.
core :: MinHeap :: comparator=
The comparator used to order the Heapserialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
core :: Queue :: defaultinit
core :: SimpleCollection :: defaultinit
core :: MinHeap :: defaultinit
core :: Collection :: defaultinit
core :: Object :: defaultinit
serialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
core :: Collection :: has_all
Does the collection contain at least each element ofother
?
core :: Collection :: has_any
Does the collection contain at least one element ofother
?
core :: Collection :: has_exactly
Does the collection contain exactly all the elements ofother
?
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: output_class_name
Display class name on stdout (debug only).core :: Collection :: permutations
Allr
-length permutations on self (all possible ordering) without repeated elements.
core :: Collection :: product
Cartesian product, overr
times self
.
core :: RemovableCollection :: remove
Remove an occurrence ofitem
core :: RemovableCollection :: remove_all
Remove all occurrences ofitem
serialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
core :: Collection :: to_concurrent
Wrapsself
in a thread-safe collection
core :: Collection :: to_counter
Create and fill up a counter with the elements of `self.core :: Collection :: to_curlslist
Convert Collection[String] to CURLSListserialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
core :: Collection :: to_shuffle
Return a new array made of elements in a random order.Serializer::serialize
# A min-heap implemented over an array
#
# The order is given by the `comparator`.
#
# ~~~
# var a = [3,4,1,2]
# var h = new MinHeap[Int].default
# h.add_all(a)
# assert h.peek == 1
# var b = h.take_all
# assert b == [1, 2, 3, 4]
# ~~~
class MinHeap[E: Object]
super Queue[E]
private var items = new Array[E]
# The comparator used to order the Heap
var comparator: Comparator
# Use the `default_comparator` on Comparable elements
#
# Require self isa MinHeap[Comparable]
init default
do
assert self isa MinHeap[Comparable]
init(default_comparator)
end
redef fun is_empty do return items.is_empty
redef fun length do return items.length
redef fun iterator do return items.iterator
redef fun clear do items.clear
redef fun peek do return items.first
redef fun add(e)
do
#assert assert_best else print "try to add {e}"
#var ol = length
var ei = items.length + 1
while ei > 1 do
var pi = ei/2
var p = items[pi-1]
if comparator.compare(p, e) <= 0 then break
# bubble-up
items[ei-1] = p
# next
ei = pi
end
items[ei-1] = e
#assert length == ol + 1
#assert assert_best else print "added {e}"
end
redef fun take do
#assert assert_best else print "tri to take"
#var ol = length
if length < 2 then return items.pop
var res = items.first
var ei = 1
var e = items.pop
var last = items.length
loop
# Check first child
var ci = ei*2
if ci > last then break # no children
var c = items[ci-1]
var upc = comparator.compare(e, c) > 0
# Check second child
var c2i = ci + 1
if c2i <= last then do
var c2 = items[c2i-1]
# possibility to bubble-down to c2, or not?
if comparator.compare(e, c2) <= 0 then break label
# c2 is a better path, or not?
if upc and comparator.compare(c2, c) > 0 then break label
# goes to c2 path
upc = true
c = c2
ci = c2i
end label
# bubble-down?
if not upc then break
items[ei-1] = c
ei = ci
end
items[ei-1] = e
#assert length == ol - 1 else print "is {length}, expected {ol - 1}"
#assert assert_best else print "dequed {res}"
return res
end
# Used internally do debug the Heap.
# Not removed because this can still be useful.
private fun assert_best: Bool
do
if is_empty then return true
var b = peek
for i in self do if comparator.compare(b, i) > 0 then
#print " peek is {b} but better found {i}"
for j in self do
#print " {j}"
end
return false
end
return true
end
end
lib/core/queue.nit:209,1--323,3
redef class MinHeap[E]
redef init from_deserializer(v)
do
v.notify_of_creation self
var items = v.deserialize_attribute("items", (new GetName[SimpleCollection[E]]).to_s)
if not items isa Array[E] then items = new Array[E]
if v.deserialize_attribute_missing then
v.errors.add new AttributeMissingError(self, "items")
end
var comparator = v.deserialize_attribute("comparator", "Comparator")
if not comparator isa Comparator then comparator = default_comparator
if v.deserialize_attribute_missing then
v.errors.add new AttributeMissingError(self, "comparator")
end
init comparator
self.items.add_all items
end
redef fun core_serialize_to(v)
do
v.serialize_attribute("items", items)
v.serialize_attribute("comparator", comparator)
end
end
lib/serialization/serialization_core.nit:363,1--390,3