Panda3D
|
00001 // Filename: bitMask.h 00002 // Created by: drose (08Jun00) 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 BITMASK_H 00016 #define BITMASK_H 00017 00018 #include "pandabase.h" 00019 #include "pbitops.h" 00020 #include "numeric_types.h" 00021 #include "typedObject.h" 00022 #include "indent.h" 00023 #include "pnotify.h" 00024 00025 #include "checksumHashGenerator.h" 00026 00027 00028 //////////////////////////////////////////////////////////////////// 00029 // Class : BitMask 00030 // Description : A general bitmask class. This stores an array of 00031 // bits of some length that must fit within a given word 00032 // of the indicated type. See also BitArray. 00033 //////////////////////////////////////////////////////////////////// 00034 template<class WType, int nbits> 00035 class BitMask { 00036 public: 00037 typedef WType WordType; 00038 enum { num_bits = nbits }; 00039 00040 PUBLISHED: 00041 INLINE BitMask(); 00042 INLINE BitMask(WordType init_value); 00043 INLINE BitMask(const BitMask<WType, nbits> ©); 00044 INLINE BitMask<WType, nbits> &operator = (const BitMask<WType, nbits> ©); 00045 00046 INLINE static BitMask<WType, nbits> all_on(); 00047 INLINE static BitMask<WType, nbits> all_off(); 00048 INLINE static BitMask<WType, nbits> lower_on(int on_bits); 00049 INLINE static BitMask<WType, nbits> bit(int index); 00050 INLINE static BitMask<WType, nbits> range(int low_bit, int size); 00051 00052 INLINE ~BitMask(); 00053 00054 INLINE static bool has_max_num_bits(); 00055 INLINE static int get_max_num_bits(); 00056 00057 INLINE static int get_num_bits(); 00058 INLINE bool get_bit(int index) const; 00059 INLINE void set_bit(int index); 00060 INLINE void clear_bit(int index); 00061 INLINE void set_bit_to(int index, bool value); 00062 INLINE bool is_zero() const; 00063 INLINE bool is_all_on() const; 00064 00065 INLINE WordType extract(int low_bit, int size) const; 00066 INLINE void store(WordType value, int low_bit, int size); 00067 INLINE bool has_any_of(int low_bit, int size) const; 00068 INLINE bool has_all_of(int low_bit, int size) const; 00069 INLINE void set_range(int low_bit, int size); 00070 INLINE void clear_range(int low_bit, int size); 00071 INLINE void set_range_to(bool value, int low_bit, int size); 00072 INLINE WordType get_word() const; 00073 INLINE void set_word(WordType value); 00074 00075 INLINE int get_num_on_bits() const; 00076 INLINE int get_num_off_bits() const; 00077 INLINE int get_lowest_on_bit() const; 00078 INLINE int get_lowest_off_bit() const; 00079 INLINE int get_highest_on_bit() const; 00080 INLINE int get_highest_off_bit() const; 00081 INLINE int get_next_higher_different_bit(int low_bit) const; 00082 00083 INLINE void invert_in_place(); 00084 INLINE bool has_bits_in_common(const BitMask<WType, nbits> &other) const; 00085 INLINE void clear(); 00086 00087 void output(ostream &out) const; 00088 void output_binary(ostream &out, int spaces_every = 4) const; 00089 void output_hex(ostream &out, int spaces_every = 4) const; 00090 void write(ostream &out, int indent_level = 0) const; 00091 00092 INLINE bool operator == (const BitMask<WType, nbits> &other) const; 00093 INLINE bool operator != (const BitMask<WType, nbits> &other) const; 00094 INLINE bool operator < (const BitMask<WType, nbits> &other) const; 00095 INLINE int compare_to(const BitMask<WType, nbits> &other) const; 00096 00097 INLINE BitMask<WType, nbits> 00098 operator & (const BitMask<WType, nbits> &other) const; 00099 00100 INLINE BitMask<WType, nbits> 00101 operator | (const BitMask<WType, nbits> &other) const; 00102 00103 INLINE BitMask<WType, nbits> 00104 operator ^ (const BitMask<WType, nbits> &other) const; 00105 00106 INLINE BitMask<WType, nbits> 00107 operator ~ () const; 00108 00109 INLINE BitMask<WType, nbits> 00110 operator << (int shift) const; 00111 00112 INLINE BitMask<WType, nbits> 00113 operator >> (int shift) const; 00114 00115 INLINE void operator &= (const BitMask<WType, nbits> &other); 00116 INLINE void operator |= (const BitMask<WType, nbits> &other); 00117 INLINE void operator ^= (const BitMask<WType, nbits> &other); 00118 INLINE void operator <<= (int shift); 00119 INLINE void operator >>= (int shift); 00120 00121 INLINE void flood_down_in_place(); 00122 INLINE void flood_up_in_place(); 00123 INLINE BitMask<WType, nbits> flood_bits_down() const; 00124 INLINE BitMask<WType, nbits> flood_bits_up() const; 00125 INLINE BitMask<WType, nbits> keep_next_highest_bit() const; 00126 INLINE BitMask<WType, nbits> keep_next_lowest_bit() const; 00127 INLINE BitMask<WType, nbits> keep_next_highest_bit(int index) const; 00128 INLINE BitMask<WType, nbits> keep_next_lowest_bit(int index) const; 00129 INLINE BitMask<WType, nbits> keep_next_highest_bit(const BitMask<WType, nbits> &other) const; 00130 INLINE BitMask<WType, nbits> keep_next_lowest_bit(const BitMask<WType, nbits> &other) const; 00131 00132 INLINE int get_key() const; 00133 00134 public: 00135 INLINE void generate_hash(ChecksumHashGenerator &hashgen) const; 00136 00137 private: 00138 WordType _word; 00139 00140 public: 00141 static TypeHandle get_class_type() { 00142 return _type_handle; 00143 } 00144 static void init_type(); 00145 00146 private: 00147 static TypeHandle _type_handle; 00148 }; 00149 00150 #include "bitMask.I" 00151 00152 template<class WType, int nbits> 00153 INLINE ostream &operator << (ostream &out, const BitMask<WType, nbits> &bitmask) { 00154 bitmask.output(out); 00155 return out; 00156 } 00157 00158 // We need to define this temporary macro so we can pass a parameter 00159 // containing a comma through the macro. 00160 #define BITMASK32_DEF BitMask<PN_uint32, 32> 00161 EXPORT_TEMPLATE_CLASS(EXPCL_PANDA_PUTIL, EXPTP_PANDA_PUTIL, BITMASK32_DEF); 00162 00163 typedef BitMask<PN_uint16, 16> BitMask16; 00164 typedef BitMask<PN_uint32, 32> BitMask32; 00165 typedef BitMask<PN_uint64, 64> BitMask64; 00166 00167 #if NATIVE_WORDSIZE == 32 00168 typedef BitMask32 BitMaskNative; 00169 #elif NATIVE_WORDSIZE == 64 00170 typedef BitMask64 BitMaskNative; 00171 #else 00172 #error No definition for NATIVE_WORDSIZE--should be defined in dtoolbase.h. 00173 #endif // NATIVE_WORDSIZE 00174 00175 // Tell GCC that we'll take care of the instantiation explicitly here. 00176 #ifdef __GNUC__ 00177 #pragma interface 00178 #endif 00179 00180 #endif