|
3 | 3 | #include <util/generic/map.h>
|
4 | 4 | #include <util/string/cast.h>
|
5 | 5 |
|
6 |
| -struct TDist { |
7 |
| - TVector<set_member> Members; |
8 |
| - long Max = 0; |
9 |
| -}; |
10 |
| - |
11 |
| -extern "C" void ReadDistFromResource(const char* name, distribution* target) { |
12 |
| - static auto distributions = [] () { |
13 |
| - TMap<TStringBuf, TDist> result; |
14 |
| - static const auto resource = NResource::Find("dists.dss"); |
15 |
| - TStringBuf in(resource); |
16 |
| - TStringBuf line; |
17 |
| - TDist* dist = nullptr; |
| 6 | +class TDistributions { |
| 7 | +public: |
| 8 | + TDistributions() { |
| 9 | + const auto resource = NResource::Find("dists.dss"); |
| 10 | + TStringInput in(resource); |
| 11 | + TString line; |
| 12 | + TDists::iterator dist = Dists.end(); |
18 | 13 | while (in.ReadLine(line)) {
|
19 | 14 | if (line.empty() || line.find('#') != TStringBuf::npos) {
|
20 | 15 | continue;
|
21 | 16 | }
|
22 |
| - if (!dist) { |
| 17 | + if (dist == Dists.end()) { |
23 | 18 | constexpr TStringBuf prefix = "begin";
|
24 |
| - if (to_lower(TString(line.substr(0, prefix.length()))) == prefix) { |
| 19 | + if (to_lower(line.substr(0, prefix.length())) == prefix) { |
25 | 20 | const auto pos = line.find_first_of("\t ");
|
26 | 21 | if (pos != TStringBuf::npos) {
|
27 |
| - dist = &result[line.substr(pos + 1)]; |
| 22 | + const TString key(line.substr(pos + 1)); |
| 23 | + dist = Dists.emplace(key, TDist()).first; |
28 | 24 | }
|
29 | 25 | }
|
30 | 26 | } else {
|
31 | 27 | constexpr TStringBuf prefix = "end";
|
32 |
| - if (to_lower(TString(line.substr(0, prefix.length()))) == prefix) { |
33 |
| - dist = nullptr; |
| 28 | + if (to_lower(line.substr(0, prefix.length())) == prefix) { |
| 29 | + dist = Dists.end(); |
34 | 30 | continue;
|
35 | 31 | }
|
36 | 32 | TStringBuf token, weightStr;
|
37 |
| - line.Split('|', token, weightStr); |
| 33 | + TStringBuf(line).Split('|', token, weightStr); |
38 | 34 | while(weightStr.SkipPrefix(" "));
|
39 | 35 | while(weightStr.ChopSuffix(" "));
|
40 | 36 | long weight = FromString(weightStr);
|
41 | 37 | if (to_lower(TString(token)) == "count") {
|
42 |
| - dist->Members.reserve(weight); |
| 38 | + dist->second.Members.reserve(weight); |
43 | 39 | } else {
|
44 |
| - dist->Max += weight; |
45 |
| - dist->Members.emplace_back(); |
46 |
| - dist->Members.back().weight = dist->Max; |
47 |
| - dist->Members.back().text = const_cast<char*>(token.data()); |
48 |
| - dist->Members.back().text[token.length()] = '\0'; |
| 40 | + dist->second.Max += weight; |
| 41 | + dist->second.Members.emplace_back(); |
| 42 | + Strings.emplace_back(token); |
| 43 | + dist->second.Members.back().weight = dist->second.Max; |
| 44 | + dist->second.Members.back().text = Strings.back().begin(); |
49 | 45 | }
|
50 | 46 | }
|
51 | 47 | }
|
52 |
| - return result; |
53 |
| - }(); |
| 48 | + } |
54 | 49 |
|
55 |
| - if (auto* dist = MapFindPtr(distributions, name)) { |
56 |
| - target->count = dist->Members.size(); |
57 |
| - target->list = dist->Members.data(); |
58 |
| - target->permute = nullptr; |
59 |
| - target->max = dist->Max; |
| 50 | + void Fill(const char* name, distribution* target) { |
| 51 | + if (auto* dist = MapFindPtr(Dists, name)) { |
| 52 | + target->count = dist->Members.size(); |
| 53 | + target->list = dist->Members.data(); |
| 54 | + target->permute = nullptr; |
| 55 | + target->max = dist->Max; |
| 56 | + } |
60 | 57 | }
|
| 58 | + |
| 59 | +private: |
| 60 | + struct TDist { |
| 61 | + TVector<set_member> Members; |
| 62 | + long Max = 0; |
| 63 | + }; |
| 64 | + using TDists = TMap<TString, TDist>; |
| 65 | + TVector<TString> Strings; |
| 66 | + TDists Dists; |
| 67 | +}; |
| 68 | + |
| 69 | +extern "C" void ReadDistFromResource(const char* name, distribution* target) { |
| 70 | + Singleton<TDistributions>()->Fill(name, target); |
61 | 71 | }
|
0 commit comments