Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include "Matrix.h"
  2.  
  3.  
  4.  
  5. void Matrix::rehash() //Theta(capacity)
  6. {
  7. MatrixElement* newHashTable = new MatrixElement[capacity * 2];
  8. capacity *= 2;
  9. for (int i = 0; i < capacity/2; i++)
  10. {
  11. if (hashTable[i].line != -1)
  12. {
  13. int hk = hash(hashTable[i].line, hashTable[i].column);
  14. int x = 0;
  15. while (newHashTable[(hk + probing(x))%capacity].line != -1)
  16. x++;
  17. newHashTable[(hk + probing(x)) % capacity].line = hashTable[i].line;
  18. newHashTable[(hk + probing(x)) % capacity].column = hashTable[i].column;
  19. newHashTable[(hk + probing(x)) % capacity].value = hashTable[i].value;
  20. }
  21. }
  22. delete[] hashTable;
  23. hashTable = newHashTable;
  24. }
  25.  
  26. Matrix::Matrix(int nrLines, int nrCols) //Theta(1)
  27. {
  28. if (nrLines < 1 || nrCols < 1)
  29. throw std::exception();
  30. this->nrX = nrLines;
  31. this->nrY = nrCols;
  32. this->hashTable = new MatrixElement[8];
  33. this->size = 0;
  34. this->capacity = 8;
  35. }
  36.  
  37. int Matrix::nrLines() const //Theta(1)
  38. {
  39. return this->nrX;
  40. }
  41.  
  42. int Matrix::nrColumns() const //Theta(1)
  43. {
  44. return this->nrY;
  45. }
  46.  
  47. TElem Matrix::element(int i, int j) const //Theta(1)
  48. {
  49. if (i < 0 || j < 0 || i > nrX || j > nrY)
  50.  
  51. throw std::exception();
  52. int hk = hash(i, j);
  53. int x = 0;
  54. while (hashTable[(hk + probing(x))%capacity].line != -1)
  55. {
  56. if (hashTable[(hk + probing(x)) % capacity].line == i && hashTable[(hk + probing(x)) % capacity].column == j)
  57. return hashTable[(hk + probing(x)) % capacity].value;
  58. x++;
  59. }
  60. return NULL_TELEM;
  61. }
  62.  
  63.  
  64. TElem Matrix::modify(int i, int j, TElem e)//Theta(1)
  65. {
  66. if (i < 0 || j < 0 || i > nrX || j > nrY)
  67. throw std::exception();
  68. if (size == (int)(capacity*alpha))
  69. rehash();
  70. int hk = hash(i, j);
  71. int x = 0;
  72.  
  73. while (hashTable[(hk + probing(x)) % capacity].line != -1)
  74. {
  75. if (hashTable[(hk + probing(x)) % capacity].line == i && hashTable[(hk + probing(x)) % capacity].column == j)
  76. {
  77. TElem val = hashTable[(hk + probing(x)) % capacity].value;
  78. hashTable[(hk + probing(x)) % capacity].value = e;
  79. if (e == NULL_TELEM && val != NULL_TELEM)
  80. size--;
  81. size++;
  82. return val;
  83. }
  84. x++;
  85. }
  86. hashTable[(hk + probing(x)) % capacity].line = i;
  87. hashTable[(hk + probing(x)) % capacity].column = j;
  88. hashTable[(hk + probing(x)) % capacity].value = e;
  89. size++;
  90. return NULL_TELEM;
  91. }
  92.  
  93.  
  94. int Matrix::hash(int i, int j) const //Theta(1)
  95. {
  96. return i + j;
  97.  
  98. }
  99.  
  100. int Matrix::probing(int x) const //Theta(1)
  101. {
  102. return x * (x + 1) / 2;
  103. }
  104.  
  105. Matrix::~Matrix()
  106. {
  107. delete[] hashTable;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement