File: ExplicitBitVect.cpp

package info (click to toggle)
rdkit 201203-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 37,840 kB
  • sloc: cpp: 93,902; python: 51,897; java: 5,192; ansic: 3,497; xml: 2,499; sql: 1,641; yacc: 1,518; lex: 1,076; makefile: 325; fortran: 183; sh: 153; cs: 51
file content (179 lines) | stat: -rw-r--r-- 5,154 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// $Id: ExplicitBitVect.cpp 1625 2011-01-13 04:22:56Z glandrum $
//
// Copyright (c) 2001-2008 greg Landrum and Rational Discovery LLC
//
//  @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#include <iostream>
#include <RDBoost/Exceptions.h>
#include "ExplicitBitVect.h"
#include <RDGeneral/StreamOps.h>
#include "base64.h"
#include <sstream>
#include <limits>
#ifdef WIN32
#include <ios>
#endif
#include <boost/cstdint.hpp>

ExplicitBitVect::ExplicitBitVect(const std::string &s)
{
  d_size=0;dp_bits = 0;d_numOnBits=0;
  initFromText(s.c_str(),s.length());
}
ExplicitBitVect::ExplicitBitVect(const char *data,const unsigned int dataLen)
{
  d_size=0;dp_bits = 0;d_numOnBits=0;
  initFromText(data,dataLen);
}

  ExplicitBitVect::ExplicitBitVect(const ExplicitBitVect& other){
    d_size = other.d_size;
    dp_bits = new boost::dynamic_bitset<>(*(other.dp_bits));
    d_numOnBits=other.d_numOnBits;
  };

  ExplicitBitVect& ExplicitBitVect::operator=(const ExplicitBitVect& other){
    d_size = other.d_size;
    dp_bits = new boost::dynamic_bitset<>(*(other.dp_bits));
    d_numOnBits=other.d_numOnBits;
    return *this;
  };
  bool ExplicitBitVect::operator[] (const unsigned int which) const {
    if(which >= d_size){
      throw IndexErrorException(which);
    }
    return (bool)(*dp_bits)[which];
  };
  bool ExplicitBitVect::setBit(const unsigned int which){
    if(which >= d_size){
      throw IndexErrorException(which);
    }
    if((bool)(*dp_bits)[which]){
      return true;
    } else {
      (*dp_bits)[which] = 1;
      ++d_numOnBits;
      return false;
    }
  };
  bool ExplicitBitVect::unsetBit(const unsigned int which){
    if(which >= d_size){
      throw IndexErrorException(which);
    }
    if((bool)(*dp_bits)[which]){
      (*dp_bits)[which] = 0;
      --d_numOnBits;
      return true;
    } else {
      return false;
    }
  };
  bool ExplicitBitVect::getBit(const unsigned int which) const {
    if(which >= d_size){
      throw IndexErrorException(which);
    }
    return((bool)(*dp_bits)[which]);
  };

  ExplicitBitVect ExplicitBitVect::operator^ (const ExplicitBitVect &other) const {
    ExplicitBitVect ans(d_size);
    *(ans.dp_bits) = (*dp_bits) ^ *(other.dp_bits);
    ans.d_numOnBits=ans.dp_bits->count();
    return(ans);
  };

  ExplicitBitVect ExplicitBitVect::operator& (const ExplicitBitVect &other) const {
    ExplicitBitVect ans(d_size);
    *(ans.dp_bits) = (*dp_bits) & *(other.dp_bits);
    ans.d_numOnBits=ans.dp_bits->count();
    return(ans);
  };

  ExplicitBitVect ExplicitBitVect::operator| (const ExplicitBitVect &other) const {
    ExplicitBitVect ans(d_size);
    *(ans.dp_bits) = (*dp_bits) | *(other.dp_bits);
    ans.d_numOnBits=ans.dp_bits->count();
    return(ans);
  };
  
  ExplicitBitVect ExplicitBitVect::operator~ () const {
    ExplicitBitVect ans(d_size);
    *(ans.dp_bits) = ~(*dp_bits);
    ans.d_numOnBits=ans.dp_bits->count();
    return(ans);
  };

  unsigned int ExplicitBitVect::getNumBits() const {
    return d_size;
  };
  unsigned int ExplicitBitVect::getNumOnBits() const {
    return d_numOnBits;
  };
  unsigned int ExplicitBitVect::getNumOffBits() const {
    return d_size - d_numOnBits;
  };

  // the contents of v are blown out
  void ExplicitBitVect::getOnBits (IntVect& v) const {
    unsigned int nOn = getNumOnBits();
    if(!v.empty()) IntVect().swap(v);
    v.reserve(nOn);
    for(unsigned int i=0;i<d_size;i++){
      if((bool)(*dp_bits)[i]) v.push_back(i);
    }
  };

  void ExplicitBitVect::_initForSize(unsigned int size) {
    d_size = size;
    if(dp_bits) delete dp_bits;
    dp_bits = new boost::dynamic_bitset<>(size);
    d_numOnBits=0;
  };

  ExplicitBitVect::~ExplicitBitVect() {
    if(dp_bits) delete dp_bits;
  };

std::string
ExplicitBitVect::toString() const
{
  // This Function replaces the older version (version 16) of writing the onbits to
  // a string
  // the old version does not perform any run length encoding, it only checks to see if 
  // the length of the bitvect can be short ints and writes the on bits as shorts 
  // other wise the onbits are all written as ints

  // here we do run length encoding and the version number has been bumped to 32 as well. 
  // only the reader needs to take care of readinf all legacy versions
  // also in this scheme each bit number written to the string is checked to see how many 
  // bytes it needs
  std::stringstream ss(std::ios_base::binary|std::ios_base::out|std::ios_base::in);

  boost::int32_t tInt = ci_BITVECT_VERSION*-1;
  RDKit::streamWrite(ss,tInt);
  tInt=d_size;
  RDKit::streamWrite(ss,tInt);
  tInt=getNumOnBits();
  RDKit::streamWrite(ss,tInt);

  int prev = -1;
  unsigned int zeroes;
  for(unsigned int i=0;i<d_size;i++){
    if( (bool)(*dp_bits)[i] ){
      zeroes = i - prev -1;
      RDKit::appendPackedIntToStream(ss, zeroes);
      prev = i;
    }
  }
  zeroes = d_size - prev -1;
  RDKit::appendPackedIntToStream(ss, zeroes);
  std::string res(ss.str());
  return res;
}