Class: IntervalList::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/intervals.rb

Instance Method Summary collapse

Constructor Details

#initializeTree

Returns a new instance of Tree.



258
259
260
# File 'lib/intervals.rb', line 258

def initialize
  @intervals = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



308
309
310
# File 'lib/intervals.rb', line 308

def method_missing sym, *args, &block
  tree.send(sym, *args, &block)
end

Instance Method Details

#<<(int) ⇒ Object



262
263
264
# File 'lib/intervals.rb', line 262

def << int
  @intervals << int
end

#build_treeObject



266
267
268
# File 'lib/intervals.rb', line 266

def build_tree
  IntervalList::TreeNode.new intervals_start
end

#intervals_startObject



270
271
272
# File 'lib/intervals.rb', line 270

def intervals_start
  @intervals_start ||= @intervals.sort_by &:start
end

#intervals_stopObject



274
275
276
# File 'lib/intervals.rb', line 274

def intervals_stop
  @intervals_stop ||= @intervals.sort_by { |i| -1 * i.stop }
end

#nearest(interval) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/intervals.rb', line 278

def nearest interval
  # first see if you have an overlap
  ols = overlap(interval)

  unless ols.empty?
    return ols.min do |int| 
      int.dist(interval)
    end
  end

  # you can just use the sorted intervals to do this
  lowest_start = intervals_start.bsearch do |i|
    i.above? interval
  end
  highest_stop = intervals_stop.bsearch do |i|
    i.below? interval
  end
  [ lowest_start, highest_stop ].compact.min do |i|
    i.dist(interval)
  end
end

#respond_to_missing?(sym, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


304
305
306
# File 'lib/intervals.rb', line 304

def respond_to_missing? sym, include_all = false
  tree.respond_to?(sym) || super
end

#treeObject



300
301
302
# File 'lib/intervals.rb', line 300

def tree
  @tree ||= build_tree
end