OpenTTD Source 20261123-master-g1c55c72fd0
SpiralTileIterator Class Reference

Helper class for SpiralTileSequence. More...

#include <tilearea_spiral.h>

Public Types

using value_type = TileIndex
 value_type iterator trait
using difference_type = std::ptrdiff_t
 difference_type iterator trait
using iterator_category = std::forward_iterator_tag
 iterator_category iterator trait
using pointer = void
 pointer iterator trait
using reference = void
 reference iterator trait

Public Member Functions

 SpiralTileIterator (TileIndex center, uint diameter)
 Create the iterator.
 SpiralTileIterator (TileIndex start_north, uint radius, uint w, uint h)
 Create the iterator.
bool operator== (const SpiralTileIterator &rhs) const
 Compare with other iterator.
bool operator== (const std::default_sentinel_t &) const
 Compare with end sentinel.
TileIndex operator* () const
 Get the current tile.
SpiralTileIterator & operator++ ()
 Increment the iterator and set it to the next position.

Private Member Functions

void SkipOutsideMap ()
 Advance the internal state until it reaches a valid tile or the end.
void InitPosition ()
 Initialise "position" after "dir" was changed.
void Increment ()
 Advance the internal state to the next potential tile.
bool IsEnd () const
 Test whether the iterator reached the end.

Private Attributes

const uint max_radius
 The maximum radius of the spiral.
DiagDirectionIndexArray< uint > extent
 Size of hole in the centre of the spiral.
uint cur_radius
 The current radius.
DiagDirection dir
 The current spiral direction.
uint position
 The current position along an edge.
uint x
 The current tile X-coordinate.
uint y
 The current tile Y-coordinate.

Detailed Description

Helper class for SpiralTileSequence.

Definition at line 20 of file tilearea_spiral.h.

Member Typedef Documentation

◆ difference_type

using SpiralTileIterator::difference_type = std::ptrdiff_t

difference_type iterator trait

Definition at line 23 of file tilearea_spiral.h.

◆ iterator_category

using SpiralTileIterator::iterator_category = std::forward_iterator_tag

iterator_category iterator trait

Definition at line 24 of file tilearea_spiral.h.

◆ pointer

pointer iterator trait

Definition at line 25 of file tilearea_spiral.h.

◆ reference

reference iterator trait

Definition at line 26 of file tilearea_spiral.h.

◆ value_type

value_type iterator trait

Definition at line 22 of file tilearea_spiral.h.

Constructor & Destructor Documentation

◆ SpiralTileIterator() [1/2]

SpiralTileIterator::SpiralTileIterator ( TileIndex center,
uint diameter )

Create the iterator.

Generate TileIndices for a square area around a center tile.

The size of the square is given by the length of the edge. If the size is even, the south extent will be larger than the north extent.

Example for diameter=4, [ ] is the "center": 1 1 1 1 [0] 1 1 0 0 1 1 0 1 1 1 1 The sequence starts with the "0" tiles, and continues with the shells around it.

Parameters
centerCenter of the square area.
diameterEdge length of the square.
Precondition
diameter > 0
Note
This constructor uses a "diameter", unlike the other constructor using a "radius".

Definition at line 19 of file tilearea_spiral.cpp.

References Begin, cur_radius, dir, extent, InitPosition(), Invalid, max_radius, position, SkipOutsideMap(), TileX(), TileY(), x, and y.

Referenced by operator++(), and operator==().

◆ SpiralTileIterator() [2/2]

SpiralTileIterator::SpiralTileIterator ( TileIndex start_north,
uint radius,
uint w,
uint h )

Create the iterator.

Generate TileIndices for a rectangular area with an optional rectangular hole in the center.

The TileIndices will be sorted by increasing distance from the center (hole).

Example for radius=2, w=2, h=1, [ ] is "start_north": 1 1 1 1 [0] 1 1 0 0 1 1 0 H 0 1 1 0 H 0 1 1 0 0 1 1 0 1 1 1 1 The sequence starts with the "0" tiles, and continues with the shells around it.

Parameters
start_northTile directly north from the center hole.
radiusRadial distance between outer rectangle and center hole.
wWidth of the inner rectangular hole.
hHeight of the inner rectangular hole.
Precondition
radius > 0
Note
This constructor uses a "radius", unlike the other constructor using a "diameter".

Definition at line 43 of file tilearea_spiral.cpp.

References cur_radius, dir, extent, InitPosition(), max_radius, SkipOutsideMap(), TileX(), TileY(), x, and y.

Member Function Documentation

◆ Increment()

void SpiralTileIterator::Increment ( )
private

Advance the internal state to the next potential tile.

The tile may be outside the map though.

Definition at line 73 of file tilearea_spiral.cpp.

References Begin, cur_radius, dir, End, InitPosition(), Invalid, IsEnd(), position, TileIndexDiffCByDiagDir(), TileIndexDiffCByDir(), W, x, and y.

Referenced by operator++(), and SkipOutsideMap().

◆ InitPosition()

void SpiralTileIterator::InitPosition ( )
private

Initialise "position" after "dir" was changed.

Definition at line 64 of file tilearea_spiral.cpp.

References cur_radius, dir, extent, and position.

Referenced by Increment(), SpiralTileIterator(), and SpiralTileIterator().

◆ IsEnd()

bool SpiralTileIterator::IsEnd ( ) const
inlineprivate

Test whether the iterator reached the end.

Returns
true iff the end of the iteration is reached.

Definition at line 90 of file tilearea_spiral.h.

References Invalid.

Referenced by Increment(), operator==(), and SkipOutsideMap().

◆ operator*()

TileIndex SpiralTileIterator::operator* ( ) const
inline

Get the current tile.

Returns
The current tile.

Definition at line 54 of file tilearea_spiral.h.

References TileXY(), x, and y.

◆ operator++()

SpiralTileIterator & SpiralTileIterator::operator++ ( )
inline

Increment the iterator and set it to the next position.

Returns
This iterator after incrementing.

Definition at line 63 of file tilearea_spiral.h.

References Increment(), SkipOutsideMap(), and SpiralTileIterator().

◆ operator==() [1/2]

bool SpiralTileIterator::operator== ( const SpiralTileIterator & rhs) const
inline

Compare with other iterator.

Parameters
rhsThe other iterator to compare with.
Returns
true iff the other iterator matches this value.

Definition at line 36 of file tilearea_spiral.h.

References SpiralTileIterator(), x, and y.

◆ operator==() [2/2]

bool SpiralTileIterator::operator== ( const std::default_sentinel_t & ) const
inline

Compare with end sentinel.

Returns
true iff this iterator is at the end.

Definition at line 45 of file tilearea_spiral.h.

References IsEnd().

◆ SkipOutsideMap()

void SpiralTileIterator::SkipOutsideMap ( )
private

Advance the internal state until it reaches a valid tile or the end.

Definition at line 56 of file tilearea_spiral.cpp.

References Increment(), IsEnd(), Map::SizeX(), Map::SizeY(), x, and y.

Referenced by operator++(), SpiralTileIterator(), and SpiralTileIterator().

Field Documentation

◆ cur_radius

uint SpiralTileIterator::cur_radius
private

The current radius.

Definition at line 76 of file tilearea_spiral.h.

Referenced by Increment(), InitPosition(), SpiralTileIterator(), and SpiralTileIterator().

◆ dir

DiagDirection SpiralTileIterator::dir
private

The current spiral direction.

Definition at line 77 of file tilearea_spiral.h.

Referenced by Increment(), InitPosition(), SpiralTileIterator(), and SpiralTileIterator().

◆ extent

DiagDirectionIndexArray<uint> SpiralTileIterator::extent
private

Size of hole in the centre of the spiral.

Definition at line 73 of file tilearea_spiral.h.

Referenced by InitPosition(), SpiralTileIterator(), and SpiralTileIterator().

◆ max_radius

const uint SpiralTileIterator::max_radius
private

The maximum radius of the spiral.

Definition at line 72 of file tilearea_spiral.h.

Referenced by SpiralTileIterator(), and SpiralTileIterator().

◆ position

uint SpiralTileIterator::position
private

The current position along an edge.

Definition at line 78 of file tilearea_spiral.h.

Referenced by Increment(), InitPosition(), and SpiralTileIterator().

◆ x

uint SpiralTileIterator::x
private

The current tile X-coordinate.

Definition at line 79 of file tilearea_spiral.h.

Referenced by Increment(), operator*(), operator==(), SkipOutsideMap(), SpiralTileIterator(), and SpiralTileIterator().

◆ y

uint SpiralTileIterator::y
private

The current tile Y-coordinate.

Definition at line 80 of file tilearea_spiral.h.

Referenced by Increment(), operator*(), operator==(), SkipOutsideMap(), SpiralTileIterator(), and SpiralTileIterator().


The documentation for this class was generated from the following files: