-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplot-stacked.rb
executable file
·107 lines (97 loc) · 3.04 KB
/
plot-stacked.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env ruby
# Mix member data of input files to stdout using partial sums.
#
# (C) 2011 - 2014, Stephan Beyer
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the
# following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the
# following disclaimer in the documentation and/or other
# materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY STEPHAN BEYER ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL STEPHAN BEYER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation
# are those of the authors and should not be interpreted as representing
# official policies, either expressed or implied, of Stephan Beyer.
DEBUG = false
# Do something for each date given by current_idx and input...
# Note that current_idx is changed to a suitable value.
def each_current_date(current_idx, input)
def foo(current_idx, input, j, i)
lines = input[i]
loop do
return nil if j >= lines.length
line = lines[j]
tmp = line.match(/^([0-9]+)\t([0-9]+)/)
#tmp = line.match(/^([0-9]+)\t[^\t]+\t([0-9]+)/)
if tmp.nil?
j = current_idx[i] += 1
else
return tmp
end
end
end
current_idx.each_with_index do |j,i|
tmp = foo(current_idx, input, j, i)
yield(tmp[1].to_i, tmp[2].to_i, i) unless tmp.nil?
end
end
def dprintf(*args)
printf(*args)
STDERR.printf(*args) if DEBUG
end
# read input files
input = []
ARGV.each do |fn|
input << File.readlines(fn)
end
# init
MAX = ARGV.length
current_idx = [0]*MAX
current = [0]*MAX
# combine input files and sum up from left to right
loop do
# initialize current_date to minimum date of all input files
current_date = nil
each_current_date(current_idx, input) do |date, val, i|
if current_date.nil? # init
current_date = date
elsif date < current_date # find min
current_date = date
end
end
break if current_date.nil?
# update vals for each minimum date
each_current_date(current_idx, input) do |date, val, i|
if date == current_date
current[i] = val
current_idx[i] += 1
end
end
# output sums
tmp = 0
dprintf("%d", current_date)
current.each do |val|
tmp += val
dprintf("\t%d", tmp)
end
dprintf("\n")
end