Coverage Report

Created: 2025-09-03 03:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/nix/store/i1aar97n7b4yf8rk94p66if25brfvvdx-gqvzl8a5pvrg3xj44q0msrndcripi8dk-source/src/analysis/handnode.h
Line
Count
Source
1
#pragma once
2
#include <algorithm>
3
#include <cstddef>
4
#include <iostream>
5
#include <iterator>
6
#include <memory>
7
#include <string>
8
#include <vector>
9
10
#include "types/piecetype.h"
11
#include "types/sets.h"
12
13
namespace mahjong {
14
15
class Node : public std::enable_shared_from_this<Node> {
16
 public:
17
  bool operator!=(const Node& n) const;
18
19
423
  explicit Node(int id) : id_(id) {}
20
21
  Node(int id, SetType type, Piece start, Node* parent)
22
4.40k
      : id_(id), type_(type), start_(start), parent_(parent) {}
23
24
  std::string typeToStr() const;
25
26
4.40k
  Node* addLeaf(Piece start, SetType type, int id) {
27
4.40k
    leaves_.push_back(std::make_unique<Node>(
28
4.40k
        /*id=*/id,
29
4.40k
        /*type=*/type,
30
4.40k
        /*start=*/start,
31
4.40k
        /*parent=*/this));
32
4.40k
    return leaves_.back().get();
33
4.40k
  }
34
35
  [[nodiscard]] static std::vector<std::vector<const Node*>> AsBranchVectors(
36
      const Node* root);
37
38
  std::ostream& DumpAsTGF(std::ostream& os) const;
39
  std::ostream& DumpAsDot(std::ostream& os) const;
40
  [[nodiscard]] bool IsComplete() const;
41
42
  // Iterators
43
  class ConstIterator {
44
    const Node* root_;
45
    bool end_;
46
    friend Node;
47
    explicit ConstIterator(const Node* root, bool end)
48
0
        : root_(root), end_(end) {}
49
50
   public:
51
    ConstIterator& operator++();
52
    const Node& operator*() const;
53
    bool operator!=(const ConstIterator& other) const;
54
    // iterator traits
55
    using difference_type = std::ptrdiff_t;
56
    using pointer = const Node*;
57
    using iterator_category = std::forward_iterator_tag;
58
  };
59
  class Iterator {
60
    ConstIterator itr_;
61
    friend Node;
62
0
    explicit Iterator(Node* root, bool end) : itr_(root, end) {}
63
64
   public:
65
    Iterator& operator++();
66
    Node& operator*() const;
67
    bool operator!=(const Iterator& other) const;
68
    // iterator traits
69
    using difference_type = std::ptrdiff_t;
70
    using pointer = Node*;
71
    using iterator_category = std::forward_iterator_tag;
72
  };
73
  [[nodiscard]] ConstIterator begin() const;
74
  [[nodiscard]] ConstIterator end() const;
75
  [[nodiscard]] Iterator begin();
76
  [[nodiscard]] Iterator end();
77
78
4.83k
  size_t leafPosInParent() const {
79
4.83k
    if (!parent_) {
80
424
      return 0;
81
424
    }
82
4.41k
    auto leaf_it = std::find_if(
83
4.41k
        parent_->leaves_.begin(), parent_->leaves_.end(),
84
4.85k
        [id = this->id_](const auto& leaf) { return leaf->id_ == id; });
85
4.41k
    return std::distance(parent_->leaves_.begin(), leaf_it);
86
4.83k
  }
87
88
0
  int id() const { return id_; }
89
4.72k
  SetType type() const { return type_; }
90
3.51k
  Piece start() const { return start_; }
91
1.90k
  Node* parent() { return parent_; }
92
0
  Node const* parent() const { return parent_; }
93
0
  const std::vector<std::unique_ptr<Node>>& leaves() const { return leaves_; }
94
0
  std::vector<std::unique_ptr<Node>>& leaves() { return leaves_; }
95
96
 private:
97
  int id_;
98
  SetType type_;
99
  Piece start_;
100
  Node* parent_ = nullptr;  // not owned.
101
  std::vector<std::unique_ptr<Node>> leaves_;
102
};
103
104
}  // namespace mahjong