ranges_merger 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -93,6 +93,26 @@ parts than interval, or without them.
93
93
  # result => [[1, 4], [4, 7], [7, 9]]
94
94
 
95
95
 
96
+ == Dividing with equal interval
97
+
98
+ r = RangesMerger.new
99
+
100
+ r += [[1, 9]] # store 1 as lowest value
101
+
102
+ r -= [[1, 2], [4, 5]]
103
+
104
+ result = r % 2
105
+
106
+ # result => [[2, 4], [5, 7], [7, 9]]
107
+
108
+ result = r / 2
109
+
110
+ # result => [[2, 4], [5, 7], [7, 9]]
111
+
112
+ result = r.divide_eqi(2)
113
+
114
+ # result => [[5, 7], [7, 9]]
115
+
96
116
 
97
117
  == Ranges are not only numbers
98
118
 
@@ -125,8 +145,8 @@ object.
125
145
  == Calculation of energy (beta)
126
146
 
127
147
  Imagine you have information about voltage and current and you want to calculate work/energy.
128
- Theese measurements are not set to one point in time but to a time range ('from' and 'to').
129
- Theese time ranges are not identical for voltage and current. So what would you do? You
148
+ These measurements are not set to one point in time but to a time range ('from' and 'to').
149
+ These time ranges are not identical for voltage and current. So what would you do? You
130
150
  can use this gem! :)
131
151
 
132
152
  # currents = [
@@ -150,8 +170,8 @@ can use this gem! :)
150
170
  # result => 4
151
171
 
152
172
 
153
- This is early version, it should work and calculate correct amount of energy, but
154
- it could not :]
173
+ This is an early version, it should work and calculate correct amount of energy, but
174
+ it could not :].
155
175
 
156
176
 
157
177
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
@@ -2,6 +2,7 @@ require 'ranges_merger/ranges_merger_merging'
2
2
  require 'ranges_merger/ranges_merger_utils'
3
3
  require 'ranges_merger/ranges_merger_exclusion'
4
4
  require 'ranges_merger/ranges_merger_divider'
5
+ require 'ranges_merger/ranges_merger_divider_eq_interval'
5
6
  require 'ranges_merger/ranges_merger_energy_calculation'
6
7
 
7
8
  class RangesMerger
@@ -9,16 +10,19 @@ class RangesMerger
9
10
  extend RangesMergerUtils
10
11
  extend RangesMergerExclusion
11
12
  extend RangesMergerDivider
13
+ extend RangesMergerDividerEqInterval
12
14
 
13
15
  attr_reader :ranges
14
16
 
15
17
  def initialize(_array = [])
18
+ @total_min = nil
16
19
  @ranges = self.class.merge(
17
20
  self.class.normalize_array(_array)
18
21
  )
19
22
  end
20
23
 
21
24
  def add(_array)
25
+ register_min(_array)
22
26
  @ranges = self.class.merge(@ranges + _array)
23
27
  return self
24
28
  end
@@ -48,6 +52,10 @@ class RangesMerger
48
52
  divide(interval, false)
49
53
  end
50
54
 
55
+ def divide_eqi(interval, total_min = @total_min)
56
+ return self.class.divide_eqi(@ranges, interval, total_min)
57
+ end
58
+
51
59
  def to_ranges
52
60
  self.class.array_to_ranges(@ranges)
53
61
  end
@@ -56,4 +64,16 @@ class RangesMerger
56
64
  @ranges
57
65
  end
58
66
 
67
+ private
68
+
69
+ # Hack for maintaining proper interval and start for 'divide_eqi'
70
+ def register_min(ranges)
71
+ _min = ranges.sort{|a,b| a[0] <=> b[0]}.first[0]
72
+ if @total_min.nil?
73
+ @total_min = _min
74
+ else
75
+ @total_min = _min if @total_min > _min
76
+ end
77
+ end
78
+
59
79
  end
@@ -0,0 +1,21 @@
1
+ module RangesMergerDividerEqInterval
2
+ # Divide with equal interval
3
+ def divide_eqi(_array, interval, total_min)
4
+ # calculate for whole range
5
+ joined_array = merge(_array)
6
+
7
+ min = joined_array.first[0]
8
+ min = total_min if total_min < min and not total_min.nil? # before merging
9
+ max = joined_array.last[1]
10
+ divided_array = divide([[min, max]], interval)
11
+
12
+ # remove sub-ranges
13
+ filtered_array = Array.new
14
+ joined_array.each do |ja|
15
+ filtered_array += divided_array.select{|da| ja[0] <= da[0] and ja[1] >= da[1] }
16
+ end
17
+ filtered_array = filtered_array.sort.uniq
18
+
19
+ return filtered_array
20
+ end
21
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ranges_merger"
8
- s.version = "0.4.0"
8
+ s.version = "0.4.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Aleksander Kwiatkowski"]
12
- s.date = "2012-05-23"
12
+ s.date = "2012-08-06"
13
13
  s.description = "Simple gem used for merging range-like objects. If you have problems with operations on ranges this gem can save you some time."
14
14
  s.email = "bobikx@poczta.fm"
15
15
  s.extra_rdoc_files = [
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "VERSION",
29
29
  "lib/ranges_merger.rb",
30
30
  "lib/ranges_merger/ranges_merger_divider.rb",
31
+ "lib/ranges_merger/ranges_merger_divider_eq_interval.rb",
31
32
  "lib/ranges_merger/ranges_merger_energy_calculation.rb",
32
33
  "lib/ranges_merger/ranges_merger_exclusion.rb",
33
34
  "lib/ranges_merger/ranges_merger_merging.rb",
@@ -35,6 +36,7 @@ Gem::Specification.new do |s|
35
36
  "ranges_merger.gemspec",
36
37
  "spec/ranges_merger_energy_calculation_spec.rb",
37
38
  "spec/ranges_merger_spec.rb",
39
+ "spec/ranges_merger_spec_equal_interval.rb",
38
40
  "spec/spec_helper.rb"
39
41
  ]
40
42
  s.homepage = "http://github.com/akwiatkowski/ranges_merger"
@@ -1,6 +1,6 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
- describe "RangesMerger" do
3
+ describe RangesMerger do
4
4
 
5
5
  # Merging
6
6
 
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe RangesMergerDividerEqInterval do
4
+
5
+ it "should divide into ranges w/o changing 'interval'" do
6
+ a = [[0, 10]]
7
+ b = [[1, 2]]
8
+
9
+ r = RangesMerger.new
10
+ r += a
11
+ r -= b
12
+
13
+ result = r.divide_eqi(2)
14
+ result.should == [[2, 4], [4, 6], [6, 8], [8, 10]]
15
+ end
16
+
17
+ it "should divide into ranges w/o changing 'interval' (2)" do
18
+ a = [[0, 10]]
19
+ b = [[1, 2], [5, 6], [8, 9]]
20
+
21
+ r = RangesMerger.new
22
+ r += a
23
+ r -= b
24
+
25
+ result = r.divide_eqi(2)
26
+ result.should == [[2, 4], [6, 8]]
27
+ end
28
+
29
+ it 'readme' do
30
+ r = RangesMerger.new
31
+ r += [[1, 9]]
32
+ r -= [[1, 2], [4, 5]]
33
+ result = r % 2
34
+ #puts result.inspect
35
+
36
+ result = r / 2
37
+ #puts result.inspect
38
+
39
+ result = r.divide_eqi(2)
40
+ #puts result.inspect
41
+ end
42
+
43
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ranges_merger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-23 00:00:00.000000000Z
12
+ date: 2012-08-06 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &27977180 !ruby/object:Gem::Requirement
16
+ requirement: &12681880 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.3.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *27977180
24
+ version_requirements: *12681880
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &19793280 !ruby/object:Gem::Requirement
27
+ requirement: &13765160 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *19793280
35
+ version_requirements: *13765160
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &21103540 !ruby/object:Gem::Requirement
38
+ requirement: &14139740 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.6.4
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *21103540
46
+ version_requirements: *14139740
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rcov
49
- requirement: &22223640 !ruby/object:Gem::Requirement
49
+ requirement: &15781800 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *22223640
57
+ version_requirements: *15781800
58
58
  description: Simple gem used for merging range-like objects. If you have problems
59
59
  with operations on ranges this gem can save you some time.
60
60
  email: bobikx@poczta.fm
@@ -75,6 +75,7 @@ files:
75
75
  - VERSION
76
76
  - lib/ranges_merger.rb
77
77
  - lib/ranges_merger/ranges_merger_divider.rb
78
+ - lib/ranges_merger/ranges_merger_divider_eq_interval.rb
78
79
  - lib/ranges_merger/ranges_merger_energy_calculation.rb
79
80
  - lib/ranges_merger/ranges_merger_exclusion.rb
80
81
  - lib/ranges_merger/ranges_merger_merging.rb
@@ -82,6 +83,7 @@ files:
82
83
  - ranges_merger.gemspec
83
84
  - spec/ranges_merger_energy_calculation_spec.rb
84
85
  - spec/ranges_merger_spec.rb
86
+ - spec/ranges_merger_spec_equal_interval.rb
85
87
  - spec/spec_helper.rb
86
88
  homepage: http://github.com/akwiatkowski/ranges_merger
87
89
  licenses:
@@ -98,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
98
100
  version: '0'
99
101
  segments:
100
102
  - 0
101
- hash: 3481524600643469531
103
+ hash: 1390856309896833355
102
104
  required_rubygems_version: !ruby/object:Gem::Requirement
103
105
  none: false
104
106
  requirements: