Darwin  1.10(beta)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
drwnBitArray.h
1 /*****************************************************************************
2 ** DARWIN: A FRAMEWORK FOR MACHINE LEARNING RESEARCH AND DEVELOPMENT
3 ** Distributed under the terms of the BSD license (see the LICENSE file)
4 ** Copyright (c) 2007-2015, Stephen Gould
5 ** All rights reserved.
6 **
7 ******************************************************************************
8 ** FILENAME: drwnBitArray.h
9 ** AUTHOR(S): Stephen Gould <stephen.gould@anu.edu.au>
10 ** Paul Baumstarck <pbaumstarck@stanford.edu>
11 ** DESCRIPTION:
12 ** Implements a packed array of bits.
13 **
14 *****************************************************************************/
15 
16 #pragma once
17 
18 #include <stdlib.h>
19 #include <iostream>
20 #include <cstring>
21 
22 using namespace std;
23 
24 #define DRWN_INT_BIT_SIZE (8*sizeof(int))
25 #define DRWN_BIT_GET(x,b) ( ( (x)[(b)/DRWN_INT_BIT_SIZE] & (1<<((b)%DRWN_INT_BIT_SIZE)) ) != 0 )
26 #define DRWN_BIT_SET(x,b) ( (x)[(b)/DRWN_INT_BIT_SIZE] |= (1<<((b)%DRWN_INT_BIT_SIZE)) )
27 #define DRWN_BIT_CLEAR(x,b) ( (x)[(b)/DRWN_INT_BIT_SIZE] &= (unsigned int)(-1) - (1<<((b)%DRWN_INT_BIT_SIZE)) )
28 #define DRWN_BIT_FLIP(x,b) ( (x)[(b)/DRWN_INT_BIT_SIZE] ^= (1<<((b)%DRWN_INT_BIT_SIZE)) )
29 #define DRWN_BITMAP_SIZE(x) int( ((x+DRWN_INT_BIT_SIZE-1)/DRWN_INT_BIT_SIZE) )
30 
42 class drwnBitArray {
43  protected:
44  static const int NUMSETLOOKUP[256];
45  int _sz;
46  int _map_sz;
47  int *_map;
48  int _mask;
49 
50  public:
51  drwnBitArray();
53  drwnBitArray(int sz);
55  drwnBitArray(const drwnBitArray &m);
56  virtual ~drwnBitArray();
57 
59  inline bool empty() const { return (_sz == 0); }
61  inline int size() const { return _sz; }
63  int count() const;
64 
66  inline bool get(int i) const { return DRWN_BIT_GET(_map, i); }
68  inline void set(int i) { DRWN_BIT_SET(_map, i); }
70  inline void clear(int i) { DRWN_BIT_CLEAR(_map, i); }
72  inline void flip(int i) { DRWN_BIT_FLIP(_map, i); }
73 
75  drwnBitArray& ones();
77  drwnBitArray& zeros();
79  drwnBitArray& negate();
81  drwnBitArray& bitwiseand(const drwnBitArray& c);
83  drwnBitArray& bitwiseor(const drwnBitArray& c);
85  drwnBitArray& bitwisenand(const drwnBitArray& c);
87  drwnBitArray& bitwisenor(const drwnBitArray& c);
89  drwnBitArray& bitwisexor(const drwnBitArray& c);
90 
92  drwnBitArray& operator=(const drwnBitArray &c);
94  bool operator==(const drwnBitArray& c) const;
96  bool operator!=(const drwnBitArray& c) const;
98  bool operator<=(const drwnBitArray& c) const;
100  inline bool operator[](int i) const { return DRWN_BIT_GET(_map, i); }
101 
103  void print(ostream &os = cout, int stride = -1) const;
104 };
int _map_sz
size of the array in words
Definition: drwnBitArray.h:46
int size() const
returns the number of bits in the array
Definition: drwnBitArray.h:61
int _mask
mask for the last word in _map
Definition: drwnBitArray.h:48
void set(int i)
sets the i-th bit of the array to true
Definition: drwnBitArray.h:68
void flip(int i)
negates the i-th bit of the array
Definition: drwnBitArray.h:72
Implements an efficient packed array of bits.
Definition: drwnBitArray.h:42
bool empty() const
returns true if the array is empty
Definition: drwnBitArray.h:59
void clear(int i)
sets the i-th bit of the array to false
Definition: drwnBitArray.h:70
bool operator[](int i) const
returns true if the i-th bit of the array is set (see drwnBitArray::get)
Definition: drwnBitArray.h:100
int * _map
storage for the array
Definition: drwnBitArray.h:47
bool operator==(const CvRect &r, const CvRect &s)
equality operator for CvRect objects
Definition: drwnOpenCVUtils.cpp:83
int _sz
size of the array in bits
Definition: drwnBitArray.h:45