/nix/store/rr0r40cw3mzzbz0sxrnkvnrjpzb82wpc-j27cjzgh5nmq3y8k6hwa6zpi3qgcjia8-source/src/analysis/analysis.cc
Line | Count | Source |
1 | | #include "analysis/analysis.h" |
2 | | |
3 | | #include <algorithm> |
4 | | #include <memory> |
5 | | #include <span> |
6 | | #include <utility> |
7 | | #include <vector> |
8 | | |
9 | | #include "analysis/handnode.h" |
10 | | #include "analysis/handtree.h" |
11 | | #include "types/hand.h" |
12 | | #include "types/meld.h" |
13 | | #include "types/piecetype.h" |
14 | | #include "types/sets.h" |
15 | | |
16 | | namespace mahjong { |
17 | | namespace { |
18 | 142 | void countPieces(Breakdown* b) { |
19 | 1.84k | for (const auto& p : b->pieces) { |
20 | 1.84k | b->counts[p]++; |
21 | 1.84k | } |
22 | 142 | } |
23 | | } // namespace |
24 | | |
25 | 13 | std::vector<Branch> AnalyzeHand(const Hand& hand, const bool only_complete) { |
26 | 13 | Branch base_branch; |
27 | 13 | base_branch.open = !hand.melds.empty(); |
28 | 13 | for (const Meld& meld : hand.melds_range()) { |
29 | 0 | switch (meld.type) { |
30 | 0 | case SetType::kChi: |
31 | 0 | base_branch.chis.emplace_back(meld.start); |
32 | 0 | break; |
33 | 0 | case SetType::kPon: |
34 | 0 | base_branch.pons.emplace_back(meld.start); |
35 | 0 | break; |
36 | 0 | case SetType::kConcealedKan: |
37 | 0 | case SetType::kKan: |
38 | 0 | base_branch.kans.emplace_back(meld.start); |
39 | 0 | break; |
40 | 0 | default: |
41 | 0 | break; |
42 | 0 | } |
43 | 0 | } |
44 | 13 | std::vector<Branch> branches; |
45 | 13 | const std::unique_ptr<Node> root = breakdownHand(hand.live_range()); |
46 | 15 | for (const auto& branch_vec : Node::AsBranchVectors(root.get())) { |
47 | 15 | branches.push_back(base_branch); |
48 | 15 | Branch& branch = branches.back(); |
49 | 142 | for (const Node* node : branch_vec) { |
50 | 142 | switch (node->type()) { |
51 | 20 | case SetType::kChi: |
52 | 20 | branch.chis.emplace_back(node->start()); |
53 | 20 | break; |
54 | 2 | case SetType::kPon: |
55 | 2 | branch.pons.emplace_back(node->start()); |
56 | 2 | break; |
57 | 0 | case SetType::kConcealedKan: |
58 | 0 | case SetType::kKan: |
59 | 0 | branch.kans.emplace_back(node->start()); |
60 | 0 | break; |
61 | 21 | case SetType::kPair: |
62 | 21 | branch.pairs.emplace_back(node->start()); |
63 | 21 | break; |
64 | 99 | case SetType::kSingle: |
65 | 99 | branch.singles.emplace_back(node->start()); |
66 | 99 | break; |
67 | 142 | } |
68 | 142 | } |
69 | 15 | branch.complete = branch.singles.empty(); |
70 | 15 | if (only_complete && !branch.complete) { |
71 | 0 | branches.pop_back(); |
72 | 0 | } |
73 | 15 | } |
74 | 13 | return branches; |
75 | 13 | } |
76 | | |
77 | 142 | std::unique_ptr<Node> breakdownHand(const std::span<const Piece>& pieces) { |
78 | 142 | Breakdown b; |
79 | 142 | b.rootNode = std::make_unique<Node>(/*id=*/b.id++); |
80 | 142 | b.currentNode = b.rootNode.get(); |
81 | 142 | b.pieces = std::vector<Piece>(pieces.begin(), pieces.end()); |
82 | 142 | countPieces(&b); |
83 | 142 | std::ranges::sort(b.pieces); |
84 | 142 | auto [begin, end] = std::ranges::unique(b.pieces); |
85 | 142 | b.pieces.erase(begin, end); |
86 | | |
87 | 142 | driver(&b); |
88 | | |
89 | 142 | return std::move(b.rootNode); |
90 | 142 | } |
91 | | |
92 | | } // namespace mahjong |