MicroMouse Maze Library  3703225
公開メンバ関数 | 公開変数類 | 静的公開変数類 | フレンド | 全メンバ一覧
MazeLib::WallIndex 構造体

区画ベースではなく、壁ベースの管理ID [詳解]

#include <Maze.h>

公開メンバ関数

constexpr WallIndex ()
 デフォルトコンストラク [詳解]
 
constexpr WallIndex (const int8_t x, const int8_t y, const uint8_t z)
 成分を受け取ってそのまま格納するコンストラクタ [詳解]
 
constexpr WallIndex (const Position p, const Direction d)
 表現の冗長性を除去して格納するコンストラクタ [詳解]
 
constexpr WallIndex (const uint16_t i)
 IDを使って初期化するコンストラクタ [詳解]
 
bool operator== (const WallIndex i) const
 等号 [詳解]
 
bool operator!= (const WallIndex i) const
 等号否定 [詳解]
 
uint16_t getIndex () const
 迷路内の壁を一意な通し番号として表現したIDを返す。 [詳解]
 
Position getPosition () const
 位置の取得 [詳解]
 
Direction getDirection () const
 方向の取得 [詳解]
 
bool isInsideOfField () const
 壁がフィールド内か判定する関数 [詳解]
 
WallIndex next (const Direction d) const
 引数方向の WallIndex を取得する関数 [詳解]
 
std::array< Direction, 6 > getNextDirection6 () const
 現在壁に隣接する、柱ではない6方向を取得 [詳解]
 

公開変数類

union {
   struct {
      int8_t   x
 区画座標のx成分 [詳解]
 
      int8_t   y: 7
 区画座標のy成分 [詳解]
 
      uint8_t   z: 1
 区画内の壁の位置。0:East, 1:North [詳解]
 
   } 
 
   uint16_t   data
 データ全体へのアクセス用 [詳解]
 
}; 
 

静的公開変数類

static constexpr int SIZE = MAZE_SIZE_MAX * MAZE_SIZE_MAX * 2
 壁を unique な通し番号として表現したときの総数。 配列の確保などで使用できる。 [詳解]
 

フレンド

std::ostream & operator<< (std::ostream &os, const WallIndex i)
 表示用演算子のオーバーロード。 ( x, y, d) の形式 [詳解]
 

詳解

区画ベースではなく、壁ベースの管理ID

uint16_t にキャストすることで全部の壁が通し番号になったIDを 取得できるという特徴がある。 迷路内部の壁の総数 WallIndex::SIZE 個の配列を確保しておけば、 取得したIDをインデックスとして使える。そのとき、 WallIndex が 迷路の内部にあるかどうか確認すること。(配列の範囲外アクセス防止) isInsideOfField() 関数により迷路の内部に位置するか確認できる。 最初から全部が通し番号のIDで保持してしまうと、 迷路の範囲外の壁を表現できなくなってしまうため、 必要に応じてIDを生成するようになっている。

[x, y] : Cell Position
z : Wall Distinction in the Cell; 0:East, 1:North
=> (x, y, z) : Wall Index
+-------------+-------------+-------------+
| | | |
| Cell Wall | |
| | | |
+--- z = 1 ---+- (x, y, 1) -+-------------+
| | | |
| (x-1, y, 0) [ x, y] (x, y, 0) |
| | | |
+--- z = 1 ---+- (x,y-1,1) -+-------------+
| | | |
| z = 0 z = 0 |
| | | |
+-------------+-------------+-------------+
int8_t y
区画座標のy成分
Definition: Maze.h:466
int8_t x
区画座標のx成分
Definition: Maze.h:465
uint8_t z
区画内の壁の位置。0:East, 1:North
Definition: Maze.h:467

構築子と解体子

◆ WallIndex() [1/4]

constexpr MazeLib::WallIndex::WallIndex ( )
inlineconstexpr

デフォルトコンストラク

477 : data(0) {}
uint16_t data
データ全体へのアクセス用
Definition: Maze.h:469

◆ WallIndex() [2/4]

constexpr MazeLib::WallIndex::WallIndex ( const int8_t  x,
const int8_t  y,
const uint8_t  z 
)
inlineconstexpr

成分を受け取ってそのまま格納するコンストラクタ

482  : x(x), y(y), z(z) {}

◆ WallIndex() [3/4]

constexpr MazeLib::WallIndex::WallIndex ( const Position  p,
const Direction  d 
)
inlineconstexpr

表現の冗長性を除去して格納するコンストラクタ

引数
p区画位置
d区画内方向。4方位
488  : x(p.x), y(p.y) {
489  uniquify(d);
490  }
unsigned int d
壁の方向
Definition: Maze.h:609

◆ WallIndex() [4/4]

constexpr MazeLib::WallIndex::WallIndex ( const uint16_t  i)
inlineconstexpr

IDを使って初期化するコンストラクタ

引数
i壁の通し番号ID。迷路内の壁であること。
注意
迷路外の壁の場合未定義動作となる。
497  : x(i & (MAZE_SIZE_MAX - 1)),
498  y((i >> MAZE_SIZE_BIT) & (MAZE_SIZE_MAX - 1)),
499  z(i >> (2 * MAZE_SIZE_BIT)) {}
static constexpr int MAZE_SIZE_MAX
迷路の1辺の区画数の最大値。2のbit数乗の値。
Definition: Maze.h:114
static constexpr int MAZE_SIZE_BIT
迷路の1辺の区画数の bit 数。bit shift などに用いる。
Definition: Maze.h:110

関数詳解

◆ getDirection()

Direction MazeLib::WallIndex::getDirection ( ) const
inline

方向の取得

523  {
524  // return z == 0 ? Direction::East : Direction::North;
525  return z << 1; //< 高速化
526  }

◆ getIndex()

uint16_t MazeLib::WallIndex::getIndex ( ) const
inline

迷路内の壁を一意な通し番号として表現したIDを返す。

注意
迷路外の壁の場合未定義動作となる。 WallIndex::isInsideOfField() で迷路区画内か確認すること。
戻り値
uint16_t ID
516  {
517  // return (z << (2 * MAZE_SIZE_BIT)) | (y << MAZE_SIZE_BIT) | x;
518  return (z << (MAZE_SIZE_BIT << 1)) | (y << MAZE_SIZE_BIT) | x; //< 高速化
519  }

◆ getNextDirection6()

std::array<Direction, 6> MazeLib::WallIndex::getNextDirection6 ( ) const
inline

現在壁に隣接する、柱ではない6方向を取得

戻り値
std::array<Direction, 6> 隣接方向の配列
557  {
558  const auto d = getDirection();
559  return {{
561  d + Direction::Back,
566  }};
567  }
@ Back
Definition: Maze.h:168
@ Left45
Definition: Maze.h:165
@ Right45
Definition: Maze.h:171
@ Right135
Definition: Maze.h:169
@ Front
Definition: Maze.h:164
@ Left135
Definition: Maze.h:167
Direction getDirection() const
方向の取得
Definition: Maze.h:523
呼び出し関係図:

◆ getPosition()

Position MazeLib::WallIndex::getPosition ( ) const
inline

位置の取得

521 { return Position(x, y); }

◆ isInsideOfField()

bool MazeLib::WallIndex::isInsideOfField ( ) const
inline

壁がフィールド内か判定する関数

(x, y) が (0, 0) と (MAZE_SIZE-1, MAZE_SIZE-1) の間、かつ、 z が外周上でない

戻り値
true フィールド内
false フィールド外(外周上を含む)
538  {
539  /* x,y が フィールド内かつ、外周上にいない */
540  // return !(x < 0 || y < 0 || x >= MAZE_SIZE || y >= MAZE_SIZE ||
541  // (z == 0 && (x == MAZE_SIZE - 1)) ||
542  // (z == 1 && (y == MAZE_SIZE - 1)));
543  /* 高速化 */
544  return (static_cast<uint8_t>(x) < MAZE_SIZE - 1 + z) &&
545  (static_cast<uint8_t>(y) < MAZE_SIZE - z);
546  }
static constexpr int MAZE_SIZE
迷路の1辺の区画数の定数。
Definition: Maze.h:106

◆ next()

WallIndex MazeLib::WallIndex::next ( const Direction  d) const

引数方向の WallIndex を取得する関数

引数
d隣接方向
戻り値
WallIndex 隣接壁
72  {
73  switch (d) {
74  case Direction::East:
75  return WallIndex(x + 1, y, z);
77  return WallIndex(x + 1 - z, y + z, 1 - z);
78  case Direction::North:
79  return WallIndex(x, y + 1, z);
81  return WallIndex(x - z, y + z, 1 - z);
82  case Direction::West:
83  return WallIndex(x - 1, y, z);
85  return WallIndex(x - z, y - 1 + z, 1 - z);
86  case Direction::South:
87  return WallIndex(x, y - 1, z);
89  return WallIndex(x + 1 - z, y - 1 + z, 1 - z);
90  default:
91  MAZE_LOGE << d << std::endl;
92  return WallIndex(x, y, z);
93  }
94 }
#define MAZE_LOGE
Definition: Maze.h:78
@ SouthEast
Definition: Maze.h:158
@ North
Definition: Maze.h:153
@ South
Definition: Maze.h:157
@ NorthWest
Definition: Maze.h:154
@ NorthEast
Definition: Maze.h:152
@ East
Definition: Maze.h:151
@ West
Definition: Maze.h:155
@ SouthWest
Definition: Maze.h:156
constexpr WallIndex()
デフォルトコンストラク
Definition: Maze.h:477
呼び出し関係図:

◆ operator!=()

bool MazeLib::WallIndex::operator!= ( const WallIndex  i) const
inline

等号否定

506  {
507  // return x != i.x || y != i.y || z != i.z;
508  return data != i.data; //< 高速化
509  }

◆ operator==()

bool MazeLib::WallIndex::operator== ( const WallIndex  i) const
inline

等号

501  {
502  // return x == i.x && y == i.y && z == i.z;
503  return data == i.data; //< 高速化
504  }

フレンドと関連関数の詳解

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const WallIndex  i 
)
friend

表示用演算子のオーバーロード。 ( x, y, d) の形式

95  {
96  return os << "( " << std::setw(2) << +i.x << ", " << std::setw(2) << +i.y
97  << ", " << i.getDirection().toChar() << ")";
98 }

メンバ詳解

◆ 

union { ... }

◆ data

uint16_t MazeLib::WallIndex::data

データ全体へのアクセス用

◆ SIZE

constexpr int MazeLib::WallIndex::SIZE = MAZE_SIZE_MAX * MAZE_SIZE_MAX * 2
staticconstexpr

壁を unique な通し番号として表現したときの総数。 配列の確保などで使用できる。

◆ x

int8_t MazeLib::WallIndex::x

区画座標のx成分

◆ y

int8_t MazeLib::WallIndex::y

区画座標のy成分

◆ z

uint8_t MazeLib::WallIndex::z

区画内の壁の位置。0:East, 1:North


この構造体詳解は次のファイルから抽出されました: