tesseract  5.0.0-alpha-619-ge9db
tesseract::StrideMap::Index Class Reference

#include <stridemap.h>

Public Member Functions

 Index (const StrideMap &stride_map)
 
 Index (const StrideMap &stride_map, int batch, int y, int x)
 
int t () const
 
int index (FlexDimensions dimension) const
 
void InitToFirst ()
 
void InitToLast ()
 
bool IsValid () const
 
bool IsLast (FlexDimensions dimension) const
 
int MaxIndexOfDim (FlexDimensions dim) const
 
bool AddOffset (int offset, FlexDimensions dimension)
 
bool Increment ()
 
bool Decrement ()
 

Detailed Description

Definition at line 44 of file stridemap.h.

Constructor & Destructor Documentation

◆ Index() [1/2]

tesseract::StrideMap::Index::Index ( const StrideMap stride_map)
inlineexplicit

Definition at line 46 of file stridemap.h.

46  : stride_map_(&stride_map) {
47  InitToFirst();
48  }

◆ Index() [2/2]

tesseract::StrideMap::Index::Index ( const StrideMap stride_map,
int  batch,
int  y,
int  x 
)
inline

Definition at line 49 of file stridemap.h.

50  : stride_map_(&stride_map) {
51  indices_[FD_BATCH] = batch;
52  indices_[FD_HEIGHT] = y;
53  indices_[FD_WIDTH] = x;
54  SetTFromIndices();
55  }

Member Function Documentation

◆ AddOffset()

bool tesseract::StrideMap::Index::AddOffset ( int  offset,
FlexDimensions  dimension 
)

Definition at line 62 of file stridemap.cpp.

62  {
63  indices_[dimension] += offset;
64  SetTFromIndices();
65  return IsValid();
66 }

◆ Decrement()

bool tesseract::StrideMap::Index::Decrement ( )

Definition at line 87 of file stridemap.cpp.

87  {
88  for (int d = FD_DIMSIZE - 1; d >= 0; --d) {
89  if (indices_[d] > 0) {
90  --indices_[d];
91  if (d == FD_BATCH) {
92  // The upper limits of the other dimensions may have changed as a result
93  // of a different batch index, so they have to be reset.
94  InitToLastOfBatch(indices_[FD_BATCH]);
95  } else {
96  t_ -= stride_map_->t_increments_[d];
97  }
98  return true;
99  }
100  indices_[d] = MaxIndexOfDim(static_cast<FlexDimensions>(d));
101  t_ += stride_map_->t_increments_[d] * indices_[d];
102  // Now borrow from the next dimension.
103  }
104  return false;
105 }

◆ Increment()

bool tesseract::StrideMap::Index::Increment ( )

Definition at line 70 of file stridemap.cpp.

70  {
71  for (int d = FD_DIMSIZE - 1; d >= 0; --d) {
72  if (!IsLast(static_cast<FlexDimensions>(d))) {
73  t_ += stride_map_->t_increments_[d];
74  ++indices_[d];
75  return true;
76  }
77  t_ -= stride_map_->t_increments_[d] * indices_[d];
78  indices_[d] = 0;
79  // Now carry to the next dimension.
80  }
81  return false;
82 }

◆ index()

int tesseract::StrideMap::Index::index ( FlexDimensions  dimension) const
inline

Definition at line 58 of file stridemap.h.

58 { return indices_[dimension]; }

◆ InitToFirst()

void tesseract::StrideMap::Index::InitToFirst ( )
inline

Definition at line 60 of file stridemap.h.

60  {
61  memset(indices_, 0, sizeof(indices_));
62  t_ = 0;
63  }

◆ InitToLast()

void tesseract::StrideMap::Index::InitToLast ( )
inline

Definition at line 65 of file stridemap.h.

65 { InitToLastOfBatch(MaxIndexOfDim(FD_BATCH)); }

◆ IsLast()

bool tesseract::StrideMap::Index::IsLast ( FlexDimensions  dimension) const

Definition at line 37 of file stridemap.cpp.

37  {
38  return MaxIndexOfDim(dimension) == indices_[dimension];
39 }

◆ IsValid()

bool tesseract::StrideMap::Index::IsValid ( ) const

Definition at line 24 of file stridemap.cpp.

24  {
25  // Cheap check first.
26  for (int index : indices_) {
27  if (index < 0) return false;
28  }
29  for (int d = 0; d < FD_DIMSIZE; ++d) {
30  if (indices_[d] > MaxIndexOfDim(static_cast<FlexDimensions>(d)))
31  return false;
32  }
33  return true;
34 }

◆ MaxIndexOfDim()

int tesseract::StrideMap::Index::MaxIndexOfDim ( FlexDimensions  dim) const

Definition at line 43 of file stridemap.cpp.

43  {
44  int max_index = stride_map_->shape_[dim] - 1;
45  if (dim == FD_BATCH) return max_index;
46  assert(0 <= indices_[FD_BATCH]);
47  const size_t batch = indices_[FD_BATCH];
48  if (dim == FD_HEIGHT) {
49  if (batch >= stride_map_->heights_.size() ||
50  stride_map_->heights_[batch] > max_index)
51  return max_index;
52  return stride_map_->heights_[batch] - 1;
53  }
54  if (batch >= stride_map_->widths_.size() ||
55  stride_map_->widths_[batch] > max_index)
56  return max_index;
57  return stride_map_->widths_[batch] - 1;
58 }

◆ t()

int tesseract::StrideMap::Index::t ( ) const
inline

Definition at line 57 of file stridemap.h.

57 { return t_; }

The documentation for this class was generated from the following files:
tesseract::StrideMap::Index::IsLast
bool IsLast(FlexDimensions dimension) const
Definition: stridemap.cpp:37
tesseract::StrideMap::Index::index
int index(FlexDimensions dimension) const
Definition: stridemap.h:58
tesseract::FD_WIDTH
Definition: stridemap.h:35
tesseract::StrideMap::Index::MaxIndexOfDim
int MaxIndexOfDim(FlexDimensions dim) const
Definition: stridemap.cpp:43
tesseract::StrideMap::Index::InitToFirst
void InitToFirst()
Definition: stridemap.h:60
tesseract::FD_DIMSIZE
Definition: stridemap.h:36
tesseract::FD_HEIGHT
Definition: stridemap.h:34
tesseract::StrideMap::Index::IsValid
bool IsValid() const
Definition: stridemap.cpp:24
tesseract::FD_BATCH
Definition: stridemap.h:33