Created
February 24, 2016 19:35
-
-
Save v3c70r/8058db357af318fc64c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright (C) | |
* 2016 - Tsing Gu | |
* This program is free software; you can redistribute it and/or | |
* modify it under the terms of the GNU General Public License | |
* as published by the Free Software Foundation; either version 2 | |
* of the License, or (at your option) any later version. | |
* | |
* This program is distributed in the hope that it will be useful, | |
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
* GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with this program; if not, write to the Free Software | |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
* | |
*/ | |
#pragma once | |
#include <iostream> | |
#include <vector> | |
#include <bitset> | |
using std::ostream; | |
class Dynamic_Bitset{ | |
private: | |
std::vector<unsigned char> bits; | |
const size_t BITS_PER_BYTE; | |
/* Set or clear a bit in uchar | |
* position should be within BIT_PER_BYTE | |
* http://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c-c | |
*/ | |
/** | |
* @brief Set bit of a char to 1 | |
* | |
* @param ch Char to operate | |
* @param pos Position within the char | |
*/ | |
void setUChar(unsigned char& ch, const size_t &pos) { ch |= 1<<pos; } | |
/** | |
* @brief Set bit of a char to 0 | |
* | |
* @param ch Char to operate | |
* @param pos Position within the char | |
*/ | |
void clearUChar(unsigned char& ch, const size_t &pos) { ch &= ~(1<<pos);} | |
/** | |
* @brief Get bit of within a char | |
* | |
* @param ch Char to query | |
* @param pos position of bit in char | |
* | |
* @return Ture if 1; otherwise, 0. | |
*/ | |
bool getUChar(const unsigned char& ch, const size_t &pos)const {return (ch>>pos)&1;} | |
public: | |
/** | |
* @brief Init an empty bitset | |
*/ | |
Dynamic_Bitset(): BITS_PER_BYTE{8}{} | |
/** | |
* @brief Init an empty bitset with given length | |
* | |
* @param sz number of bits | |
*/ | |
Dynamic_Bitset(const size_t &sz):BITS_PER_BYTE{8}{resize(sz);} | |
/** | |
* @brief Get number of bits | |
* | |
* @return number of bits | |
*/ | |
size_t size() const {return bits.size()*BITS_PER_BYTE;} | |
/** | |
* @brief Resize bit array length, will set bit array to 0 | |
* | |
* @param sz number of bits | |
*/ | |
void resize(const size_t &sz) {bits.resize((sz+BITS_PER_BYTE-1)/ BITS_PER_BYTE, 0);} | |
/** | |
* @brief Set a bit to 1 | |
* | |
* @param pos 0-based position of bit | |
*/ | |
void setBit(const size_t &pos){ //Set the bit to one | |
setUChar(bits[pos/BITS_PER_BYTE], pos%BITS_PER_BYTE); | |
} | |
/** | |
* @brief Set a bit to 0 | |
* | |
* @param pos 0-based position of bit | |
*/ | |
void clearBit(const size_t &pos){ //Set the bit to zero | |
clearUChar(bits[pos/BITS_PER_BYTE], pos%BITS_PER_BYTE); | |
} | |
/** | |
* @brief Get the value of bit | |
* | |
* @param pos 0-based position of bit | |
* | |
* @return Ture if the bit is 1; otherwise, false. | |
*/ | |
bool getBit(const size_t &pos) const | |
{ | |
return getUChar(bits[pos/BITS_PER_BYTE], pos%BITS_PER_BYTE); | |
} | |
//Friend operators | |
friend std::ostream& operator<<(std::ostream& os, const Dynamic_Bitset& db); | |
}; | |
std::ostream& operator<<(std::ostream& os, const Dynamic_Bitset& db) | |
{ | |
//for (size_t i=db.bits.size()-1; i >=0; i--) | |
for (std::vector<unsigned char>::const_reverse_iterator rit = db.bits.rbegin(); | |
rit != db.bits.rend(); rit++) | |
os << (std::bitset<8>)(*rit); | |
return os; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment