LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
trace_iat2bins.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_API_TRACE_TRACE_IAT2BINS_H
6#define LINE_API_TRACE_TRACE_IAT2BINS_H
7
8/**
9 * @file
10 * @ingroup api_trace
11 * Bins a trace on a fixed time grid: the number of arrivals falling in each
12 * interval ((i-1)*scale, i*scale], and the bin index of each arrival.
13 *
14 * Templated port of matlab/lib/kpctoolbox/trace/trace_iat2bins.m,
15 * cross-checked against
16 * `jar/src/main/java/jline/api/trace/Trace_var.java#trace_iat2bins` (same
17 * algorithm; the JAR stops one bin earlier because its outer loop runs to
18 * `bins` while MATLAB runs to `bins+1`, so the JAR can drop the arrivals of
19 * the final, partially filled bin).
20 *
21 * Unlike trace_iat2counts, the windows here are non-overlapping and anchored
22 * at the origin, so sum(C) is the number of non-censored arrivals.
23 *
24 * ARITHMETIC: additions and comparisons plus one ceiling division for the bin
25 * count, exact in Rational.
26 */
27
28#include <cstddef>
29#include <vector>
30
32#include "line/num/number.h"
33#include "line/util/error.h"
34
35namespace line {
36namespace trace {
37
38/** Return value of trace_iat2bins, mirroring [C, bC]. */
40 std::vector<long> counts; ///< arrivals per bin
41 std::vector<long> membership; ///< 1-based bin index of each binned arrival
42};
43
44/**
45 * @brief Bins a trace on a fixed time grid: the number of arrivals falling in
46 * each interval ((i-1)*scale, i*scale], and the bin index of each
47 * arrival.
48 *
49 * @param S inter-arrival times
50 * @param scale bin width
51 */
52template <class T>
53TraceBinsResult trace_iat2bins(const std::vector<T>& S, const T& scale) {
54 detail::require_nonempty(S, "trace_iat2bins");
55 if (scale <= num_traits<T>::from_int(0))
56 throw InputError("trace_iat2bins: the bin width must be positive");
57 const long n = static_cast<long>(S.size());
58 const std::vector<T> cs = detail::cumsum0(S); // cs[k] = MATLAB CS(k)
59
60 // bins = ceil((CS(end) - CS(1)) / scale), computed without a real ceil so
61 // that the exact backends stay exact.
62 const T span = cs[static_cast<std::size_t>(n)] - cs[1];
63 long bins = 0;
64 while (num_traits<T>::from_int(bins) * scale < span) ++bins;
65
67 long cur = 1, last = 0;
68 for (long i = 1; i <= bins + 1; ++i) {
69 if (cur == n) break;
70 while (cs[static_cast<std::size_t>(cur + 1)] <= num_traits<T>::from_int(i) * scale) {
71 ++cur;
72 if (cur == n) break;
73 }
74 if (static_cast<long>(out.counts.size()) < i) out.counts.resize(static_cast<std::size_t>(i), 0);
75 out.counts[static_cast<std::size_t>(i - 1)] = cur - last;
76 for (long k = 0; k < cur - last; ++k) out.membership.push_back(i);
77 last = cur;
78 }
79 if (static_cast<long>(out.counts.size()) < bins) out.counts.resize(static_cast<std::size_t>(bins), 0);
80 return out;
81}
82
83} // namespace trace
84} // namespace line
85
86#endif // LINE_API_TRACE_TRACE_IAT2BINS_H
InputError(const std::string &what)
Definition error.h:39
The exception types the port throws.
TraceBinsResult trace_iat2bins(const std::vector< T > &S, const T &scale)
Bins a trace on a fixed time grid: the number of arrivals falling in each interval ((i-1)*scale,...
Number-type abstraction for the templated API port.
Return value of trace_iat2bins, mirroring [C, bC].
std::vector< long > counts
arrivals per bin
std::vector< long > membership
1-based bin index of each binned arrival
Shared declarations for the empirical trace statistics domain.