//=========================================================================== // @(#) $Name: arts++-1-1-a12 $ // @(#) $Id: ArtsBitString.cc,v 1.2 2004/04/21 23:51:32 kkeys Exp $ //=========================================================================== // Copyright Notice // // By accessing this software, arts++, you are duly informed // of and agree to be bound by the conditions described below in this // notice: // // This software product, arts++, is developed by Daniel W. McRobb, and // copyrighted(C) 1998 by the University of California, San Diego // (UCSD), with all rights reserved. UCSD administers the CAIDA grant, // NCR-9711092, under which part of this code was developed. // // There is no charge for arts++ software. You can redistribute it // and/or modify it under the terms of the GNU Lesser General Public // License, Version 2.1, February 1999, which is incorporated by // reference herein. // // arts++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use // of it will not infringe on any third party's intellectual // property rights. // // You should have received a copy of the GNU Lesser General Public // License along with arts++. Copies can also be obtained from: // // http://www.gnu.org/copyleft/lesser.html // // or by writing to: // // Free Software Foundation, Inc. // 59 Temple Place, Suite 330 // Boston, MA 02111-1307 // USA // // Or contact: // // info@caida.org //=========================================================================== extern "C" { #include #include } #include #include "ArtsBitString.hh" using namespace std; static const std::string rcsid = "@(#) $Name: arts++-1-1-a12 $ $Id: ArtsBitString.cc,v 1.2 2004/04/21 23:51:32 kkeys Exp $"; //------------------------------------------------------------------------- // ArtsBitString::ArtsBitString(uint32_t numBits) //......................................................................... // //------------------------------------------------------------------------- ArtsBitString::ArtsBitString(uint32_t numBits) { this->_numBytes = ((numBits - 1) >> 3) + 1; this->_bits = (uint8_t *)malloc(this->_numBytes * sizeof(uint8_t)); memset(this->_bits,0,this->_numBytes * sizeof(uint8_t)); assert(this->_bits); this->_numBits = numBits; } //------------------------------------------------------------------------- // ArtsBitString::~ArtsBitString() //......................................................................... // //------------------------------------------------------------------------- ArtsBitString::~ArtsBitString() { if (this->_bits != (uint8_t *)0) { free(this->_bits); this->_bits = (uint8_t *)0; } } //------------------------------------------------------------------------- // bool ArtsBitString::Test(uint32_t position) const //......................................................................... // //------------------------------------------------------------------------- bool ArtsBitString::Test(uint32_t position) const { assert(position < this->_numBits); if ((this->_bits[this->_BitByte(position)] & this->_BitMask(position)) > 0) { return(true); } return(false); } //------------------------------------------------------------------------- // void ArtsBitString::Set(uint32_t position) //......................................................................... // //------------------------------------------------------------------------- void ArtsBitString::Set(uint32_t position) { assert(position < this->_numBits); this->_bits[this->_BitByte(position)] |= this->_BitMask(position); return; } //------------------------------------------------------------------------- // void ArtsBitString::Unset(uint32_t position) //......................................................................... // //------------------------------------------------------------------------- void ArtsBitString::Unset(uint32_t position) { assert(position < this->_numBits); this->_bits[this->_BitByte(position)] &= ~(this->_BitMask(position)); return; } //------------------------------------------------------------------------- // void ArtsBitString::Toggle(uint32_t position) //......................................................................... // //------------------------------------------------------------------------- void ArtsBitString::Toggle(uint32_t position) { assert(position < this->_numBits); if (this->Test(position)) this->Unset(position); else this->Set(position); return; } //------------------------------------------------------------------------- // void ArtsBitString::SetAll() //......................................................................... // //------------------------------------------------------------------------- void ArtsBitString::SetAll() { memset(this->_bits,0xff,this->_numBytes); return; } //------------------------------------------------------------------------- // void ArtsBitString::UnsetAll() //......................................................................... // //------------------------------------------------------------------------- void ArtsBitString::UnsetAll() { memset(this->_bits,0x00,this->_numBytes); return; } //------------------------------------------------------------------------- // uint32_t ArtsBitString::_BitByte(uint32_t position) const //......................................................................... // //------------------------------------------------------------------------- uint32_t ArtsBitString::_BitByte(uint32_t position) const { return(position >> 3); } //------------------------------------------------------------------------- // uint8_t ArtsBitString::_BitMask(uint32_t position) //......................................................................... // //------------------------------------------------------------------------- uint8_t ArtsBitString::_BitMask(uint32_t position) const { uint8_t bitpos = position & 0x07; uint8_t rc = ((uint8_t)1 << bitpos); return(rc); }