Class \Prado\Collections\TPriorityList
TPriorityList implements a priority ordered list collection class. It allows you to specify any numeric for priorities down to a specific precision. The lower the numeric, the high the priority of the item in the list. Thus -10 has a higher priority than -5, 0, 10 (the default), 18, 10005, etc. Per round, precision may be negative and thus rounding can go by 10, 100, 1000, etc, instead of just .1, .01, .001, etc. The default precision allows for 8 decimal places. There is also a default priority of 10, if no different default priority is specified or no item specific priority is indicated. If you replace TList with this class it will work exactly the same with items inserted set to the default priority, until you start using different priorities than the default priority.
As you access the PHP array features of this class, it flattens and caches the results. If at all possible, this will keep the cache fresh even when manipulated. If this is not possible the cache is cleared. When an array of items are needed and the cache is outdated, the cache is recreated from the items and their priorities
You can access, append, insert, remove an item by using itemAt, add, insertAt, and remove. To get the number of the items in the list, use getCount. TPriorityList can also be used like a regular array as follows,
$list[]=$item; // append with the default priority. It may not be the last item if other items in the list are prioritized after the default priority
$list[$index]=$item; // $index must be between 0 and $list->Count-1. This sets the element regardless of priority. Priority stays the same.
$list[$index]=$item; // $index is $list->Count. This appends the item to the end of the list with the same priority as the last item in the list.
unset($list[$index]); // remove the item at $index
if(isset($list[$index])) // if the list has an item at $index
foreach($list as $index=>$item) // traverse each item in the list in proper priority order and add/insert order
$n=count($list); // returns the number of items in the list
To extend TPriorityList for doing your own operations with each addition or removal, override insertAtIndexInPriority() and removeAtIndexInPriority() and then call the parent.
Class hierarchy
- \Prado\Collections\TPriorityList implements IPriorityCollection uses TPriorityCollectionTrait
- \Prado\Collections\TList implements IteratorAggregate, ArrayAccess, Countable
- \Prado\TComponent
Since: 3.2a
public
|
__construct([null|array<string|int, mixed>|Iterator $data = null ][, bool|null $readOnly = null ][, numeric-string|int|float|null $defaultPriority = null ][, int|null $precision = null ]) : mixed
Constructor.
Initializes the list with an array or an iterable object. |
public
|
add(mixed $item[, null|(numeric) $priority = null ]) : int
Appends an item into the list at the end of the specified priority. The position of the added item may
not be at the end of the list.
|
public
|
clear() : void
Removes all items in the priority list by calling removeAtIndexInPriority from the last item to the first.
|
public
|
copyFrom(mixed $data) : void
Copies iterable data into the priority list.
Note, existing data in the map will be cleared first. |
public
|
|
public
|
insertAfter(mixed $indexitem, mixed $item) : int
This inserts an item after another item within the list. It uses the same priority as the
found index item and places the new item after it.
|
public
|
insertAt(int $index, mixed $item) : float|null
Inserts an item at an index. It reads the priority of the item at index within the flattened list
and then inserts the item at that priority-index.
|
public
|
insertAtIndexInPriority(mixed $item[, null|false|int $index = null ][, null|(numeric) $priority = null ][, bool $preserveCache = false ]) : mixed
Inserts an item at the specified index within a priority. Override and call this method to
insert your own functionality.
|
public
|
insertBefore(mixed $indexitem, mixed $item) : int
This inserts an item before another item within the list. It uses the same priority as the
found index item and places the new item before it.
|
public
|
itemAt(int $index) : mixed
Returns the item at the index of a flattened priority list.
offsetGet calls this method. |
public
|
itemAtIndexInPriority(int $index[, null|(numeric) $priority = null ]) : mixed
Returns the item at an index within a priority
|
public
|
mergeWith(mixed $data) : void
Merges iterable data into the priority list.
New data will be appended to the end of the existing data. If another TPriorityList is merged, the incoming parameter items will be appended at the priorities they are present. These items will be added to the end of the existing items with equal priorities, if there are any. |
public
|
offsetExists(mixed $offset) : bool
Returns whether there is an element at the specified offset.
This method is required by the interface \ArrayAccess. |
public
|
offsetSet(int $offset, mixed $item) : void
Sets the element at the specified offset. This method is required by the interface \ArrayAccess.
Setting elements in a priority list is not straight forword when appending and setting at the end boundary. When appending without an offset (a null offset), the item will be added at the default priority. The item may not be the last item in the list. When appending with an offset equal to the count of the list, the item will get be appended with the last items priority. All together, when setting the location of an item, the item stays in that location, but appending an item into a priority list doesn't mean the item is at the end of the list. |
public
|
offsetUnset(mixed $offset) : void
Unsets the element at the specified offset.
This method is required by the interface \ArrayAccess. |
public
|
priorityAt(int $index[, bool $withindex = false ]) : array<string|int, mixed>|false|(numeric)
Returns the priority of an item at a particular flattened index. The index after
the last item does not exist but receives a priority from the last item so that
priority information about any new items being appended is available.
|
public
|
priorityOf(mixed $item[, bool $withindex = false ]) : array<string|int, mixed>|false|(numeric)
Returns the priority of a particular item
|
public
|
remove(mixed $item[, null|bool|float $priority = false ]) : int
Removes an item from the priority list.
The list will search for the item. The first matching item found will be removed from the list. |
public
|
|
public
|
removeAtIndexInPriority(int $index[, null|(numeric) $priority = null ]) : mixed
Removes the item at a specific index within a priority. Override
and call this method to insert your own functionality.
|
protected
|
_getZappableSleepProps(array<string|int, mixed> &$exprops) : mixed
Returns an array with the names of all variables of this object that should NOT be serialized
because their value is the default one or useless to be cached for the next page loads.
Reimplement in derived classes to add new variables, but remember to also to call the parent implementation first. |
private
|
getPriorityCombineStyle() : bool
This is required for TPriorityCollectionTrait to determine the style of combining
arrays.
|
\Prado\TComponent::GLOBAL_RAISE_EVENT_LISTENER |