-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutility.cpp
More file actions
41 lines (37 loc) · 1.5 KB
/
utility.cpp
File metadata and controls
41 lines (37 loc) · 1.5 KB
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
#include "utility.h"
dist::DissimilarityMeasure obj2m(py::str o) {
auto s = py::cast<std::string>(py::cast<py::str>(o));
for(auto m: dist::USABLE_MEASURES) if(!std::strcmp(dist::prob2str(m), s.data())) return m;
throw std::invalid_argument("invalid key");
}
dist::DissimilarityMeasure obj2m(py::int_ o) {
auto i = py::cast<py::ssize_t>(o);
if(!dist::is_valid_measure((dist::DissimilarityMeasure)i)) {
std::fprintf(stderr, "Warning: measure %td is not considered valid. Downstream work may break.\n", std::ptrdiff_t(i));
}
return static_cast<dist::DissimilarityMeasure>(i);
}
coresets::SensitivityMethod obj2sm(py::str o) {
auto s = py::cast<std::string>(py::cast<py::str>(o));
for(auto m: coresets::CORESET_CONSTRUCTIONS)
if(s == coresets::sm2str(m)) return m;
throw std::invalid_argument("invalid key");
}
coresets::SensitivityMethod obj2sm(py::int_ o) {
auto i = py::cast<py::ssize_t>(o);
return static_cast<coresets::SensitivityMethod>(i);
}
dist::DissimilarityMeasure obj2m(py::object o) {
if(py::isinstance<py::str>(o))
return obj2m(py::cast<py::str>(o));
if(py::isinstance<py::int_>(o))
return obj2m(py::cast<py::int_>(o));
throw std::runtime_error("Unexpected object");
}
coresets::SensitivityMethod obj2sm(py::object o) {
if(py::isinstance<py::str>(o))
return obj2sm(py::cast<py::str>(o));
if(py::isinstance<py::int_>(o))
return obj2sm(py::cast<py::int_>(o));
throw std::runtime_error("Unexpected object");
}