As per Wikipedia,
In computer programming, a sentinel value (also referred to as a flag value, trip value, rogue value, signal value, or dummy data) is a special value in the context of an algorithm which uses its presence as a condition of termination, typically in a loop or recursive algorithm.
A common example is the use of -1
at the end of a positive integer array to denote the end of data when the array is larger than the data that fills it.
However, what about when we use -1
not to ensure termination, but simply as an impossible value? For example:
# `a` is an array with the numbers from 0 to 4, in random order.
# We will use `b` to track where in `a` each number is. The position in `b`
# denotes the number, and the value denotes the index in `a`.
# `calculate_indices()` is a function that does this.
a = [3, 4, 1, 0, 2]
b = [-1, -1, -1, -1, -1]
calculate_indices(a, b)
print(b) => [3, 2, 4, 0, 1]
If we were to initialize b
as [0, 0, 0, 0, 0]
, this would not work, as 0
has a real meaning here - i.e., the 0th position in a
. Therefore we use an impossible value, -1
.
Is there a name for this sort of usage?
As per Carcigenicate, and after some more research, it would appear that Wikipedia's definition is indeed too strict, and that this can also be referred to as a sentinel. I cannot find another name for this pattern.