CX · Reference guideheap
PlaygroundDownloadsAboutv0.18.0-pre.1

heap

Bundled cx-stdlib module.

heap:empty

[$heap:empty] () -> map — The heap with no items: the start of every push chain.

      [?lib 'cx-stdlib/heap' as=heap]
[$heap:peek [$heap:push [$heap:push [$heap:empty] 3] 1]]
    
      1
    

heap:of

[$heap:of] (items::any, opts::map) -> map — The heap of a sequence's or an array's items; an optional {key: FN} orders them by what it answers for each, equal keys in the items' order.

      [?lib 'cx-stdlib/heap']
[$heap:members [$heap:of (5, 1, 4, 1, 3)]]
    
      (1, 1, 3, 4, 5)
    

heap:push

[$heap:push] (h::map, x::any, opts::map) -> map — A new heap holding x besides the items of h, which is unchanged; an optional {key: FN} orders x by what it answers. O(log n).

      [?lib 'cx-stdlib/heap' as=heap]
[$heap:peek [$heap:push [$heap:push [$heap:empty] 3] 1]]
    
      1
    

heap:peek

[$heap:peek] (h::map) -> any — The least item of the heap — the earliest pushed among equal keys — or absence for an empty heap.

      [?lib 'cx-stdlib/heap' as=heap]
[$heap:peek [$heap:push [$heap:push [$heap:empty] 3] 1]]
    
      1
    

heap:pop

[$heap:pop] (h::map) -> map — The heap without its least item; an empty heap refuses CXER0100. O(log n).

      [?lib 'cx-stdlib/heap']
([$heap:peek [$heap:pop [$heap:of (4, 2, 9)]]], [$heap:size [$heap:of (4, 2, 9)]])
    
      (4, 3)
    

heap:pop-min

[$heap:pop-min] (h::map) -> any — The pair (least item, the heap without it) — Python's heappop as a value; an empty heap refuses CXER0100.

      [?lib 'cx-stdlib/heap']
[$first [$heap:pop-min [$heap:of (4, 2, 9)]]]
    
      2
    

heap:size

[$heap:size] (h::map) -> int — The number of items in the heap.

      [?lib 'cx-stdlib/heap']
([$heap:peek [$heap:pop [$heap:of (4, 2, 9)]]], [$heap:size [$heap:of (4, 2, 9)]])
    
      (4, 3)
    

heap:members

[$heap:members] (h::map) -> any — Every item of the heap, least first, equal keys in push order (the drain heappop repeated would give).

      [?lib 'cx-stdlib/heap']
[$heap:members [$heap:of (5, 1, 4, 1, 3)]]
    
      (1, 1, 3, 4, 5)