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 
21 
22 typedef set<int> IntSet;
23 typedef IntSet::iterator IntSetIter;
24 typedef IntSet::const_iterator IntSetConstIter;
25 
26 //! a class for bit vectors that are sparsely occupied.
27 /*!
28  SparseBitVect objects store only their on bits, in an
29  std::set.
30 
31  They are, as you might expect, quite memory efficient for sparsely populated
32  vectors but become rather a nightmare if they need to be negated.
33 
34  */
35 class SparseBitVect : public BitVect{
36 public:
37  SparseBitVect() : dp_bits(0), d_size(0) {};
38  //! initialize with a particular size;
39  explicit SparseBitVect(unsigned int size): dp_bits(0), d_size(0) {_initForSize(size); };
40 
41  //! copy constructor
43  d_size=0;dp_bits = 0;
44  _initForSize(other.getNumBits());
45  IntSet *bv=other.dp_bits;
46  std::copy(bv->begin(),bv->end(),std::inserter(*dp_bits,dp_bits->end()));
47  }
48  //! construct from a string pickle
49  SparseBitVect(const std::string &);
50  //! construct from a text pickle
51  SparseBitVect(const char *data,const unsigned int dataLen);
52 
54  ~SparseBitVect(){ delete dp_bits; };
55 
56  bool operator[](const unsigned int which) const;
60  SparseBitVect operator~ () const;
61 
62  //! returns a (const) pointer to our raw storage
63  const IntSet *getBitSet() const { return dp_bits;}
64 
65  unsigned int getNumBits() const { return d_size; };
66  bool setBit(const unsigned int which);
67  bool setBit(const IntSetIter which);
68  bool unsetBit(const unsigned int which);
69  bool getBit (const unsigned int which) const;
70  bool getBit(const IntVectIter which) const;
71  bool getBit(const IntSetIter which) const;
72 
73  unsigned int getNumOnBits() const { return dp_bits->size(); };
74  unsigned int getNumOffBits() const { return d_size - dp_bits->size(); };
75 
76  std::string toString() const;
77 
78  void getOnBits (IntVect& v) const;
79  void clearBits() { dp_bits->clear(); };
80  IntSet *dp_bits; //!< our raw data, exposed for the sake of efficiency
81 
82  bool operator==(const SparseBitVect &o) const {
83  return *dp_bits==*o.dp_bits;
84  }
85  bool operator!=(const SparseBitVect &o) const {
86  return *dp_bits!=*o.dp_bits;
87  }
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:74
unsigned int getNumOnBits() const
returns the number of on bits
Definition: SparseBitVect.h:73
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:79
const IntSet * getBitSet() const
returns a (const) pointer to our raw storage
Definition: SparseBitVect.h:63
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:35
bool operator!=(const SparseBitVect &o) const
Definition: SparseBitVect.h:85
void getOnBits(IntVect &v) const
replaces the contents of v with indices of our on bits
IntSet::const_iterator IntSetConstIter
Definition: SparseBitVect.h:24
IntSet::iterator IntSetIter
Definition: SparseBitVect.h:23
SparseBitVect operator&(const SparseBitVect &) const
unsigned int getNumBits() const
returns the number of bits (the length of the BitVect)
Definition: SparseBitVect.h:65
std::string toString() const
returns a serialized (pickled) version of this BitVect
bool operator==(const SparseBitVect &o) const
Definition: SparseBitVect.h:82
SparseBitVect operator~() const
set< int > IntSet
Definition: SparseBitVect.h:22
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:79
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:39