Vector class with constructor - Node.js Data Structure

Node.js examples for Data Structure:Vector

Description

Vector class with constructor

Demo Code


        if(!Array.prototype.equals) {
          Array.prototype.equals = function (array) {

            // if the other array is a falsy value, return
            //// w w  w. j  av  a 2s .  c o m
            if (!array) {
              return false;
            }

            // compare lengths - can save a lot of time
            //
            if (this.length != array.length) {
              return false;
            }

            for (var i = 0, l = this.length; i < l; i++) {

              // Check if we have nested arrays
              //
              if (this[i] instanceof Array && array[i] instanceof Array) {

                // recurse into the nested arrays
                //
                if (!this[i].equals(array[i])) {
                  return false;
                }
              }
              else if (this[i] != array[i]) {
                // Warning - two different object instances will never be equal: {x:20} != {x:20}
                //
                return false;
              }
            }
            return true;
          };

          // Hide method from for-in loops
          //
          Object.defineProperty(Array.prototype, "equals", {enumerable: false});
        }
var Vector = function(initialCapacity, maxCapacity) {
  this.storage = [];
  this.capacity = initialCapacity || 8;   // Default array size initially to 8 elements
  this.max = maxCapacity || 1 << 5;       // Default max vector size to 32
  this.length = 0;
};



Vector.prototype.insert = function(index, value) {
  this.storage.splice(index, 0, value);
};


Vector.prototype.add = function(value) {
  this.storage[this.length++] = value;
};


Vector.prototype.remove = function(index) {
  if (index === undefined || index === null) {
    delete this.storage[this.length];
  }
  else {
    this.storage.splice(index, 1);
  }
};


Vector.prototype.get = function(index) {
  return this.storage[index];
};


Vector.prototype.set = function(index, value) {
  this.storage[index] = value;
};


Vector.prototype.resize = function() {
  this.capacity *= 2;
  var tempStorage = new Array(this.capacity);

  for (var i=0; i<this.storage.length; i++) {
    tempStorage[i] = this.storage[i];
  }

  this.storage = tempStorage;
};


Vector.prototype.toArray = function() {
  var result = [];

  for (var i=0; i<this.length; i++) {
    result[i] = this.storage[i];
  }

  return result;
};



var v = new Vector();

console.log("Initialize");
console.log("  v.length should be 0: " + (v.length === 0));
console.log("  v.capacity should be 8: " + (v.capacity === 8));
console.log("  v.max should be 32: " + (v.max === 32));
console.log("  v.storage should be []: " + (v.storage.equals([])));

console.log("Add 3");
v.add(0);
v.add(1);
v.add(2);
console.log("  v.length should be 3: " + (v.length === 3));
console.log("  v.toArray() should be [0, 1, 2]: " + (v.toArray().equals([0, 1, 2])));

console.log("Add 2 more");
v.add(3);
v.add(4);
console.log("  v.length should be 5: " + (v.length === 5));
console.log("  v.toArray() should be [0, 1, 2, 3, 4]: " + (v.toArray().equals([0, 1, 2, 3, 4])));

console.log("Insert 1 at v[3]");
v.insert(3, 2.5);
console.log("  v.length should be 6: " + (v.length === 6));
console.log("  v.toArray() should be [0, 1, 2, 2.5, 3, 4]: " + (v.toArray().equals([0, 1, 2, 2.5, 3, 4])));

console.log("Remove v[3]");
v.remove(3);
console.log("  v.length should be 5: " + (v.length === 5));
console.log("  v.toArray() should be [0, 1, 2, 3, 4]: " + (v.toArray().equals([0, 1, 2, 3, 4])));

console.log("Set v[2] = 15");
v.set(2, 15);
console.log("  v.get(2) should be 15: " + (v.get(2) === 15));

console.log("Add 4 more");
v.add(5);
v.add(6);
v.add(7);
v.add(8);
console.log("  v.length should be 9: " + (v.length === 9));
console.log("  v.capacity should be 16: " + (v.capacity === 16));

console.log("Remove from the end");
v.remove();
console.log("  v.toArray() should be [0, 1, 15, 3, 4, 5, 6, 7]: " + (v.toArray().equals([0, 1, 15, 3, 4, 5, 6, 7])));

console.log("Remove v[2]");
v.remove(2);
console.log("  v.toArray() should be [0, 1, 3, 4, 5, 6, 7]: " + (v.toArray().equals([0, 1, 3, 4, 5, 6, 7])));

console.log("Remove the first");
v.remove(0);
console.log("  v.toArray() should be [1, 3, 4, 5, 6, 7]: " + (v.toArray().equals([1, 3, 4, 5, 6, 7])));
console.log("  v.length should be 6: " + (v.length === 6));
console.log("  v.capacity should be 16: " + (v.capacity === 16));

console.log("Remove all but 2");
v.remove();
v.remove();
v.remove();
v.remove();
console.log("  v.toArray() should be [1, 3]: " + (v.toArray().equals([1, 3])));
console.log("  v.length should be 2: " + (v.length === 2));
console.log("  v.capacity should be 8: " + (v.capacity === 8));

Related Tutorials