tesseract  4.0.0-1-g2a2b
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 46 of file stridemap.h.

Constructor & Destructor Documentation

◆ Index() [1/2]

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

Definition at line 48 of file stridemap.h.

48  : stride_map_(&stride_map) {
49  InitToFirst();
50  }

◆ Index() [2/2]

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

Definition at line 51 of file stridemap.h.

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

Member Function Documentation

◆ AddOffset()

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

Definition at line 63 of file stridemap.cpp.

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

◆ Decrement()

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

Definition at line 88 of file stridemap.cpp.

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

◆ Increment()

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

Definition at line 71 of file stridemap.cpp.

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

◆ index()

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

Definition at line 60 of file stridemap.h.

60 { return indices_[dimension]; }

◆ InitToFirst()

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

Definition at line 62 of file stridemap.h.

62  {
63  memset(indices_, 0, sizeof(indices_));
64  t_ = 0;
65  }

◆ InitToLast()

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

Definition at line 67 of file stridemap.h.

67 { InitToLastOfBatch(MaxIndexOfDim(FD_BATCH)); }
int MaxIndexOfDim(FlexDimensions dim) const
Definition: stridemap.cpp:44

◆ IsLast()

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

Definition at line 38 of file stridemap.cpp.

38  {
39  return MaxIndexOfDim(dimension) == indices_[dimension];
40 }
int MaxIndexOfDim(FlexDimensions dim) const
Definition: stridemap.cpp:44

◆ IsValid()

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

Definition at line 25 of file stridemap.cpp.

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

◆ MaxIndexOfDim()

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

Definition at line 44 of file stridemap.cpp.

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

◆ t()

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

Definition at line 59 of file stridemap.h.

59 { return t_; }

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