array-ring
Version:
Advanced Array and Ring structure for Large data: A High-Performance Solution, mostly created to implement array methods, bringing 2 data structures, An efficient resizable array with similar methods to javascript array which are optmized methods like shi
182 lines (181 loc) • 5.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports._Array = void 0;
class _Array {
buffer;
size;
start = 0;
end = 0;
count = 0;
constructor(size, array) {
this.size = size;
this.buffer = new Array(size).fill(undefined);
if (array !== undefined)
for (let elm of array)
this.push(elm);
}
_push(item) {
throw new Error(`not implemented`);
}
_unshift(item) {
throw new Error(`not implemented`);
}
push(...items) {
for (let item of items)
this._push(item);
}
unshift(...items) {
for (let item of items)
this._unshift(item);
}
pop() {
if (this.count === 0)
return undefined;
this.end = (this.end - 1 + this.size) % this.size;
const item = this.buffer[this.end];
this.buffer[this.end] = undefined;
this.count--;
return item;
}
shift() {
if (this.count === 0)
return undefined;
const item = this.buffer[this.start];
this.buffer[this.start] = undefined;
this.count--;
this.start = (this.start + 1) % this.size;
return item;
}
forEach(callback) {
for (let i = 0; i < this.count; i++) {
const index = (this.start + i) % this.size;
callback(this.buffer[index], i);
}
}
map(callback) {
const result = [];
for (let i = 0; i < this.count; i++) {
const index = (this.start + i) % this.size;
result.push(callback(this.buffer[index], i));
}
return result;
}
reduce(callback, initialValue) {
let accumulator = initialValue;
for (let i = 0; i < this.count; i++) {
const index = (this.start + i) % this.size;
accumulator = callback(accumulator, this.buffer[index], i);
}
return accumulator;
}
at(index) {
if (index >= 0 && this.count > index) {
const adjustedIndex = (this.start + index) % this.size;
return this.buffer[adjustedIndex];
}
else if (0 > index && index + this.count >= 0) {
const adjustedIndex = (this.start + this.count + index) % this.size;
return this.buffer[adjustedIndex];
}
}
find(predicate) {
for (let i = 0; i < this.count; i++) {
const index = (this.start + i) % this.size;
const value = this.buffer[index];
if (predicate(value, i)) {
return value;
}
}
return undefined;
}
findIndex(predicate) {
for (let i = 0; i < this.count; i++) {
const index = (this.start + i) % this.size;
const value = this.buffer[index];
if (predicate(value, i)) {
return i;
}
}
return -1;
}
some(predicate) {
for (let i = 0; i < this.count; i++) {
const index = (this.start + i) % this.size;
const value = this.buffer[index];
if (predicate(value, i)) {
return true;
}
}
return false;
}
every(predicate) {
for (let i = 0; i < this.count; i++) {
const index = (this.start + i) % this.size;
const value = this.buffer[index];
if (!predicate(value, i)) {
return false;
}
}
return true;
}
slice(start, end = this.count) {
const result = [];
if (0 > start) {
if (start + this.count < 0)
return result;
start += this.count;
}
else if (start >= this.count)
return result;
if (0 > end)
end += this.count;
end = Math.min(end, this.count);
if (start >= end)
return result;
for (let i = start; i < end; i++) {
const index = (this.start + i) % this.size;
result.push(this.buffer[index]);
}
return result;
}
filter(predicate) {
const result = [];
for (let i = 0; i < this.count; i++) {
const index = (this.start + i) % this.size;
const value = this.buffer[index];
if (predicate(value, i)) {
result.push(value);
}
}
return result;
}
clear() {
this.buffer.fill(undefined);
this.start = 0;
this.end = 0;
this.count = 0;
}
join(separator = ",") {
const result = [];
for (let i = 0; i < this.count; i++) {
const index = (this.start + i) % this.size;
const value = this.buffer[index];
if (value !== undefined) {
result.push(String(value));
}
}
return result.join(separator);
}
toArray() {
const result = [];
for (let i = 0; i < this.count; i++) {
const index = (this.start + i) % this.size;
result.push(this.buffer[index]);
}
return result;
}
get length() {
return this.count;
}
}
exports._Array = _Array;