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
139 lines (138 loc) • 4.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RingBuffer = void 0;
class RingBuffer {
buffer;
size;
start = 0;
end = 0;
count = 0;
constructor(size) {
this.size = size;
this.buffer = new Array(size).fill(null);
}
resize() {
const newSize = this.size * 2;
const newBuffer = new Array(newSize).fill(null);
for (let i = 0; i < this.count; i++) {
newBuffer[i] = this.buffer[(this.start + i) % this.size];
}
this.buffer = newBuffer;
this.size = newSize;
this.start = 0;
this.end = this.count;
}
push(item) {
if (this.count === this.size) {
this.resize();
}
this.buffer[this.end] = item;
this.end = (this.end + 1) % this.size;
this.count++;
}
pop() {
if (this.count === 0) {
return null;
}
this.end = (this.end - 1 + this.size) % this.size;
const item = this.buffer[this.end];
this.buffer[this.end] = null;
this.count--;
return item;
}
unshift(item) {
if (this.count === this.size) {
this.resize();
}
this.start = (this.start - 1 + this.size) % this.size;
this.buffer[this.start] = item;
this.count++;
}
shift() {
if (this.count === 0) {
return null;
}
const item = this.buffer[this.start];
this.buffer[this.start] = null;
this.start = (this.start + 1) % this.size;
this.count--;
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 && index < this.count) {
const adjustedIndex = (this.start + index) % this.size;
return this.buffer[adjustedIndex];
}
else if (index < 0 && Math.abs(index) <= this.count) {
const adjustedIndex = (this.start + this.count + index) % this.size;
return this.buffer[adjustedIndex];
}
return null;
}
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;
}
get length() {
return this.count;
}
}
exports.RingBuffer = RingBuffer;