LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
trace_iat2counts.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_IAT2COUNTS_H
6#define LINE_API_TRACE_TRACE_IAT2COUNTS_H
7
8/**
9 * @file
10 * @ingroup api_trace
11 * Counting process of a trace: the number of arrivals in the window of length
12 * `scale` that starts at each arrival epoch.
13 *
14 * Templated port of matlab/lib/kpctoolbox/trace/trace_iat2counts.m,
15 * cross-checked against
16 * `jar/src/main/java/jline/api/trace/Trace_var.java#trace_iat2counts`.
17 *
18 * With CS the cumulative arrival epochs, MATLAB advances `cur` while
19 * CS(cur+1) - CS(i) <= scale, i.e. it measures the window from the epoch of
20 * the i-th arrival, and returns cur-i arrivals. The series is truncated at
21 * the first window that reaches the end of the trace, because from there on
22 * the count is censored.
23 *
24 * DIVERGENCE, MATLAB vs JAR: the JAR compares CS[cur+1]-CS[i] with its CS
25 * indexed from 0, where CS[i] is the epoch of arrival i-1, not i. Its window
26 * therefore starts one arrival too early and its counts are shifted by one
27 * index relative to MATLAB. MATLAB is the reference here.
28 *
29 * ARITHMETIC: only additions and comparisons, exact in Rational.
30 */
31
32#include <cstddef>
33#include <vector>
34
36#include "line/num/number.h"
37#include "line/util/error.h"
38
39namespace line {
40namespace trace {
41
42/**
43 * @brief Counting process of a trace: the number of arrivals in the window of
44 * length `scale` that starts at each arrival epoch.
45 *
46 * @param S inter-arrival times
47 * @param scale window length
48 * @return counts, one per arrival, truncated at the first censored window
49 */
50template <class T>
51std::vector<long> trace_iat2counts(const std::vector<T>& S, const T& scale) {
52 detail::require_nonempty(S, "trace_iat2counts");
53 if (scale <= num_traits<T>::from_int(0))
54 throw InputError("trace_iat2counts: the time scale must be positive");
55 const long n = static_cast<long>(S.size());
56 const std::vector<T> cs = detail::cumsum0(S); // cs[k] = CS(k) of MATLAB
57
58 std::vector<long> C;
59 C.reserve(static_cast<std::size_t>(n > 0 ? n - 1 : 0));
60 for (long i = 1; i <= n - 1; ++i) {
61 long cur = i;
62 while (cs[static_cast<std::size_t>(cur + 1)] - cs[static_cast<std::size_t>(i)] <= scale) {
63 ++cur;
64 if (cur == n) { // the window has reached the end of the trace
65 C.push_back(cur - i);
66 return C;
67 }
68 }
69 C.push_back(cur - i);
70 }
71 return C;
72}
73
74} // namespace trace
75} // namespace line
76
77#endif // LINE_API_TRACE_TRACE_IAT2COUNTS_H
InputError(const std::string &what)
Definition error.h:39
The exception types the port throws.
std::vector< long > trace_iat2counts(const std::vector< T > &S, const T &scale)
Counting process of a trace: the number of arrivals in the window of length scale that starts at each...
Number-type abstraction for the templated API port.
Shared declarations for the empirical trace statistics domain.