Advertisement
mskf

geocpp

Jun 7th, 2020
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. //
  2. // Created by mskf on 6/7/2020.
  3. //
  4. #include <cstddef>
  5. #include <climits>
  6. #include <exception>
  7. #include <stdexcept>
  8. #include <iostream>
  9. #include <algorithm>
  10. #include "GeorgeSet.h"
  11. #include <cmath>
  12.  
  13. using namespace std;
  14.  
  15. namespace AdvCpp {
  16. GeorgeSet::GeorgeSet( size_t max ):max{max}{
  17. fill(bits,bits + BITS_LENGTH, 0);
  18. }
  19.  
  20. GeorgeSet::GeorgeSet( const GeorgeSet &other):
  21. max{other.max}, length{0} {
  22. copy(begin(bits), end(bits), begin(other.bits));
  23. }
  24.  
  25. GeorgeSet::GeorgeSet( GeorgeSet &&other) {
  26. *this = other;
  27. }
  28.  
  29. bool GeorgeSet::contains(size_t e) const{
  30. return (bits[(int)floor(e/64)]>>(e%64)) & 1;
  31. }
  32.  
  33. size_t GeorgeSet::size() const noexcept{
  34. return length;
  35. }
  36.  
  37. bool GeorgeSet::operator==(const GeorgeSet &other) const noexcept {
  38. for(int i=0;i<min(max, other.max);i++){
  39. if (bits[i]!=other.bits[i])
  40. return false;
  41. }
  42.  
  43. return true;
  44. }
  45.  
  46. void GeorgeSet::add(size_t e) {
  47. length ++;
  48. bits[(int)floor(e/64)] |= 1<<(e%64);
  49. }
  50.  
  51. void GeorgeSet::remove( size_t e){
  52. bits[(int)floor(e/64)] &= ~(1<<(e%64));
  53. }
  54.  
  55. void GeorgeSet::resize(size_t newMax) {
  56. size_t curMax = 0;
  57. for(int i = BITS_LENGTH-1;i>=0;i--){
  58. if(bits[i]>0){
  59. int curVal = 0;
  60.  
  61. for(int j = 0;j<LONG_SIZE_BITS;j++){
  62. bits[i]>>=1;
  63. if (bits[i]==0){
  64. curVal = j;
  65. break;
  66. }
  67. }
  68.  
  69. curMax = i*LONG_SIZE_BITS + bits[i]
  70. break;
  71. }
  72. }
  73. if(newMax>BITS_LENGTH || newMax<(int)ceil(length/BITS_LENGTH)){
  74. throw OutOfRange();
  75. }
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement