4 ms·
One that a professor of mine used when I was in grad school which I don't see that often these days is the use of sentinel values. For arrays/lists of unknown s
by msluyter 4y ago
One that a professor of mine used when I was in grad school which I don't see that often these days is the use of sentinel values. For arrays/lists of unknown sizes, it's sometimes nice to do something like:
while val != SENTINEL:
# ...stuff...
val = some_iterator()
You just have to be careful that you can never have real data equal the sentinel.
A variation on this can be used to stop queue processing in a multi-threaded/processing environment. You can put N sentinels (aka, poison) in the queue for N readers, and when a reader gets the poison, it quits.
- pwinnski 4y agoSentinel values are too often found in real data eventually, which is why they're most often used in academic environments, and by fresh grads. First or second time the sentinel value that could never possibly be found in real data is found in real data, you start looking for other solutions.
- CRConrad 4y agoBack in the 1980s, the statistics package -- application, really, but this was before that word had become wide-spread in academic environments for that sort of thing -- Statgraphics used -32768[1] for "Missing Value", i.e. "NULL". Had to fudge our actual -32768 values to -32767. ___ [1]: The signed-integer value of a word full of set bits ('1' values) on a 16-bit twos-complement processor like the Intel 80286? Something like that.
- vivegi 4y ago'\0' (NUL) terminated strings in C is the idiomatic example for sentinel values.