Message from discussion
Sliding Window functional data structure
Received: by 10.14.176.196 with SMTP id b44mr7058160eem.4.1346400310603;
Fri, 31 Aug 2012 01:05:10 -0700 (PDT)
X-BeenThere: haskell-cafe@googlegroups.com
Received: by 10.14.223.136 with SMTP id v8ls784564eep.7.gmail; Fri, 31 Aug
2012 01:05:10 -0700 (PDT)
Received: by 10.14.214.69 with SMTP id b45mr7061228eep.2.1346400310477;
Fri, 31 Aug 2012 01:05:10 -0700 (PDT)
Received: by 10.14.214.69 with SMTP id b45mr7061226eep.2.1346400310471;
Fri, 31 Aug 2012 01:05:10 -0700 (PDT)
Return-Path: <haskell-cafe-boun...@haskell.org>
Received: from lambda.haskell.org (lambda.haskell.org. [2a01:4f8:121:6::10])
by gmr-mx.google.com with ESMTPS id v3si5357526eep.1.2012.08.31.01.05.10
(version=TLSv1/SSLv3 cipher=OTHER);
Fri, 31 Aug 2012 01:05:10 -0700 (PDT)
Received-SPF: pass (google.com: best guess record for domain of haskell-cafe-boun...@haskell.org designates 2a01:4f8:121:6::10 as permitted sender) client-ip=2a01:4f8:121:6::10;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: best guess record for domain of haskell-cafe-boun...@haskell.org designates 2a01:4f8:121:6::10 as permitted sender) smtp.mail=haskell-cafe-boun...@haskell.org
Received: from localhost ([127.0.0.1] helo=lambda.haskell.org)
by lambda.haskell.org with esmtp (Exim 4.69)
(envelope-from <haskell-cafe-boun...@haskell.org>)
id 1T7MDI-0002ng-CI; Fri, 31 Aug 2012 10:04:24 +0200
Received: from mail1.bemta3.messagelabs.com ([195.245.230.34])
by lambda.haskell.org with esmtp (Exim 4.69)
(envelope-from <r...@soi.city.ac.uk>) id 1T7MDF-0002nN-Nw
for haskell-c...@haskell.org; Fri, 31 Aug 2012 10:04:21 +0200
Received: from [85.158.137.19:8431] by server-12.bemta-3.messagelabs.com id
B8/5E-10384-50070405; Fri, 31 Aug 2012 08:04:21 +0000
X-Env-Sender: r...@soi.city.ac.uk
X-Msg-Ref: server-6.tower-39.messagelabs.com!1346400259!3727193!1
X-Originating-IP: [138.40.77.123]
X-StarScan-Received:
X-StarScan-Version: 6.6.1.3; banners=-,-,-
X-VirusChecked: Checked
Received: (qmail 10815 invoked from network); 31 Aug 2012 08:04:19 -0000
Received: from wokingham.city.ac.uk (HELO wokingham.city.ac.uk) (138.40.77.123)
by server-6.tower-39.messagelabs.com with DHE-RSA-AES256-SHA encrypted
SMTP; 31 Aug 2012 08:04:19 -0000
Received: from vl-unix1.city.ac.uk ([138.40.65.18] helo=thatcham.city.ac.uk)
by wokingham.city.ac.uk with esmtps (TLSv1:AES256-SHA:256)
(Exim 4.63) (envelope-from <r...@soi.city.ac.uk>) id 1T7MDD-0007s6-Pt
for haskell-c...@haskell.org; Fri, 31 Aug 2012 09:04:19 +0100
Received: from f5-prv-fip.unix1.city.ac.uk ([10.200.34.19]
helo=rigel.soi.city.ac.uk)
by thatcham.city.ac.uk with esmtps (TLSv1:AES256-SHA:256) (Exim 4.63)
(envelope-from <r...@soi.city.ac.uk>) id 1T7MD8-0000Fa-Ms
for haskell-c...@haskell.org; Fri, 31 Aug 2012 09:04:14 +0100
Received: from dubhe.soi.city.ac.uk (dubhe.soi.city.ac.uk [138.40.91.29])
by rigel.soi.city.ac.uk (8.14.2/8.14.2) with ESMTP id q7V5JHe0011926
for <haskell-c...@haskell.org>; Fri, 31 Aug 2012 06:19:17 +0100
Received: from localhost (localhost [[UNIX: localhost]])
by dubhe.soi.city.ac.uk (8.13.8/8.13.8/Submit) id q7V84E7f020837
for haskell-c...@haskell.org; Fri, 31 Aug 2012 09:04:14 +0100
Date: Fri, 31 Aug 2012 09:03:52 +0100
From: Ross Paterson <r...@soi.city.ac.uk>
To: haskell-c...@haskell.org
Message-ID: <20120831080352.GA3...@soi.city.ac.uk>
Mail-Followup-To: haskell-c...@haskell.org
References: <59ACDFB2-2839-43FF-93F1-5BB3575A3...@cs.otago.ac.nz>
MIME-Version: 1.0
Content-Disposition: inline
In-Reply-To: <59ACDFB2-2839-43FF-93F1-5BB3575A3...@cs.otago.ac.nz>
User-Agent: Mutt/1.5.21 (2010-09-15)
Subject: Re: [Haskell-cafe] Sliding Window functional data structure
X-BeenThere: haskell-c...@haskell.org
X-Mailman-Version: 2.1.11
Precedence: list
List-Id: The Haskell Cafe <haskell-cafe.haskell.org>
List-Unsubscribe: <http://www.haskell.org/mailman/options/haskell-cafe>,
<mailto:haskell-cafe-requ...@haskell.org?subject=unsubscribe>
List-Archive: <http://www.haskell.org/pipermail/haskell-cafe>
List-Post: <mailto:haskell-c...@haskell.org>
List-Help: <mailto:haskell-cafe-requ...@haskell.org?subject=help>
List-Subscribe: <http://www.haskell.org/mailman/listinfo/haskell-cafe>,
<mailto:haskell-cafe-requ...@haskell.org?subject=subscribe>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Sender: haskell-cafe-boun...@haskell.org
Errors-To: haskell-cafe-boun...@haskell.org
On Fri, Aug 31, 2012 at 05:45:27AM +0100, Richard O'Keefe wrote:
> Consider the following interface
>
> type Ord k => Sliding_Window k v
>
> entries :: Sliding_Window k v -> [(k,v)]
> The cost is expected to be linear in the length of
> the result. The pairs are listed in increasing
> order of k.
>
> add :: Ord k => k -> v -> Sliding_Window k v -> Sliding_Window k v
> precondition: all (< k) [k' | (k',_) <- entries q]
> The cost should be at most O((log . length . entries) q).
> post: entries (add k v q) = entries q ++ [(k,v)]
>
> since :: Ord k => k -> Sliding_Window k v -> [(k,v)]
> answers [(k',v) | (k',v) <- entries q, k' > k]
> The cost should be at most O((log . length . entries) q
> + length result)
>
> purge :: Ord k => k -> Sliding_Window k v -> Sliding_Window k v
> answers q' such that entries q' = [(k',v) | (k',v) <- entries q,
> k' > k]
> The cost should be at most O((log . length . entries) q
> + length [k' | (k',v) <- entries q,
> k' <= k])
Any search tree implementation will do add and purge in O(log n) time.
A finger tree will do add in O(1) and purge in O(log(min(r, n-r))) time,
where r in the length of the result.
{-# LANGUAGE MultiParamTypeClasses #-}
module SlidingWindow where
import Data.FingerTree
import Data.Foldable
import Data.Monoid
data Entry k v = Entry k v
data Max k = Bot | Lift k
deriving (Eq, Ord)
instance Ord k => Monoid (Max k) where
mempty = Bot
mappend = max
instance Ord k => Measured (Max k) (Entry k v) where
measure (Entry k _) = Lift k
newtype SlidingWindow k v = SW (FingerTree (Max k) (Entry k v))
entries :: SlidingWindow k v -> [(k,v)]
entries (SW t) = [(k, v) | Entry k v <- toList t]
emptySW :: Ord k => SlidingWindow k v
emptySW = SW empty
add :: Ord k => k -> v -> SlidingWindow k v -> SlidingWindow k v
add k v (SW t) = SW (t |> Entry k v)
since :: Ord k => k -> SlidingWindow k v -> [(k,v)]
since k = entries . purge k
purge :: Ord k => k -> SlidingWindow k v -> SlidingWindow k v
purge k (SW t) = SW (dropUntil (> Lift k) t)
_______________________________________________
Haskell-Cafe mailing list
Haskell-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe