cmtkBitVector.cxx

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2010 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2464 $
00026 //
00027 //  $LastChangedDate: 2010-10-19 09:54:33 -0700 (Tue, 19 Oct 2010) $
00028 //
00029 //  $LastChangedBy: torsten_at_home $
00030 //
00031 */
00032 
00033 #include "cmtkBitVector.h"
00034 
00035 #include <System/cmtkMemory.h>
00036 
00037 #include <memory.h>
00038 
00039 namespace
00040 cmtk
00041 {
00042 
00045 
00046 BitVector::BitVector( const size_t size, const bool initial )
00047 {
00048   this->m_Size = (size+7) / 8; // +7 to allocate an extra byte for 8n+1...8n+7 bits
00049   this->m_BitVector = Memory::AllocateArray<byte>( this->m_Size );
00050 
00051   if ( initial )
00052     this->Set();
00053   else
00054     this->Reset();
00055 }
00056 
00057 BitVector::BitVector( const size_t size, byte *const bitSet )
00058 {
00059   this->m_Size = (size+7) / 8; // +7 to allocate an extra byte for 8n+1...8n+7 bits
00060   this->m_BitVector = bitSet;
00061 }
00062 
00063 BitVector::~BitVector()
00064 {
00065   Memory::DeleteArray( this->m_BitVector );
00066 }
00067 
00068 BitVector* 
00069 BitVector::Clone() const
00070 {
00071   byte *newBitVector = Memory::AllocateArray<byte>( this->m_Size );
00072   memcpy( newBitVector, this->m_BitVector, this->m_Size );
00073   return new BitVector( 8*this->m_Size, newBitVector );
00074 }
00075 
00076 void
00077 BitVector::Set()
00078 {
00079   memset( this->m_BitVector, 255, sizeof( *this->m_BitVector ) * this->m_Size );
00080 }
00081 
00082 void
00083 BitVector::Set( const size_t pos, const bool val )
00084 {
00085   if ( val )
00086     {
00087     this->m_BitVector[pos/8] |= (1<<(pos%8));
00088     } 
00089   else
00090     {
00091     this->m_BitVector[pos/8] &= ~(1<<(pos%8));
00092     }
00093 }
00094 
00095 void
00096 BitVector::Reset( const bool value )
00097 {
00098   if ( value )
00099     memset( this->m_BitVector, 255, sizeof( *this->m_BitVector ) * this->m_Size );
00100   else
00101     memset( this->m_BitVector, 0, sizeof( *this->m_BitVector ) * this->m_Size );
00102 }
00103 
00104 void
00105 BitVector::Reset( const size_t pos )
00106 {
00107   this->m_BitVector[pos/8] &= ~(1<<(pos%8));
00108 }
00109 
00110 void
00111 BitVector::Flip()
00112 {
00113   for ( size_t i=0; i<this->m_Size; ++i )
00114     this->m_BitVector[i] = ~this->m_BitVector[i];
00115 }
00116 
00117 void
00118 BitVector::Flip( const size_t pos )
00119 {
00120   this->m_BitVector[pos/8] ^= (1<<(pos%8));
00121 }
00122 
00123 bool
00124 BitVector::operator[]( const size_t pos ) const
00125 {
00126   return ( this->m_BitVector[pos/8] >> (pos%8) ) & 1;
00127 }
00128 
00129 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines