20#define QPOT_WARNING_IMPL(message, file, line, function) \
21 std::cout << std::string(file) + ":" + std::to_string(line) + " (" + std::string(function) + \
22 ")" + ": " message ") \n\t";
24#define QPOT_ASSERT_IMPL(expr, file, line, function) \
26 throw std::runtime_error( \
27 std::string(file) + ":" + std::to_string(line) + " (" + std::string(function) + ")" + \
28 ": assertion failed (" #expr ") \n\t"); \
51#ifdef QPOT_ENABLE_ASSERT
52#define QPOT_ASSERT(expr) QPOT_ASSERT_IMPL(expr, __FILE__, __LINE__, __FUNCTION__)
54#define QPOT_ASSERT(expr)
70#ifdef QPOT_ENABLE_DEBUG
71#define QPOT_DEBUG(expr) QPOT_ASSERT_IMPL(expr, __FILE__, __LINE__, __FUNCTION__)
73#define QPOT_DEBUG(expr)
81#define QPOT_REQUIRE(expr) QPOT_ASSERT_IMPL(expr, __FILE__, __LINE__, __FUNCTION__)
92#ifdef QPOT_ENABLE_WARNING_PYTHON
93#define QPOT_WARNING_PYTHON(message) QPOT_WARNING_IMPL(message, __FILE__, __LINE__, __FUNCTION__)
95#define QPOT_WARNING_PYTHON(message)
118#define QPOT_VERSION "@PROJECT_VERSION@"
128inline std::string unquote(
const std::string& arg)
130 std::string ret = arg;
131 ret.erase(std::remove(ret.begin(), ret.end(),
'\"'), ret.end());
146 return detail::unquote(std::string(QUOTE(
QPOT_VERSION)));
161 std::vector<std::string> ret;
163 ret.push_back(
"qpot=" +
version());
166 "xtensor=" + detail::unquote(std::string(QUOTE(XTENSOR_VERSION_MAJOR))) +
"." +
167 detail::unquote(std::string(QUOTE(XTENSOR_VERSION_MINOR))) +
"." +
168 detail::unquote(std::string(QUOTE(XTENSOR_VERSION_PATCH))));
170#ifdef XSIMD_VERSION_MAJOR
172 "xsimd=" + detail::unquote(std::string(QUOTE(XSIMD_VERSION_MAJOR))) +
"." +
173 detail::unquote(std::string(QUOTE(XSIMD_VERSION_MINOR))) +
"." +
174 detail::unquote(std::string(QUOTE(XSIMD_VERSION_PATCH))));
177#ifdef XTL_VERSION_MAJOR
179 "xtl=" + detail::unquote(std::string(QUOTE(XTL_VERSION_MAJOR))) +
"." +
180 detail::unquote(std::string(QUOTE(XTL_VERSION_MINOR))) +
"." +
181 detail::unquote(std::string(QUOTE(XTL_VERSION_PATCH))));
184#if defined(XTENSOR_PYTHON_VERSION_MAJOR)
186 "xtensor-python=" + detail::unquote(std::string(QUOTE(XTENSOR_PYTHON_VERSION_MAJOR))) +
187 "." + detail::unquote(std::string(QUOTE(XTENSOR_PYTHON_VERSION_MINOR))) +
"." +
188 detail::unquote(std::string(QUOTE(XTENSOR_PYTHON_VERSION_PATCH))));
191 std::sort(ret.begin(), ret.end(), std::greater<std::string>());
212template <
class It,
class T,
class R =
size_t>
214lower_bound(
const It first,
const It last,
const T& value, R guess = 0,
size_t proximity = 10)
216 if (proximity == 0) {
217 if (value <= *(first)) {
223 if (*(first + guess) < value && value <= *(first + guess + 1)) {
227 size_t l = guess > proximity ? guess - proximity : 0;
228 size_t r = std::min(guess + proximity,
static_cast<size_t>(last - first - 1));
230 if (*(first + l) < value && *(first + r) >= value) {
233 else if (value <= *(first)) {
253template <
class T,
class V,
class R>
254inline void lower_bound(
const T& matrix,
const V& value, R& index,
size_t proximity = 10)
256 QPOT_ASSERT(value.dimension() == matrix.dimension() - 1);
257 QPOT_ASSERT(value.dimension() == index.dimension());
259 auto nd = value.dimension();
260 auto stride = matrix.shape(nd);
261 auto n = value.size();
263#ifdef QPOT_ENABLE_ASSERT
264 for (
decltype(nd) i = 0; i < nd; ++i) {
270 for (
decltype(n) i = 0; i < n; ++i) {
272 &matrix.flat(i * stride),
273 &matrix.flat(i * stride) + stride,
287template <
class V,
class I>
291 QPOT_ASSERT(cumsum.dimension() == delta.dimension());
293 if (delta.size() == 0) {
297 size_t dim = cumsum.dimension();
298 size_t n = cumsum.shape(dim - 1);
299 size_t ndelta = delta.shape(dim - 1);
301 for (
size_t i = 0; i < shift.size(); ++i) {
303 if (shift.flat(i) == 0) {
307 auto* d = &delta.flat(i * ndelta);
308 auto* c = &cumsum.flat(i * n);
310 if (shift.flat(i) > 0) {
313 size_t nadd =
static_cast<size_t>(shift.flat(i));
314 size_t nkeep = n - nadd;
315 auto offset = *(c + n - 1);
316 std::copy(c + n - nkeep, c + n, c);
317 std::copy(d, d + nadd, c + nkeep);
318 *(c + nkeep) += offset;
319 std::partial_sum(c + nkeep, c + n, c + nkeep);
324 size_t nadd =
static_cast<size_t>(-shift.flat(i));
325 size_t nkeep = n - nadd;
327 std::copy(c, c + nkeep, c + nadd);
328 std::copy(d, d + nadd + 1, c);
329 std::partial_sum(c, c + nadd + 1, c);
330 offset -= *(c + nadd);
331 std::transform(c, c + nadd + 1, c, [&](
auto& v) {
return v + offset; });
352template <
class T,
class V,
class R>
353inline R
lower_bound(
const T& matrix,
const V& value,
const R& index,
size_t proximity = 10)
369template <
class T,
class V,
class R>
372 R ret = xt::zeros<typename R::value_type>(value.shape());
413template <
class V,
class I>
#define QPOT_ASSERT(expr)
All assertions are implemented as:
#define QPOT_VERSION
Current version.
Function to find items in an increase range, and to store this range in chunks.
R lower_bound(const T &matrix, const V &value)
Iterating on the last axis of an nd-array (e.g.
std::string version()
Return version string, e.g.
std::vector< std::string > version_dependencies()
Return versions of this library and of all of its dependencies.
R lower_bound(const T &matrix, const V &value, const R &index, size_t proximity=10)
Iterating on the last axis of an nd-array (e.g.
V cumsum_chunk(const V &cumsum, const V &delta, const I &shift)
Update the chunk of a cumsum computed and stored in chunks.