- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一个 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/
目录 一、状态变化 1.1 状态变化是什么 1.2 mutableStateListOf 和 mutableStateMapOf 二、重组的
这是我的真实数据列表的示例列表: df setNames(nm) } fun3 Unit: microseconds #> expr min lq me
在 clojure 中,我可以像这样解构 map : (let [{:keys [key1 key2]} {:key1 1 :key2 2}] ...) 这类似于 CoffeeScript 的方法
这个问题在这里已经有了答案: Re-Structuring a JSON (2 个答案) 关闭 9 年前。 我需要使用 Javascript/Jquery 将我的 JSON 重新组织成新的结构,但我
我有一个具有以下结构的表,列是出发地、目的地、旅行时间和出发时间。主键是出发地、目的地和出发时间的组合。有没有什么方法可以重新构造它,以便行中没有太多的起点和目的地重复? 最佳答案 “出发地和目的地太
需要将 JSON 重组为 Python 中的新结构。 例如: { 'a' : 1, 'b' : 1, 'd' : {'d1' : '1', 'd2' : 2}, 'm' : [
我正在努力了解重组的工作原理。虽然我理解它的基本概念,但我在使用 withProps 函数时遇到了问题。如果我尝试使用它来装饰带有附加 Prop 的组件的每个子组件,它根本无法工作。我所做的是: co
假设我有一个数据框列表。每个数据框都有这样的列: lists$a company, x, y ,z lists$b company, x, y, z lists$c company, x, y, z
好吧,我有这个数组 $city : [2] => Array ( [0] => Array ( [0] => fr
工作中的 subversion 存储库是在没有对其结构进行太多规划的情况下建立的。目前没有明确的标签、主干或分支配置,尽管通过使用 subclipse:tags 存在一些标签元数据 目前存储库的格式为
我有一个具有以下文件夹结构的 Git 存储库: allprojectfiles --otherfolders --source ----projectname ------projectname --
我有一个像这样的 git repo 结构- main-repo -file1 -file2 我想把它转换成类似的东西- main-repo -javascript -fil
我有一个像这样的 git repo 结构- main-repo -file1 -file2 我想把它转换成类似的东西- main-repo -javascript -fil
有人建议我从类结构(定义我自己的类)转移到 pandas DataFrame 领域,因为我设想对我的数据进行许多操作。 此时我有一个如下所示的数据框: ID Name Recordin
我想重构我的 pandas 数据框,其中 h1、h2 等是与小时相关的值。目前看起来像: h1 h2 h3 h4 h5 h6 h7 h8 h9
我在 postgresql 上使用查询返回这样的结果。 schedule | day | subject | grade | ========================
我有一个这样组织的数据框... **Name** | **Mealtime** | **Food** John | 8:00 am | cereal John | 1:00 pm | salad
我有基本的脚本知识,但我不知道如何解决这个问题。我正在尝试将银行自动生成的 CSV 文件转换为 YNAB(您需要预算)可以理解的格式。 YNAB 格式(所需的 csv 文件格式) Date,Payee
我正在重组我的 meteor 应用程序,突然每个逻辑(JS 脚本)与初始文件分离的 View (html 模板)都停止工作。 最初我的项目看起来像这样 -project -.meteor
我试图让 children 不影响导航栏,并像下拉菜单一样显示。 当前示例位于:dev4you.byethost15.com 函数应该如下: 用户将鼠标悬停在父项上 子项显示在类似列表、Underne
我是一名优秀的程序员,十分优秀!