PHP Класс 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 {@link 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 {@link itemAt}, {@link add}, {@link insertAt}, and {@link remove}. To get the number of the items in the list, use {@link 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 {@link insertAtIndexInPriority()} and {@link removeAtIndexInPriority()} and then call the parent.
С версии: 3.2a
Автор: Brad Anderson ([email protected])
Наследование: extends TList
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
__construct ( $data = null, $readOnly = false, $defaultPriority = 10, $precision = 8 ) Constructor.
add ( $item, $priority = null ) : integer 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.
clear ( ) Removes all items in the priority list by calling removeAtIndexInPriority from the last item to the first.
contains ( $item ) : boolean
copyFrom ( $data ) Copies iterable data into the priority list.
count ( ) : integer Returns the number of items in the list.
getCount ( ) : integer Returns the total number of items in the list
getDefaultPriority ( ) : numeric
getIterator ( ) : Iterator Returns an iterator for traversing the items in the list.
getPrecision ( ) : integer
getPriorities ( ) : array This returns a list of the priorities within this list, ordered lowest to highest.
getPriorityCount ( $priority = null ) : integer Gets the number of items at a priority within the list
indexOf ( $item ) : integer
insertAfter ( $indexitem, $item ) : integer 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.
insertAt ( $index, $item ) 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.
insertAtIndexInPriority ( $item, $index = false, $priority = null, $preserveCache = false ) Inserts an item at the specified index within a priority. Override and call this method to insert your own functionality.
insertBefore ( $indexitem, $item ) : integer 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.
itemAt ( $index ) : mixed Returns the item at the index of a flattened priority list.
itemAtIndexInPriority ( $index, $priority = null ) : mixed Returns the item at an index within a priority
itemsAtPriority ( $priority = null ) : array Gets all the items at a specific priority.
mergeWith ( $data ) Merges iterable data into the priority list.
offsetExists ( $offset ) : boolean Returns whether there is an element at the specified offset.
offsetGet ( $offset ) : mixed Returns the element at the specified offset.
offsetSet ( $offset, $item ) Sets the element at the specified offset. This method is required by the interface \ArrayAccess.
offsetUnset ( $offset ) Unsets the element at the specified offset.
priorityAt ( $index, $withindex = false ) : numeric | array Retutrns the priority of an item at a particular flattened index.
priorityOf ( $item, $withindex = false ) : numeric | array Returns the priority of a particular item
remove ( $item, $priority = false ) : integer Removes an item from the priority list.
removeAt ( $index ) : mixed Removes an item at the specified index in the flattened list.
removeAtIndexInPriority ( $index, $priority = null ) : mixed Removes the item at a specific index within a priority. Override and call this method to insert your own functionality.
toArray ( ) : array
toArrayAbovePriority ( $priority, $inclusive = true ) : array Combines the map elements which have a priority above the parameter value
toArrayBelowPriority ( $priority, $inclusive = false ) : array Combines the map elements which have a priority below the parameter value
toPriorityArray ( ) : array

Защищенные методы

Метод Описание
flattenPriorities ( ) : array This flattens the priority list into a flat array [0,.
setDefaultPriority ( $value ) This must be called internally or when instantiated.
setPrecision ( $value ) This must be called internally or when instantiated.
sortPriorities ( ) This orders the priority list internally.

Описание методов

__construct() публичный Метод

Initializes the list with an array or an iterable object.
public __construct ( $data = null, $readOnly = false, $defaultPriority = 10, $precision = 8 )

add() публичный Метод

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 add ( $item, $priority = null ) : integer
Результат integer the index within the flattened array

clear() публичный Метод

Removes all items in the priority list by calling removeAtIndexInPriority from the last item to the first.
public clear ( )

contains() публичный Метод

public contains ( $item ) : boolean
Результат boolean whether the list contains the item

copyFrom() публичный Метод

Note, existing data in the map will be cleared first.
public copyFrom ( $data )

count() публичный Метод

This method is required by \Countable interface.
public count ( ) : integer
Результат integer number of items in the list.

flattenPriorities() защищенный Метод

..,n-1]
protected flattenPriorities ( ) : array
Результат array array of items in the list in priority and index order

getCount() публичный Метод

Returns the total number of items in the list
public getCount ( ) : integer
Результат integer the number of items in the list

getDefaultPriority() публичный Метод

public getDefaultPriority ( ) : numeric
Результат numeric gets the default priority of inserted items without a specified priority

getIterator() публичный Метод

This method is required by the interface \IteratorAggregate.
public getIterator ( ) : Iterator
Результат Iterator an iterator for traversing the items in the list.

getPrecision() публичный Метод

public getPrecision ( ) : integer
Результат integer The precision of numeric priorities, defaults to 8

getPriorities() публичный Метод

This returns a list of the priorities within this list, ordered lowest to highest.
public getPriorities ( ) : array
Результат array the array of priority numerics in decreasing priority order

getPriorityCount() публичный Метод

Gets the number of items at a priority within the list
public getPriorityCount ( $priority = null ) : integer
Результат integer the number of items in the list at the specified priority

indexOf() публичный Метод

public indexOf ( $item ) : integer
Результат integer the index of the item in the flattened list (0 based), -1 if not found.

insertAfter() публичный Метод

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 insertAfter ( $indexitem, $item ) : integer
Результат integer where the item has been inserted in the flattened list

insertAt() публичный Метод

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 insertAt ( $index, $item )

insertAtIndexInPriority() публичный Метод

Inserts an item at the specified index within a priority. Override and call this method to insert your own functionality.
public insertAtIndexInPriority ( $item, $index = false, $priority = null, $preserveCache = false )

insertBefore() публичный Метод

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 insertBefore ( $indexitem, $item ) : integer
Результат integer where the item has been inserted in the flattened list

itemAt() публичный Метод

{@link offsetGet} calls this method.
public itemAt ( $index ) : mixed
Результат mixed the element at the offset

itemAtIndexInPriority() публичный Метод

Returns the item at an index within a priority
public itemAtIndexInPriority ( $index, $priority = null ) : mixed
Результат mixed the element at the offset, false if no element is found at the offset

itemsAtPriority() публичный Метод

Gets all the items at a specific priority.
public itemsAtPriority ( $priority = null ) : array
Результат array all items at priority in index order, null if there are no items at that priority

mergeWith() публичный Метод

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 mergeWith ( $data )

offsetExists() публичный Метод

This method is required by the interface \ArrayAccess.
public offsetExists ( $offset ) : boolean
Результат boolean

offsetGet() публичный Метод

This method is required by the interface \ArrayAccess.
public offsetGet ( $offset ) : mixed
Результат mixed the element at the offset, null if no element is found at the offset

offsetSet() публичный Метод

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 offsetSet ( $offset, $item )

offsetUnset() публичный Метод

This method is required by the interface \ArrayAccess.
public offsetUnset ( $offset )

priorityAt() публичный Метод

Retutrns the priority of an item at a particular flattened index.
public priorityAt ( $index, $withindex = false ) : numeric | array
Результат numeric | array the priority of the item in the list, false if not found. if withindex is true, an array is returned of [0 => $priority, 1 => $priorityIndex, 2 => flattenedIndex, 'priority' => $priority, 'index' => $priorityIndex, 'absindex' => flattenedIndex]

priorityOf() публичный Метод

Returns the priority of a particular item
public priorityOf ( $item, $withindex = false ) : numeric | array
Результат numeric | array the priority of the item in the list, false if not found. if withindex is true, an array is returned of [0 => $priority, 1 => $priorityIndex, 2 => flattenedIndex, 'priority' => $priority, 'index' => $priorityIndex, 'absindex' => flattenedIndex]

remove() публичный Метод

The list will search for the item. The first matching item found will be removed from the list.
public remove ( $item, $priority = false ) : integer
Результат integer index within the flattened list at which the item is being removed

removeAt() публичный Метод

Removes an item at the specified index in the flattened list.
public removeAt ( $index ) : mixed
Результат mixed the removed item.

removeAtIndexInPriority() публичный Метод

Removes the item at a specific index within a priority. Override and call this method to insert your own functionality.
public removeAtIndexInPriority ( $index, $priority = null ) : mixed
Результат mixed the removed item.

setDefaultPriority() защищенный Метод

This must be called internally or when instantiated.
protected setDefaultPriority ( $value )

setPrecision() защищенный Метод

This must be called internally or when instantiated.
protected setPrecision ( $value )

sortPriorities() защищенный Метод

This orders the priority list internally.
protected sortPriorities ( )

toArray() публичный Метод

public toArray ( ) : array
Результат array the priority list of items in array

toArrayAbovePriority() публичный Метод

Combines the map elements which have a priority above the parameter value
public toArrayAbovePriority ( $priority, $inclusive = true ) : array
Результат array the array of priorities keys with values of arrays of items that are above a specified priority. The priorities are sorted so important priorities, lower numerics, are first.

toArrayBelowPriority() публичный Метод

Combines the map elements which have a priority below the parameter value
public toArrayBelowPriority ( $priority, $inclusive = false ) : array
Результат array the array of priorities keys with values of arrays of items that are below a specified priority. The priorities are sorted so important priorities, lower numerics, are first.

toPriorityArray() публичный Метод

public toPriorityArray ( ) : array
Результат array the array of priorities keys with values of arrays of items. The priorities are sorted so important priorities, lower numerics, are first.