开发者

What is the best way to see if a group of items is placed within a specific part of an array?

(See example below to fully understand what I'm asking)

I have an array of items that I will put other items into and remove items out of depending on user choices. This array always has an "active set" of contiguous items of no more than 30 items (and it's only less than 30 when 开发者_运维知识库the total set of items is less than 30, otherwise, it must be full). The user can change the start of the active set.

What I want to know is what's the "cleanest" code, and most efficient way to check if new items added-to/removed-from the list fall within the active set, either in part or whole? (Also, if there are currently 10 total items, thus making the active set items 0-9, and they add 10 items in front of item 0 - which makes item 0 become item 10 - then I want to be able to know to change the active set to the new item 0 till item 19)

EXAMPLE:

//For this example, I'm making the active set be 10 items so I can write less
var activeSetLength = 10;

//The total set
var allItems = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o" ];

//First item in active set (in this example, this makes "m" the last active item)
var activeSet = allItems.slice( 3, 3 + activeSetLength );

/*
 * Now imagine the user is trying to add items into our list, how best do I handle
 * figuring out whether to add them to the activeset list?.
 */
//Adding items into list at position 1 (so only some of these will now be in the active set)
addItems( [ "one", "two", "three", "four" ], 1 );

Please let me know if you need more explanation as this may not be the clearest question. Thanks!

NOTE: In the "real world" my activeSet is implemented as a DOM node so the items are changed in it via appendChild/insertBefore/removeChild (this note added for clarification)


I ended up taking a snapshot before/after to handle the comparison and did something along these lines:

addItems: function(items, insertAt)
{
    //The current items in the active set as an array
    var snapshot = this.getSnapshot();

    //Add to items array at position indicated
    this.saveItems( items, insertAt );

    //Change the items in active set
    this.updateActiveSet( snapshot );
}

updateActiveSet: function(oldSnapshot)
{
    //Take a new snapshot
    var currentSnapshot = this.getSnapshot();

    //Last dealt with item's index
    var lastHandled = -1;

    for ( var i = 0, length = oldSnapshot.length; i < length; i++ )
    {
        var old = oldSnapshot[ i ];
        var indexInCurrent = currentSnapshot.indexOf( old );

        //This item is still in the active set
        if ( indexInCurrent != -1 )
        {
            //We need to insert all current items before this
            for ( var iNew = lastHandled + 1; iNew < indexInCurrent; iNew++ ) activeSet.insertBefore( currentSnapshot[ iNew ], old );

            //Update last handled
            lastHandled = indexInCurrent;
        }

        //Remove from active set dom
        else activeSet.removeChild( old );
    }

    //Now append all new items that weren't inserted before a remaining item
    for ( var i = lastHandled + 1, length = currentSnapshot.length; i < length; i++ ) activeSet.appendChild( currentSnapshot[ i ] );
}

getSnapshot: function()
{
    return this.allItems.slice( this.startOfActiveSet, this.startOfActiveSet + this.activeSetLength );
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜