// Filename: bitMask.I
// Created by:  drose (08Jun00)
//
////////////////////////////////////////////////////////////////////
//
// PANDA 3D SOFTWARE
// Copyright (c) 2001 - 2004, Disney Enterprises, Inc.  All rights reserved
//
// All use of this software is subject to the terms of the Panda 3d
// Software license.  You should have received a copy of this license
// along with this source code; you will also find a current copy of
// the license at http://etc.cmu.edu/panda3d/docs/license/ .
//
// To contact the maintainers of this program write to
// panda3d-general@lists.sourceforge.net .
//
////////////////////////////////////////////////////////////////////

template<class WType, int nbits>
TypeHandle BitMask<WType, nbits>::_type_handle;

////////////////////////////////////////////////////////////////////
//     Function: BitMask::Constructor
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits>::
BitMask() :
  _word(0)
{
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::Constructor
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits>::
BitMask(WordType init_value) :
  _word(init_value)
{
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::Copy Constructor
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits>::
BitMask(const BitMask<WType, nbits> &copy) :
  _word(copy._word)
{
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::Copy Assignment Operator
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits> &BitMask<WType, nbits>::
operator = (const BitMask<WType, nbits> &copy) {
  _word = copy._word;
  return *this;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::Named all_on constructor
//       Access: Published, Static
//  Description: Returns a BitMask whose bits are all on.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits> BitMask<WType, nbits>::
all_on() {
  BitMask result;
  result._word = (WordType)~0;
  return result;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::Named all_on constructor
//       Access: Published, Static
//  Description: Returns a BitMask whose bits are all off.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits> BitMask<WType, nbits>::
all_off() {
  BitMask result;
  result._word = 0;
  return result;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::Named lower_on constructor
//       Access: Published, Static
//  Description: Returns a BitMask whose lower on_bits bits are on.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits> BitMask<WType, nbits>::
lower_on(int on_bits) {
  if (on_bits <= 0) {
    return all_off();
  } else if (on_bits >= num_bits) {
    return all_on();
  }
  BitMask result;
  result._word = ((WordType)1 << on_bits) - 1;
  return result;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::Named bit constructor
//       Access: Published, Static
//  Description: Returns a BitMask with only the indicated bit on.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits> BitMask<WType, nbits>::
bit(int index) {
  BitMask result;
  result.set_bit(index);
  return result;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::Named range constructor
//       Access: Published, Static
//  Description: Returns a BitMask whose size bits, beginning at
//               low_bit, are on.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits> BitMask<WType, nbits>::
range(int low_bit, int size) {
  BitMask result;
  if (size <= 0) {
    result._word = 0;
  } else if (size >= num_bits) {
    result._word = (WordType)~0;
  } else {
    result._word = ((WordType)1 << size) - 1;
  }
  result._word <<= low_bit;
  return result;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::Destructor
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits>::
~BitMask() {
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::has_max_num_bits
//       Access: Published, Static
//  Description: Returns true if there is a maximum number of bits
//               that may be stored in this structure, false
//               otherwise.  If this returns true, the number may be
//               queried in get_max_num_bits().
//
//               This method always returns true.  This method is
//               defined so generic programming algorithms can use
//               BitMask or BitArray interchangeably.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE bool BitMask<WType, nbits>::
has_max_num_bits() {
  return true;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::get_max_num_bits
//       Access: Published, Static
//  Description: If get_max_num_bits() returned true, this method may
//               be called to return the maximum number of bits that
//               may be stored in this structure.  It is an error to
//               call this if get_max_num_bits() return false.
//
//               It is never an error to call this method.  This
//               returns the same thing as get_num_bits().  This
//               method is defined so generic programming algorithms
//               can use BitMask or BitArray interchangeably.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE int BitMask<WType, nbits>::
get_max_num_bits() {
  return num_bits;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::get_num_bits
//       Access: Published, Static
//  Description: Returns the number of bits available to set in the
//               bitmask.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE int BitMask<WType, nbits>::
get_num_bits() {
  return num_bits;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::get_bit
//       Access: Published
//  Description: Returns true if the nth bit is set, false if it is
//               cleared.  index must be in the range [0,
//               num_bits).
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE bool BitMask<WType, nbits>::
get_bit(int index) const {
  nassertr(index >= 0 && index < num_bits, false);
  return (_word & ((WordType)1 << index)) != 0;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::set_bit
//       Access: Published
//  Description: Sets the nth bit on.  index must be in the range
//               [0, num_bits).
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
set_bit(int index) {
  nassertv(index >= 0 && index < num_bits);
  _word |= ((WordType)1 << index);
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::clear_bit
//       Access: Published
//  Description: Sets the nth bit off.  index must be in the range
//               [0, num_bits).
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
clear_bit(int index) {
  nassertv(index >= 0 && index < num_bits);
  _word &= ~((WordType)1 << index);
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::set_bit_to
//       Access: Published
//  Description: Sets the nth bit either on or off, according to the
//               indicated bool value.  index must be in the range [0,
//               num_bits).
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
set_bit_to(int index, bool value) {
  if (value) {
    set_bit(index);
  } else {
    clear_bit(index);
  }
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::is_zero
//       Access: Published
//  Description: Returns true if the entire bitmask is zero, false
//               otherwise.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE bool BitMask<WType, nbits>::
is_zero() const {
  return (_word == 0);
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::is_all_on
//       Access: Published
//  Description: Returns true if the entire bitmask is one, false
//               otherwise.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE bool BitMask<WType, nbits>::
is_all_on() const {
  return (~_word == 0);
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::extract
//       Access: Published
//  Description: Returns a word that represents only the indicated
//               range of bits within this BitMask, shifted to the
//               least-significant position.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE TYPENAME BitMask<WType, nbits>::WordType BitMask<WType, nbits>::
extract(int low_bit, int size) const {
  return (_word >> low_bit) &
    BitMask<WType, nbits>::lower_on(size)._word;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::store
//       Access: Published
//  Description: Stores the indicated word into the indicated range of
//               bits with this BitMask.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
store(WordType value, int low_bit, int size) {
  WordType mask = BitMask<WType, nbits>::range(low_bit, size)._word;
  _word = (_word & ~mask) | ((value << low_bit) & mask);
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::has_any_of
//       Access: Published
//  Description: Returns true if any bit in the indicated range is
//               set, false otherwise.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE bool BitMask<WType, nbits>::
has_any_of(int low_bit, int size) const {
  WordType mask = BitMask<WType, nbits>::range(low_bit, size)._word;
  return (_word & mask) != 0;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::has_all_of
//       Access: Published
//  Description: Returns true if all bits in the indicated range are
//               set, false otherwise.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE bool BitMask<WType, nbits>::
has_all_of(int low_bit, int size) const {
  WordType mask = BitMask<WType, nbits>::range(low_bit, size)._word;
  return (_word & mask) == mask;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::set_range
//       Access: Published
//  Description: Sets the indicated range of bits on.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
set_range(int low_bit, int size) {
  WordType mask = BitMask<WType, nbits>::range(low_bit, size)._word;
  _word |= mask;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::clear_range
//       Access: Published
//  Description: Sets the indicated range of bits off.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
clear_range(int low_bit, int size) {
  WordType mask = BitMask<WType, nbits>::range(low_bit, size)._word;
  _word &= ~mask;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::set_range_to
//       Access: Published
//  Description: Sets the indicated range of bits to either on or off.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
set_range_to(bool value, int low_bit, int size) {
  if (value) {
    set_range(low_bit, size);
  } else {
    clear_range(low_bit, size);
  }
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::get_word
//       Access: Published
//  Description: Returns the entire BitMask as a single word.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE TYPENAME BitMask<WType, nbits>::WordType BitMask<WType, nbits>::
get_word() const {
  return _word;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::set_word
//       Access: Published
//  Description: Sets the entire BitMask to the value indicated by the
//               given word.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
set_word(WordType value) {
  _word = value;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::invert_in_place
//       Access: Published
//  Description: Inverts all the bits in the BitMask.  This is
//               equivalent to mask = ~mask.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
invert_in_place() {
  _word = ~_word;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::has_bits_in_common
//       Access: Published
//  Description: Returns true if this BitMask has any "one" bits in
//               common with the other one, false otherwise.
//
//               This is equivalent to (mask & other) != 0, but may be
//               faster.  (Actually, it should only be faster in the
//               BitArray case, but this method is provided for the
//               benefit of generic programming algorithms).
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE bool BitMask<WType, nbits>::
has_bits_in_common(const BitMask<WType, nbits> &other) const {
  return (_word & other._word) != 0;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::clear
//       Access: Published
//  Description: Sets all the bits in the BitMask off.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
clear() {
  _word = 0;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::output
//       Access: Published
//  Description: Writes the BitMask out as a binary or a hex number,
//               according to the number of bits.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
void BitMask<WType, nbits>::
output(ostream &out) const {
  if (num_bits >= 40) {
    output_hex(out);
  } else {
    output_binary(out);
  }
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::output_binary
//       Access: Published
//  Description: Writes the BitMask out as a binary number, with
//               spaces every four bits.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
void BitMask<WType, nbits>::
output_binary(ostream &out, int spaces_every) const {
  for (int i = num_bits - 1; i >= 0; i--) {
    if (spaces_every != 0 && ((i % spaces_every) == spaces_every - 1)) {
      out << ' ';
    }
    out << (get_bit(i) ? '1' : '0');
  }
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::output_hex
//       Access: Published
//  Description: Writes the BitMask out as a hexadecimal number, with
//               spaces every four digits.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
void BitMask<WType, nbits>::
output_hex(ostream &out, int spaces_every) const {
  int num_digits = (num_bits + 3) / 4;

  for (int i = num_digits - 1; i >= 0; i--) {
    WordType digit = extract(i * 4, 4);
    if (spaces_every != 0 && ((i % spaces_every) == spaces_every - 1)) {
      out << ' ';
    }
    if (digit > 9) {
      out << (char)(digit - 10 + 'a');
    } else {
      out << (char)(digit + '0');
    }
  }
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::write
//       Access: Published
//  Description: Writes the BitMask out as a binary or a hex number,
//               according to the number of bits.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
void BitMask<WType, nbits>::
write(ostream &out, int indent_level) const {
  indent(out, indent_level) << *this << "\n";
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator ==
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE bool BitMask<WType, nbits>::
operator == (const BitMask<WType, nbits> &other) const {
  return _word == other._word;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator !=
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE bool BitMask<WType, nbits>::
operator != (const BitMask<WType, nbits> &other) const {
  return _word != other._word;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator <
//       Access: Published
//  Description: The ordering operator is of limited usefulness with a
//               BitMask, however, it has a definition which places
//               all unique BitMasks into a unique ordering.  It may
//               be useful when defining ordered STL containers of
//               BitMasks, for instance; and it's required in order to
//               export any STL container (ordered or unordered) of
//               BitMask under Windows.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE bool BitMask<WType, nbits>::
operator < (const BitMask<WType, nbits> &other) const {
  return _word < other._word;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::compare_to
//       Access: Published
//  Description: Returns a number less than zero if this BitMask sorts
//               before the indicated other BitMask, greater than zero
//               if it sorts after, or 0 if they are equivalent.  This
//               is based on the same ordering defined by operator <.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE int BitMask<WType, nbits>::
compare_to(const BitMask<WType, nbits> &other) const {
  if ((*this) < other) {
    return -1;
  } else if (other < (*this)) {
    return 1;
  } else {
    return 0;
  }
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator &
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits> BitMask<WType, nbits>::
operator & (const BitMask<WType, nbits> &other) const {
  BitMask<WType, nbits> result(*this);
  result &= other;
  return result;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator |
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits> BitMask<WType, nbits>::
operator | (const BitMask<WType, nbits> &other) const {
  BitMask<WType, nbits> result(*this);
  result |= other;
  return result;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator ^
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits> BitMask<WType, nbits>::
operator ^ (const BitMask<WType, nbits> &other) const {
  BitMask<WType, nbits> result(*this);
  result ^= other;
  return result;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator ~
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits> BitMask<WType, nbits>::
operator ~ () const {
  return BitMask<WType, nbits>(~_word);
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator <<
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits> BitMask<WType, nbits>::
operator << (int shift) const {
  BitMask<WType, nbits> result(*this);
  result <<= shift;
  return result;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator >>
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE BitMask<WType, nbits> BitMask<WType, nbits>::
operator >> (int shift) const {
  BitMask<WType, nbits> result(*this);
  result >>= shift;
  return result;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator &=
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
operator &= (const BitMask<WType, nbits> &other) {
  _word &= other._word;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator |=
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
operator |= (const BitMask<WType, nbits> &other) {
  _word |= other._word;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator ^=
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
operator ^= (const BitMask<WType, nbits> &other) {
  _word ^= other._word;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator <<=
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
operator <<= (int shift) {
  _word <<= shift;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::operator >>=
//       Access: Published
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
operator >>= (int shift) {
  _word >>= shift;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::get_key
//       Access: Published
//  Description: Returns a mostly unique integer key per unique
//               bitmask, suitable for using in a hash table.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE int BitMask<WType, nbits>::
get_key() const {
  return (int)_word;
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::generate_hash
//       Access: Public
//  Description: Adds the bitmask to the indicated hash generator.
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
INLINE void BitMask<WType, nbits>::
generate_hash(ChecksumHashGenerator &hashgen) const {
  hashgen.add_int(_word);
}

////////////////////////////////////////////////////////////////////
//     Function: BitMask::init_type
//       Access: Public
//  Description:
////////////////////////////////////////////////////////////////////
template<class WType, int nbits>
void BitMask<WType, nbits>::
init_type() {
  ostringstream str;
  str << "BitMask" << num_bits;
  register_type(_type_handle, str.str());
}
