Prado\Collections\TPriorityList::offsetSet PHP Метод

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 )
    public function offsetSet($offset, $item)
    {
        if ($offset === null) {
            return $this->add($item);
        }
        if ($offset === $this->getCount()) {
            $priority = $this->priorityAt($offset - 1, true);
            $priority[1]++;
        } else {
            $priority = $this->priorityAt($offset, true);
            $this->removeAtIndexInPriority($priority[1], $priority[0]);
        }
        $this->insertAtIndexInPriority($item, $priority[1], $priority[0]);
    }

Usage Example

Пример #1
0
 public function testOffsetSetAppendTPriorityList()
 {
     $list = new TPriorityList();
     $list->add(2);
     $list->add(1, 5);
     $list->add(3, 15);
     $list->offsetSet(3, 4);
     self::assertEquals(array(1, 2, 3, 4), $list->toArray());
 }