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



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
27
28
# File 'lib/time_frame/collection.rb', line 24

def all_covering(time)
  [].tap do |result|
    add_covering(time, @root, result) if any?
  end
end

#all_intersecting(time_frame) ⇒ Object



30
31
32
33
34
# File 'lib/time_frame/collection.rb', line 30

def all_intersecting(time_frame)
  [].tap do |result|
    add_intersecting(time_frame, @root, result) if any?
  end
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