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
파일 보기 프로젝트 열기: pradosoft/prado 1 사용 예제들

공개 메소드들

메소드 설명
__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.