aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/morling/onebrc/CalculateAverage_jeevjyot.java
blob: 191e407bc18317cc93ead83625148cd487eb1643 (plain)
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
/*
 *  Copyright 2023 The original authors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package dev.morling.onebrc;

import static java.lang.Math.round;
import static java.util.stream.Collectors.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collector;

public class CalculateAverage_jeevjyot {

    public static final String MEAUREMENT_FILE = "./measurements.txt";

    public static void main(String[] args) throws IOException {
        Map<String, tempMeasurement> result = new ConcurrentHashMap<>();
        Files.lines(Path.of(MEAUREMENT_FILE))
                .parallel()
                .forEach(s -> {
                    var separatorIndex = s.indexOf(";");
                    var stationName = s.substring(0, separatorIndex);
                    var temp = s.substring(separatorIndex + 1);
                    result.computeIfAbsent(stationName, d -> new tempMeasurement(parseDoubleFast(temp)))
                            .recordTemp(parseDoubleFast(temp));
                });

        TreeMap<String, tempMeasurement> sortedStats = new TreeMap<>(result);
        System.out.println(sortedStats);
    }

    public static double parseDoubleFast(String str) {
        // Simple implementation - can be improved with more error checking and support for different formats
        boolean negative = false;
        double result = 0;
        int length = str.length();
        int i = 0;
        if (str.charAt(0) == '-') {
            negative = true;
            i++;
        }
        for (; i < length; i++) {
            char c = str.charAt(i);
            if (c == '.') {
                int divisor = 1;
                for (i++; i < length; i++) {
                    result += (double) (str.charAt(i) - '0') / (divisor *= 10);
                }
                break;
            }
            result = result * 10 + (c - '0');
        }
        return negative ? -result : result;
    }

    private static double round(double value) {
        return Math.round(value * 10.0) / 10.0;
    }

    public static class tempMeasurement {
        double minTemp;
        double maxTemp;
        double sum;
        int count;

        public tempMeasurement(double temString) {
            this.minTemp = temString;
            this.maxTemp = temString;
            this.sum = 0.0;
            this.count = 0;
        }

        public synchronized void recordTemp(Double temp) {
            this.minTemp = Math.min(minTemp, temp);
            this.maxTemp = Math.max(maxTemp, temp);
            sum += temp;
            count++;
        }

        double getAverage() {
            return round(sum) / count;
        }

        @Override
        public String toString() {
            return String.format("%.1f/%.1f/%.1f", round(minTemp), round(getAverage()), round(maxTemp));
        }
    }
}