Mercurial > gemma
annotate pkg/common/round.go @ 4401:9842812b92e0
identify: fix numbers for nsc
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Mon, 16 Sep 2019 16:46:29 +0200 |
parents | 6a985796f401 |
children |
rev | line source |
---|---|
4353
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
3 // |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
6 // |
4367 | 7 // Copyright (C) 2019 by via donau |
4353
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
10 // |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
12 // * Sascha Wilde <wilde@sha-bang.de> |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
13 |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
14 package common |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
15 |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
16 import ( |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
17 "math" |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
18 "sort" |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
19 "time" |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
20 ) |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
21 |
4365
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
22 // SumPreservingRound rounds the values of arr preserving the sum. |
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
23 func SumPreservingRound(arr []float64) []int { |
4353
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
24 |
4365
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
25 type rest struct { |
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
26 key int |
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
27 rest float64 |
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
28 } |
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
29 |
4353
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
30 var ( |
4365
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
31 result = make([]int, len(arr)) |
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
32 rests = make([]rest, len(arr)) |
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
33 sum float64 |
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
34 newSum int |
4353
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
35 ) |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
36 |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
37 // floor all values |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
38 for i, v := range arr { |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
39 sum += v |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
40 result[i] = int(v) |
4365
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
41 newSum += int(v) |
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
42 rests[i] = rest{key: i, rest: v - float64(result[i])} |
4353
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
43 } |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
44 |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
45 // spread delta over values with highest rest |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
46 sort.Slice(rests, func(i, j int) bool { |
4365
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
47 return rests[i].rest > rests[j].rest |
4353
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
48 }) |
4365
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
49 |
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
50 // find the difference in sums |
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
51 delta := int(math.Round(sum)) - newSum |
4353
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
52 for _, v := range rests { |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
53 if delta <= 0 { |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
54 break |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
55 } |
4365
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
56 result[v.key]++ |
4353
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
57 delta-- |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
58 } |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
59 |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
60 return result |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
61 } |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
62 |
4365
e739a4806d7c
rounding to full days: Fixed typos. Make golint happy with the comments. Unexport local type. Simplify and tightend code.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
4353
diff
changeset
|
63 // RoundToFullDays rounds durations to full days. |
4353
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
64 func RoundToFullDays(durations []time.Duration) []int { |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
65 days := make([]float64, len(durations)) |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
66 for i, v := range durations { |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
67 days[i] = v.Hours() / 24 |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
68 } |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
69 return SumPreservingRound(days) |
97312d7954ba
FA diagrams: round to full days in back end.
Sascha Wilde <wilde@intevation.de>
parents:
diff
changeset
|
70 } |