Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by mskf on 6/7/2020.
- //
- #include <cstddef>
- #include <climits>
- #include <exception>
- #include <stdexcept>
- #include <iostream>
- #include <algorithm>
- #include "GeorgeSet.h"
- #include <cmath>
- using namespace std;
- namespace AdvCpp {
- GeorgeSet::GeorgeSet( size_t max ):max{max}{
- fill(bits,bits + BITS_LENGTH, 0);
- }
- GeorgeSet::GeorgeSet( const GeorgeSet &other):
- max{other.max}, length{0} {
- copy(begin(bits), end(bits), begin(other.bits));
- }
- GeorgeSet::GeorgeSet( GeorgeSet &&other) {
- *this = other;
- }
- bool GeorgeSet::contains(size_t e) const{
- return (bits[(int)floor(e/64)]>>(e%64)) & 1;
- }
- size_t GeorgeSet::size() const noexcept{
- return length;
- }
- bool GeorgeSet::operator==(const GeorgeSet &other) const noexcept {
- for(int i=0;i<min(max, other.max);i++){
- if (bits[i]!=other.bits[i])
- return false;
- }
- return true;
- }
- void GeorgeSet::add(size_t e) {
- length ++;
- bits[(int)floor(e/64)] |= 1<<(e%64);
- }
- void GeorgeSet::remove( size_t e){
- bits[(int)floor(e/64)] &= ~(1<<(e%64));
- }
- void GeorgeSet::resize(size_t newMax) {
- size_t curMax = 0;
- for(int i = BITS_LENGTH-1;i>=0;i--){
- if(bits[i]>0){
- int curVal = 0;
- for(int j = 0;j<LONG_SIZE_BITS;j++){
- bits[i]>>=1;
- if (bits[i]==0){
- curVal = j;
- break;
- }
- }
- curMax = i*LONG_SIZE_BITS + bits[i]
- break;
- }
- }
- if(newMax>BITS_LENGTH || newMax<(int)ceil(length/BITS_LENGTH)){
- throw OutOfRange();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement