加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 重新组织SplObjectStorage实例的子项

发布时间:2020-12-13 16:23:14 所属栏目:PHP教程 来源:网络整理
导读:我有一个SplObjectStorage实例,它存储要在容器中呈现的元素对象.我希望能够有效地添加和删除商店中任意随机位置的对象. 例: ?php$store = new SplObjectStorageWrapper;$obj1 = new Obj;$obj2 = new Obj;$obj3 = new Obj;$store-attach($obj1);$store-attac
我有一个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(整数)和indexOf(对象)

解决方法

事实证明,最简单(并且显然最有效)的方法是扩展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));

    }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读