Repository URL to install this package:
Version:
1.2.7 ▾
|
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @author @see https://github.com/processing-js/processing-js/blob/master/src/Objects/ArrayList.js
* An ArrayList stores a variable number of objects.
*/
var Iterator_1 = require("./Iterator");
var equals = Object.is;
var InvalidArgumentsError = /** @class */ (function (_super) {
__extends(InvalidArgumentsError, _super);
function InvalidArgumentsError() {
return _super !== null && _super.apply(this, arguments) || this;
}
return InvalidArgumentsError;
}(Error));
/**
* @todo extends Array
* @todo could `setPrototype` for `instanceof`?
*/
var ArrayList = /** @class */ (function () {
function ArrayList(array) {
if (Array.isArray(array)) {
this.array = array;
}
else if (array.toArray) {
this.array = array.toArray();
}
else {
this.array = [];
}
}
/**
* ArrayList.get() Returns the element at the specified position in this list.
* @param i index of element to return
* @returns the element at the specified position in this list.
*/
ArrayList.prototype.get = function (index) {
return this.array[index];
};
ArrayList.prototype.indexOf = function (item) {
for (var i = 0, len = this.array.length; i < len; ++i) {
if (equals(item, this.array[i])) {
return i;
}
}
return -1;
};
/**
* ArrayList.lastIndexOf() Returns the index of the last occurrence of the specified element in this list,
* or -1 if this list does not contain the element. More formally, returns the highest index i such that
* (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
*
* @param item element to search for.
* @returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
*/
ArrayList.prototype.lastIndexOf = function (item) {
for (var i = this.array.length - 1; i >= 0; --i) {
if (equals(item, this.array[i])) {
return i;
}
}
return -1;
};
/**
* ArrayList.clear() Removes all of the elements from this list.
* The list will be empty after this call returns.
*/
ArrayList.prototype.clear = function () {
this.array.length = 0;
};
/**
* ArrayList.isEmpty() Tests if this list has no elements.
* @returns true if this list has no elements; false otherwise
*/
ArrayList.prototype.isEmpty = function () {
return this.array.length !== 0;
};
/**
* ArrayList.clone() Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.)
* @returns a clone of this ArrayList instance
*/
ArrayList.prototype.clone = function () {
return new ArrayList(this);
};
/**
* ArrayList.toArray() Returns an array containing all of the elements in this list in the correct order.
* @returns Returns an array containing all of the elements in this list in the correct order
*/
ArrayList.prototype.toArray = function () {
return this.array.slice(0);
};
/**
* @name toIterator
* @name entries
*/
ArrayList.prototype.iterator = function () {
return Iterator_1.toIterator(this.array);
};
Object.defineProperty(ArrayList.prototype, "size", {
/**
* @alias length
* ArrayList.size() Returns the number of elements in this list.
* @returns the number of elements in this list
*/
get: function () {
return this.array.length;
},
enumerable: true,
configurable: true
});
Object.defineProperty(ArrayList.prototype, "length", {
get: function () {
return this.array.length;
},
enumerable: true,
configurable: true
});
/**
* ArrayList.remove() Removes an element either based on index, if the argument is a number, or
* by equality check, if the argument is an object.
*
* @param item either the index of the element to be removed, or the element itself.
* @returns If removal is by index, the element that was removed, or null if nothing was removed. If removal is by object, true if removal occurred, otherwise false.
*/
ArrayList.prototype.remove = function (item) {
if (typeof item === 'number') {
return this.array.splice(item, 1)[0];
}
var index = this.indexOf(item);
if (index > -1) {
this.array.splice(index, 1);
return true;
}
return false;
};
/**
* ArrayList.set() Replaces the element at the specified position in this list with the specified element.
*
* @param index index of element to replace
* @param object element to be stored at the specified position
*/
ArrayList.prototype.set = function (index, value) {
if (arguments.length === 2) {
var arg0 = arguments[0];
if (typeof arg0 === 'number') {
if (arg0 >= 0 && arg0 < this.array.length) {
this.array.splice(arg0, 1, arguments[1]);
}
else {
throw arg0 + " is not a valid index.";
}
}
else {
throw typeof arg0 + " is not a number";
}
}
else {
throw 'Please use the proper number of parameters.';
}
};
/**
* ArrayList.add() Adds the specified element to this list.
* @param index optional index at which the specified element is to be inserted
* @param object element to be added to the list
*/
ArrayList.prototype.add = function (index, value) {
if (arguments.length === 1) {
this.array.push(arguments[0]); // for add(Object)
}
else if (arguments.length === 2) {
var arg0 = arguments[0];
if (typeof arg0 === 'number') {
if (arg0 >= 0 && arg0 <= this.array.length) {
this.array.splice(arg0, 0, arguments[1]); // for add(i, Object)
}
else {
throw arg0 + " is not a valid index";
}
}
else {
throw typeof arg0 + " is not a number";
}
}
else {
throw new InvalidArgumentsError('Please use the proper number of parameters.');
}
};
/**
* @memberof ArrayList
* ArrayList.addAll(collection) appends all of the elements in the specified
* Collection to the end of this list, in the order that they are returned by
* the specified Collection's Iterator.
*
* When called as addAll(index, collection) the elements are inserted into
* this list at the position indicated by index.
*
* @param {index} Optional; specifies the position the colletion should be inserted at
* @param {collection} Any iterable object (ArrayList, HashMap.keySet(), etc.)
* @throws out of bounds error for negative index, or index greater than list size.
*/
ArrayList.prototype.addAll = function (arg1, arg2) {
// addAll(int, Collection)
var it;
if (typeof arg1 === 'number') {
if (arg1 < 0 || arg1 > this.array.length) {
throw "Index out of bounds for addAll: " + arg1 + " greater or equal than " + this.array.length;
}
it = Iterator_1.toIterator(arg2);
while (it.hasNext()) {
this.array.splice(arg1++, 0, it.next());
}
}
// addAll(Collection)
else {
it = Iterator_1.toIterator(arg1);
while (it.hasNext()) {
this.array.push(it.next());
}
}
};
ArrayList.prototype.includes = function (x) {
return this.array.includes(x);
};
/**
* ArrayList.removeAll Removes from this List all of the elements from
* the current ArrayList which are present in the passed in paramater ArrayList 'c'.
* Shifts any succeeding elements to the left (reduces their index).
*
* @param the ArrayList to compare to the current ArrayList
*
* @returns true if the ArrayList had an element removed; false otherwise
*/
ArrayList.prototype.removeAll = function (arrayList) {
var i;
var x;
var item;
var newList = new ArrayList();
newList.addAll(this);
this.clear();
// For every item that exists in the original ArrayList and not in the c ArrayList
// copy it into the empty 'this' ArrayList to create the new 'this' Array.
for (i = 0, x = 0; i < newList.size; i++) {
item = newList.get(i);
if (arrayList.includes(item) === false) {
this.add(x++, item);
}
}
if (this.size < newList.size) {
return true;
}
return false;
};
return ArrayList;
}());
exports.ArrayList = ArrayList;
exports.default = ArrayList;
//# sourceMappingURL=ArrayList.js.map