inline void init_LabelSetView()

in python/bindings.cpp [145:187]


inline void init_LabelSetView(py::module &m) {
  py::class_<LabelSetView>(
      m, "LabelSetView",
      "A read-only set-like object containing 64-bit integers. Use this object "
      "like a regular Python :py:class:`set` object, by either iterating "
      "through it, or checking for membership with the ``in`` operator.")
      .def("__repr__",
           [](LabelSetView &self) {
             std::ostringstream ss;
             ss << "<voyager.LabelSetView";
             ss << " num_elements=" << self.map.size();
             ss << " at " << &self;
             ss << ">";
             return ss.str();
           })
      .def("__len__", [](LabelSetView &self) { return self.map.size(); })
      .def("__iter__",
           [](LabelSetView &self) {
             // Python iterates much faster through a list of longs
             // than when jumping back and forth between Python and C++
             // every time next(iter(...)) is called.
             std::vector<hnswlib::labeltype> ids;
             {
               py::gil_scoped_release release;
               ids.reserve(self.map.size());
               for (auto const &kv : self.map) {
                 ids.push_back(kv.first);
               }
             }

             return py::cast(ids).attr("__iter__")();
           })
      .def(
          "__contains__",
          [](LabelSetView &self, hnswlib::labeltype element) {
            return self.map.find(element) != self.map.end();
          },
          py::arg("id"))
      .def(
          "__contains__",
          [](LabelSetView &, const py::object &) { return false; },
          py::arg("id"));
}