Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Sentinel value

From Wikipedia, the free encyclopedia
In-band data value that must be handled specially by computer code
Not to be confused withsentinel node orcanary value.

Incomputer programming, asentinel value (also referred to as aflag value,trip value,rogue value,signal value, ordummy data) is a specialvalue in the context of analgorithm which uses its presence as a condition of termination, typically in aloop or recursive algorithm.

The sentinel value is a form ofin-band data that makes it possible to detect the end of the data when noout-of-band data (such as an explicit size indication) is provided. The value should be selected in such a way that it is guaranteed to be distinct from all legal data values since otherwise, the presence of such values would prematurely signal the end of the data (thesemipredicate problem). A sentinel value is sometimes known as an "Elephant in Cairo", due to a joke where this is used as a physical sentinel. In safe languages, most sentinel values could be replaced withoption types, which enforce explicit handling of the exceptional case.

Examples

[edit]

Some examples of common sentinel values and their uses:

Variants

[edit]

A related practice, used in slightly different circumstances, is to place some specific value at the end of the data, in order to avoid the need for an explicit test for termination in some processing loop, because the value will trigger termination by the tests already present for other reasons. Unlike the above uses, this is not how the data is naturally stored or processed, but is instead an optimization, compared to the straightforward algorithm that checks for termination. This is typically used in searching.[1][2]

For instance, when searching for a particular value in an unsortedlist, every element will be compared against this value, with the loop terminating when equality is found; however, to deal with the case that the value should be absent, one must also test after each step for having completed the search unsuccessfully. By appending the value searched for to the end of the list, an unsuccessful search is no longer possible, and no explicit termination test is required in theinner loop. After the search, one must decide whether a true match was found, but this test needs to be performed only once rather than at each iteration.[3] Knuth calls the value so placed at the end of the data, adummy value rather than a sentinel.

Examples

[edit]

Array

[edit]

For example, if searching for a value in an array in C, a straightforward implementation is as follows; note the use of a negative number (invalid index) to solve the semipredicate problem of returning "no result":

intfind(intarr[],size_tlen,intval){for(inti=0;i<len;i++)if(arr[i]==val)returni;return-1;// not found}

However, this does two tests at each iteration of the loop: whether the value has been found and whether the end of the array has been reached. This latter test is what is avoided by using a sentinel value. Assuming the array can be extended by one element (without memory allocation or cleanup; this is more realistic for a linked list, as below), this can be rewritten as:

intfind(intarr[],size_tlen,intval){inti;arr[len]=val;// add sentinel valuefor(i=0;;i++)if(arr[i]==val)break;if(i<len)returni;elsereturn-1;// not found}

The test fori < len is still present, but it has been moved outside the loop, which now contains only a single test (for the value), and is guaranteed to terminate due to the sentinel value. There is a single check on termination if the sentinel value has been hit, which replaces a test for each iteration.

It is also possible to temporarilyreplace the last element of the array by a sentinel and handle it, especially if it is reached:

intfind(intarr[],size_tlen,intval){intlast;if(len==0)return-1;last=arr[len-1];arr[len-1]=val;// add sentinel valueinti;for(i=0;;i++)if(arr[i]==val)break;arr[len-1]=last;if(arr[i]==val)returni;elsereturn-1;// not found}

See also

[edit]

References

[edit]
  1. ^Mehlhorn, Kurt;Sanders, Peter (2008)."3. Representing Sequences by Arrays and Linked Lists"(PDF).Algorithms and Data Structures: The Basic Toolbox. Springer. p. 63.ISBN 978-3-540-77977-3.
  2. ^McConnell, Steve (2004).Code Complete (2nd ed.). Redmond: Microsoft Press. p. 621.ISBN 0-7356-1967-0.
  3. ^Knuth, Donald (1973).Sorting and searching.The Art of Computer Programming. Vol. 3.Addison-Wesley. p. 395.ISBN 0-201-03803-X.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Sentinel_value&oldid=1273553828"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp