// $Id$ // // Copyright (C) 2007 Greg Landrum // // @@ All Rights Reserved @@ // #include #include #include #include #include using namespace RDKit; template struct siv_pickle_suite : python::pickle_suite { static python::tuple getinitargs(const SparseIntVect& self) { return python::make_tuple(self.toString()); }; }; namespace { template void pyUpdateFromSequence(SparseIntVect &vect, python::object &seq){ PySequenceHolder seqL(seq); for(unsigned int i=0;i python::dict pyGetNonzeroElements(SparseIntVect &vect){ python::dict res; typename SparseIntVect::StorageType::const_iterator iter=vect.getNonzeroElements().begin(); while(iter!=vect.getNonzeroElements().end()){ res[iter->first]=iter->second; ++iter; } return res; } } std::string sparseIntVectDoc="A container class for storing integer\n\ values within a particular range.\n\ \n\ The length of the vector is set at construction time.\n\ \n\ As you would expect, _SparseIntVects_ support a set of binary operations\n\ so you can do things like:\n\ Arithmetic:\n\ siv1 += siv2\n\ siv3 = siv1 + siv2\n\ siv1 -= siv3\n\ siv3 = siv1 - siv2\n\ \"Fuzzy\" binary operations:\n\ siv3 = siv1 & siv2 the result contains the smallest value in each entry\n\ siv3 = siv1 | siv2 the result contains the largest value in each entry\n\ \n\ Elements can be set and read using indexing (i.e. bv[i] = 4 or val=bv[i])\n\ \n"; struct sparseIntVec_wrapper { template static void wrapOne(const char *className){ python::class_ >(className, sparseIntVectDoc.c_str(), python::init("Constructor")) .def(python::init()) // Note: we cannot support __len__ because, at least at the moment // (BPL v1.34.1), it must return an int. .def("__setitem__", &SparseIntVect::setVal, "Set the value at a specified location") .def("__getitem__", &SparseIntVect::getVal, "Get the value at a specified location") .def(python::self & python::self) .def(python::self | python::self) .def(python::self - python::self) .def(python::self -= python::self) .def(python::self + python::self) .def(python::self += python::self) .def(python::self == python::self) .def(python::self != python::self) .def("GetTotalVal", &SparseIntVect::getTotalVal, (python::args("useAbs")=false), "Get the sum of the values in the vector, basically L1 norm") .def("GetLength", &SparseIntVect::getLength, (python::args("useAbs")=false), "Returns the length of the vector") .def("ToBinary", &SparseIntVect::toString, "returns a binary (pickle) representation of the vector") .def("UpdateFromSequence", &pyUpdateFromSequence, "update the vector based on the values in the list or tuple") .def("GetNonzeroElements", &pyGetNonzeroElements, "returns a dictionary of the nonzero elements") .def_pickle(siv_pickle_suite()) ; } static void wrap() { wrapOne("IntSparseIntVect"); wrapOne("LongSparseIntVect"); } }; void wrap_sparseIntVect() { sparseIntVec_wrapper::wrap(); }