Class: TimeFrame::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/time_frame/collection.rb,
lib/time_frame/tree_node.rb

Overview

This collection supports the concept of interval trees to improve the access speed to intervals (or objects containing intervals) intersecting given time_frames or covering time elements

Defined Under Namespace

Classes: TreeNode

Instance Method Summary collapse

Constructor Details

#initialize(item_list = [], sorted = false, &block) ⇒ Collection

Returns a new instance of Collection.



10
11
12
13
14
15
16
# File 'lib/time_frame/collection.rb', line 10

def initialize(item_list = [], sorted = false, &block)
  block ||= ->(item) { item }
  @tree_nodes = item_list.map do |item|
    TreeNode.new(item: item, &block)
  end
  build_tree(sorted) if @tree_nodes.any?
end

Instance Method Details

#all_covering(time) ⇒ Object



24
25
26
# File 'lib/time_frame/collection.rb', line 24

def all_covering(time)
  all_matching { |element| element.cover? time }
end

#all_intersecting(time_frame) ⇒ Object



28
29
30
# File 'lib/time_frame/collection.rb', line 28

def all_intersecting(time_frame)
  all_matching { |element| element.overlaps? time_frame }
end

#each(&block) ⇒ Object



18
19
20
21
22
# File 'lib/time_frame/collection.rb', line 18

def each(&block)
  @tree_nodes.each do |node|
    block.call(node.item)
  end
end