Method GetSentinelObject
GetSentinelObject()
This method can be overridden by extending classes to return a sentinel object which will be used by the PriorityQueue(Int32, Boolean) constructor to fill the queue, so that the code which uses that queue can always assume it's full and only change the top without attempting to insert any new object.
Those sentinel values should always compare worse than any non-sentinel value (i.e., LessThan(T, T) should always favor the non-sentinel values).
By default, this method returns false
, which means the queue will not be
filled with sentinel values. Otherwise, the value returned will be used to
pre-populate the queue. Adds sentinel values to the queue.
If this method is extended to return a non-null value, then the following usage pattern is recommended:
// extends GetSentinelObject() to return a non-null value.
PriorityQueue<MyObject> pq = new MyQueue<MyObject>(numHits);
// save the 'top' element, which is guaranteed to not be null.
MyObject pqTop = pq.Top;
<...>
// now in order to add a new element, which is 'better' than top (after
// you've verified it is better), it is as simple as:
pqTop.Change().
pqTop = pq.UpdateTop();
NOTE: if this method returns a non-null
value, it will be called by
the PriorityQueue(Int32, Boolean) constructor
Count times, relying on a new object to be returned and will not
check if it's null
again. Therefore you should ensure any call to this
method creates a new instance and behaves consistently, e.g., it cannot
return null
if it previously returned non-null
.
Declaration
protected virtual T GetSentinelObject()
Returns
Type | Description |
---|---|
T | The sentinel object to use to pre-populate the queue, or |