Panda3D
 All Classes Functions Variables Enumerations
bitArray.h
00001 // Filename: bitArray.h
00002 // Created by:  drose (20Jan06)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef BITARRAY_H
00016 #define BITARRAY_H
00017 
00018 #include "pandabase.h"
00019 
00020 #include "bitMask.h"
00021 #include "numeric_types.h"
00022 #include "typedObject.h"
00023 #include "indent.h"
00024 #include "pointerToArray.h"
00025 
00026 #include "checksumHashGenerator.h"
00027 
00028 class SparseArray;
00029 class BamWriter;
00030 class BamReader;
00031 class Datagram;
00032 class DatagramIterator;
00033 
00034 ////////////////////////////////////////////////////////////////////
00035 //       Class : BitArray
00036 // Description : A dynamic array with an unlimited number of bits.
00037 //
00038 //               This is similar to a BitMask, except it appears to
00039 //               contain an infinite number of bits.  You can use it
00040 //               very much as you would use a BitMask.
00041 ////////////////////////////////////////////////////////////////////
00042 class EXPCL_PANDA_PUTIL BitArray {
00043 public:
00044   typedef BitMaskNative MaskType;
00045   typedef MaskType::WordType WordType;
00046   enum { num_bits_per_word = MaskType::num_bits };
00047 
00048 PUBLISHED:
00049   INLINE BitArray();
00050   INLINE BitArray(WordType init_value);
00051   INLINE BitArray(const BitArray &copy);
00052   INLINE BitArray &operator = (const BitArray &copy);
00053   BitArray(const SparseArray &from);
00054 
00055   INLINE static BitArray all_on();
00056   INLINE static BitArray all_off();
00057   INLINE static BitArray lower_on(int on_bits);
00058   INLINE static BitArray bit(int index);
00059   INLINE static BitArray range(int low_bit, int size);
00060 
00061   INLINE ~BitArray();
00062 
00063   INLINE static bool has_max_num_bits();
00064   INLINE static int get_max_num_bits();
00065 
00066   INLINE static int get_num_bits_per_word();
00067   INLINE int get_num_bits() const;
00068   INLINE bool get_bit(int index) const;
00069   INLINE void set_bit(int index);
00070   INLINE void clear_bit(int index);
00071   INLINE void set_bit_to(int index, bool value);
00072   INLINE bool get_highest_bits() const;
00073   bool is_zero() const;
00074   bool is_all_on() const;
00075 
00076   INLINE WordType extract(int low_bit, int size) const;
00077   INLINE void store(WordType value, int low_bit, int size);
00078   bool has_any_of(int low_bit, int size) const;
00079   bool has_all_of(int low_bit, int size) const;
00080   void set_range(int low_bit, int size);
00081   void clear_range(int low_bit, int size);
00082   INLINE void set_range_to(bool value, int low_bit, int size);
00083 
00084   int get_num_on_bits() const;
00085   int get_num_off_bits() const;
00086   int get_lowest_on_bit() const;
00087   int get_lowest_off_bit() const;
00088   int get_highest_on_bit() const;
00089   int get_highest_off_bit() const;
00090   int get_next_higher_different_bit(int low_bit) const;
00091 
00092   INLINE int get_num_words() const;
00093   INLINE MaskType get_word(int n) const;
00094   INLINE void set_word(int n, MaskType value);
00095 
00096   void invert_in_place();
00097   bool has_bits_in_common(const BitArray &other) const;
00098   INLINE void clear();
00099 
00100   void output(ostream &out) const;
00101   void output_binary(ostream &out, int spaces_every = 4) const;
00102   void output_hex(ostream &out, int spaces_every = 4) const;
00103   void write(ostream &out, int indent_level = 0) const;
00104 
00105   INLINE bool operator == (const BitArray &other) const;
00106   INLINE bool operator != (const BitArray &other) const;
00107   INLINE bool operator < (const BitArray &other) const;
00108   int compare_to(const BitArray &other) const;
00109 
00110   INLINE BitArray
00111   operator & (const BitArray &other) const;
00112 
00113   INLINE BitArray
00114   operator | (const BitArray &other) const;
00115 
00116   INLINE BitArray
00117   operator ^ (const BitArray &other) const;
00118 
00119   INLINE BitArray
00120   operator ~ () const;
00121 
00122   INLINE BitArray
00123   operator << (int shift) const;
00124 
00125   INLINE BitArray
00126   operator >> (int shift) const;
00127 
00128   void operator &= (const BitArray &other);
00129   void operator |= (const BitArray &other);
00130   void operator ^= (const BitArray &other);
00131   void operator <<= (int shift);
00132   void operator >>= (int shift);
00133 
00134 public:
00135   void generate_hash(ChecksumHashGenerator &hashgen) const;
00136 
00137 private:
00138   INLINE void copy_on_write();
00139   void ensure_has_word(int n);
00140   void normalize();
00141 
00142 private:
00143   typedef PTA(MaskType) Array;
00144   Array _array;
00145   int _highest_bits;  // Either 0 or 1.
00146 
00147 public:
00148   void write_datagram(BamWriter *manager, Datagram &dg) const;
00149   void read_datagram(DatagramIterator &scan, BamReader *manager);
00150 
00151 public:
00152   static TypeHandle get_class_type() {
00153     return _type_handle;
00154   }
00155   static void init_type() {
00156     register_type(_type_handle, "BitArray");
00157   }
00158 
00159 private:
00160   static TypeHandle _type_handle;
00161 };
00162 
00163 #include "bitArray.I"
00164 
00165 INLINE ostream &
00166 operator << (ostream &out, const BitArray &array) {
00167   array.output(out);
00168   return out;
00169 }
00170 
00171 #endif
 All Classes Functions Variables Enumerations