php - 重组 SplObjectStorage 实例的子实例

标签 php spl

我有一个 SplObjectStorage 实例,它存储要在容器中呈现的元素对象。我希望能够从商店中的任意随机位置高效地添加和删除对象。

例子:

<?php
$store = new SplObjectStorageWrapper;
$obj1 = new Obj;
$obj2 = new Obj;
$obj3 = new Obj;

$store->attach($obj1);
$store->attach($obj2);
$store->insertAtIndex($obj3, 1);

//Storage should now be organized as $obj1, $obj3, $obj2

我将如何着手实现 insertAtIndex 方法?我是否使用 LimitIterator 在特定位置后分离和重新附加子项?使用基于数组的对象存储已被证明比 SplObjectStorage 实例慢得多。

我想实现的其他方法包括 removeAtIndex(integer)indexOf(object)

最佳答案

事实证明,最简单(显然也是最有效)的方法是扩展 SplObjectStorage 并使用 LimitIterator。下面的代码示例:

<?php
/**
 * Extends the SplObjectStorage class to provide index functions
 */
class ObjectStorage extends SplObjectStorage {

    /**
     * Returns the index of a given object, or false if not found
     * @param object $object
     */
    function indexOf($object){

        if(!$this->contains($object)) return false;

        foreach($this as $index => $obj) if($obj === $object) return $index;

    }

    /**
     * Returns the object at the given index
     */
    function itemAtIndex($index){

        $it = new LimitIterator($this, $index, 1);
        foreach($it as $obj) return $obj;

    }

    /**
     * Returns the sequence of objects as specified by the offset and length
     * @param int $offset
     * @param int $length
     */
    function slice($offset, $length){

        $out = array();
        $it = new LimitIterator($this, $offset, $length);
        foreach($it as $obj) $out[] = $obj;
        return $out;

    }

    /**
     * Inserts an object (or an array of objects) at a certain point
     * @param mixed $object A single object or an array of objects
     * @param integer $index
     */
    function insertAt($object, $index){

        if(!is_array($object)) $object = array($object);

        //Check to ensure that objects don't already exist in the collection
        foreach($object as $k => $obj):
            if($this->contains($obj)) unset($object[$k]);
        endforeach;

        //Do we have any objects left?
        if(!$object) return;

        //Detach any objects at or past this index
        $remaining = array();
        if($index < $this->count()):
            $remaining = $this->slice($index, $this->count() - $index);
            foreach($remaining as $obj) $this->detach($obj);
        endif;

        //Add the new objects we're splicing in
        foreach($object as $obj) $this->attach($obj);

        //Attach the objects we previously detached
        foreach($remaining as $obj) $this->attach($obj);

    }

    /**
     * Removes the object at the given index
     * @param integer $index
     */
    function removeAt($index){

        $this->detach($this->itemAtIndex($index));

    }

}

关于php - 重组 SplObjectStorage 实例的子实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8731771/

相关文章:

php - ArrayAccess 如何工作?

php - SplFixedArray 是在幕后用 C/C++ 实现的吗?

php - 获取集合中的最后一个元素

php - 如何正确编写 Doctrine 2 DQL 查询?

php - 同一页面上的 Ajax 到 PHP

php - 使用 DirectoryIterator 对文件进行排序

php - iterator_to_array

php - Woocommerce 结构消失了

javascript - jQuery DataTable 返回带有数据的 PHP 脚本

像 min() 这样的 PHP 原生函数不支持固定数组