aboutsummaryrefslogtreecommitdiff
path: root/treybastian_1brc.cbl
blob: c417940567a80bf48be980568fe60693dd8f9b5e (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
       IDENTIFICATION DIVISION.
       PROGRAM-ID. 1brc.
       AUTHOR. Trey Bastian.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT measurements-file ASSIGN TO "./measurements.txt"
           ORGANIZATION IS LINE SEQUENTIAL.

       DATA DIVISION.
       FILE SECTION.
       FD measurements-file.
         01 measurement-line PIC X(106).

       WORKING-STORAGE SECTION.
       01 results-table.
         02 stations OCCURS 10000 TIMES INDEXED BY idx.
           03 name PIC A(100).
           03 min-temp PIC S9(2)V9 VALUE ZEROS.
           03 mean-temp PIC S9(2)V9 VALUE ZEROS.
           03 max-temp PIC S9(2)V9 VALUE ZEROS.
           03 temp-count PIC 9(10) VALUE ZEROS.
           03 total PIC S9(10)V9(2) VALUE ZEROS.

       77 last-idx PIC 9(6) VALUE 1.
       77 station-name PIC A(100).
       77 temperature PIC S9(2)V9 VALUE ZEROS.
       77 temp-str PIC -(2)9.9 VALUE ZEROS.
       01 pic x.
             88 eof VALUE "Y".
             88 eof-n VALUE "N".

       PROCEDURE DIVISION.
           OPEN INPUT measurements-file.
           SET eof-n TO TRUE.
           PERFORM UNTIL eof
             READ measurements-file AT END
                 SET eof TO TRUE
             NOT AT END
               UNSTRING measurement-line DELIMITED BY ";"
                 INTO station-name, temperature
               END-UNSTRING
              MOVE 1 to idx
              SEARCH stations
                AT END
                  MOVE station-name TO name(last-idx)
                  MOVE temperature TO min-temp(last-idx)
                  MOVE temperature TO mean-temp(last-idx)
                  MOVE temperature TO max-temp(last-idx)
                  MOVE temperature TO total(last-idx)
                  ADD 1 TO temp-count(last-idx)
                  ADD 1 TO last-idx
                WHEN name(idx) = station-name
                  ADD 1 TO temp-count(idx)
                  IF min-temp(idx) > temperature THEN
                    MOVE temperature TO min-temp(idx)
                  END-IF
                  IF max-temp(idx) < temperature THEN
                   MOVE temperature TO max-temp(idx)
                  END-IF
                  ADD temperature TO total(idx)
                  COMPUTE mean-temp(idx) ROUNDED = total(idx) /
                  temp-count(idx)
               END-SEARCH

             END-READ
           END-PERFORM.
           CLOSE measurements-file.

           SORT stations ASCENDING name.
           PERFORM VARYING idx FROM 1 BY 1 UNTIL idx = 10001
             IF name(idx) NOT EQUAL SPACES THEN
               DISPLAY FUNCTION TRIM(name(idx) TRAILING)
                 WITH NO ADVANCING
               DISPLAY ";" WITH NO ADVANCING
               MOVE min-temp(idx) TO temp-str
               DISPLAY FUNCTION TRIM(temp-str LEADING)
                 WITH NO ADVANCING
               DISPLAY ";" WITH NO ADVANCING
               MOVE mean-temp(idx) TO temp-str
               DISPLAY FUNCTION TRIM(temp-str LEADING)
                 WITH NO ADVANCING
               DISPLAY ";" WITH NO ADVANCING
               MOVE max-temp(idx) TO temp-str
               DISPLAY FUNCTION TRIM(temp-str LEADING)
             END-IF
           END-PERFORM.

       STOP-RUN.