RDKit: SparseBitVect.h Source File

| RDKit Open-source cheminformatics and machine learning.

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
54 SparseBitVect &operator=(const SparseBitVect &);
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|
SparseBitVect operator|(const SparseBitVect &) const
SparseBitVect::getNumOffBits
unsigned int getNumOffBits() const
returns the number of off bits
Definition: SparseBitVect.h:75
SparseBitVect::getNumOnBits
unsigned int getNumOnBits() const
returns the number of on bits
Definition: SparseBitVect.h:74
SparseBitVect::operator^
SparseBitVect operator^(const SparseBitVect &) const
IntVectIter
IntVect::iterator IntVectIter
Definition: BitVect.h:17
SparseBitVect::dp_bits
IntSet * dp_bits
our raw data, exposed for the sake of efficiency
Definition: SparseBitVect.h:80
SparseBitVect::getBitSet
const IntSet * getBitSet() const
returns a (const) pointer to our raw storage
Definition: SparseBitVect.h:64
SparseBitVect::setBit
bool setBit(const unsigned int which)
sets a particular bit and returns its original value
SparseBitVect
a class for bit vectors that are sparsely occupied.
Definition: SparseBitVect.h:33
SparseBitVect::operator!=
bool operator!=(const SparseBitVect &o) const
Definition: SparseBitVect.h:86
SparseBitVect::getOnBits
void getOnBits(IntVect &v) const
replaces the contents of v with indices of our on bits
IntSetConstIter
IntSet::const_iterator IntSetConstIter
Definition: SparseBitVect.h:22
IntSetIter
IntSet::iterator IntSetIter
Definition: SparseBitVect.h:21
SparseBitVect::~SparseBitVect
~SparseBitVect()
Definition: SparseBitVect.h:55
SparseBitVect::operator&
SparseBitVect operator&(const SparseBitVect &) const
SparseBitVect::getNumBits
unsigned int getNumBits() const
returns the number of bits (the length of the BitVect)
Definition: SparseBitVect.h:66
SparseBitVect::toString
std::string toString() const
returns a serialized (pickled) version of this BitVect
SparseBitVect::operator==
bool operator==(const SparseBitVect &o) const
Definition: SparseBitVect.h:83
SparseBitVect::operator~
SparseBitVect operator~() const
IntSet
set< int > IntSet
Definition: SparseBitVect.h:20
BitVect::size
unsigned int size() const
Definition: BitVect.h:61
SparseBitVect::operator=
SparseBitVect & operator=(const SparseBitVect &)
SparseBitVect::getBit
bool getBit(const unsigned int which) const
returns the value of a particular bit
SparseBitVect::clearBits
void clearBits()
clears (sets to off) all of our bits
Definition: SparseBitVect.h:80
BitVect.h
IntVect
std::vector< int > IntVect
Definition: BitVect.h:16
SparseBitVect::unsetBit
bool unsetBit(const unsigned int which)
unsets a particular bit and returns its original value
SparseBitVect::SparseBitVect
SparseBitVect(const SparseBitVect &other)
copy constructor
Definition: SparseBitVect.h:42
SparseBitVect::SparseBitVect
SparseBitVect()
Definition: SparseBitVect.h:35
BitVect
Abstract base class for storing BitVectors.
Definition: BitVect.h:23
SparseBitVect::operator[]
bool operator[](const unsigned int which) const
SparseBitVect::SparseBitVect
SparseBitVect(unsigned int size)
initialize with a particular size;
Definition: SparseBitVect.h:37