# Is there a dictionary-based data structure where the keys are disjoint ranges, and indexing with an integer looks up the matching range?

**URL:** <https://discourse.julialang.org/t/is-there-a-dictionary-based-data-structure-where-the-keys-are-disjoint-ranges-and-indexing-with-an-integer-looks-up-the-matching-range/38125>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 24, 2020, 6:45am UTC](https://discourse.julialang.org/t/is-there-a-dictionary-based-data-structure-where-the-keys-are-disjoint-ranges-and-indexing-with-an-integer-looks-up-the-matching-range/38125 "2020-04-24T06:45:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [April 24, 2020, 6:45am UTC](https://discourse.julialang.org/t/is-there-a-dictionary-based-data-structure-where-the-keys-are-disjoint-ranges-and-indexing-with-an-integer-looks-up-the-matching-range/38125/1 "2020-04-24T06:45:21Z")

</div>

For example,

```julia
julia> d = Dict(1:4=>"abc",10:14=>"def")
Dict{UnitRange{Int64},String} with 2 entries:
  10:14 => "def"
  1:4 => "abc"

# What I want
julia> d[3]
"abc"

julia> d[11]
"def"

julia> d[5]
ERROR: KeyError: key 5 not found

```

I couldn’t find any package that provides this after a brief search on Juliahub, and I’ll be grateful if someone could point one out. If there’s nothing then I’ll create a package that implements this.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 24, 2020, 6:55am UTC](https://discourse.julialang.org/t/is-there-a-dictionary-based-data-structure-where-the-keys-are-disjoint-ranges-and-indexing-with-an-integer-looks-up-the-matching-range/38125/2 "2020-04-24T06:55:46Z")

</div>

yes:

> **[GitHub - BioJulia/IntervalTrees.jl: A data structure for efficient...](https://github.com/BioJulia/IntervalTrees.jl)**
>
> A data structure for efficient manipulation of sets of intervals - GitHub - BioJulia/IntervalTrees.jl: A data structure for efficient manipulation of sets of intervals

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [April 24, 2020, 7:18am UTC](https://discourse.julialang.org/t/is-there-a-dictionary-based-data-structure-where-the-keys-are-disjoint-ranges-and-indexing-with-an-integer-looks-up-the-matching-range/38125/3 "2020-04-24T07:18:11Z")

</div>

Thanks, this looks promising. However I’m not sure if the package hasn’t been updated for a while, as it’s failing tests on julia1.2 and 1.4 (edit: there seems to be an issue filed about this). I’m also not sure how to use it, as a [documentation example](https://biojulia.net/IntervalTrees.jl/v1.0.0/man/dict.html) results in an error:

```julia
julia> using IntervalTrees

julia> # Create an interval tree mapping (Int, Int) intervals to Strings.
       xs = IntervalMap{Int, String}()
IntervalTrees.IntervalBTree{Int64,IntervalValue{Int64,String},64}

julia> # Insert values
       xs[(1,100)] = "Low"
"Low"

julia> xs[(101,1000)] = "Medium"
"Medium"

julia> xs[(1001,10000)] = "High"
"High"

julia> # Search for values
       println(xs[(1001,10000)]) # prints "High"
ERROR: MethodError: no method matching getindex(::IntervalTrees.IntervalBTree{Int64,IntervalValue{Int64,String},64}, ::Tuple{Int64,Int64})
Closest candidates are:
  getindex(::IntervalTrees.IntervalBTree{K,V,B}, ::AbstractInterval{K}) where {K, V, B} at /home/jishnu/.julia/packages/IntervalTrees/wh2ex/src/map.jl:25
Stacktrace:
 [1] top-level scope at REPL[9]:2

```

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [April 24, 2020, 7:30am UTC](https://discourse.julialang.org/t/is-there-a-dictionary-based-data-structure-where-the-keys-are-disjoint-ranges-and-indexing-with-an-integer-looks-up-the-matching-range/38125/4 "2020-04-24T07:30:17Z")

</div>

Sorry, not sure what’s up with that. I think I’m using it successfully, but maybe I’m stuck on an older version. Hopefully the maintainers might ping in. BioJulia is a largeish and awesome organization.
