RDKit
Open-source cheminformatics and machine learning.
SparseBitVect.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2003-2008 greg Landrum and Rational Discovery LLC
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 #ifndef __RD_SPARSEBITVECTS_H__
11 #define __RD_SPARSEBITVECTS_H__
12 
13 #include "BitVect.h"
14 
15 #include <set>
16 using std::set;
17 #include <iterator>
18 #include <algorithm>
19 
20 typedef set<int> IntSet;
21 typedef IntSet::iterator IntSetIter;
22 typedef IntSet::const_iterator IntSetConstIter;
23 
24 //! a class for bit vectors that are sparsely occupied.
25 /*!
26  SparseBitVect objects store only their on bits, in an
27  std::set.
28 
29  They are, as you might expect, quite memory efficient for sparsely populated
30  vectors but become rather a nightmare if they need to be negated.
31 
32  */
33 class SparseBitVect : public BitVect {
34  public:
35  SparseBitVect() : dp_bits(0), d_size(0){};
36  //! initialize with a particular size;
37  explicit SparseBitVect(unsigned int size) : dp_bits(0), d_size(0) {
38  _initForSize(size);
39  };
40 
41  //! copy constructor
42  SparseBitVect(const SparseBitVect &other) : BitVect(other) {
43  d_size = 0;
44  dp_bits = 0;
45  _initForSize(other.getNumBits());
46  IntSet *bv = other.dp_bits;
47  std::copy(bv->begin(), bv->end(), std::inserter(*dp_bits, dp_bits->end()));
48  }
49  //! construct from a string pickle
50  SparseBitVect(const std::string &);
51  //! construct from a text pickle
52  SparseBitVect(const char *data, const unsigned int dataLen);
53 
55  ~SparseBitVect() { delete dp_bits; };
56 
57  bool operator[](const unsigned int which) const;
58  SparseBitVect operator|(const SparseBitVect &) const;
59  SparseBitVect operator&(const SparseBitVect &) const;
60  SparseBitVect operator^(const SparseBitVect &) const;
61  SparseBitVect operator~() const;
62 
63  //! returns a (const) pointer to our raw storage
64  const IntSet *getBitSet() const { return dp_bits; }
65 
66  unsigned int getNumBits() const { return d_size; };
67  bool setBit(const unsigned int which);
68  bool setBit(const IntSetIter which);
69  bool unsetBit(const unsigned int which);
70  bool getBit(const unsigned int which) const;
71  bool getBit(const IntVectIter which) const;
72  bool getBit(const IntSetIter which) const;
73 
74  unsigned int getNumOnBits() const { return static_cast<unsigned int>(dp_bits->size()); };
75  unsigned int getNumOffBits() const { return d_size - static_cast<unsigned int>(dp_bits->size()); };
76 
77  std::string toString() const;
78 
79  void getOnBits(IntVect &v) const;
80  void clearBits() { dp_bits->clear(); };
81  IntSet *dp_bits; //!< our raw data, exposed for the sake of efficiency
82 
83  bool operator==(const SparseBitVect &o) const {
84  return *dp_bits == *o.dp_bits;
85  }
86  bool operator!=(const SparseBitVect &o) const {
87  return *dp_bits != *o.dp_bits;
88  }
89 
90  private:
91  unsigned int d_size;
92  void _initForSize(const unsigned int size);
93 };
94 
95 #endif
SparseBitVect operator|(const SparseBitVect &) const
unsigned int getNumOffBits() const
returns the number of off bits
Definition: SparseBitVect.h:75
unsigned int getNumOnBits() const
returns the number of on bits
Definition: SparseBitVect.h:74
SparseBitVect operator^(const SparseBitVect &) const
IntVect::iterator IntVectIter
Definition: BitVect.h:17
IntSet * dp_bits
our raw data, exposed for the sake of efficiency
Definition: SparseBitVect.h:80
const IntSet * getBitSet() const
returns a (const) pointer to our raw storage
Definition: SparseBitVect.h:64
bool setBit(const unsigned int which)
sets a particular bit and returns its original value
a class for bit vectors that are sparsely occupied.
Definition: SparseBitVect.h:33
bool operator!=(const SparseBitVect &o) const
Definition: SparseBitVect.h:86
void getOnBits(IntVect &v) const
replaces the contents of v with indices of our on bits
IntSet::const_iterator IntSetConstIter
Definition: SparseBitVect.h:22
IntSet::iterator IntSetIter
Definition: SparseBitVect.h:21
SparseBitVect operator&(const SparseBitVect &) const
unsigned int getNumBits() const
returns the number of bits (the length of the BitVect)
Definition: SparseBitVect.h:66
std::string toString() const
returns a serialized (pickled) version of this BitVect
bool operator==(const SparseBitVect &o) const
Definition: SparseBitVect.h:83
SparseBitVect operator~() const
set< int > IntSet
Definition: SparseBitVect.h:20
unsigned int size() const
Definition: BitVect.h:61
SparseBitVect & operator=(const SparseBitVect &)
bool getBit(const unsigned int which) const
returns the value of a particular bit
void clearBits()
clears (sets to off) all of our bits
Definition: SparseBitVect.h:80
std::vector< int > IntVect
Definition: BitVect.h:16
bool unsetBit(const unsigned int which)
unsets a particular bit and returns its original value
SparseBitVect(const SparseBitVect &other)
copy constructor
Definition: SparseBitVect.h:42
Abstract base class for storing BitVectors.
Definition: BitVect.h:23
bool operator[](const unsigned int which) const
SparseBitVect(unsigned int size)
initialize with a particular size;
Definition: SparseBitVect.h:37