1function [cnew, depClass, chain] = passAndSwap(c, p, G)
2% [CNEW, DEPCLASS, CHAIN] = PASSANDSWAP(C,
P, G)
4% Applies
the pass-and-swap mechanism (Dorsman & Gardner 2024, Queueing
5% Systems 107:205-256, Sect. 2.3) triggered by
the service completion of
the
6% job in position
P of an order-independent/pass-and-swap (PAS) queue.
9% c - 1 x n ordered state vector of class indices; c(1)
is the oldest job.
10% p - position (1..n) of
the job whose service token completes.
11% G - (nclasses x nclasses) swapping graph adjacency (G(i,j)~=0 iff a class-i
12% job may take
the place of a class-j job; undirected, self-loops allowed).
15% cnew - 1 x (n-1) ordered state vector after
the pass-and-swap transition.
16% depClass - class index of
the job that departs
the system.
17% chain - sequence of positions (in
the original c) visited by
the scan;
18% chain(1)=p
is the vacated slot, c(chain(end))
is the departing job.
20% Mechanism: starting at position p,
the completing job scans backwards (towards
21% newer/later positions) for
the first job whose class
is swappable with its own
22% per G; it takes that job's place, ejecting it. The ejected job repeats
the
23% scan from its new position. The chain ends when an ejected job finds no
24% swappable successor: that job departs. Classes shift one step along
the chain
25% and
the head-of-chain slot
is removed.
27% Copyright (c) 2012-2026, Imperial College London
32 line_error(mfilename, 'Completion position p
is out of range for state c.');
35chain = p; % q0: position of
the completing job (becomes vacated)
36movingCls = c(p); % class currently looking for a swappable successor
47 break; % movingCls finds no swappable successor -> it departs
49 chain(end+1) = q; %
#ok<AGROW>
54depClass = c(chain(end)); %
the last ejected job departs
56% Shift classes one step along
the chain: c(chain(i)) -> chain(i+1).
57% The
class previously at chain(end)
is overwritten (it departed);
the
58% head-of-chain slot chain(1)
is then removed.
60for i = 1:numel(chain)-1
61 cnew(chain(i+1)) = c(chain(i));