📄 bitmask.hpp
字号:
/* Copyright (C) 2003 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#ifndef NDB_BITMASK_H#define NDB_BITMASK_H#include <ndb_global.h>/** * Bitmask implementation. Size is given explicitly * (as first argument). All methods are static. */class BitmaskImpl {public: STATIC_CONST( NotFound = (unsigned)-1 ); /** * get - Check if bit n is set. */ static bool get(unsigned size, const Uint32 data[], unsigned n); /** * set - Set bit n to given value (true/false). */ static void set(unsigned size, Uint32 data[], unsigned n, bool value); /** * set - Set bit n. */ static void set(unsigned size, Uint32 data[], unsigned n); /** * set - Set all bits. */ static void set(unsigned size, Uint32 data[]); /** * assign - Set all bits in <em>dst</em> to corresponding in <em>src/<em> */ static void assign(unsigned size, Uint32 dst[], const Uint32 src[]); /** * clear - Clear bit n. */ static void clear(unsigned size, Uint32 data[], unsigned n); /** * clear - Clear all bits. */ static void clear(unsigned size, Uint32 data[]); /** * isclear - Check if all bits are clear. This is faster * than checking count() == 0. */ static bool isclear(unsigned size, const Uint32 data[]); /** * count - Count number of set bits. */ static unsigned count(unsigned size, const Uint32 data[]); /** * find - Find first set bit, starting at given position. * Returns NotFound when not found. */ static unsigned find(unsigned size, const Uint32 data[], unsigned n); /** * equal - Bitwise equal. */ static bool equal(unsigned size, const Uint32 data[], const Uint32 data2[]); /** * bitOR - Bitwise (x | y) into first operand. */ static void bitOR(unsigned size, Uint32 data[], const Uint32 data2[]); /** * bitAND - Bitwise (x & y) into first operand. */ static void bitAND(unsigned size, Uint32 data[], const Uint32 data2[]); /** * bitANDC - Bitwise (x & ~y) into first operand. */ static void bitANDC(unsigned size, Uint32 data[], const Uint32 data2[]); /** * bitXOR - Bitwise (x ^ y) into first operand. */ static void bitXOR(unsigned size, Uint32 data[], const Uint32 data2[]); /** * bitXORC - Bitwise (x ^ ~y) into first operand. */ static void bitXORC(unsigned size, Uint32 data[], const Uint32 data2[]); /** * contains - Check if all bits set in data2 are set in data */ static bool contains(unsigned size, Uint32 data[], const Uint32 data2[]); /** * overlaps - Check if any bit set in data is set in data2 */ static bool overlaps(unsigned size, Uint32 data[], const Uint32 data2[]); /** * getField - Get bitfield at given position and length (max 32 bits) */ static Uint32 getField(unsigned size, const Uint32 data[], unsigned pos, unsigned len); /** * setField - Set bitfield at given position and length (max 32 bits) */ static void setField(unsigned size, Uint32 data[], unsigned pos, unsigned len, Uint32 val); /** * getField - Get bitfield at given position and length */ static void getField(unsigned size, const Uint32 data[], unsigned pos, unsigned len, Uint32 dst[]); /** * setField - Set bitfield at given position and length */ static void setField(unsigned size, Uint32 data[], unsigned pos, unsigned len, const Uint32 src[]); /** * getText - Return as hex-digits (only for debug routines). */ static char* getText(unsigned size, const Uint32 data[], char* buf);private: static void getFieldImpl(const Uint32 data[], unsigned, unsigned, Uint32 []); static void setFieldImpl(Uint32 data[], unsigned, unsigned, const Uint32 []);};inline boolBitmaskImpl::get(unsigned size, const Uint32 data[], unsigned n){ assert(n < (size << 5)); return (data[n >> 5] & (1 << (n & 31))) != 0;}inline voidBitmaskImpl::set(unsigned size, Uint32 data[], unsigned n, bool value){ value ? set(size, data, n) : clear(size, data, n);}inline voidBitmaskImpl::set(unsigned size, Uint32 data[], unsigned n){ assert(n < (size << 5)); data[n >> 5] |= (1 << (n & 31));}inline voidBitmaskImpl::set(unsigned size, Uint32 data[]){ for (unsigned i = 0; i < size; i++) { data[i] = ~0; }}inline void BitmaskImpl::assign(unsigned size, Uint32 dst[], const Uint32 src[]){ for (unsigned i = 0; i < size; i++) { dst[i] = src[i]; }}inline voidBitmaskImpl::clear(unsigned size, Uint32 data[], unsigned n){ assert(n < (size << 5)); data[n >> 5] &= ~(1 << (n & 31));}inline voidBitmaskImpl::clear(unsigned size, Uint32 data[]){ for (unsigned i = 0; i < size; i++) { data[i] = 0; }}inline boolBitmaskImpl::isclear(unsigned size, const Uint32 data[]){ for (unsigned i = 0; i < size; i++) { if (data[i] != 0) return false; } return true;}inline unsignedBitmaskImpl::count(unsigned size, const Uint32 data[]){ unsigned cnt = 0; for (unsigned i = 0; i < size; i++) { Uint32 x = data[i]; while (x) { x &= (x - 1); cnt++; } } return cnt;}inline unsignedBitmaskImpl::find(unsigned size, const Uint32 data[], unsigned n){ while (n < (size << 5)) { // XXX make this smarter if (get(size, data, n)) { return n; } n++; } return NotFound;}inline boolBitmaskImpl::equal(unsigned size, const Uint32 data[], const Uint32 data2[]){ for (unsigned i = 0; i < size; i++) { if (data[i] != data2[i]) return false; } return true;}inline voidBitmaskImpl::bitOR(unsigned size, Uint32 data[], const Uint32 data2[]){ for (unsigned i = 0; i < size; i++) { data[i] |= data2[i]; }}inline voidBitmaskImpl::bitAND(unsigned size, Uint32 data[], const Uint32 data2[]){ for (unsigned i = 0; i < size; i++) { data[i] &= data2[i]; }}inline voidBitmaskImpl::bitANDC(unsigned size, Uint32 data[], const Uint32 data2[]){ for (unsigned i = 0; i < size; i++) { data[i] &= ~data2[i]; }}inline voidBitmaskImpl::bitXOR(unsigned size, Uint32 data[], const Uint32 data2[]){ for (unsigned i = 0; i < size; i++) { data[i] ^= data2[i]; }}inline voidBitmaskImpl::bitXORC(unsigned size, Uint32 data[], const Uint32 data2[]){ for (unsigned i = 0; i < size; i++) { data[i] ^= ~data2[i]; }}inline boolBitmaskImpl::contains(unsigned size, Uint32 data[], const Uint32 data2[]){ for (unsigned int i = 0; i < size; i++) if ((data[i] & data2[i]) != data2[i]) return false; return true;}inline boolBitmaskImpl::overlaps(unsigned size, Uint32 data[], const Uint32 data2[]){ for (unsigned int i = 0; i < size; i++) if ((data[i] & data2[i]) != 0) return true; return false;}inline Uint32BitmaskImpl::getField(unsigned size, const Uint32 data[], unsigned pos, unsigned len){ Uint32 val = 0; for (unsigned i = 0; i < len; i++) val |= get(size, data, pos + i) << i; return val;}inline voidBitmaskImpl::setField(unsigned size, Uint32 data[], unsigned pos, unsigned len, Uint32 val){ for (unsigned i = 0; i < len; i++) set(size, data, pos + i, val & (1 << i));}inline char *BitmaskImpl::getText(unsigned size, const Uint32 data[], char* buf){ char * org = buf; const char* const hex = "0123456789abcdef"; for (int i = (size-1); i >= 0; i--) { Uint32 x = data[i]; for (unsigned j = 0; j < 8; j++) { buf[7-j] = hex[x & 0xf]; x >>= 4; } buf += 8; } *buf = 0; return org;}/** * Bitmasks. The size is number of 32-bit words (Uint32). * Unused bits in the last word must be zero. * * XXX replace size by length in bits */template <unsigned size>struct BitmaskPOD {public: /** * POD data representation */ struct Data { Uint32 data[size];#if 0 Data & operator=(const BitmaskPOD<size> & src) { src.copyto(size, data); return *this; }#endif };private: Data rep;public: STATIC_CONST( Size = size ); STATIC_CONST( NotFound = BitmaskImpl::NotFound ); STATIC_CONST( TextLength = size * 8 ); /** * assign - Set all bits in <em>dst</em> to corresponding in <em>src/<em> */ void assign(const typename BitmaskPOD<size>::Data & src); /** * assign - Set all bits in <em>dst</em> to corresponding in <em>src/<em> */ static void assign(Uint32 dst[], const Uint32 src[]); static void assign(Uint32 dst[], const BitmaskPOD<size> & src); void assign(const BitmaskPOD<size> & src); /** * copy this to <em>dst</em> */ void copyto(unsigned sz, Uint32 dst[]) const; /** * assign <em>this</em> according to <em>src/em> */ void assign(unsigned sz, const Uint32 src[]); /** * get - Check if bit n is set. */ static bool get(const Uint32 data[], unsigned n); bool get(unsigned n) const; /** * set - Set bit n to given value (true/false). */ static void set(Uint32 data[], unsigned n, bool value); void set(unsigned n, bool value); /** * set - Set bit n. */ static void set(Uint32 data[], unsigned n); void set(unsigned n); /** * set - set all bits. */ static void set(Uint32 data[]); void set(); /** * clear - Clear bit n. */ static void clear(Uint32 data[], unsigned n); void clear(unsigned n); /** * clear - Clear all bits.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -