<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<!-- Generated by HsColour, http://code.haskell.org/~malcolm/hscolour/ -->
<title>GHC/Event/PSQ.hs</title>
<link type='text/css' rel='stylesheet' href='hscolour.css' />
</head>
<body>
<pre><a name="line-1"></a><span class='hs-comment'>{-# LANGUAGE Trustworthy #-}</span>
<a name="line-2"></a><span class='hs-comment'>{-# LANGUAGE BangPatterns, NoImplicitPrelude #-}</span>
<a name="line-3"></a>
<a name="line-4"></a><span class='hs-comment'>-- Copyright (c) 2008, Ralf Hinze</span>
<a name="line-5"></a><span class='hs-comment'>-- All rights reserved.</span>
<a name="line-6"></a><span class='hs-comment'>--</span>
<a name="line-7"></a><span class='hs-comment'>-- Redistribution and use in source and binary forms, with or without</span>
<a name="line-8"></a><span class='hs-comment'>-- modification, are permitted provided that the following conditions</span>
<a name="line-9"></a><span class='hs-comment'>-- are met:</span>
<a name="line-10"></a><span class='hs-comment'>--</span>
<a name="line-11"></a><span class='hs-comment'>--     * Redistributions of source code must retain the above</span>
<a name="line-12"></a><span class='hs-comment'>--       copyright notice, this list of conditions and the following</span>
<a name="line-13"></a><span class='hs-comment'>--       disclaimer.</span>
<a name="line-14"></a><span class='hs-comment'>--</span>
<a name="line-15"></a><span class='hs-comment'>--     * Redistributions in binary form must reproduce the above</span>
<a name="line-16"></a><span class='hs-comment'>--       copyright notice, this list of conditions and the following</span>
<a name="line-17"></a><span class='hs-comment'>--       disclaimer in the documentation and/or other materials</span>
<a name="line-18"></a><span class='hs-comment'>--       provided with the distribution.</span>
<a name="line-19"></a><span class='hs-comment'>--</span>
<a name="line-20"></a><span class='hs-comment'>--     * The names of the contributors may not be used to endorse or</span>
<a name="line-21"></a><span class='hs-comment'>--       promote products derived from this software without specific</span>
<a name="line-22"></a><span class='hs-comment'>--       prior written permission.</span>
<a name="line-23"></a><span class='hs-comment'>--</span>
<a name="line-24"></a><span class='hs-comment'>-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span>
<a name="line-25"></a><span class='hs-comment'>-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span>
<a name="line-26"></a><span class='hs-comment'>-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS</span>
<a name="line-27"></a><span class='hs-comment'>-- FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE</span>
<a name="line-28"></a><span class='hs-comment'>-- COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,</span>
<a name="line-29"></a><span class='hs-comment'>-- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES</span>
<a name="line-30"></a><span class='hs-comment'>-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR</span>
<a name="line-31"></a><span class='hs-comment'>-- SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)</span>
<a name="line-32"></a><span class='hs-comment'>-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,</span>
<a name="line-33"></a><span class='hs-comment'>-- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)</span>
<a name="line-34"></a><span class='hs-comment'>-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED</span>
<a name="line-35"></a><span class='hs-comment'>-- OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<a name="line-36"></a>
<a name="line-37"></a><span class='hs-comment'>-- | A /priority search queue/ (henceforth /queue/) efficiently</span>
<a name="line-38"></a><span class='hs-comment'>-- supports the operations of both a search tree and a priority queue.</span>
<a name="line-39"></a><span class='hs-comment'>-- An 'Elem'ent is a product of a key, a priority, and a</span>
<a name="line-40"></a><span class='hs-comment'>-- value. Elements can be inserted, deleted, modified and queried in</span>
<a name="line-41"></a><span class='hs-comment'>-- logarithmic time, and the element with the least priority can be</span>
<a name="line-42"></a><span class='hs-comment'>-- retrieved in constant time.  A queue can be built from a list of</span>
<a name="line-43"></a><span class='hs-comment'>-- elements, sorted by keys, in linear time.</span>
<a name="line-44"></a><span class='hs-comment'>--</span>
<a name="line-45"></a><span class='hs-comment'>-- This implementation is due to Ralf Hinze with some modifications by</span>
<a name="line-46"></a><span class='hs-comment'>-- Scott Dillard and Johan Tibell.</span>
<a name="line-47"></a><span class='hs-comment'>--</span>
<a name="line-48"></a><span class='hs-comment'>-- * Hinze, R., /A Simple Implementation Technique for Priority Search</span>
<a name="line-49"></a><span class='hs-comment'>-- Queues/, ICFP 2001, pp. 110-121</span>
<a name="line-50"></a><span class='hs-comment'>--</span>
<a name="line-51"></a><span class='hs-comment'>-- &lt;<a href="http://citeseer.ist.psu.edu/hinze01simple.html">http://citeseer.ist.psu.edu/hinze01simple.html</a>&gt;</span>
<a name="line-52"></a><span class='hs-keyword'>module</span> <span class='hs-conid'>GHC</span><span class='hs-varop'>.</span><span class='hs-conid'>Event</span><span class='hs-varop'>.</span><span class='hs-conid'>PSQ</span>
<a name="line-53"></a>    <span class='hs-layout'>(</span>
<a name="line-54"></a>    <span class='hs-comment'>-- * Binding Type</span>
<a name="line-55"></a>    <span class='hs-conid'>Elem</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span>
<a name="line-56"></a>    <span class='hs-layout'>,</span> <span class='hs-conid'>Key</span>
<a name="line-57"></a>    <span class='hs-layout'>,</span> <span class='hs-conid'>Prio</span>
<a name="line-58"></a>
<a name="line-59"></a>    <span class='hs-comment'>-- * Priority Search Queue Type</span>
<a name="line-60"></a>    <span class='hs-layout'>,</span> <span class='hs-conid'>PSQ</span>
<a name="line-61"></a>
<a name="line-62"></a>    <span class='hs-comment'>-- * Query</span>
<a name="line-63"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>size</span>
<a name="line-64"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>null</span>
<a name="line-65"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>lookup</span>
<a name="line-66"></a>
<a name="line-67"></a>    <span class='hs-comment'>-- * Construction</span>
<a name="line-68"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>empty</span>
<a name="line-69"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>singleton</span>
<a name="line-70"></a>
<a name="line-71"></a>    <span class='hs-comment'>-- * Insertion</span>
<a name="line-72"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>insert</span>
<a name="line-73"></a>
<a name="line-74"></a>    <span class='hs-comment'>-- * Delete/Update</span>
<a name="line-75"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>delete</span>
<a name="line-76"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>adjust</span>
<a name="line-77"></a>
<a name="line-78"></a>    <span class='hs-comment'>-- * Conversion</span>
<a name="line-79"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>toList</span>
<a name="line-80"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>toAscList</span>
<a name="line-81"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>toDescList</span>
<a name="line-82"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>fromList</span>
<a name="line-83"></a>
<a name="line-84"></a>    <span class='hs-comment'>-- * Min</span>
<a name="line-85"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>findMin</span>
<a name="line-86"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>deleteMin</span>
<a name="line-87"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>minView</span>
<a name="line-88"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>atMost</span>
<a name="line-89"></a>    <span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-90"></a>
<a name="line-91"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>GHC</span><span class='hs-varop'>.</span><span class='hs-conid'>Base</span> <span class='hs-varid'>hiding</span> <span class='hs-layout'>(</span><span class='hs-varid'>empty</span><span class='hs-layout'>)</span>
<a name="line-92"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>GHC</span><span class='hs-varop'>.</span><span class='hs-conid'>Num</span> <span class='hs-layout'>(</span><span class='hs-conid'>Num</span><span class='hs-layout'>(</span><span class='hs-keyglyph'>..</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-93"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>GHC</span><span class='hs-varop'>.</span><span class='hs-conid'>Show</span> <span class='hs-layout'>(</span><span class='hs-conid'>Show</span><span class='hs-layout'>(</span><span class='hs-varid'>showsPrec</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-94"></a><span class='hs-keyword'>import</span> <span class='hs-conid'>GHC</span><span class='hs-varop'>.</span><span class='hs-conid'>Event</span><span class='hs-varop'>.</span><span class='hs-conid'>Unique</span> <span class='hs-layout'>(</span><span class='hs-conid'>Unique</span><span class='hs-layout'>)</span>
<a name="line-95"></a>
<a name="line-96"></a><a name="Elem"></a><span class='hs-comment'>-- | @E k p@ binds the key @k@ with the priority @p@.</span>
<a name="line-97"></a><a name="Elem"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>E</span>
<a name="line-98"></a>    <span class='hs-layout'>{</span> <span class='hs-varid'>key</span>   <span class='hs-keyglyph'>::</span> <span class='hs-comment'>{-# UNPACK #-}</span> <span class='hs-varop'>!</span><span class='hs-conid'>Key</span>
<a name="line-99"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>prio</span>  <span class='hs-keyglyph'>::</span> <span class='hs-comment'>{-# UNPACK #-}</span> <span class='hs-varop'>!</span><span class='hs-conid'>Prio</span>
<a name="line-100"></a>    <span class='hs-layout'>,</span> <span class='hs-varid'>value</span> <span class='hs-keyglyph'>::</span> <span class='hs-varid'>a</span>
<a name="line-101"></a>    <span class='hs-layout'>}</span> <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span><span class='hs-layout'>)</span>
<a name="line-102"></a>
<a name="line-103"></a><span class='hs-comment'>------------------------------------------------------------------------</span>
<a name="line-104"></a><span class='hs-comment'>-- | A mapping from keys @k@ to priorites @p@.</span>
<a name="line-105"></a>
<a name="line-106"></a><a name="Prio"></a><span class='hs-keyword'>type</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Double</span>
<a name="line-107"></a><a name="Key"></a><span class='hs-keyword'>type</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Unique</span>
<a name="line-108"></a>
<a name="line-109"></a><a name="PSQ"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Void</span>
<a name="line-110"></a>           <span class='hs-keyglyph'>|</span> <span class='hs-conid'>Winner</span> <span class='hs-comment'>{-# UNPACK #-}</span> <span class='hs-varop'>!</span><span class='hs-layout'>(</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-111"></a>                    <span class='hs-varop'>!</span><span class='hs-layout'>(</span><span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-112"></a>                    <span class='hs-comment'>{-# UNPACK #-}</span> <span class='hs-varop'>!</span><span class='hs-conid'>Key</span>  <span class='hs-comment'>-- max key</span>
<a name="line-113"></a>           <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span><span class='hs-layout'>)</span>
<a name="line-114"></a>
<a name="line-115"></a><a name="size"></a><span class='hs-comment'>-- | /O(1)/ The number of elements in a queue.</span>
<a name="line-116"></a><span class='hs-definition'>size</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Int</span>
<a name="line-117"></a><span class='hs-definition'>size</span> <span class='hs-conid'>Void</span>            <span class='hs-keyglyph'>=</span> <span class='hs-num'>0</span>
<a name="line-118"></a><span class='hs-definition'>size</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>lt</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>1</span> <span class='hs-varop'>+</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>lt</span>
<a name="line-119"></a>
<a name="line-120"></a><a name="null"></a><span class='hs-comment'>-- | /O(1)/ True if the queue is empty.</span>
<a name="line-121"></a><span class='hs-definition'>null</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Bool</span>
<a name="line-122"></a><span class='hs-definition'>null</span> <span class='hs-conid'>Void</span>           <span class='hs-keyglyph'>=</span> <span class='hs-conid'>True</span>
<a name="line-123"></a><span class='hs-definition'>null</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>False</span>
<a name="line-124"></a>
<a name="line-125"></a><a name="lookup"></a><span class='hs-comment'>-- | /O(log n)/ The priority and value of a given key, or Nothing if</span>
<a name="line-126"></a><span class='hs-comment'>-- the key is not bound.</span>
<a name="line-127"></a><span class='hs-definition'>lookup</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Maybe</span> <span class='hs-layout'>(</span><span class='hs-conid'>Prio</span><span class='hs-layout'>,</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-128"></a><span class='hs-definition'>lookup</span> <span class='hs-varid'>k</span> <span class='hs-varid'>q</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>tourView</span> <span class='hs-varid'>q</span> <span class='hs-keyword'>of</span>
<a name="line-129"></a>    <span class='hs-conid'>Null</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Nothing</span>
<a name="line-130"></a>    <span class='hs-conid'>Single</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k'</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span>
<a name="line-131"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>k</span> <span class='hs-varop'>==</span> <span class='hs-varid'>k'</span>   <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Just</span> <span class='hs-layout'>(</span><span class='hs-varid'>p</span><span class='hs-layout'>,</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span>
<a name="line-132"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Nothing</span>
<a name="line-133"></a>    <span class='hs-varid'>tl</span> <span class='hs-varop'>`Play`</span> <span class='hs-varid'>tr</span>
<a name="line-134"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>k</span> <span class='hs-varop'>&lt;=</span> <span class='hs-varid'>maxKey</span> <span class='hs-varid'>tl</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>lookup</span> <span class='hs-varid'>k</span> <span class='hs-varid'>tl</span>
<a name="line-135"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span>      <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>lookup</span> <span class='hs-varid'>k</span> <span class='hs-varid'>tr</span>
<a name="line-136"></a>
<a name="line-137"></a><span class='hs-comment'>------------------------------------------------------------------------</span>
<a name="line-138"></a><span class='hs-comment'>-- Construction</span>
<a name="line-139"></a>
<a name="line-140"></a><a name="empty"></a><span class='hs-definition'>empty</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span>
<a name="line-141"></a><span class='hs-definition'>empty</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Void</span>
<a name="line-142"></a>
<a name="line-143"></a><a name="singleton"></a><span class='hs-comment'>-- | /O(1)/ Build a queue with one element.</span>
<a name="line-144"></a><span class='hs-definition'>singleton</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span>
<a name="line-145"></a><span class='hs-definition'>singleton</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Winner</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-conid'>Start</span> <span class='hs-varid'>k</span>
<a name="line-146"></a>
<a name="line-147"></a><span class='hs-comment'>------------------------------------------------------------------------</span>
<a name="line-148"></a><span class='hs-comment'>-- Insertion</span>
<a name="line-149"></a>
<a name="line-150"></a><a name="insert"></a><span class='hs-comment'>-- | /O(log n)/ Insert a new key, priority and value in the queue.  If</span>
<a name="line-151"></a><span class='hs-comment'>-- the key is already present in the queue, the associated priority</span>
<a name="line-152"></a><span class='hs-comment'>-- and value are replaced with the supplied priority and value.</span>
<a name="line-153"></a><span class='hs-definition'>insert</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span>
<a name="line-154"></a><span class='hs-definition'>insert</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>q</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>q</span> <span class='hs-keyword'>of</span>
<a name="line-155"></a>    <span class='hs-conid'>Void</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>singleton</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span>
<a name="line-156"></a>    <span class='hs-conid'>Winner</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k'</span> <span class='hs-varid'>p'</span> <span class='hs-varid'>v'</span><span class='hs-layout'>)</span> <span class='hs-conid'>Start</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>compare</span> <span class='hs-varid'>k</span> <span class='hs-varid'>k'</span> <span class='hs-keyword'>of</span>
<a name="line-157"></a>        <span class='hs-conid'>LT</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>singleton</span> <span class='hs-varid'>k</span>  <span class='hs-varid'>p</span>  <span class='hs-varid'>v</span>  <span class='hs-varop'>`play`</span> <span class='hs-varid'>singleton</span> <span class='hs-varid'>k'</span> <span class='hs-varid'>p'</span> <span class='hs-varid'>v'</span>
<a name="line-158"></a>        <span class='hs-conid'>EQ</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>singleton</span> <span class='hs-varid'>k</span>  <span class='hs-varid'>p</span>  <span class='hs-varid'>v</span>
<a name="line-159"></a>        <span class='hs-conid'>GT</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>singleton</span> <span class='hs-varid'>k'</span> <span class='hs-varid'>p'</span> <span class='hs-varid'>v'</span> <span class='hs-varop'>`play`</span> <span class='hs-varid'>singleton</span> <span class='hs-varid'>k</span>  <span class='hs-varid'>p</span>  <span class='hs-varid'>v</span>
<a name="line-160"></a>    <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span>
<a name="line-161"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>k</span> <span class='hs-varop'>&lt;=</span> <span class='hs-varid'>m</span>    <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>insert</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`play`</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-162"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`play`</span> <span class='hs-varid'>insert</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-163"></a>    <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span>
<a name="line-164"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>k</span> <span class='hs-varop'>&lt;=</span> <span class='hs-varid'>m</span>    <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>insert</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`play`</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-165"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`play`</span> <span class='hs-varid'>insert</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-166"></a>
<a name="line-167"></a><span class='hs-comment'>------------------------------------------------------------------------</span>
<a name="line-168"></a><span class='hs-comment'>-- Delete/Update</span>
<a name="line-169"></a>
<a name="line-170"></a><a name="delete"></a><span class='hs-comment'>-- | /O(log n)/ Delete a key and its priority and value from the</span>
<a name="line-171"></a><span class='hs-comment'>-- queue.  When the key is not a member of the queue, the original</span>
<a name="line-172"></a><span class='hs-comment'>-- queue is returned.</span>
<a name="line-173"></a><span class='hs-definition'>delete</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span>
<a name="line-174"></a><span class='hs-definition'>delete</span> <span class='hs-varid'>k</span> <span class='hs-varid'>q</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>q</span> <span class='hs-keyword'>of</span>
<a name="line-175"></a>    <span class='hs-conid'>Void</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>empty</span>
<a name="line-176"></a>    <span class='hs-conid'>Winner</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k'</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-conid'>Start</span> <span class='hs-keyword'>_</span>
<a name="line-177"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>k</span> <span class='hs-varop'>==</span> <span class='hs-varid'>k'</span>   <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>empty</span>
<a name="line-178"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>singleton</span> <span class='hs-varid'>k'</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span>
<a name="line-179"></a>    <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span>
<a name="line-180"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>k</span> <span class='hs-varop'>&lt;=</span> <span class='hs-varid'>m</span>    <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>delete</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`play`</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-181"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`play`</span> <span class='hs-varid'>delete</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-182"></a>    <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span>
<a name="line-183"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>k</span> <span class='hs-varop'>&lt;=</span> <span class='hs-varid'>m</span>    <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>delete</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`play`</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-184"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`play`</span> <span class='hs-varid'>delete</span> <span class='hs-varid'>k</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-185"></a>
<a name="line-186"></a><a name="adjust"></a><span class='hs-comment'>-- | /O(log n)/ Update a priority at a specific key with the result</span>
<a name="line-187"></a><span class='hs-comment'>-- of the provided function.  When the key is not a member of the</span>
<a name="line-188"></a><span class='hs-comment'>-- queue, the original queue is returned.</span>
<a name="line-189"></a><span class='hs-definition'>adjust</span> <span class='hs-keyglyph'>::</span> <span class='hs-layout'>(</span><span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span>
<a name="line-190"></a><span class='hs-definition'>adjust</span> <span class='hs-varid'>f</span> <span class='hs-varid'>k</span> <span class='hs-varid'>q0</span> <span class='hs-keyglyph'>=</span>  <span class='hs-varid'>go</span> <span class='hs-varid'>q0</span>
<a name="line-191"></a>  <span class='hs-keyword'>where</span>
<a name="line-192"></a>    <span class='hs-varid'>go</span> <span class='hs-varid'>q</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>q</span> <span class='hs-keyword'>of</span>
<a name="line-193"></a>        <span class='hs-conid'>Void</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>empty</span>
<a name="line-194"></a>        <span class='hs-conid'>Winner</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k'</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-conid'>Start</span> <span class='hs-keyword'>_</span>
<a name="line-195"></a>            <span class='hs-keyglyph'>|</span> <span class='hs-varid'>k</span> <span class='hs-varop'>==</span> <span class='hs-varid'>k'</span>   <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>singleton</span> <span class='hs-varid'>k'</span> <span class='hs-layout'>(</span><span class='hs-varid'>f</span> <span class='hs-varid'>p</span><span class='hs-layout'>)</span> <span class='hs-varid'>v</span>
<a name="line-196"></a>            <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>singleton</span> <span class='hs-varid'>k'</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span>
<a name="line-197"></a>        <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span>
<a name="line-198"></a>            <span class='hs-keyglyph'>|</span> <span class='hs-varid'>k</span> <span class='hs-varop'>&lt;=</span> <span class='hs-varid'>m</span>    <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>go</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`unsafePlay`</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-199"></a>            <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`unsafePlay`</span> <span class='hs-varid'>go</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-200"></a>        <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span>
<a name="line-201"></a>            <span class='hs-keyglyph'>|</span> <span class='hs-varid'>k</span> <span class='hs-varop'>&lt;=</span> <span class='hs-varid'>m</span>    <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>go</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`unsafePlay`</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-202"></a>            <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-varop'>`unsafePlay`</span> <span class='hs-varid'>go</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-203"></a><span class='hs-comment'>{-# INLINE adjust #-}</span>
<a name="line-204"></a>
<a name="line-205"></a><span class='hs-comment'>------------------------------------------------------------------------</span>
<a name="line-206"></a><span class='hs-comment'>-- Conversion</span>
<a name="line-207"></a>
<a name="line-208"></a><a name="fromList"></a><span class='hs-comment'>-- | /O(n*log n)/ Build a queue from a list of key/priority/value</span>
<a name="line-209"></a><span class='hs-comment'>-- tuples.  If the list contains more than one priority and value for</span>
<a name="line-210"></a><span class='hs-comment'>-- the same key, the last priority and value for the key is retained.</span>
<a name="line-211"></a><span class='hs-definition'>fromList</span> <span class='hs-keyglyph'>::</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span>
<a name="line-212"></a><span class='hs-definition'>fromList</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>foldr</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-varid'>q</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>insert</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>q</span><span class='hs-layout'>)</span> <span class='hs-varid'>empty</span>
<a name="line-213"></a>
<a name="line-214"></a><a name="toList"></a><span class='hs-comment'>-- | /O(n)/ Convert to a list of key/priority/value tuples.</span>
<a name="line-215"></a><span class='hs-definition'>toList</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span>
<a name="line-216"></a><span class='hs-definition'>toList</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>toAscList</span>
<a name="line-217"></a>
<a name="line-218"></a><a name="toAscList"></a><span class='hs-comment'>-- | /O(n)/ Convert to an ascending list.</span>
<a name="line-219"></a><span class='hs-definition'>toAscList</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-keyglyph'>[</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span>
<a name="line-220"></a><span class='hs-definition'>toAscList</span> <span class='hs-varid'>q</span>  <span class='hs-keyglyph'>=</span> <span class='hs-varid'>seqToList</span> <span class='hs-layout'>(</span><span class='hs-varid'>toAscLists</span> <span class='hs-varid'>q</span><span class='hs-layout'>)</span>
<a name="line-221"></a>
<a name="line-222"></a><a name="toAscLists"></a><span class='hs-definition'>toAscLists</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Sequ</span> <span class='hs-layout'>(</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-223"></a><span class='hs-definition'>toAscLists</span> <span class='hs-varid'>q</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>tourView</span> <span class='hs-varid'>q</span> <span class='hs-keyword'>of</span>
<a name="line-224"></a>    <span class='hs-conid'>Null</span>         <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>emptySequ</span>
<a name="line-225"></a>    <span class='hs-conid'>Single</span> <span class='hs-varid'>e</span>     <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>singleSequ</span> <span class='hs-varid'>e</span>
<a name="line-226"></a>    <span class='hs-varid'>tl</span> <span class='hs-varop'>`Play`</span> <span class='hs-varid'>tr</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>toAscLists</span> <span class='hs-varid'>tl</span> <span class='hs-varop'>&lt;&gt;</span> <span class='hs-varid'>toAscLists</span> <span class='hs-varid'>tr</span>
<a name="line-227"></a>
<a name="line-228"></a><a name="toDescList"></a><span class='hs-comment'>-- | /O(n)/ Convert to a descending list.</span>
<a name="line-229"></a><span class='hs-definition'>toDescList</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-keyglyph'>[</span> <span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>]</span>
<a name="line-230"></a><span class='hs-definition'>toDescList</span> <span class='hs-varid'>q</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>seqToList</span> <span class='hs-layout'>(</span><span class='hs-varid'>toDescLists</span> <span class='hs-varid'>q</span><span class='hs-layout'>)</span>
<a name="line-231"></a>
<a name="line-232"></a><a name="toDescLists"></a><span class='hs-definition'>toDescLists</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Sequ</span> <span class='hs-layout'>(</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-233"></a><span class='hs-definition'>toDescLists</span> <span class='hs-varid'>q</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>tourView</span> <span class='hs-varid'>q</span> <span class='hs-keyword'>of</span>
<a name="line-234"></a>    <span class='hs-conid'>Null</span>         <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>emptySequ</span>
<a name="line-235"></a>    <span class='hs-conid'>Single</span> <span class='hs-varid'>e</span>     <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>singleSequ</span> <span class='hs-varid'>e</span>
<a name="line-236"></a>    <span class='hs-varid'>tl</span> <span class='hs-varop'>`Play`</span> <span class='hs-varid'>tr</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>toDescLists</span> <span class='hs-varid'>tr</span> <span class='hs-varop'>&lt;&gt;</span> <span class='hs-varid'>toDescLists</span> <span class='hs-varid'>tl</span>
<a name="line-237"></a>
<a name="line-238"></a><span class='hs-comment'>------------------------------------------------------------------------</span>
<a name="line-239"></a><span class='hs-comment'>-- Min</span>
<a name="line-240"></a>
<a name="line-241"></a><a name="findMin"></a><span class='hs-comment'>-- | /O(1)/ The element with the lowest priority.</span>
<a name="line-242"></a><span class='hs-definition'>findMin</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Maybe</span> <span class='hs-layout'>(</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-243"></a><span class='hs-definition'>findMin</span> <span class='hs-conid'>Void</span>           <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Nothing</span>
<a name="line-244"></a><span class='hs-definition'>findMin</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-varid'>e</span>
<a name="line-245"></a>
<a name="line-246"></a><a name="deleteMin"></a><span class='hs-comment'>-- | /O(log n)/ Delete the element with the lowest priority.  Returns</span>
<a name="line-247"></a><span class='hs-comment'>-- an empty queue if the queue is empty.</span>
<a name="line-248"></a><span class='hs-definition'>deleteMin</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span>
<a name="line-249"></a><span class='hs-definition'>deleteMin</span> <span class='hs-conid'>Void</span>           <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Void</span>
<a name="line-250"></a><span class='hs-definition'>deleteMin</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>t</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>secondBest</span> <span class='hs-varid'>t</span> <span class='hs-varid'>m</span>
<a name="line-251"></a>
<a name="line-252"></a><a name="minView"></a><span class='hs-comment'>-- | /O(log n)/ Retrieve the binding with the least priority, and the</span>
<a name="line-253"></a><span class='hs-comment'>-- rest of the queue stripped of that binding.</span>
<a name="line-254"></a><span class='hs-definition'>minView</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Maybe</span> <span class='hs-layout'>(</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-layout'>,</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-255"></a><span class='hs-definition'>minView</span> <span class='hs-conid'>Void</span>           <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Nothing</span>
<a name="line-256"></a><span class='hs-definition'>minView</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>t</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Just</span> <span class='hs-layout'>(</span><span class='hs-varid'>e</span><span class='hs-layout'>,</span> <span class='hs-varid'>secondBest</span> <span class='hs-varid'>t</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span>
<a name="line-257"></a>
<a name="line-258"></a><a name="secondBest"></a><span class='hs-definition'>secondBest</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span>
<a name="line-259"></a><span class='hs-definition'>secondBest</span> <span class='hs-conid'>Start</span> <span class='hs-keyword'>_</span>                 <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Void</span>
<a name="line-260"></a><span class='hs-definition'>secondBest</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varop'>`play`</span> <span class='hs-varid'>secondBest</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span>
<a name="line-261"></a><span class='hs-definition'>secondBest</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>secondBest</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varop'>`play`</span> <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span>
<a name="line-262"></a>
<a name="line-263"></a><a name="atMost"></a><span class='hs-comment'>-- | /O(r*(log n - log r))/ Return a list of elements ordered by</span>
<a name="line-264"></a><span class='hs-comment'>-- key whose priorities are at most @pt@.</span>
<a name="line-265"></a><span class='hs-definition'>atMost</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>[</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>,</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-266"></a><span class='hs-definition'>atMost</span> <span class='hs-varid'>pt</span> <span class='hs-varid'>q</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>let</span> <span class='hs-layout'>(</span><span class='hs-varid'>sequ</span><span class='hs-layout'>,</span> <span class='hs-varid'>q'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>atMosts</span> <span class='hs-varid'>pt</span> <span class='hs-varid'>q</span>
<a name="line-267"></a>              <span class='hs-keyword'>in</span> <span class='hs-layout'>(</span><span class='hs-varid'>seqToList</span> <span class='hs-varid'>sequ</span><span class='hs-layout'>,</span> <span class='hs-varid'>q'</span><span class='hs-layout'>)</span>
<a name="line-268"></a>
<a name="line-269"></a><a name="atMosts"></a><span class='hs-definition'>atMosts</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-layout'>(</span><span class='hs-conid'>Sequ</span> <span class='hs-layout'>(</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span><span class='hs-layout'>,</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-270"></a><a name="!"></a><span class='hs-definition'>atMosts</span> <span class='hs-varop'>!</span><span class='hs-varid'>pt</span> <span class='hs-varid'>q</span> <span class='hs-keyglyph'>=</span> <span class='hs-keyword'>case</span> <span class='hs-varid'>q</span> <span class='hs-keyword'>of</span>
<a name="line-271"></a>    <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span>
<a name="line-272"></a>        <span class='hs-keyglyph'>|</span> <span class='hs-varid'>prio</span> <span class='hs-varid'>e</span> <span class='hs-varop'>&gt;</span> <span class='hs-varid'>pt</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-layout'>(</span><span class='hs-varid'>emptySequ</span><span class='hs-layout'>,</span> <span class='hs-varid'>q</span><span class='hs-layout'>)</span>
<a name="line-273"></a>    <span class='hs-conid'>Void</span>              <span class='hs-keyglyph'>-&gt;</span> <span class='hs-layout'>(</span><span class='hs-varid'>emptySequ</span><span class='hs-layout'>,</span> <span class='hs-conid'>Void</span><span class='hs-layout'>)</span>
<a name="line-274"></a>    <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-conid'>Start</span> <span class='hs-keyword'>_</span>  <span class='hs-keyglyph'>-&gt;</span> <span class='hs-layout'>(</span><span class='hs-varid'>singleSequ</span> <span class='hs-varid'>e</span><span class='hs-layout'>,</span> <span class='hs-conid'>Void</span><span class='hs-layout'>)</span>
<a name="line-275"></a>    <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span> <span class='hs-keyglyph'>-&gt;</span>
<a name="line-276"></a>        <span class='hs-keyword'>let</span> <span class='hs-layout'>(</span><span class='hs-varid'>sequ</span><span class='hs-layout'>,</span> <span class='hs-varid'>q'</span><span class='hs-layout'>)</span>   <span class='hs-keyglyph'>=</span> <span class='hs-varid'>atMosts</span> <span class='hs-varid'>pt</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span>
<a name="line-277"></a>            <span class='hs-layout'>(</span><span class='hs-varid'>sequ'</span><span class='hs-layout'>,</span> <span class='hs-varid'>q''</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>atMosts</span> <span class='hs-varid'>pt</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-278"></a>        <span class='hs-keyword'>in</span> <span class='hs-layout'>(</span><span class='hs-varid'>sequ</span> <span class='hs-varop'>&lt;&gt;</span> <span class='hs-varid'>sequ'</span><span class='hs-layout'>,</span> <span class='hs-varid'>q'</span> <span class='hs-varop'>`play`</span> <span class='hs-varid'>q''</span><span class='hs-layout'>)</span>
<a name="line-279"></a>    <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span> <span class='hs-keyglyph'>-&gt;</span>
<a name="line-280"></a>        <span class='hs-keyword'>let</span> <span class='hs-layout'>(</span><span class='hs-varid'>sequ</span><span class='hs-layout'>,</span> <span class='hs-varid'>q'</span><span class='hs-layout'>)</span>   <span class='hs-keyglyph'>=</span> <span class='hs-varid'>atMosts</span> <span class='hs-varid'>pt</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span>
<a name="line-281"></a>            <span class='hs-layout'>(</span><span class='hs-varid'>sequ'</span><span class='hs-layout'>,</span> <span class='hs-varid'>q''</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>atMosts</span> <span class='hs-varid'>pt</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span>
<a name="line-282"></a>        <span class='hs-keyword'>in</span> <span class='hs-layout'>(</span><span class='hs-varid'>sequ</span> <span class='hs-varop'>&lt;&gt;</span> <span class='hs-varid'>sequ'</span><span class='hs-layout'>,</span> <span class='hs-varid'>q'</span> <span class='hs-varop'>`play`</span> <span class='hs-varid'>q''</span><span class='hs-layout'>)</span>
<a name="line-283"></a>
<a name="line-284"></a><span class='hs-comment'>------------------------------------------------------------------------</span>
<a name="line-285"></a><span class='hs-comment'>-- Loser tree</span>
<a name="line-286"></a>
<a name="line-287"></a><a name="Size"></a><span class='hs-keyword'>type</span> <span class='hs-conid'>Size</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Int</span>
<a name="line-288"></a>
<a name="line-289"></a><a name="LTree"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Start</span>
<a name="line-290"></a>             <span class='hs-keyglyph'>|</span> <span class='hs-conid'>LLoser</span> <span class='hs-comment'>{-# UNPACK #-}</span> <span class='hs-varop'>!</span><span class='hs-conid'>Size</span>
<a name="line-291"></a>                      <span class='hs-comment'>{-# UNPACK #-}</span> <span class='hs-varop'>!</span><span class='hs-layout'>(</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-292"></a>                      <span class='hs-varop'>!</span><span class='hs-layout'>(</span><span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-293"></a>                      <span class='hs-comment'>{-# UNPACK #-}</span> <span class='hs-varop'>!</span><span class='hs-conid'>Key</span>  <span class='hs-comment'>-- split key</span>
<a name="line-294"></a>                      <span class='hs-varop'>!</span><span class='hs-layout'>(</span><span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-295"></a>             <span class='hs-keyglyph'>|</span> <span class='hs-conid'>RLoser</span> <span class='hs-comment'>{-# UNPACK #-}</span> <span class='hs-varop'>!</span><span class='hs-conid'>Size</span>
<a name="line-296"></a>                      <span class='hs-comment'>{-# UNPACK #-}</span> <span class='hs-varop'>!</span><span class='hs-layout'>(</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-297"></a>                      <span class='hs-varop'>!</span><span class='hs-layout'>(</span><span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-298"></a>                      <span class='hs-comment'>{-# UNPACK #-}</span> <span class='hs-varop'>!</span><span class='hs-conid'>Key</span>  <span class='hs-comment'>-- split key</span>
<a name="line-299"></a>                      <span class='hs-varop'>!</span><span class='hs-layout'>(</span><span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-300"></a>             <span class='hs-keyword'>deriving</span> <span class='hs-layout'>(</span><span class='hs-conid'>Eq</span><span class='hs-layout'>,</span> <span class='hs-conid'>Show</span><span class='hs-layout'>)</span>
<a name="line-301"></a>
<a name="line-302"></a><a name="size'"></a><span class='hs-definition'>size'</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Size</span>
<a name="line-303"></a><span class='hs-definition'>size'</span> <span class='hs-conid'>Start</span>              <span class='hs-keyglyph'>=</span> <span class='hs-num'>0</span>
<a name="line-304"></a><span class='hs-definition'>size'</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-varid'>s</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>s</span>
<a name="line-305"></a><span class='hs-definition'>size'</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-varid'>s</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>s</span>
<a name="line-306"></a>
<a name="line-307"></a><a name="left"></a><span class='hs-definition'>left</span><span class='hs-layout'>,</span> <span class='hs-varid'>right</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-308"></a>
<a name="line-309"></a><span class='hs-definition'>left</span> <span class='hs-conid'>Start</span>                <span class='hs-keyglyph'>=</span> <span class='hs-varid'>moduleError</span> <span class='hs-str'>"left"</span> <span class='hs-str'>"empty loser tree"</span>
<a name="line-310"></a><span class='hs-definition'>left</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>tl</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>tl</span>
<a name="line-311"></a><span class='hs-definition'>left</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>tl</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>tl</span>
<a name="line-312"></a>
<a name="line-313"></a><a name="right"></a><span class='hs-definition'>right</span> <span class='hs-conid'>Start</span>                <span class='hs-keyglyph'>=</span> <span class='hs-varid'>moduleError</span> <span class='hs-str'>"right"</span> <span class='hs-str'>"empty loser tree"</span>
<a name="line-314"></a><span class='hs-definition'>right</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span>  <span class='hs-keyword'>_</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>tr</span>
<a name="line-315"></a><span class='hs-definition'>right</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span>  <span class='hs-keyword'>_</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>tr</span>
<a name="line-316"></a>
<a name="line-317"></a><a name="maxKey"></a><span class='hs-definition'>maxKey</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span>
<a name="line-318"></a><span class='hs-definition'>maxKey</span> <span class='hs-conid'>Void</span>           <span class='hs-keyglyph'>=</span> <span class='hs-varid'>moduleError</span> <span class='hs-str'>"maxKey"</span> <span class='hs-str'>"empty queue"</span>
<a name="line-319"></a><span class='hs-definition'>maxKey</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>m</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>m</span>
<a name="line-320"></a>
<a name="line-321"></a><a name="lloser"></a><span class='hs-definition'>lloser</span><span class='hs-layout'>,</span> <span class='hs-varid'>rloser</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-322"></a><span class='hs-definition'>lloser</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>LLoser</span> <span class='hs-layout'>(</span><span class='hs-num'>1</span> <span class='hs-varop'>+</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>tl</span> <span class='hs-varop'>+</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span>
<a name="line-323"></a><a name="rloser"></a><span class='hs-definition'>rloser</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>RLoser</span> <span class='hs-layout'>(</span><span class='hs-num'>1</span> <span class='hs-varop'>+</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>tl</span> <span class='hs-varop'>+</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span>
<a name="line-324"></a>
<a name="line-325"></a><span class='hs-comment'>------------------------------------------------------------------------</span>
<a name="line-326"></a><span class='hs-comment'>-- Balancing</span>
<a name="line-327"></a>
<a name="line-328"></a><a name="omega"></a><span class='hs-comment'>-- | Balance factor</span>
<a name="line-329"></a><span class='hs-definition'>omega</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Int</span>
<a name="line-330"></a><span class='hs-definition'>omega</span> <span class='hs-keyglyph'>=</span> <span class='hs-num'>4</span>
<a name="line-331"></a>
<a name="line-332"></a><a name="lbalance"></a><span class='hs-definition'>lbalance</span><span class='hs-layout'>,</span> <span class='hs-varid'>rbalance</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-333"></a>
<a name="line-334"></a><span class='hs-definition'>lbalance</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-335"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>l</span> <span class='hs-varop'>+</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>r</span> <span class='hs-varop'>&lt;</span> <span class='hs-num'>2</span>     <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lloser</span>        <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-336"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>r</span> <span class='hs-varop'>&gt;</span> <span class='hs-varid'>omega</span> <span class='hs-varop'>*</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>l</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lbalanceLeft</span>  <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-337"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>l</span> <span class='hs-varop'>&gt;</span> <span class='hs-varid'>omega</span> <span class='hs-varop'>*</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>r</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lbalanceRight</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-338"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span>                 <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lloser</span>        <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-339"></a>
<a name="line-340"></a><a name="rbalance"></a><span class='hs-definition'>rbalance</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-341"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>l</span> <span class='hs-varop'>+</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>r</span> <span class='hs-varop'>&lt;</span> <span class='hs-num'>2</span>     <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rloser</span>        <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-342"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>r</span> <span class='hs-varop'>&gt;</span> <span class='hs-varid'>omega</span> <span class='hs-varop'>*</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>l</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rbalanceLeft</span>  <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-343"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>l</span> <span class='hs-varop'>&gt;</span> <span class='hs-varid'>omega</span> <span class='hs-varop'>*</span> <span class='hs-varid'>size'</span> <span class='hs-varid'>r</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rbalanceRight</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-344"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span>                 <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rloser</span>        <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-345"></a>
<a name="line-346"></a><a name="lbalanceLeft"></a><span class='hs-definition'>lbalanceLeft</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-347"></a><span class='hs-definition'>lbalanceLeft</span>  <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-348"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>size'</span> <span class='hs-layout'>(</span><span class='hs-varid'>left</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-varop'>&lt;</span> <span class='hs-varid'>size'</span> <span class='hs-layout'>(</span><span class='hs-varid'>right</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lsingleLeft</span>  <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-349"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span>                        <span class='hs-keyglyph'>=</span> <span class='hs-varid'>ldoubleLeft</span>  <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-350"></a>
<a name="line-351"></a><a name="lbalanceRight"></a><span class='hs-definition'>lbalanceRight</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-352"></a><span class='hs-definition'>lbalanceRight</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-353"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>size'</span> <span class='hs-layout'>(</span><span class='hs-varid'>left</span> <span class='hs-varid'>l</span><span class='hs-layout'>)</span> <span class='hs-varop'>&gt;</span> <span class='hs-varid'>size'</span> <span class='hs-layout'>(</span><span class='hs-varid'>right</span> <span class='hs-varid'>l</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lsingleRight</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-354"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span>                        <span class='hs-keyglyph'>=</span> <span class='hs-varid'>ldoubleRight</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-355"></a>
<a name="line-356"></a><a name="rbalanceLeft"></a><span class='hs-definition'>rbalanceLeft</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-357"></a><span class='hs-definition'>rbalanceLeft</span>  <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-358"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>size'</span> <span class='hs-layout'>(</span><span class='hs-varid'>left</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-varop'>&lt;</span> <span class='hs-varid'>size'</span> <span class='hs-layout'>(</span><span class='hs-varid'>right</span> <span class='hs-varid'>r</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rsingleLeft</span>  <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-359"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span>                        <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rdoubleLeft</span>  <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-360"></a>
<a name="line-361"></a><a name="rbalanceRight"></a><span class='hs-definition'>rbalanceRight</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-362"></a><span class='hs-definition'>rbalanceRight</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-363"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>size'</span> <span class='hs-layout'>(</span><span class='hs-varid'>left</span> <span class='hs-varid'>l</span><span class='hs-layout'>)</span> <span class='hs-varop'>&gt;</span> <span class='hs-varid'>size'</span> <span class='hs-layout'>(</span><span class='hs-varid'>right</span> <span class='hs-varid'>l</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rsingleRight</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-364"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span>                        <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rdoubleRight</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>l</span> <span class='hs-varid'>m</span> <span class='hs-varid'>r</span>
<a name="line-365"></a>
<a name="line-366"></a><a name="lsingleLeft"></a><span class='hs-definition'>lsingleLeft</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-367"></a><span class='hs-definition'>lsingleLeft</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span>
<a name="line-368"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>p1</span> <span class='hs-varop'>&lt;=</span> <span class='hs-varid'>p2</span>  <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lloser</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-varid'>rloser</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span>
<a name="line-369"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>lloser</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-layout'>(</span><span class='hs-varid'>lloser</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span>
<a name="line-370"></a><span class='hs-definition'>lsingleLeft</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-371"></a>    <span class='hs-varid'>rloser</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-layout'>(</span><span class='hs-varid'>lloser</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span>
<a name="line-372"></a><span class='hs-definition'>lsingleLeft</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>moduleError</span> <span class='hs-str'>"lsingleLeft"</span> <span class='hs-str'>"malformed tree"</span>
<a name="line-373"></a>
<a name="line-374"></a><a name="rsingleLeft"></a><span class='hs-definition'>rsingleLeft</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-375"></a><span class='hs-definition'>rsingleLeft</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-376"></a>    <span class='hs-varid'>rloser</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-varid'>rloser</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span>
<a name="line-377"></a><span class='hs-definition'>rsingleLeft</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-378"></a>    <span class='hs-varid'>rloser</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-layout'>(</span><span class='hs-varid'>rloser</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span>
<a name="line-379"></a><span class='hs-definition'>rsingleLeft</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>moduleError</span> <span class='hs-str'>"rsingleLeft"</span> <span class='hs-str'>"malformed tree"</span>
<a name="line-380"></a>
<a name="line-381"></a><a name="lsingleRight"></a><span class='hs-definition'>lsingleRight</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-382"></a><span class='hs-definition'>lsingleRight</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span> <span class='hs-keyglyph'>=</span>
<a name="line-383"></a>    <span class='hs-varid'>lloser</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-varid'>lloser</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span>
<a name="line-384"></a><span class='hs-definition'>lsingleRight</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span> <span class='hs-keyglyph'>=</span>
<a name="line-385"></a>    <span class='hs-varid'>lloser</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-varid'>lloser</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span>
<a name="line-386"></a><span class='hs-definition'>lsingleRight</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>moduleError</span> <span class='hs-str'>"lsingleRight"</span> <span class='hs-str'>"malformed tree"</span>
<a name="line-387"></a>
<a name="line-388"></a><a name="rsingleRight"></a><span class='hs-definition'>rsingleRight</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-389"></a><span class='hs-definition'>rsingleRight</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span> <span class='hs-keyglyph'>=</span>
<a name="line-390"></a>    <span class='hs-varid'>lloser</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-varid'>rloser</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span>
<a name="line-391"></a><span class='hs-definition'>rsingleRight</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span>
<a name="line-392"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>p1</span> <span class='hs-varop'>&lt;=</span> <span class='hs-varid'>p2</span>  <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rloser</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-varid'>lloser</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span>
<a name="line-393"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>rloser</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-varid'>rloser</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span>
<a name="line-394"></a><span class='hs-definition'>rsingleRight</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>moduleError</span> <span class='hs-str'>"rsingleRight"</span> <span class='hs-str'>"malformed tree"</span>
<a name="line-395"></a>
<a name="line-396"></a><a name="ldoubleLeft"></a><span class='hs-definition'>ldoubleLeft</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-397"></a><span class='hs-definition'>ldoubleLeft</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-398"></a>    <span class='hs-varid'>lsingleLeft</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-varid'>lsingleRight</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span>
<a name="line-399"></a><span class='hs-definition'>ldoubleLeft</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-400"></a>    <span class='hs-varid'>lsingleLeft</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-varid'>rsingleRight</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span>
<a name="line-401"></a><span class='hs-definition'>ldoubleLeft</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>moduleError</span> <span class='hs-str'>"ldoubleLeft"</span> <span class='hs-str'>"malformed tree"</span>
<a name="line-402"></a>
<a name="line-403"></a><a name="ldoubleRight"></a><span class='hs-definition'>ldoubleRight</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-404"></a><span class='hs-definition'>ldoubleRight</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span> <span class='hs-keyglyph'>=</span>
<a name="line-405"></a>    <span class='hs-varid'>lsingleRight</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-varid'>lsingleLeft</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span>
<a name="line-406"></a><span class='hs-definition'>ldoubleRight</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span> <span class='hs-keyglyph'>=</span>
<a name="line-407"></a>    <span class='hs-varid'>lsingleRight</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-varid'>rsingleLeft</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span>
<a name="line-408"></a><span class='hs-definition'>ldoubleRight</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>moduleError</span> <span class='hs-str'>"ldoubleRight"</span> <span class='hs-str'>"malformed tree"</span>
<a name="line-409"></a>
<a name="line-410"></a><a name="rdoubleLeft"></a><span class='hs-definition'>rdoubleLeft</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-411"></a><span class='hs-definition'>rdoubleLeft</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-412"></a>    <span class='hs-varid'>rsingleLeft</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-varid'>lsingleRight</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span>
<a name="line-413"></a><span class='hs-definition'>rdoubleLeft</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-414"></a>    <span class='hs-varid'>rsingleLeft</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-layout'>(</span><span class='hs-varid'>rsingleRight</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t2</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span><span class='hs-layout'>)</span>
<a name="line-415"></a><span class='hs-definition'>rdoubleLeft</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>moduleError</span> <span class='hs-str'>"rdoubleLeft"</span> <span class='hs-str'>"malformed tree"</span>
<a name="line-416"></a>
<a name="line-417"></a><a name="rdoubleRight"></a><span class='hs-definition'>rdoubleRight</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Prio</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Key</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>LTree</span> <span class='hs-varid'>a</span>
<a name="line-418"></a><span class='hs-definition'>rdoubleRight</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span> <span class='hs-keyglyph'>=</span>
<a name="line-419"></a>    <span class='hs-varid'>rsingleRight</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-varid'>lsingleLeft</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span>
<a name="line-420"></a><span class='hs-definition'>rdoubleRight</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span><span class='hs-layout'>)</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span> <span class='hs-keyglyph'>=</span>
<a name="line-421"></a>    <span class='hs-varid'>rsingleRight</span> <span class='hs-varid'>k1</span> <span class='hs-varid'>p1</span> <span class='hs-varid'>v1</span> <span class='hs-layout'>(</span><span class='hs-varid'>rsingleLeft</span> <span class='hs-varid'>k2</span> <span class='hs-varid'>p2</span> <span class='hs-varid'>v2</span> <span class='hs-varid'>t1</span> <span class='hs-varid'>m1</span> <span class='hs-varid'>t2</span><span class='hs-layout'>)</span> <span class='hs-varid'>m2</span> <span class='hs-varid'>t3</span>
<a name="line-422"></a><span class='hs-definition'>rdoubleRight</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyword'>_</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>moduleError</span> <span class='hs-str'>"rdoubleRight"</span> <span class='hs-str'>"malformed tree"</span>
<a name="line-423"></a>
<a name="line-424"></a><a name="play"></a><span class='hs-comment'>-- | Take two pennants and returns a new pennant that is the union of</span>
<a name="line-425"></a><span class='hs-comment'>-- the two with the precondition that the keys in the ﬁrst tree are</span>
<a name="line-426"></a><span class='hs-comment'>-- strictly smaller than the keys in the second tree.</span>
<a name="line-427"></a><span class='hs-definition'>play</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span>
<a name="line-428"></a><span class='hs-conid'>Void</span> <span class='hs-varop'>`play`</span> <span class='hs-varid'>t'</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>t'</span>
<a name="line-429"></a><a name="play"></a><span class='hs-definition'>t</span> <span class='hs-varop'>`play`</span> <span class='hs-conid'>Void</span>  <span class='hs-keyglyph'>=</span> <span class='hs-varid'>t</span>
<a name="line-430"></a><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-varid'>t</span> <span class='hs-varid'>m</span> <span class='hs-varop'>`play`</span> <span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k'</span> <span class='hs-varid'>p'</span> <span class='hs-varid'>v'</span><span class='hs-layout'>)</span> <span class='hs-varid'>t'</span> <span class='hs-varid'>m'</span>
<a name="line-431"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>p</span> <span class='hs-varop'>&lt;=</span> <span class='hs-varid'>p'</span>   <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-varid'>rbalance</span> <span class='hs-varid'>k'</span> <span class='hs-varid'>p'</span> <span class='hs-varid'>v'</span> <span class='hs-varid'>t</span> <span class='hs-varid'>m</span> <span class='hs-varid'>t'</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span>
<a name="line-432"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-layout'>(</span><span class='hs-varid'>lbalance</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>t</span> <span class='hs-varid'>m</span> <span class='hs-varid'>t'</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span>
<a name="line-433"></a><span class='hs-comment'>{-# INLINE play #-}</span>
<a name="line-434"></a>
<a name="line-435"></a><a name="unsafePlay"></a><span class='hs-comment'>-- | A version of 'play' that can be used if the shape of the tree has</span>
<a name="line-436"></a><span class='hs-comment'>-- not changed or if the tree is known to be balanced.</span>
<a name="line-437"></a><span class='hs-definition'>unsafePlay</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span>
<a name="line-438"></a><span class='hs-conid'>Void</span> <span class='hs-varop'>`unsafePlay`</span> <span class='hs-varid'>t'</span> <span class='hs-keyglyph'>=</span>  <span class='hs-varid'>t'</span>
<a name="line-439"></a><a name="unsafePlay"></a><span class='hs-definition'>t</span> <span class='hs-varop'>`unsafePlay`</span> <span class='hs-conid'>Void</span>  <span class='hs-keyglyph'>=</span>  <span class='hs-varid'>t</span>
<a name="line-440"></a><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span><span class='hs-layout'>)</span> <span class='hs-varid'>t</span> <span class='hs-varid'>m</span> <span class='hs-varop'>`unsafePlay`</span> <span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span><span class='hs-keyglyph'>@</span><span class='hs-layout'>(</span><span class='hs-conid'>E</span> <span class='hs-varid'>k'</span> <span class='hs-varid'>p'</span> <span class='hs-varid'>v'</span><span class='hs-layout'>)</span> <span class='hs-varid'>t'</span> <span class='hs-varid'>m'</span>
<a name="line-441"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>p</span> <span class='hs-varop'>&lt;=</span> <span class='hs-varid'>p'</span>   <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-varid'>rloser</span> <span class='hs-varid'>k'</span> <span class='hs-varid'>p'</span> <span class='hs-varid'>v'</span> <span class='hs-varid'>t</span> <span class='hs-varid'>m</span> <span class='hs-varid'>t'</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span>
<a name="line-442"></a>    <span class='hs-keyglyph'>|</span> <span class='hs-varid'>otherwise</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-layout'>(</span><span class='hs-varid'>lloser</span> <span class='hs-varid'>k</span> <span class='hs-varid'>p</span> <span class='hs-varid'>v</span> <span class='hs-varid'>t</span> <span class='hs-varid'>m</span> <span class='hs-varid'>t'</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span>
<a name="line-443"></a><span class='hs-comment'>{-# INLINE unsafePlay #-}</span>
<a name="line-444"></a>
<a name="line-445"></a><a name="TourView"></a><span class='hs-keyword'>data</span> <span class='hs-conid'>TourView</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Null</span>
<a name="line-446"></a>                <span class='hs-keyglyph'>|</span> <span class='hs-conid'>Single</span> <span class='hs-comment'>{-# UNPACK #-}</span> <span class='hs-varop'>!</span><span class='hs-layout'>(</span><span class='hs-conid'>Elem</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-447"></a>                <span class='hs-keyglyph'>|</span> <span class='hs-layout'>(</span><span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-varop'>`Play`</span> <span class='hs-layout'>(</span><span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-448"></a>
<a name="line-449"></a><a name="tourView"></a><span class='hs-definition'>tourView</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>PSQ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>TourView</span> <span class='hs-varid'>a</span>
<a name="line-450"></a><span class='hs-definition'>tourView</span> <span class='hs-conid'>Void</span>               <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Null</span>
<a name="line-451"></a><span class='hs-definition'>tourView</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-conid'>Start</span> <span class='hs-keyword'>_</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Single</span> <span class='hs-varid'>e</span>
<a name="line-452"></a><span class='hs-definition'>tourView</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-conid'>RLoser</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-453"></a>    <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varop'>`Play`</span> <span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span>
<a name="line-454"></a><span class='hs-definition'>tourView</span> <span class='hs-layout'>(</span><span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-layout'>(</span><span class='hs-conid'>LLoser</span> <span class='hs-keyword'>_</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varid'>tr</span><span class='hs-layout'>)</span> <span class='hs-varid'>m'</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span>
<a name="line-455"></a>    <span class='hs-conid'>Winner</span> <span class='hs-varid'>e'</span> <span class='hs-varid'>tl</span> <span class='hs-varid'>m</span> <span class='hs-varop'>`Play`</span> <span class='hs-conid'>Winner</span> <span class='hs-varid'>e</span> <span class='hs-varid'>tr</span> <span class='hs-varid'>m'</span>
<a name="line-456"></a>
<a name="line-457"></a><span class='hs-comment'>------------------------------------------------------------------------</span>
<a name="line-458"></a><span class='hs-comment'>-- Utility functions</span>
<a name="line-459"></a>
<a name="line-460"></a><a name="moduleError"></a><span class='hs-definition'>moduleError</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>String</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span>
<a name="line-461"></a><span class='hs-definition'>moduleError</span> <span class='hs-varid'>fun</span> <span class='hs-varid'>msg</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>error</span> <span class='hs-layout'>(</span><span class='hs-str'>"GHC.Event.PSQ."</span> <span class='hs-varop'>++</span> <span class='hs-varid'>fun</span> <span class='hs-varop'>++</span> <span class='hs-chr'>':'</span> <span class='hs-conop'>:</span> <span class='hs-chr'>' '</span> <span class='hs-conop'>:</span> <span class='hs-varid'>msg</span><span class='hs-layout'>)</span>
<a name="line-462"></a><span class='hs-comment'>{-# NOINLINE moduleError #-}</span>
<a name="line-463"></a>
<a name="line-464"></a><span class='hs-comment'>------------------------------------------------------------------------</span>
<a name="line-465"></a><span class='hs-comment'>-- Hughes's efficient sequence type</span>
<a name="line-466"></a>
<a name="line-467"></a><a name="Sequ"></a><span class='hs-keyword'>newtype</span> <span class='hs-conid'>Sequ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Sequ</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span><span class='hs-layout'>)</span>
<a name="line-468"></a>
<a name="line-469"></a><a name="emptySequ"></a><span class='hs-definition'>emptySequ</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Sequ</span> <span class='hs-varid'>a</span>
<a name="line-470"></a><span class='hs-definition'>emptySequ</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Sequ</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-keyword'>as</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-keyword'>as</span><span class='hs-layout'>)</span>
<a name="line-471"></a>
<a name="line-472"></a><a name="singleSequ"></a><span class='hs-definition'>singleSequ</span> <span class='hs-keyglyph'>::</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Sequ</span> <span class='hs-varid'>a</span>
<a name="line-473"></a><span class='hs-definition'>singleSequ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Sequ</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-keyword'>as</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>a</span> <span class='hs-conop'>:</span> <span class='hs-keyword'>as</span><span class='hs-layout'>)</span>
<a name="line-474"></a>
<a name="line-475"></a><a name="%3c%3e"></a><span class='hs-layout'>(</span><span class='hs-varop'>&lt;&gt;</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Sequ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Sequ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-conid'>Sequ</span> <span class='hs-varid'>a</span>
<a name="line-476"></a><span class='hs-conid'>Sequ</span> <span class='hs-varid'>x1</span> <span class='hs-varop'>&lt;&gt;</span> <span class='hs-conid'>Sequ</span> <span class='hs-varid'>x2</span> <span class='hs-keyglyph'>=</span> <span class='hs-conid'>Sequ</span> <span class='hs-layout'>(</span><span class='hs-keyglyph'>\</span><span class='hs-keyword'>as</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-varid'>x1</span> <span class='hs-layout'>(</span><span class='hs-varid'>x2</span> <span class='hs-keyword'>as</span><span class='hs-layout'>)</span><span class='hs-layout'>)</span>
<a name="line-477"></a><span class='hs-keyword'>infixr</span> <span class='hs-num'>5</span> <span class='hs-varop'>&lt;&gt;</span>
<a name="line-478"></a>
<a name="line-479"></a><a name="seqToList"></a><span class='hs-definition'>seqToList</span> <span class='hs-keyglyph'>::</span> <span class='hs-conid'>Sequ</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>-&gt;</span> <span class='hs-keyglyph'>[</span><span class='hs-varid'>a</span><span class='hs-keyglyph'>]</span>
<a name="line-480"></a><span class='hs-definition'>seqToList</span> <span class='hs-layout'>(</span><span class='hs-conid'>Sequ</span> <span class='hs-varid'>x</span><span class='hs-layout'>)</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>x</span> <span class='hs-conid'>[]</span>
<a name="line-481"></a>
<a name="line-482"></a><a name="instance%20Show%20(Sequ%20a)"></a><span class='hs-keyword'>instance</span> <span class='hs-conid'>Show</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=&gt;</span> <span class='hs-conid'>Show</span> <span class='hs-layout'>(</span><span class='hs-conid'>Sequ</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span> <span class='hs-keyword'>where</span>
<a name="line-483"></a>    <span class='hs-varid'>showsPrec</span> <span class='hs-varid'>d</span> <span class='hs-varid'>a</span> <span class='hs-keyglyph'>=</span> <span class='hs-varid'>showsPrec</span> <span class='hs-varid'>d</span> <span class='hs-layout'>(</span><span class='hs-varid'>seqToList</span> <span class='hs-varid'>a</span><span class='hs-layout'>)</span>
<a name="line-484"></a>
</pre></body>
</html>
