range_tree 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +73 -58
- data/lib/range_tree.rb +21 -3
- data/lib/range_tree/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 552e16f04941f365c8d797abeecadf997bc3236b
|
4
|
+
data.tar.gz: 988458a90161548d0315f71ae4a1a792b32ee3b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5046afdd74928607ad7d5aeab0ee19def4b1c4e9c521213af524e5175919073446f76b1e67c6aa6c1f27306d2c115c376b8bff2365234ad4175f4cb31313777
|
7
|
+
data.tar.gz: a162d96f995118d6ca4902cbacd9cae3ef1f717d1719453fbda7b56d4cfd99c12c71c4da30d4abd120e3657e5cdbdaabc4e65219ad67092f050af61265f7cf98
|
data/README.md
CHANGED
@@ -14,7 +14,7 @@ For small trees you can use the default settings
|
|
14
14
|
which rebuild the tree every time you add a value.
|
15
15
|
|
16
16
|
If you need to build a very large tree with 1000's of values
|
17
|
-
you need to pass
|
17
|
+
you need to pass a first argument of (rebuild_on_add) ```false``` to the tree.
|
18
18
|
Then add everything to your tree and only once it is complete
|
19
19
|
you must call ```tree.rebuild``` to build your index.
|
20
20
|
|
@@ -36,61 +36,76 @@ Or install it yourself as:
|
|
36
36
|
|
37
37
|
## Usage
|
38
38
|
|
39
|
-
# Below is an example using integers (formatted as dates) as keys. You can just as easily use dates, times
|
40
|
-
# floats or other comparable types.
|
41
|
-
|
42
|
-
require 'range_tree'
|
43
|
-
|
44
|
-
staff_holidays = RangeTree::Tree.new
|
45
|
-
|
46
|
-
staff_holidays[2000_01_01, 2000_01_15] = "John"
|
47
|
-
staff_holidays[2000_01_15, 2000_01_31] = "Greig"
|
48
|
-
staff_holidays[2000_01_20, 2000_02_05] = "Eve"
|
49
|
-
staff_holidays[2000_01_03, 2000_01_08] = "Alice"
|
50
|
-
staff_holidays[2000_01_18, 2000_01_20] = "Bob"
|
51
|
-
|
52
|
-
# Check who is on holiday on certain dates
|
53
|
-
staff_holidays[2000_01_04]
|
54
|
-
=> ["Alice", "John"]
|
55
|
-
|
56
|
-
# Check all staff members on holiday within a certain period by passing a range
|
57
|
-
staff_holidays[2000_01_12..2011_01_19]
|
58
|
-
=> ["John", "Bob", "Greig", "Eve"]
|
59
|
-
|
60
|
-
|
61
|
-
# Iterate through all periods where the system state changes
|
62
|
-
staff_holidays.distinct.each do |date, staff|
|
63
|
-
puts "From #{date} #{staff} will be on holiday"
|
64
|
-
end
|
65
|
-
|
66
|
-
=>
|
67
|
-
From 20000101 ["John"] will be on holiday
|
68
|
-
From 20000103 ["John", "Alice"] will be on holiday
|
69
|
-
From 20000109 ["John"] will be on holiday
|
70
|
-
From 20000115 ["John", "Greig"] will be on holiday
|
71
|
-
From 20000116 ["Greig"] will be on holiday
|
72
|
-
From 20000118 ["Greig", "Bob"] will be on holiday
|
73
|
-
From 20000120 ["Greig", "Bob", "Eve"] will be on holiday
|
74
|
-
From 20000121 ["Greig", "Eve"] will be on holiday
|
75
|
-
From 20000132 ["Eve"] will be on holiday
|
76
|
-
From 20000206 [] will be on holiday
|
77
|
-
|
78
|
-
# Iterate though all periods in the tree.
|
79
|
-
staff_holidays.all.each do |date, staff|
|
80
|
-
puts "#{staff} is on holiday on #{date}"
|
81
|
-
end
|
82
|
-
|
83
|
-
=>
|
84
|
-
["John"] is on holiday on 20000101
|
85
|
-
["John"] is on holiday on 20000102
|
86
|
-
["John", "Alice"] is on holiday on 20000103
|
87
|
-
["John", "Alice"] is on holiday on 20000104
|
88
|
-
["John", "Alice"] is on holiday on 20000105
|
89
|
-
["John", "Alice"] is on holiday on 20000106
|
90
|
-
["John", "Alice"] is on holiday on 20000107
|
91
|
-
["John", "Alice"] is on holiday on 20000108
|
92
|
-
["John"] is on holiday on 20000109
|
93
|
-
["John"] is on holiday on 20000110
|
94
|
-
["John"] is on holiday on 20000111
|
95
|
-
...
|
96
39
|
|
40
|
+
```ruby
|
41
|
+
# Below is an example using integers (formatted as dates) as keys. You can just as easily use dates, times
|
42
|
+
# floats or other comparable types.
|
43
|
+
|
44
|
+
require 'range_tree'
|
45
|
+
|
46
|
+
staff_holidays = RangeTree::Tree.new
|
47
|
+
|
48
|
+
staff_holidays[2000_01_01, 2000_01_15] = "John"
|
49
|
+
staff_holidays[2000_01_15, 2000_01_31] = "Greig"
|
50
|
+
staff_holidays[2000_01_20, 2000_02_05] = "Eve"
|
51
|
+
staff_holidays[2000_01_03, 2000_01_08] = "Alice"
|
52
|
+
staff_holidays[2000_01_18, 2000_01_20] = "Bob"
|
53
|
+
|
54
|
+
# Check who is on holiday on certain dates
|
55
|
+
staff_holidays[2000_01_04]
|
56
|
+
=> ["Alice", "John"]
|
57
|
+
|
58
|
+
# Check all staff members on holiday within a certain period by passing a range
|
59
|
+
staff_holidays[2000_01_12..2011_01_19]
|
60
|
+
=> ["John", "Bob", "Greig", "Eve"]
|
61
|
+
|
62
|
+
|
63
|
+
# Iterate through all periods where the system state changes
|
64
|
+
staff_holidays.distinct.each do |date, staff|
|
65
|
+
puts "From #{date} #{staff} will be on holiday"
|
66
|
+
end
|
67
|
+
|
68
|
+
=>
|
69
|
+
From 20000101 ["John"] will be on holiday
|
70
|
+
From 20000103 ["John", "Alice"] will be on holiday
|
71
|
+
From 20000109 ["John"] will be on holiday
|
72
|
+
From 20000115 ["John", "Greig"] will be on holiday
|
73
|
+
From 20000116 ["Greig"] will be on holiday
|
74
|
+
From 20000118 ["Greig", "Bob"] will be on holiday
|
75
|
+
From 20000120 ["Greig", "Bob", "Eve"] will be on holiday
|
76
|
+
From 20000121 ["Greig", "Eve"] will be on holiday
|
77
|
+
From 20000132 ["Eve"] will be on holiday
|
78
|
+
From 20000206 [] will be on holiday
|
79
|
+
|
80
|
+
# Iterate though all periods in the tree.
|
81
|
+
staff_holidays.all.each do |date, staff|
|
82
|
+
puts "#{staff} is on holiday on #{date}"
|
83
|
+
end
|
84
|
+
|
85
|
+
=>
|
86
|
+
["John"] is on holiday on 20000101
|
87
|
+
["John"] is on holiday on 20000102
|
88
|
+
["John", "Alice"] is on holiday on 20000103
|
89
|
+
["John", "Alice"] is on holiday on 20000104
|
90
|
+
["John", "Alice"] is on holiday on 20000105
|
91
|
+
["John", "Alice"] is on holiday on 20000106
|
92
|
+
["John", "Alice"] is on holiday on 20000107
|
93
|
+
["John", "Alice"] is on holiday on 20000108
|
94
|
+
["John"] is on holiday on 20000109
|
95
|
+
["John"] is on holiday on 20000110
|
96
|
+
["John"] is on holiday on 20000111
|
97
|
+
...
|
98
|
+
|
99
|
+
# Remove values from tree
|
100
|
+
|
101
|
+
staff_holidays >> "John"
|
102
|
+
# or
|
103
|
+
staff_holidays.remove "John"
|
104
|
+
|
105
|
+
# Get smallest or largest keys in tree
|
106
|
+
staff_holidays.max
|
107
|
+
=> 2000_02_05
|
108
|
+
|
109
|
+
staff_holidays.min
|
110
|
+
=> 2000_01_01
|
111
|
+
```
|
data/lib/range_tree.rb
CHANGED
@@ -5,9 +5,7 @@ module RangeTree
|
|
5
5
|
require 'set'
|
6
6
|
attr_accessor :children, :root, :starts, :ends, :rebuild_on_add
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
def initialize(rebuild_on_add: true)
|
8
|
+
def initialize(rebuild_on_add=true)
|
11
9
|
self.children = []
|
12
10
|
self.rebuild_on_add = rebuild_on_add
|
13
11
|
self.starts = Hash.new{|h,k| h[k] = []}
|
@@ -30,6 +28,14 @@ module RangeTree
|
|
30
28
|
remove(value)
|
31
29
|
end
|
32
30
|
|
31
|
+
def min
|
32
|
+
self.root && self.root.min
|
33
|
+
end
|
34
|
+
|
35
|
+
def max
|
36
|
+
self.root && self.root.max
|
37
|
+
end
|
38
|
+
|
33
39
|
def remove(value)
|
34
40
|
return unless node_to_remove = self.children.find{|child| child.value == value}
|
35
41
|
self.children.delete(node_to_remove)
|
@@ -68,6 +74,18 @@ module RangeTree
|
|
68
74
|
end
|
69
75
|
end
|
70
76
|
|
77
|
+
def to_s
|
78
|
+
if self.root
|
79
|
+
"RangeTree[#{root.min}..#{root.max}]"
|
80
|
+
else
|
81
|
+
"RangeTree[empty]"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def inspect
|
86
|
+
"#{self}"
|
87
|
+
end
|
88
|
+
|
71
89
|
private
|
72
90
|
def iterate(range)
|
73
91
|
return [] unless self.root
|
data/lib/range_tree/version.rb
CHANGED