ranges_merger 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -9,9 +9,7 @@ Very simple gem for merging range-like objects.
9
9
  If you want to merge please use code just like below.
10
10
 
11
11
  a = [1, 3]
12
-
13
12
  b = [2, 4]
14
-
15
13
  c = [4, 6]
16
14
 
17
15
  result = RangesMerger.merge([a, b, c])
@@ -25,13 +23,9 @@ If you want to merge please use code just like below.
25
23
  If you want to merge Range objects.
26
24
 
27
25
  ranges = [
28
-
29
26
  (1..2),
30
-
31
27
  (2..3),
32
-
33
28
  (5..8)
34
-
35
29
  ]
36
30
 
37
31
  result = RangesMerger.merge(ranges)
@@ -47,15 +41,12 @@ If you want to exclude please use code just like below.
47
41
  First - base:
48
42
 
49
43
  a = [1, 3]
50
-
51
44
  b = [3, 5]
52
-
53
45
  c = [9, 11]
54
46
 
55
47
  Seconds - exclusions:
56
48
 
57
49
  x = [2, 4]
58
-
59
50
  y = [4, 10]
60
51
 
61
52
  And run some processing
@@ -73,13 +64,11 @@ If you want to have instance, add and remove ranges as you wish just do somethin
73
64
  r = RangesMerger.new
74
65
 
75
66
  r += [[1, 10]]
76
-
77
67
  r += [[8, 15]]
78
68
 
79
69
  # r.to_array => [[1, 15]]
80
70
 
81
71
  r -= [[2, 4]]
82
-
83
72
  r -= [[11, 12]]
84
73
 
85
74
  # r.to_array => [[1, 2], [4,11], [12,15]]
@@ -133,6 +122,39 @@ object.
133
122
 
134
123
 
135
124
 
125
+ == Calculation of energy (beta)
126
+
127
+ 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
130
+ can use this gem! :)
131
+
132
+ # currents = [
133
+ # [ time_from, time_to, value ],
134
+ # ...
135
+ # ]
136
+ #
137
+
138
+ currents = [
139
+ [1, 2, 1],
140
+ [2, 3, 2]
141
+ ]
142
+
143
+ voltages = [
144
+ [1, 2, 2],
145
+ [2, 3, 1]
146
+ ]
147
+
148
+ result = RangesMerger.energy_calculation(voltages, currents)
149
+
150
+ # result => 4
151
+
152
+
153
+ This is early version, it should work and calculate correct amount of energy, but
154
+ it could not :]
155
+
156
+
157
+
136
158
  == Contributing to ranges_merger
137
159
 
138
160
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.1
1
+ 0.4.0
data/lib/ranges_merger.rb CHANGED
@@ -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_energy_calculation'
5
6
 
6
7
  class RangesMerger
7
8
  extend RangesMergerMerging
@@ -0,0 +1,68 @@
1
+ class RangesMerger
2
+
3
+ def self.energy_calculation(voltages, currents, _debug = false)
4
+ return 0 if voltages.size == 0 or currents.size == 0
5
+
6
+ # sort time ranges
7
+ voltages = voltages.sort { |a, b| a[0] <=> b[0] }
8
+ currents = currents.sort { |a, b| a[0] <=> b[0] }
9
+ # without checking of overlaps
10
+
11
+ # for loop
12
+ voltage_i = 0
13
+ current_i = 0
14
+
15
+ # output
16
+ energy = 0.0
17
+
18
+ voltage_first = true
19
+ voltage_first = false if currents[0][0] < voltages[0][0]
20
+
21
+ while voltage_i < voltages.size and current_i < currents.size
22
+ v = voltages[voltage_i]
23
+ c = currents[current_i]
24
+
25
+ # common time range
26
+ _time_from = v[0]
27
+ _time_from = c[0] if c[0] > v[0]
28
+
29
+ _time_to = v[1]
30
+ _time_to = c[1] if c[1] < v[1]
31
+
32
+ # adding to output
33
+ if _time_to > _time_from
34
+ _energy_part = v[2] * c[2] * (_time_to - _time_from)
35
+ else
36
+ # it should not enter here... probably
37
+ _energy_part = 0.0
38
+ end
39
+ energy += _energy_part
40
+
41
+ puts "#{voltage_i}, #{current_i} = #{_energy_part}, #{v.inspect}, #{c.inspect}" if _debug
42
+
43
+ # next from array
44
+ if c[1] > v[1]
45
+ voltage_i += 1
46
+ else
47
+ current_i += 1
48
+ end
49
+
50
+ end
51
+
52
+ #currents = [
53
+ # [1, 2] => 1,
54
+ # [2, 3] => 2
55
+ #]
56
+ #
57
+ #voltages = [
58
+ # [1, 2] => 2,
59
+ # [2, 3] => 1
60
+ #]
61
+
62
+
63
+ start_time = voltages
64
+
65
+ return energy
66
+ end
67
+
68
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ranges_merger"
8
- s.version = "0.3.1"
8
+ s.version = "0.4.0"
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 = "2011-12-08"
12
+ s.date = "2012-05-23"
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,17 +28,19 @@ 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_energy_calculation.rb",
31
32
  "lib/ranges_merger/ranges_merger_exclusion.rb",
32
33
  "lib/ranges_merger/ranges_merger_merging.rb",
33
34
  "lib/ranges_merger/ranges_merger_utils.rb",
34
35
  "ranges_merger.gemspec",
36
+ "spec/ranges_merger_energy_calculation_spec.rb",
35
37
  "spec/ranges_merger_spec.rb",
36
38
  "spec/spec_helper.rb"
37
39
  ]
38
40
  s.homepage = "http://github.com/akwiatkowski/ranges_merger"
39
41
  s.licenses = ["LGPLv3"]
40
42
  s.require_paths = ["lib"]
41
- s.rubygems_version = "1.8.10"
43
+ s.rubygems_version = "1.8.15"
42
44
  s.summary = "Small gem for merging range-like objects"
43
45
 
44
46
  if s.respond_to? :specification_version then
@@ -0,0 +1,105 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "RangesMerger" do
4
+
5
+ # Merging
6
+
7
+ it "should calculate energy (1)" do
8
+ # time_from, time_to, value
9
+
10
+ currents = [
11
+ [1, 2, 1],
12
+ [2, 3, 2]
13
+ ]
14
+
15
+ voltages = [
16
+ [1, 2, 2],
17
+ [2, 3, 1]
18
+ ]
19
+
20
+ result = RangesMerger.energy_calculation(voltages, currents)
21
+ result.should == 4
22
+ end
23
+
24
+ it "should calculate energy (2)" do
25
+ # time_from, time_to, value
26
+
27
+ currents = [
28
+ [0, 1, 0],
29
+ [2, 5, 1],
30
+ [5, 6, 0]
31
+ ]
32
+
33
+ voltages = [
34
+ [0, 1, 5],
35
+ [2, 5, 0],
36
+ [5, 6, 5]
37
+ ]
38
+
39
+ result = RangesMerger.energy_calculation(voltages, currents)
40
+ result.should == 0
41
+ end
42
+
43
+ it "should calculate energy (3)" do
44
+ # time_from, time_to, value
45
+
46
+ currents = [
47
+ [0, 5, 0],
48
+ [5, 10, 1]
49
+ ]
50
+
51
+ voltages = [
52
+ [0, 1, 1],
53
+ [1, 2, 0],
54
+ #
55
+ [2, 3, 1],
56
+ [3, 4, 0],
57
+ #
58
+ [4, 5, 1],
59
+ [5, 6, 0],
60
+ #
61
+ [6, 7, 1],
62
+ [7, 8, 0],
63
+ #
64
+ [8, 9, 1],
65
+ [9, 10, 0],
66
+ ]
67
+
68
+ result = RangesMerger.energy_calculation(voltages, currents)
69
+ result.should == 2
70
+ end
71
+
72
+ it "should calculate energy (4)" do
73
+ # time_from, time_to, value
74
+
75
+ currents = [
76
+ [0, 0.1, 1],
77
+ [0.2, 0.5, 2]
78
+ ]
79
+
80
+ voltages = [
81
+ [1, 2, 1]
82
+ ]
83
+
84
+ result = RangesMerger.energy_calculation(voltages, currents)
85
+ result.should == 0
86
+ end
87
+
88
+ it "should calculate energy (5)" do
89
+ # time_from, time_to, value
90
+
91
+ currents = [
92
+ [0, 0.1, 1],
93
+ [0.2, 0.5, 2]
94
+ ]
95
+
96
+ voltages = [
97
+ [0, 1, 1]
98
+ ]
99
+
100
+ result = RangesMerger.energy_calculation(voltages, currents)
101
+ result.should == 0.1 + (0.5 - 0.2) * 2
102
+ end
103
+
104
+
105
+ 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.3.1
4
+ version: 0.4.0
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: 2011-12-08 00:00:00.000000000Z
12
+ date: 2012-05-23 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &88199790 !ruby/object:Gem::Requirement
16
+ requirement: &27977180 !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: *88199790
24
+ version_requirements: *27977180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &88135780 !ruby/object:Gem::Requirement
27
+ requirement: &19793280 !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: *88135780
35
+ version_requirements: *19793280
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &88135480 !ruby/object:Gem::Requirement
38
+ requirement: &21103540 !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: *88135480
46
+ version_requirements: *21103540
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rcov
49
- requirement: &88135180 !ruby/object:Gem::Requirement
49
+ requirement: &22223640 !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: *88135180
57
+ version_requirements: *22223640
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,10 +75,12 @@ 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_energy_calculation.rb
78
79
  - lib/ranges_merger/ranges_merger_exclusion.rb
79
80
  - lib/ranges_merger/ranges_merger_merging.rb
80
81
  - lib/ranges_merger/ranges_merger_utils.rb
81
82
  - ranges_merger.gemspec
83
+ - spec/ranges_merger_energy_calculation_spec.rb
82
84
  - spec/ranges_merger_spec.rb
83
85
  - spec/spec_helper.rb
84
86
  homepage: http://github.com/akwiatkowski/ranges_merger
@@ -96,7 +98,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
98
  version: '0'
97
99
  segments:
98
100
  - 0
99
- hash: -918554511
101
+ hash: 3481524600643469531
100
102
  required_rubygems_version: !ruby/object:Gem::Requirement
101
103
  none: false
102
104
  requirements:
@@ -105,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
107
  version: '0'
106
108
  requirements: []
107
109
  rubyforge_project:
108
- rubygems_version: 1.8.10
110
+ rubygems_version: 1.8.15
109
111
  signing_key:
110
112
  specification_version: 3
111
113
  summary: Small gem for merging range-like objects