actionscript-3 - 快速确定数据在数组中的位置

标签 actionscript-3 algorithm arrays data-structures loops

我的数据结构如下图所示。我需要快速找出突出显示的单元格组右侧或左侧的单元格索引。

some data

您可以在下面的代码中看到,我天真地遍历了每个索引处的所有单元格,以确定请求的索引处是否有单元格。当我有几个(一百个)细胞时,这很好用,但当我有数千个细胞时,它很快就会崩溃。

在这种特殊情况下,突出显示的组是移动的,并且只能移动到上一个/下一个占用的单元格之前或之后的索引。所以 groupMinX/maxX 是它可以根据行中其他单元格的位置移动的最小和最大 x 值。

            private var movingGroup:CellGroup; //selected group

    public function getCellAtIndex(index:int):ICell
    {
        for each(var cell:ICell in cells)
        {
            if(cell.index==index)
                return cell;
        }

        return null;
    }

    public function groupMinX(xPos:Number):Number
    {
        var index:int = xPos/cellSize;
        var cellsOnLeft:Array = getAllCellsOnLeft(index-1);
        if(cellsOnLeft.length > 0)
            return cellsOnLeft[cellsOnLeft.length-1].x + cellSize;
        return 0;
    }

    public function groupMaxX(xPos:Number):Number
    {
        var index:int = xPos/cellSize;
        var cellsOnRight:Array = getAllCellsOnRight(index);
        if(cellsOnRight.length > 0)
            return cellsOnRight[0].x;
        return (maxIndex)*cellSize;
    }

    private function getAllCellsOnLeft(ofIndex:int):Array
    {
        var index:int = 1;
        var cells:Array = [];
        while( ofIndex >= 0 )
        {
            var cell:ICell = getCellAtIndex(ofIndex);
            if(cell && !movingGroup.containsCell(cell))
                cells.unshift( cell );
            ofIndex--;
        }
        return cells;       
    }

    private function getAllCellsOnRight(ofIndex:int):Array
    {
        var index:int = 1;
        var cells:Array = [];
        while( index <= maxIndex )
        {
            var cell:ICell = getCellAtIndex( ofIndex + index );
            if(cell && !movingGroup.containsCell(cell))
                cells.push( cell );
            index++;
        }
        return cells;       
    }

我正在寻找一种扫描/跟踪细胞的有效方法。我正在遍历的数组实际上并不包含空白单元格,但它包含具有 index 属性的单元格。

最佳答案

糟糕,我在推文中添加了错误的链接。使用链表。让你的“Cell”类实现一个链表节点接口(interface)。无需循环或使用条件。

http://en.wikipedia.org/wiki/Linked_list

关于actionscript-3 - 快速确定数据在数组中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1910585/

相关文章:

php - HTML 表单 -> Flash 提交按钮 -> 在 Flash 按钮中设置 PHP 变量 -> 提交表单

actionscript-3 - 使用 SQLite 开发 AIR 应用程序的最佳实践

arrays - 已排序矩阵中的第 K 个最小元素

javascript - 如何在 Typescript 中生成 * 数组

php - 如何确定 while 循环内 tep_db_fetch_array 的大小?

actionscript-3 - 将颈背 body 推向特定位置

actionscript-3 - 使用播放/暂停/停止按钮控制Flash视频上的声音

algorithm - 如何在特定时间访问变量值?

algorithm - 以最少的 Action 同时解决所有 4x4 迷宫

html - 循环遍历元素数组以每 X 秒显示在浏览器上