Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Range Map library

Is there a Haskell library that allows me to have a Map from ranges to values? (Preferable somewhat efficient.)

let myRangeMap = RangeMap [(range 1 3, "foo"),(range 2 7, "bar"),(range 9 12, "baz")]
in  rangeValues 2
==> ["foo","bar"]
like image 664
mrueg Avatar asked Oct 08 '10 18:10

mrueg


3 Answers

I've written a library to search in overlapping intervals because the existing ones did not fit my needs. I think it may have a more approachable interface than for example SegmentTree:

https://www.chr-breitkopf.de/comp/IntervalMap/index.html

It's also available on Hackage: https://hackage.haskell.org/package/IntervalMap

like image 146
Chris Avatar answered Nov 18 '22 14:11

Chris


Perhaps the rangemin library does what you want?

Good old Data.Map (and its more efficient Data.IntMap cousin) has a function

splitLookup :: Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)

which splits a map into submaps of keys less than / greater than a given key. This can be used for certain kinds of range searching.

like image 30
keegan Avatar answered Nov 18 '22 16:11

keegan


This task is called a stabbing query on a set of intervals. An efficient data structure for it is called (one-dimensional) segment tree.

The SegmentTree package provides an implementation of this data structure, but unfortunately I cannot figure out how to use it. (I feel that the interface of this package does not provide the right level of abstraction.)

like image 7
Tsuyoshi Ito Avatar answered Nov 18 '22 15:11

Tsuyoshi Ito