bitmap.h
#ifndef _BITMAP_H_
#define _BITMAP_H_
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <limits.h>
#include <cstring>
using namespace std;
class BitMap {
public:
BitMap(uint64_t size);
~BitMap();
uint64_t getSize() { return size; }
bool getBit(uint64_t pos);
void setBit(uint64_t pos);
void resetBit(uint64_t pos);
private:
uint64_t size;
char* pointer;
};
#endif
bitmap.cpp
#include "bitmap.h"
BitMap::BitMap(uint64_t size_) {
if (size < UINT64_MAX) {
size = size_;
uint64_t byteNum = size / 8 + 1;
pointer = (char*)malloc(sizeof(char) * byteNum);
memset(pointer, 0, byteNum);
} else {
cout << "Error! Over the max size.\n";
exit(-1);
}
}
BitMap::~BitMap(void) {
if (pointer) {
free(pointer);
}
}
bool BitMap::getBit(uint64_t pos) {
uint64_t index = pos / 8;
int offset = pos % 8;
return (*(pointer + index) & (1 << offset));
}
void BitMap::setBit(uint64_t pos) {
uint64_t numberOfchar = pos / 8;
int restOfchar = pos % 8;
*(pointer + numberOfchar) |= 1 << restOfchar;
}
void BitMap::resetBit(uint64_t pos) {
uint64_t index = pos / 8;
int offset = pos % 8;
char tmp = *(pointer + index);
*(pointer + index) &= ~(1 << offset);
}
排序
void sortByBitMap(vector<uint64_t>& nums) {
uint64_t size = nums.size();
BitMap bm = BitMap(size);
for (uint64_t x : nums) {
bm.setBit(x);
}
uint64_t index = 0;
for (uint64_t i = 1; i <= bm.getSize(); i++) {
if (bm.getBit(i))
nums[index++] = i;
}
}