etrobocon2018 feat.KatLab  770af34cce41ae9c30c41303275e1add2daae0c3 (with uncommitted changes)
 全て クラス 名前空間 ファイル 関数 変数 列挙型 列挙値 フレンド マクロ定義 ページ
AIAnswerArray.h
[詳解]
1 
7 #ifndef AI_ANSWER_ARRAY_H
8 #define AI_ANSWER_ARRAY_H
9 #include <algorithm>
10 #include <array>
11 #include <cstdint>
12 #include <initializer_list>
13 
14 namespace AI_Answer {
15  template <typename T, std::uint8_t N = 6>
16  class array {
17  private:
18  T sequence[N];
19  std::uint8_t pos;
20 
21  public:
22  std::array<float, 8> probs;
23  constexpr array() : sequence{}, pos(0), probs() {}
25  constexpr std::uint8_t size() noexcept { return N; }
26 
28  constexpr std::uint8_t position() noexcept { return pos; }
29 
31  std::int8_t maxProbabilityNumber() noexcept
32  {
33  auto max_it = std::max_element(probs.begin(), probs.end());
34  return static_cast<std::int8_t>(std::distance(probs.begin(), max_it));
35  }
36 
38  void push_back(const T& value) noexcept
39  {
40  if(pos >= N) pos = 0;
41  sequence[pos] = value;
42  ++pos;
43  }
44 
45  void push_back(std::initializer_list<T> values) noexcept
46  {
47  for(auto value : values) push_back(value);
48  }
49 
50  constexpr T at(const std::uint8_t i) noexcept { return sequence[i]; }
51 
52  constexpr T operator[](const std::uint8_t i) noexcept { return sequence[i]; }
53 
54  // 手書き数字の判定
55  std::int8_t handwriting() noexcept
56  {
57  if(size() != 6) return -1;
58  if(sequence[0] == cast(0)) addProb(0, 1);
59  if(sequence[0] == cast(1)) addProb(2, 3, 4, 5, 6, 7);
60 
61  if(sequence[1] == cast(0)) addProb(0, 1, 2, 3, 7);
62  if(sequence[1] == cast(1)) addProb(4, 5, 6);
63 
64  if(sequence[2] == cast(0)) addProb(1, 3, 5, 7);
65  if(sequence[2] == cast(1)) addProb(0, 2, 4, 6);
66 
67  if(sequence[3] == cast(0)) addProb(4);
68  if(sequence[3] == cast(1)) addProb(0, 1, 2, 3, 5, 6, 7);
69 
70  if(sequence[4] == cast(0)) addProb(1, 2, 7);
71  if(sequence[4] == cast(1)) addProb(0, 3, 4, 5, 6);
72 
73  if(sequence[5] == cast(0)) addProb();
74  if(sequence[5] == cast(1)) addProb(1, 2, 3, 4, 5, 6, 7);
75 
76  return maxProbabilityNumber();
77  }
78 
79  // デジタル数字の判定
80  std::int8_t digitalNumber() noexcept
81  {
82  if(size() != 6) return -1;
83  if(sequence[0] == cast(0)) addProb(1, 4);
84  if(sequence[0] == cast(1)) addProb(0, 2, 3, 5, 6, 7);
85 
86  if(sequence[1] == cast(0)) addProb(0, 1, 7);
87  if(sequence[1] == cast(1)) addProb(2, 3, 4, 5, 6);
88 
89  if(sequence[2] == cast(0)) addProb(1, 4, 7);
90  if(sequence[2] == cast(1)) addProb(0, 2, 3, 5, 6);
91 
92  if(sequence[3] == cast(0)) addProb(1, 3, 4, 5, 7);
93  if(sequence[3] == cast(1)) addProb(0, 2, 6);
94 
95  if(sequence[4] == cast(0)) addProb(5, 6);
96  if(sequence[4] == cast(1)) addProb(0, 1, 2, 3, 4, 7);
97 
98  if(sequence[5] == cast(0)) addProb(1, 2, 3, 7);
99  if(sequence[5] == cast(1)) addProb(0, 4, 5, 6);
100 
101  return maxProbabilityNumber();
102  }
103 
104  constexpr T cast(int i) { return static_cast<T>(i); }
105 
106  // 確率を加える
107  template <class... Args>
108  void addProb(const Args&... args)
109  {
110  for(int i : std::initializer_list<int>{ args... }) probs[i] += 1.0 / size();
111  }
112  };
113 } // namespace AI_Answer
114 
115 #endif
constexpr T at(const std::uint8_t i) noexcept
Definition: AIAnswerArray.h:50
constexpr T cast(int i)
std::array< float, 8 > probs
Definition: AIAnswerArray.h:22
void addProb(const Args &...args)
std::int8_t handwriting() noexcept
Definition: AIAnswerArray.h:55
std::int8_t digitalNumber() noexcept
Definition: AIAnswerArray.h:80
std::int8_t maxProbabilityNumber() noexcept
確率を入れた配列のうち、一番確率の高い要素のインデックスを返す
Definition: AIAnswerArray.h:31
constexpr std::uint8_t position() noexcept
配列の要素を指し示すポインタを返す
Definition: AIAnswerArray.h:28
void push_back(std::initializer_list< T > values) noexcept
Definition: AIAnswerArray.h:45
constexpr array()
Definition: AIAnswerArray.h:23
constexpr T operator[](const std::uint8_t i) noexcept
Definition: AIAnswerArray.h:52
constexpr std::uint8_t size() noexcept
配列のサイズを返す
Definition: AIAnswerArray.h:25
void push_back(const T &value) noexcept
配列の要素を入れる
Definition: AIAnswerArray.h:38