简体   繁体   中英

Creating an object similar to an array or extending the Array prototype

I'm trying to make something similar to an array.

I need to be able to "release" an Index (set its value to undefined) but I don't want to lose the Index. The "released" Indexes should be re-used whenever a new item is put into the array.

I'd like to be able to do something like:

example = new MyArray();

a = example.leaseIndex(); // returns 0
example[a] = "example value";

b = example.leaseIndex(); // returns 1
example[b] = "another value";

example.releaseIndex(0);

c = example.leaseIndex(); // returns 0
example[c] = "yet another value";

In my example leaseIndex finds an available index or if none are available pushes a new item onto the array and returns the index of that item.

I want to do this so that the array doesn't just grow bigger over time needlessly. I can't remove the "released" items as each item in the array contains a reference to another item in the same array.

I've had minor successes with functions and arrays outside of the main one to keep track of the available indexes and assign and release them, but ideally I'd like the functionality to be part of the main array.

Would I have to add my functions to the Array (or its prototype) or is there another way? As not all my arrays need this functionality.

Hope this makes sense :/

update

I am trying to store wiring loom layouts, which are essentially a net diagram (points and information as to how the points are connected).

The picture shows an example loom. It has 3 Connectors; Red (0) with 2 Lines, Yellow (1) with 3 Lines and Green (2) with 2 Lines. One of the lines on the Red Connector is Spliced (allowing multiple Lines to connect to a Single Line, the blue square)

在此处输入图片说明

This is how that loom would be stored.

loom = {
    points = [
        { self: 0, list: [ 2 ] },
        { self: 1, list: [ 7 ] },
        { self: 2, list: [ 0 ] },
        { self: 3, list: [ 7 ] },
        { self: 4, list: [ 6 ] },
        { self: 5, list: [ 7 ] },
        { self: 6, list: [ 4 ] },
        { self: 7, owner: 1, list: [ 1, 3, 5 ] }
    ],
    connectors = [
        [ 0, 1 ],
        [ 2, 3, 4 ],
        [ 5, 6 ]
    ]
}

the elements in the connectors array contain the indexes of points in the points array. the list array inside each points object contains the index of its destination(s) which are points too.

Im trying to make functions to help make managing the indexes easier, just wanted to know if there is a way to extend the array, or make something similar that incorporates the functionality. Using static functions would be OK and is what I've been using. I just wanted to see if i could extend the array, or use something like one so I didnt need to use the static functions.

Here is a simple implementation using some static functions (without needing to fuss with methods):

var hop = function(obj, prop){
    return Object.prototype.hasOwnProperty.call(obj, prop);
};

var leaseIndex = function(arr, value){
    var i;
    for(i=0; i<arr.length; i++){
        if(!hop(arr, i)){
           break;
        }
    }
    arr[i] = value;
    return i;
};

var releaseIndex = function(arr, i){
    delete arr[i];
};

Of course, I have no idea if this is what you really want, since my algorithm is potentially O(N) and I am not sure you need all this complication.

I would add the methods to the Array's prototype, like this:

Array.prototype.leaseIndex = function () {
   for (var i = 0; i < this.length; i++) {
     if(typeof this[i] === "undefined") {
       return i;
     }
   }
   return this.length;
};
Array.prototype.releaseIndex = function (index) {
  delete this[index];
};

So, your code would look like this:

example = [];

a = example.leaseIndex(); // returns 0
example[a] = "example value";

b = example.leaseIndex(); // returns 1
example[b] = "another value";

example.releaseIndex(0);

c = example.leaseIndex(); // returns 0
example[c] = "yet another value";

I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM