OpenTTD Source 20260311-master-g511d3794ce
SpiralTileIterator Class Reference

Helper class for SpiralTileSequence. More...

#include <tilearea_type.h>

Public Types

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

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
bool operator== (const std::default_sentinel_t &) const
TileIndex operator* () const
SpiralTileIteratoroperator++ ()
SpiralTileIterator operator++ (int)

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

uint max_radius
std::array< uint, DIAGDIR_ENDextent
uint cur_radius
DiagDirection dir
uint position
uint x
uint y

Detailed Description

Helper class for SpiralTileSequence.

Definition at line 267 of file tilearea_type.h.

Member Typedef Documentation

◆ difference_type

using SpiralTileIterator::difference_type = std::ptrdiff_t

Definition at line 270 of file tilearea_type.h.

◆ iterator_category

using SpiralTileIterator::iterator_category = std::forward_iterator_tag

Definition at line 271 of file tilearea_type.h.

◆ pointer

using SpiralTileIterator::pointer = void

Definition at line 272 of file tilearea_type.h.

◆ reference

using SpiralTileIterator::reference = void

Definition at line 273 of file tilearea_type.h.

◆ value_type

using SpiralTileIterator::value_type = TileIndex

Definition at line 269 of file tilearea_type.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 301 of file tilearea.cpp.

References DIAGDIR_BEGIN, InitPosition(), INVALID_DIAGDIR, SkipOutsideMap(), TileX(), and TileY().

◆ 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 328 of file tilearea.cpp.

References DIAGDIR_BEGIN, InitPosition(), SkipOutsideMap(), TileX(), and TileY().

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 362 of file tilearea.cpp.

References DIAGDIR_BEGIN, DIAGDIR_END, DIR_W, InitPosition(), INVALID_DIAGDIR, IsEnd(), TileIndexDiffCByDiagDir(), and TileIndexDiffCByDir().

Referenced by SkipOutsideMap().

◆ InitPosition()

void SpiralTileIterator::InitPosition ( )
private

Initialise "position" after "dir" was changed.

Definition at line 353 of file tilearea.cpp.

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 316 of file tilearea_type.h.

References INVALID_DIAGDIR.

Referenced by Increment(), and SkipOutsideMap().

◆ operator*()

TileIndex SpiralTileIterator::operator* ( ) const
inline

Definition at line 281 of file tilearea_type.h.

◆ operator++() [1/2]

SpiralTileIterator & SpiralTileIterator::operator++ ( )
inline

Definition at line 283 of file tilearea_type.h.

◆ operator++() [2/2]

SpiralTileIterator SpiralTileIterator::operator++ ( int )
inline

Definition at line 290 of file tilearea_type.h.

◆ operator==() [1/2]

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

Definition at line 278 of file tilearea_type.h.

◆ operator==() [2/2]

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

Definition at line 279 of file tilearea_type.h.

◆ SkipOutsideMap()

void SpiralTileIterator::SkipOutsideMap ( )
private

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

Definition at line 345 of file tilearea.cpp.

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

Referenced by SpiralTileIterator(), and SpiralTileIterator().

Field Documentation

◆ cur_radius

uint SpiralTileIterator::cur_radius
private

Definition at line 303 of file tilearea_type.h.

◆ dir

DiagDirection SpiralTileIterator::dir
private

Definition at line 304 of file tilearea_type.h.

◆ extent

std::array<uint, DIAGDIR_END> SpiralTileIterator::extent
private

Definition at line 300 of file tilearea_type.h.

◆ max_radius

uint SpiralTileIterator::max_radius
private

Definition at line 299 of file tilearea_type.h.

◆ position

uint SpiralTileIterator::position
private

Definition at line 305 of file tilearea_type.h.

◆ x

uint SpiralTileIterator::x
private

Definition at line 306 of file tilearea_type.h.

◆ y

uint SpiralTileIterator::y
private

Definition at line 306 of file tilearea_type.h.


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