-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
90 lines (79 loc) · 2.54 KB
/
main.cpp
File metadata and controls
90 lines (79 loc) · 2.54 KB
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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 Jussi Pakkanen
#include <stdio.h>
#include <pystd2026.hpp>
#include <pystd2026_introsort.hpp>
#include <assert.h>
struct WordCount {
const pystd2026::U8String *str;
size_t count;
int operator<=>(const WordCount &o) const {
auto diff = (int64_t)o.count - (int64_t)count;
if(diff != 0) {
return diff;
}
return (int64_t)o.str->size_bytes() - (int64_t)str->size_bytes();
}
};
int file_main(int argc, char **argv) {
if(argc != 2) {
printf("%s <infile>\n", argv[0]);
return 0;
}
try {
pystd2026::HashMap<pystd2026::U8String, size_t> counts;
pystd2026::File f(argv[1], "r");
for(auto &&line : f) {
pystd2026::U8String u8line(move(line));
auto words = u8line.split_ascii();
for(const auto &w : words) {
++counts[w];
}
}
pystd2026::Vector<WordCount> stats;
for(const auto item : counts) {
stats.push_back(WordCount{item.key, *item.value});
}
pystd2026::introsort(stats.begin(), stats.end());
for(const auto &i : stats) {
printf("%d %s\n", (int)i.count, i.str->c_str());
}
} catch(const pystd2026::PyException &e) {
printf("%s\n", e.what().c_str());
return 1;
}
return 0;
}
int hashmap_main(int, char **) {
pystd2026::HashMap<pystd2026::U8String, size_t> wordcounter;
pystd2026::U8String key1("key1");
pystd2026::U8String key2("key2");
printf("Initial size: %d\n", (int)wordcounter.size());
printf("Contains key1: %d\n", (int)wordcounter.contains(key1));
printf("Contains key2: %d\n", (int)wordcounter.contains(key2));
printf("Inserting key1.\n");
wordcounter.insert(key1, 66);
printf("Size: %d\n", (int)wordcounter.size());
printf("Contains key1: %d\n", (int)wordcounter.contains(key1));
printf("Value of key1: %d\n", (int)*wordcounter.lookup(key1));
printf("Contains key2: %d\n", (int)wordcounter.contains(key2));
return 0;
}
int split_main(int, char **) {
pystd2026::U8String text("aa bb cc");
auto r = text.split_ascii();
printf("Split array size: %d\n", (int)r.size());
for(size_t i = 0; i < r.size(); ++i) {
printf(" %s\n", r[i].c_str());
}
return 0;
}
int main(int argc, char **argv) {
if(false) {
return hashmap_main(argc, argv);
} else if(false) {
return split_main(argc, argv);
} else {
return file_main(argc, argv);
}
}