//# BitVector.cc: A BitVector class with variable bit vector size //# Copyright (C) 1993,1994,1995,2001 //# Associated Universities, Inc. Washington DC, USA. //# //# This library is free software; you can redistribute it and/or modify it //# under the terms of the GNU Library General Public License as published by //# the Free Software Foundation; either version 2 of the License, or (at your //# option) any later version. //# //# This library 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 Library General Public //# License for more details. //# //# You should have received a copy of the GNU Library General Public License //# along with this library; if not, write to the Free Software Foundation, //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. //# //# Correspondence concerning AIPS++ should be addressed as follows: //# Internet email: aips2-request@nrao.edu. //# Postal address: AIPS++ Project Office //# National Radio Astronomy Observatory //# 520 Edgemont Road //# Charlottesville, VA 22903-2475 USA //# //# $Id$ #include #include namespace casacore { //# NAMESPACE CASACORE - BEGIN BitVector::BitVector() : size_p (0), bits_p (0) {} BitVector::BitVector (uInt length, Bool state) : size_p (length), bits_p ((length + WORDSIZE - 1) / WORDSIZE, uInt(0)) { if (state) { set (state); } } BitVector::BitVector (const BitVector& that) : size_p (that.size_p), bits_p (that.bits_p) {} BitVector::~BitVector() {} BitVector& BitVector::operator= (const BitVector& that) { size_p = that.size_p; bits_p = that.bits_p; return *this; } BitVector& BitVector::operator= (Bool state) { set (state); return *this; } void BitVector::putBit (uInt pos, Bool state) { if (state) { setBit (pos); }else{ clearBit (pos); } } Bool BitVector::toggleBit (uInt pos) { Bool result = getBit (pos); putBit (pos, (!result)); return result; } Bool BitVector::getBit (uInt pos) const { DebugAssert (pos < size_p, AipsError); uInt index = pos/WORDSIZE; Bool result = True; if ((bits_p[index] & (1 << (pos - index*WORDSIZE))) == 0) { result = False; } return result; } void BitVector::resize (uInt length, Bool state, Bool copy) { //# Do a true resize. uInt oldSize = size_p; bits_p.resize ((length + WORDSIZE - 1) / WORDSIZE, True, copy); size_p = length; if (!copy) { set (state); }else{ if (length > oldSize) { set (oldSize, length-oldSize, state); } } } void BitVector::set (Bool state) { uInt value = 0; if (state) { value = ~value; } for (uInt i=0; i size_p) { throw (AipsError ("BitVector::set past end-of-vector")); } if (length == 0) { return; } //# Determine the full words that can be set. //# When setting till the end of the vector, make endWord last word. uInt beginWord = (start + WORDSIZE - 1) / WORDSIZE; uInt endWord = end / WORDSIZE; if (end == size_p) { endWord = bits_p.nelements(); } uInt i; //# When there are no full words, we have to do part of a word only. if (beginWord >= endWord) { for (i=start; i size_p) { throw (AipsError ("BitVector::set past end-of-thisvector")); } if (thatStart+length > that.size_p) { throw (AipsError ("BitVector::set past end-of-thatvector")); } for (uInt i=0; i