dastal
Version:
Data Structures & Algorithms implementations
157 lines • 4.99 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayList = void 0;
const arrayUtils_1 = require("src/utils/arrayUtils");
const numberUtils_1 = require("src/math/numberUtils");
/**
* An implementation of the {@link List} interface using an array
*/
class ArrayList {
/**
* Instantiate the list.
*
* @param elements - A set of elements to initialize the list with.
*/
constructor(elements) {
this.array = elements ? Array.from(elements) : [];
}
add(index, element) {
if (index >= 0 && index <= this.size) {
this.array.splice(index, 0, element);
}
return this.size;
}
addAll(index, elements) {
if (index >= 0 && index <= this.size) {
arrayUtils_1.splice(this.array, index, 0, Array.from(elements));
}
return this.size;
}
clear() {
this.array.length = 0;
}
concat(...lists) {
const out = new ArrayList(this);
for (const list of lists) {
out.addAll(out.size, list);
}
return out;
}
copyWithin(index, min, max) {
index = numberUtils_1.clamp(numberUtils_1.wrapLeft(index, 0, this.size), 0, this.size);
min = numberUtils_1.clamp(numberUtils_1.wrapLeft(min ?? 0, 0, this.size), 0, this.size);
max = numberUtils_1.clamp(numberUtils_1.wrapLeft(max ?? this.size, 0, this.size), 0, this.size);
this.array.copyWithin(index, min, max);
return this;
}
fill(element, min, max) {
min = numberUtils_1.clamp(numberUtils_1.wrapLeft(min ?? 0, 0, this.size), 0, this.size);
max = numberUtils_1.clamp(numberUtils_1.wrapLeft(max ?? this.size, 0, this.size), 0, this.size);
this.array.fill(element, min, max);
return this;
}
get(index) {
return index < 0 || index >= this.size ? undefined : this.array[index];
}
getSet(index, callback) {
let value = undefined;
if (index >= 0 && index < this.size) {
value = this.array[index];
this.array[index] = callback(value);
}
return value;
}
pop() {
return this.array.pop();
}
push(element) {
return this.array.push(element);
}
remove(index) {
return index < 0 || index >= this.size ? undefined : this.array.splice(index, 1)[0];
}
reverse(min, max) {
min = numberUtils_1.clamp(numberUtils_1.wrapLeft(min ?? 0, 0, this.size), 0, this.size);
max = numberUtils_1.clamp(numberUtils_1.wrapLeft(max ?? this.size, 0, this.size), 0, this.size) - 1;
while (min < max) {
const temp = this.array[min];
this.array[min++] = this.array[max];
this.array[max--] = temp;
}
return this;
}
set(index, element) {
let prev = undefined;
if (index >= 0 && index < this.size) {
prev = this.array[index];
this.array[index] = element;
}
return prev;
}
shift() {
return this.array.shift();
}
get size() {
return this.array.length;
}
slice(min, max) {
return new ArrayList(this.array.slice(min, max));
}
splice(start, count, elements) {
return new ArrayList(arrayUtils_1.splice(this.array, start, count, Array.from(elements || [])));
}
sort(compareFn) {
this.array.sort(compareFn);
return this;
}
/**
* Receive an iterator through the list.
*
* **Note:** Unexpected behavior can occur if the collection is modified during iteration.
*
* @returns An iterator through the list
*/
[Symbol.iterator]() {
return this.array[Symbol.iterator]();
}
unshift(element) {
return this.array.unshift(element);
}
update(min, max, callback) {
if (callback == null) {
if (arguments.length < 2) {
callback = min;
min = undefined;
}
else {
callback = max;
max = undefined;
}
}
min = numberUtils_1.clamp(numberUtils_1.wrapLeft(min ?? 0, 0, this.size), 0, this.size);
max = numberUtils_1.clamp(numberUtils_1.wrapLeft(max ?? this.size, 0, this.size), 0, this.size);
while (min < max) {
this.array[min] = callback(this.array[min], min);
++min;
}
return this;
}
*view(min, max) {
min = numberUtils_1.clamp(numberUtils_1.wrapLeft(min ?? 0, 0, this.size), 0, this.size);
let len;
if (max == null) {
len = () => this.size;
}
else if (max >= 0) {
len = () => Math.min(max, this.size);
}
else {
len = () => this.size + max;
}
while (min < len()) {
yield this.array[min++];
}
}
}
exports.ArrayList = ArrayList;
//# sourceMappingURL=arrayList.js.map