Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. Set::Set()
  2. {
  3. for (int i = 0; i < size; i++)
  4. {
  5. data[i] = 0;
  6. }
  7. }
  8.  
  9. size_t Set::getPosition(char symbol)
  10. {
  11. size_t position;
  12. position = (symbol - 'a') % (sizeof(char)*8) ;
  13. return position ;
  14. }
  15.  
  16. size_t Set::getArrayIndex(char symbol)
  17. {
  18. size_t arrIndex;
  19. arrIndex = (symbol - 'a') / (sizeof(char) * 8) ;
  20. return arrIndex ;
  21. }
  22.  
  23. bool Set::isInSet(char symbol)
  24. {
  25. size_t index;
  26. index = getArrayIndex(symbol);
  27. size_t position;
  28. position = getPosition(symbol);
  29.  
  30. bool result;
  31. result = false;
  32.  
  33. if (data[index] & (0x1 << position))
  34. {
  35. result = true;
  36. }
  37. return result;
  38. }
  39.  
  40. void Set::add(char symbol)
  41. {
  42. size_t index;
  43. index = getArrayIndex(symbol);
  44. size_t position;
  45. position = getPosition(symbol);
  46. data[index] |= 0x1 << position;
  47. }
  48.  
  49. void Set::remove(char symbol)
  50. {
  51. size_t index;
  52. index = getArrayIndex(symbol);
  53. size_t position;
  54. position = getPosition(symbol);
  55.  
  56. if (!(data[index] & (0x1 << position)))
  57. {
  58. throw "this element does not exits at this set \n";
  59. }
  60.  
  61. data[index] &= ~(0x1 << position);
  62. }
  63.  
  64. bool Set::isEmpty()
  65. {
  66. for (size_t i = 0; i < this->size; i++)
  67. {
  68. if (data[i] != 0)
  69. {
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75.  
  76. Set Set::combineSets(const Set& set)
  77. {
  78. Set newSet;
  79. for (size_t i = 0; i < size; i++)
  80. {
  81. newSet.data[i] = this->data[i] | set.data[i];
  82. }
  83. return newSet;
  84. }
  85.  
  86. Set Set::intersectionOfSets(const Set& set)
  87. {
  88. Set newSet;
  89. for (size_t i = 0; i < size; i++)
  90. {
  91. newSet.data[i] = this->data[i] & set.data[i];
  92. }
  93. return newSet;
  94. }
  95.  
  96. Set Set::subtractionOfSets(const Set& set)
  97. {
  98. Set newSet;
  99. newSet = (*this);
  100.  
  101. for (size_t i = 0; i < size; i++)
  102. {
  103. newSet.data[i] &= ~set.data[i];
  104. }
  105. return newSet;
  106. }
  107.  
  108. char Set::getMinElement()
  109. {
  110. int index;
  111. index = -1;
  112. int partOfSet;
  113. partOfSet = 0;
  114. bool isFound;
  115. isFound = false;
  116. for (size_t i = 0; i < size; i++)
  117. {
  118. size_t pos = 0;
  119. while (pos < 8)
  120. {
  121. if (data[i] & (0x1 << pos))
  122. {
  123. index = pos;
  124. partOfSet = i;
  125. isFound = true;
  126. break;
  127. }
  128. pos++;
  129. }
  130. if (isFound)
  131. {
  132. break;
  133. }
  134. }
  135. if (index == -1)
  136. {
  137. throw "min element not found ";
  138. }
  139. return char (index+ 8*partOfSet +'a');
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement