CellRange.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. class CCellID
  3. {
  4. // Attributes
  5. public:
  6. int row, col;
  7. // Operations
  8. public:
  9. explicit CCellID(int nRow = -1, int nCol = -1) : row(nRow), col(nCol) {}
  10. int IsValid() const { return (row >= 0 && col >= 0); }
  11. int operator==(const CCellID& rhs) const { return (row == rhs.row && col == rhs.col); }
  12. int operator!=(const CCellID& rhs) const { return !operator==(rhs); }
  13. };
  14. class CCellRange
  15. {
  16. public:
  17. CCellRange(int nMinRow = -1, int nMinCol = -1, int nMaxRow = -1, int nMaxCol = -1)
  18. {
  19. Set(nMinRow, nMinCol, nMaxRow, nMaxCol);
  20. }
  21. void Set(int nMinRow = -1, int nMinCol = -1, int nMaxRow = -1, int nMaxCol = -1);
  22. int IsValid() const;
  23. int InRange(int row, int col) const;
  24. int InRange(const CCellID& cellID) const;
  25. int Count() { return (m_nMaxRow - m_nMinRow + 1) * (m_nMaxCol - m_nMinCol + 1); }
  26. CCellID GetTopLeft() const;
  27. CCellRange Intersect(const CCellRange& rhs) const;
  28. int GetMinRow() const { return m_nMinRow; }
  29. void SetMinRow(int minRow) { m_nMinRow = minRow; }
  30. int GetMinCol() const { return m_nMinCol; }
  31. void SetMinCol(int minCol) { m_nMinCol = minCol; }
  32. int GetMaxRow() const { return m_nMaxRow; }
  33. void SetMaxRow(int maxRow) { m_nMaxRow = maxRow; }
  34. int GetMaxCol() const { return m_nMaxCol; }
  35. void SetMaxCol(int maxCol) { m_nMaxCol = maxCol; }
  36. int GetRowSpan() const { return m_nMaxRow - m_nMinRow + 1; }
  37. int GetColSpan() const { return m_nMaxCol - m_nMinCol + 1; }
  38. void operator=(const CCellRange& rhs);
  39. int operator==(const CCellRange& rhs);
  40. int operator!=(const CCellRange& rhs);
  41. protected:
  42. int m_nMinRow;
  43. int m_nMinCol;
  44. int m_nMaxRow;
  45. int m_nMaxCol;
  46. };
  47. inline void CCellRange::Set(int minRow, int minCol, int maxRow, int maxCol)
  48. {
  49. m_nMinRow = minRow;
  50. m_nMinCol = minCol;
  51. m_nMaxRow = maxRow;
  52. m_nMaxCol = maxCol;
  53. }
  54. inline void CCellRange::operator=(const CCellRange& rhs)
  55. {
  56. if (this != &rhs) Set(rhs.m_nMinRow, rhs.m_nMinCol, rhs.m_nMaxRow, rhs.m_nMaxCol);
  57. }
  58. inline int CCellRange::operator==(const CCellRange& rhs)
  59. {
  60. return ((m_nMinRow == rhs.m_nMinRow) && (m_nMinCol == rhs.m_nMinCol) &&
  61. (m_nMaxRow == rhs.m_nMaxRow) && (m_nMaxCol == rhs.m_nMaxCol));
  62. }
  63. inline int CCellRange::operator!=(const CCellRange& rhs)
  64. {
  65. return !operator==(rhs);
  66. }
  67. inline int CCellRange::IsValid() const
  68. {
  69. return (m_nMinRow >= 0 && m_nMinCol >= 0 && m_nMaxRow >= 0 && m_nMaxCol >= 0 &&
  70. m_nMinRow <= m_nMaxRow && m_nMinCol <= m_nMaxCol);
  71. }
  72. inline int CCellRange::InRange(int row, int col) const
  73. {
  74. return (row >= m_nMinRow && row <= m_nMaxRow && col >= m_nMinCol && col <= m_nMaxCol);
  75. }
  76. inline int CCellRange::InRange(const CCellID& cellID) const
  77. {
  78. return InRange(cellID.row, cellID.col);
  79. }
  80. inline CCellID CCellRange::GetTopLeft() const
  81. {
  82. return CCellID(m_nMinRow, m_nMinCol);
  83. }
  84. inline CCellRange CCellRange::Intersect(const CCellRange& rhs) const
  85. {
  86. return CCellRange(max(m_nMinRow, rhs.m_nMinRow), max(m_nMinCol, rhs.m_nMinCol),
  87. min(m_nMaxRow, rhs.m_nMaxRow), min(m_nMaxCol, rhs.m_nMaxCol));
  88. }