Creating and accessing frozenset using Python Boost

I have some C ++ methods that have std::set<std::string> as an argument or return value. I would like to match this with Python frozenset (or regular set ), but there seems to be no easy way to do this. Does anyone know how to accomplish this task.

+4
source share
2 answers

Unfortunately, the standard indexing_suite from Boost.Python does not support std::set . There is indexing_suite v2 that works in all stl containers. (Http://mail.python.org/pipermail/cplusplus-sig/2009-July/014704.html)

It may not have gotten into the official distribution, but you can find it by asking around. (Http://mail.python.org/pipermail/cplusplus-sig/2009-July/014691.html)

It was harder for me to use the original indexing_suite , but it could fit your needs.

If this does not work, you can simply wrap std::set<std::string> manually, just like any other class. This will give you std::set<std::string> in python, where you can easily turn it into a python set .


I think both of them are more work, but this is required. Here is what I will do:

First, wrap a C ++ function with one that has the same signature, but load the returned data into std::vector<std::string> instead of std::set<std::string> . set this function, not the original

You now have data in python.

Secondly, wrap the C ++ function in a python function that takes the data in std::vector<std::string> and inserts it into the python set .

Yes, this is pretty stupid in terms of design aesthetics, not the most advanced code in the world, but you get to where you go with minimal code, and it's pretty reliable.

0
source

Or you can use std::map<YourType, int> instead of std::set<YourType> , the value can be, for example, 0. 0. std :: map has the same insertion / search time complexity as std :: set, it also stores the ordered keys, it inflates the memory a bit. Then you can use a map indexing set, and in python you can hide the difference in some wrapper class if necessary. The downside is that you need to slightly modify the existing C ++ code.

0
source

Source: https://habr.com/ru/post/1339050/


All Articles