File: wrap_ExplicitBV.cpp

package info (click to toggle)
rdkit 202503.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 580; xml: 229; fortran: 183; sh: 105
file content (132 lines) | stat: -rw-r--r-- 5,390 bytes parent folder | download | duplicates (2)
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
//
//  Copyright (C) 2003-2021 greg Landrum and other RDKit contributors
//
//  @@ 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 <RDBoost/Wrap.h>
#include <DataStructs/BitVects.h>
#include <RDBoost/PySequenceHolder.h>
#include "wrap_helpers.h"

namespace python = boost::python;

// allows BitVects to be pickled
struct ebv_pickle_suite : rdkit_pickle_suite {
  static python::tuple getinitargs(const ExplicitBitVect &self) {
    std::string res = self.toString();
    python::object retval = python::object(
        python::handle<>(PyBytes_FromStringAndSize(res.c_str(), res.length())));
    return python::make_tuple(retval);
  };
};

python::list ExplicitToList(const ExplicitBitVect &sv) {
  python::list l;
  if (!sv.dp_bits) {
    return l;
  }

  auto count = sv.getNumBits();
  if (count) {
    l.append(0);
    l *= count;
    auto pos = sv.dp_bits->find_first();
    while (pos != boost::dynamic_bitset<>::npos) {
      l[pos] = 1;
      pos = sv.dp_bits->find_next(pos);
    }
  }
  return l;
}

std::string ebvClassDoc =
    "A class to store explicit bit vectors.\n\
\n\
This class is most useful for situations where the size of the vector\n\
is relatively small (tens of thousands or smaller).\n\
\n\
For larger vectors, use the _SparseBitVect_ class instead.\n\
\n\
As you would expect, _ExplicitBitVects_ support a set of binary operations\n\
so you can do things like:\n\
  bv3 = bv1 & bv2  (bitwise and)\n\
  bv3 = bv1 | bv2  (bitwise or)\n\
  bv3 = bv1 ^ bv2  (bitwise xor)\n\
  bv3 = ~bv1       (bitwise negation)\n\
\n\
Bits can be set and read using either the Set/UnsetBit() and GetBit() methods\n\
or by indexing (i.e. bv[i] = 1 or if bv[i]).\n\
\n";

struct EBV_wrapper {
  static void wrap() {
    python::class_<ExplicitBitVect, boost::shared_ptr<ExplicitBitVect>>(
        "ExplicitBitVect", ebvClassDoc.c_str(),
        python::init<unsigned int>(python::args("self", "size")))
        .def(python::init<std::string>(python::args("self", "pkl")))
        .def(python::init<unsigned int, bool>(
            python::args("self", "size", "bitsSet")))
        .def("SetBit", (bool(EBV::*)(unsigned int)) & EBV::setBit,
             python::args("self", "which"),
             "Turns on a particular bit.  Returns the original state of the "
             "bit.\n")
        .def("SetBitsFromList",
             (void (*)(EBV *, python::object))SetBitsFromList,
             python::args("self", "onBitList"),
             "Turns on a set of bits.  The argument should be a tuple or list "
             "of bit ids.\n")
        .def("UnSetBit", (bool(EBV::*)(unsigned int)) & EBV::unsetBit,
             python::args("self", "which"),
             "Turns off a particular bit.  Returns the original state of the "
             "bit.\n")
        .def("UnSetBitsFromList",
             (void (*)(EBV *, python::object))UnSetBitsFromList,
             python::args("self", "offBitList"),
             "Turns off a set of bits.  The argument should be a tuple or list "
             "of bit ids.\n")
        .def("GetBit", (bool(EBV::*)(unsigned int) const) & EBV::getBit,
             python::args("self", "which"), "Returns the value of a bit.\n")
        .def("GetNumBits", &EBV::getNumBits, python::args("self"),
             "Returns the number of bits in the vector (the vector's size).\n")
        .def("__len__", &EBV::getNumBits, python::args("self"))
        .def("GetNumOnBits", &EBV::getNumOnBits, python::args("self"),
             "Returns the number of on bits.\n")
        .def("GetNumOffBits", &EBV::getNumOffBits, python::args("self"),
             "Returns the number of off bits.\n")
        .def("__getitem__", (int (*)(const EBV &, int))get_VectItem,
             python::args("self", "which"))
        .def("__setitem__", (int (*)(EBV &, int, int))set_VectItem,
             python::args("self", "which", "val"))
        .def("GetOnBits", (IntVect(*)(const EBV &))GetOnBits,
             python::args("self"),
             "Returns a tuple containing IDs of the on bits.\n")
        .def("ToBinary", (python::object(*)(const EBV &))BVToBinary,
             python::args("self"),
             "Returns an internal binary representation of the vector.\n")
        .def("FromBase64", (void (*)(EBV &, const std::string &))InitFromBase64,
             python::args("self", "inD"),
             "Initializes the vector from a base64 encoded binary string.\n")
        .def("ToBase64", (std::string(*)(EBV &))ToBase64, python::args("self"),
             "Converts the vector to a base64 string (the base64 encoded "
             "version of the results of ToString()).\n")
        .def("ToList", (python::list(*)(const EBV &))ExplicitToList,
             python::args("self"),
             "Return the Bitvector as a python list (faster than list(vect))")
        .def(python::self & python::self)
        .def(python::self | python::self)
        .def(python::self ^ python::self)
        .def(python::self + python::self)
        .def(~python::self)
        .def(python::self == python::self)
        .def(python::self != python::self)
        .def(python::self += python::self)

        .def_pickle(ebv_pickle_suite());
  }
};

void wrap_EBV() { EBV_wrapper::wrap(); }