htable.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "htable.h"
  2. #include "hdef.h"
  3. HTable::HTable(){
  4. row = col = 0;
  5. }
  6. void HTable::init(int row, int col){
  7. this->row = row;
  8. this->col = col;
  9. m_mapCells.clear();
  10. for (int r = 1; r <= row; ++r){
  11. for (int c = 1; c <= col; ++c){
  12. int id = (r-1) * col + c;
  13. m_mapCells[id] = HTableCell(r,r,c,c);
  14. }
  15. }
  16. }
  17. bool HTable::getTableCell(int id, HTableCell& rst){
  18. if (m_mapCells.find(id) != m_mapCells.end()){
  19. rst = m_mapCells[id];
  20. return true;
  21. }
  22. return false;
  23. }
  24. HTableCell HTable::merge(int lt, int rb){
  25. HTableCell cell_lt,cell_rb;
  26. if (getTableCell(lt, cell_lt) && getTableCell(rb, cell_rb)){
  27. int r1 = MIN(cell_lt.r1, cell_rb.r1);
  28. int r2 = MAX(cell_lt.r2, cell_rb.r2);
  29. int c1 = MIN(cell_lt.c1, cell_rb.c1);
  30. int c2 = MAX(cell_lt.c2, cell_rb.c2);
  31. HTableCell cell(r1, r2, c1, c2);
  32. std::map<int, HTableCell>::iterator iter = m_mapCells.begin();
  33. while (iter != m_mapCells.end()){
  34. if (cell.contain(iter->second)){
  35. iter = m_mapCells.erase(iter);
  36. }else
  37. ++iter;
  38. }
  39. m_mapCells[lt] = cell;
  40. return cell;
  41. }
  42. return HTableCell(0,0,0,0);
  43. }