time_frame 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/time_frame/collection.rb +35 -30
- data/lib/time_frame/version.rb +1 -1
- data/spec/collection_spec.rb +18 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 00c76a3400b8cad304f5c52c6b8d619f69dfd222
|
4
|
+
data.tar.gz: 0f77d91410ec48f03bf77a4522b53d6cad56caee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 452f866f8489f4c3611265981fe38c516e6f54f6d377476d7e34f927dc72121d74d824f0ebcebc09cbc51437c82d57a5793a902b61389295c603dee7c349ffd6
|
7
|
+
data.tar.gz: ea33d048127eea7b77e5470b0057ac0739cfe0e5a2955b9f16e9e0ddb515ba698eecf184b7d3d27b65d2d106b1149d5621e0f4755522457fae4fd7a026ee5520
|
@@ -6,70 +6,75 @@ class TimeFrame
|
|
6
6
|
# given time_frames or covering time elements
|
7
7
|
class Collection
|
8
8
|
include Enumerable
|
9
|
-
|
9
|
+
|
10
10
|
def initialize(item_list = [], sorted = false, &block)
|
11
|
-
|
11
|
+
block ||= ->(item) { item }
|
12
12
|
@tree_nodes = item_list.map do |item|
|
13
|
-
TreeNode.new(item: item,
|
13
|
+
TreeNode.new(item: item, &block)
|
14
14
|
end
|
15
|
-
|
16
|
-
sort_list(@tree_nodes) unless sorted
|
17
|
-
build_tree(0, @tree_nodes.size - 1)
|
18
|
-
@root = @tree_nodes[(@tree_nodes.size - 1) / 2]
|
15
|
+
build_tree(sorted) if @tree_nodes.any?
|
19
16
|
end
|
20
17
|
|
21
18
|
def each(&block)
|
22
|
-
tree_nodes.
|
19
|
+
@tree_nodes.each do |node|
|
20
|
+
block.call(node.item)
|
21
|
+
end
|
23
22
|
end
|
24
23
|
|
25
24
|
def all_covering(time)
|
26
|
-
|
27
|
-
|
28
|
-
|
25
|
+
[].tap do |result|
|
26
|
+
add_covering(time, @root, result) if any?
|
27
|
+
end
|
29
28
|
end
|
30
29
|
|
31
30
|
def all_intersecting(time_frame)
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
[].tap do |result|
|
32
|
+
add_intersecting(time_frame, @root, result) if any?
|
33
|
+
end
|
35
34
|
end
|
36
35
|
|
37
36
|
private
|
38
37
|
|
39
|
-
def
|
40
|
-
|
38
|
+
def sort_nodes
|
39
|
+
@tree_nodes.sort_by! do |item|
|
41
40
|
[item.time_frame.min, item.time_frame.max]
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
45
|
-
def build_tree(
|
44
|
+
def build_tree(sorted)
|
45
|
+
sort_nodes unless sorted
|
46
|
+
build_sub_tree(0, @tree_nodes.size - 1)
|
47
|
+
@root = @tree_nodes[(@tree_nodes.size - 1) / 2]
|
48
|
+
end
|
49
|
+
|
50
|
+
def build_sub_tree(lower, upper, ancestor = nil, side = nil)
|
46
51
|
mid = (lower + upper) / 2
|
47
52
|
node = @tree_nodes[mid]
|
48
53
|
|
49
54
|
node.update_ancestor_relation(ancestor, side) if ancestor && side
|
50
55
|
|
51
|
-
|
52
|
-
|
56
|
+
build_sub_tree(lower, mid - 1, node, :left) unless lower == mid
|
57
|
+
build_sub_tree(mid + 1, upper, node, :right) unless upper == mid
|
53
58
|
|
54
59
|
node.update_child_frame(node.child_time_frame) if lower == upper
|
55
60
|
end
|
56
61
|
|
57
62
|
def add_covering(time, node, result)
|
63
|
+
search_left = node.continue_left_side_search_for_time?(time)
|
64
|
+
search_right = node.continue_right_side_search_for_time?(time)
|
65
|
+
|
66
|
+
add_covering(time, node.left_child, result) if search_left
|
58
67
|
result << node.item if node.time_frame.cover?(time)
|
59
|
-
|
60
|
-
add_covering(time, node.left_child, result)
|
61
|
-
end
|
62
|
-
return unless node.continue_right_side_search_for_time?(time)
|
63
|
-
add_covering(time, node.right_child, result)
|
68
|
+
add_covering(time, node.right_child, result) if search_right
|
64
69
|
end
|
65
70
|
|
66
71
|
def add_intersecting(time_frame, node, result)
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
add_intersecting(time_frame, node.right_child, result)
|
72
|
+
search_left = node.continue_left_side_search_for_time_frame?(time_frame)
|
73
|
+
search_right = node.continue_right_side_search_for_time_frame?(time_frame)
|
74
|
+
|
75
|
+
add_intersecting(time_frame, node.left_child, result) if search_left
|
76
|
+
result << node.item if node.time_frame.overlaps? time_frame
|
77
|
+
add_intersecting(time_frame, node.right_child, result) if search_right
|
73
78
|
end
|
74
79
|
end
|
75
80
|
end
|
data/lib/time_frame/version.rb
CHANGED
data/spec/collection_spec.rb
CHANGED
@@ -16,6 +16,11 @@ describe TimeFrame::Collection do
|
|
16
16
|
|
17
17
|
describe '#all_covering' do
|
18
18
|
context 'when a pure time_frame tree is given' do
|
19
|
+
it 'returns an empty array if the collection is empty' do
|
20
|
+
collection = TimeFrame::Collection.new([])
|
21
|
+
expect(collection.all_covering(time)).to eq []
|
22
|
+
end
|
23
|
+
|
19
24
|
it 'returns all covering time_frames' do
|
20
25
|
time_frames = 20.times.map { |i| time_frame.shift_by((5 * i).days) }
|
21
26
|
tree = TimeFrame::Collection.new(time_frames)
|
@@ -49,6 +54,11 @@ describe TimeFrame::Collection do
|
|
49
54
|
end
|
50
55
|
|
51
56
|
context 'when objects containing time_frames are given' do
|
57
|
+
it 'returns an empty array if the collection is empty' do
|
58
|
+
collection = TimeFrame::Collection.new([]) { |item| item.time_frame }
|
59
|
+
expect(collection.all_covering(time)).to eq []
|
60
|
+
end
|
61
|
+
|
52
62
|
it 'returns all covering time_frames' do
|
53
63
|
objects = 20.times.map do |i|
|
54
64
|
OpenStruct.new(time_frame: time_frame.shift_by((5 * i).days))
|
@@ -90,6 +100,10 @@ describe TimeFrame::Collection do
|
|
90
100
|
|
91
101
|
describe '#all_intersecting' do
|
92
102
|
context 'when a pure time_frame tree is given' do
|
103
|
+
it 'returns an empty array if the collection is empty' do
|
104
|
+
collection = TimeFrame::Collection.new([])
|
105
|
+
expect(collection.all_intersecting(time)).to eq []
|
106
|
+
end
|
93
107
|
it 'returns all intersecting time_frames' do
|
94
108
|
time_frames = 20.times.map { |i| time_frame.shift_by((5 * i).days) }
|
95
109
|
tree = TimeFrame::Collection.new(time_frames)
|
@@ -138,6 +152,10 @@ describe TimeFrame::Collection do
|
|
138
152
|
end
|
139
153
|
|
140
154
|
context 'when objects containing time_frames are given' do
|
155
|
+
it 'returns an empty array if the collection is empty' do
|
156
|
+
collection = TimeFrame::Collection.new([]) { |item| item.time_frame }
|
157
|
+
expect(collection.all_intersecting(time)).to eq []
|
158
|
+
end
|
141
159
|
it 'returns all intersecting time_frames' do
|
142
160
|
objects = 20.times.map do |i|
|
143
161
|
OpenStruct.new(time_frame: time_frame.shift_by((5 * i).days))
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: time_frame
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Derichs
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-10-
|
13
|
+
date: 2014-10-21 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|