deque-typed
v2.4.3
Published
Deque
Maintainers
Keywords
Readme
What
Brief
This is a standalone Deque data structure from the data-structure-typed collection. If you wish to access more data structures or advanced features, you can transition to directly installing the complete data-structure-typed package
How
install
npm
npm i deque-typed --saveyarn
yarn add deque-typedsnippet
basic Deque creation and push/pop operations
// Create a simple Deque with initial values
const deque = new Deque([1, 2, 3, 4, 5]);
// Verify the deque maintains insertion order
console.log([...deque]); // [1, 2, 3, 4, 5];
// Check length
console.log(deque.length); // 5;
// Push to the end
deque.push(6);
console.log(deque.length); // 6;
// Pop from the end
const last = deque.pop();
console.log(last); // 6;Deque shift and unshift operations
const deque = new Deque<number>([20, 30, 40]);
// Unshift adds to the front
deque.unshift(10);
console.log([...deque]); // [10, 20, 30, 40];
// Shift removes from the front (O(1) complexity!)
const first = deque.shift();
console.log(first); // 10;
// Verify remaining elements
console.log([...deque]); // [20, 30, 40];
console.log(deque.length); // 3;Deque peek at both ends
const deque = new Deque<number>([10, 20, 30, 40, 50]);
// Get first element without removing
const first = deque.at(0);
console.log(first); // 10;
// Get last element without removing
const last = deque.at(deque.length - 1);
console.log(last); // 50;
// Length unchanged
console.log(deque.length); // 5;Deque for...of iteration and reverse
const deque = new Deque<string>(['A', 'B', 'C', 'D']);
// Iterate forward
const forward: string[] = [];
for (const item of deque) {
forward.push(item);
}
console.log(forward); // ['A', 'B', 'C', 'D'];
// Reverse the deque
deque.reverse();
const backward: string[] = [];
for (const item of deque) {
backward.push(item);
}
console.log(backward); // ['D', 'C', 'B', 'A'];Deque as sliding window for stream processing
interface DataPoint {
timestamp: number;
value: number;
sensor: string;
}
// Create a deque-based sliding window for real-time data aggregation
const windowSize = 3;
const dataWindow = new Deque<DataPoint>();
// Simulate incoming sensor data stream
const incomingData: DataPoint[] = [
{ timestamp: 1000, value: 25.5, sensor: 'temp-01' },
{ timestamp: 1100, value: 26.2, sensor: 'temp-01' },
{ timestamp: 1200, value: 25.8, sensor: 'temp-01' },
{ timestamp: 1300, value: 27.1, sensor: 'temp-01' },
{ timestamp: 1400, value: 26.9, sensor: 'temp-01' }
];
const windowResults: Array<{ avgValue: number; windowSize: number }> = [];
for (const dataPoint of incomingData) {
// Add new data to the end
dataWindow.push(dataPoint);
// Remove oldest data when window exceeds size (O(1) from front)
if (dataWindow.length > windowSize) {
dataWindow.shift();
}
// Calculate average of current window
let sum = 0;
for (const point of dataWindow) {
sum += point.value;
}
const avg = sum / dataWindow.length;
windowResults.push({
avgValue: Math.round(avg * 10) / 10,
windowSize: dataWindow.length
});
}
// Verify sliding window behavior
console.log(windowResults.length); // 5;
console.log(windowResults[0].windowSize); // 1; // First window has 1 element
console.log(windowResults[2].windowSize); // 3; // Windows are at max size from 3rd onwards
console.log(windowResults[4].windowSize); // 3; // Last window still has 3 elements
console.log(dataWindow.length); // 3;API docs & Examples
Examples Repository
