UNPKG

deque-typed

Version:
293 lines (234 loc) 8.44 kB
![NPM](https://img.shields.io/npm/l/deque-typed) ![GitHub top language](https://img.shields.io/github/languages/top/zrwusa/data-structure-typed) ![npm](https://img.shields.io/npm/dw/deque-typed) ![eslint](https://aleen42.github.io/badges/src/eslint.svg) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/deque-typed) ![npm bundle size](https://img.shields.io/bundlephobia/min/deque-typed) ![npm](https://img.shields.io/npm/v/deque-typed) # 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](https://www.npmjs.com/package/data-structure-typed) package # How ## install ### npm ```bash npm i deque-typed --save ``` ### yarn ```bash yarn add deque-typed ``` ### snippet [//]: # (No deletion!!! Start of Example Replace Section) ### basic Deque creation and push/pop operations ```typescript // 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 ```typescript 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 ```typescript 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 ```typescript 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 ```typescript 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; ``` [//]: # (No deletion!!! End of Example Replace Section) ## API docs & Examples [API Docs](https://data-structure-typed-docs.vercel.app) [Live Examples](https://vivid-algorithm.vercel.app) <a href="https://github.com/zrwusa/vivid-algorithm" target="_blank">Examples Repository</a> ## Data Structures <table> <thead> <tr> <th>Data Structure</th> <th>Unit Test</th> <th>Performance Test</th> <th>API Docs</th> </tr> </thead> <tbody> <tr> <td>Deque</td> <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td> <td><img src="https://raw.githubusercontent.com/zrwusa/assets/master/images/data-structure-typed/assets/tick.svg" alt=""></td> <td><a href="https://data-structure-typed-docs.vercel.app/classes/Deque.html"><span>Deque</span></a></td> </tr> </tbody> </table> ## Standard library data structure comparison <table> <thead> <tr> <th>Data Structure Typed</th> <th>C++ STL</th> <th>java.util</th> <th>Python collections</th> </tr> </thead> <tbody> <tr> <td>Deque&lt;E&gt;</td> <td>deque&lt;T&gt;</td> <td>ArrayDeque&lt;E&gt;</td> <td>deque</td> </tr> </tbody> </table> ## Benchmark [//]: # (No deletion!!! Start of Replace Section) <div class="json-to-html-collapse clearfix 0"> <div class='collapsible level0' ><span class='json-to-html-label'>deque</span></div> <div class="content"><table style="display: table; width:100%; table-layout: fixed;"><tr><th>test name</th><th>time taken (ms)</th><th>executions per sec</th><th>sample deviation</th></tr><tr><td>1,000,000 push</td><td>14.55</td><td>68.72</td><td>6.91e-4</td></tr><tr><td>1,000,000 push & pop</td><td>23.40</td><td>42.73</td><td>5.94e-4</td></tr><tr><td>1,000,000 push & shift</td><td>24.41</td><td>40.97</td><td>1.45e-4</td></tr><tr><td>1,000,000 unshift & shift</td><td>22.56</td><td>44.32</td><td>1.30e-4</td></tr></table></div> </div> [//]: # (No deletion!!! End of Replace Section) ## Built-in classic algorithms <table> <thead> <tr> <th>Algorithm</th> <th>Function Description</th> <th>Iteration Type</th> </tr> </thead> <tbody> </tbody> </table> ## Software Engineering Design Standards <table> <tr> <th>Principle</th> <th>Description</th> </tr> <tr> <td>Practicality</td> <td>Follows ES6 and ESNext standards, offering unified and considerate optional parameters, and simplifies method names.</td> </tr> <tr> <td>Extensibility</td> <td>Adheres to OOP (Object-Oriented Programming) principles, allowing inheritance for all data structures.</td> </tr> <tr> <td>Modularization</td> <td>Includes data structure modularization and independent NPM packages.</td> </tr> <tr> <td>Efficiency</td> <td>All methods provide time and space complexity, comparable to native JS performance.</td> </tr> <tr> <td>Maintainability</td> <td>Follows open-source community development standards, complete documentation, continuous integration, and adheres to TDD (Test-Driven Development) patterns.</td> </tr> <tr> <td>Testability</td> <td>Automated and customized unit testing, performance testing, and integration testing.</td> </tr> <tr> <td>Portability</td> <td>Plans for porting to Java, Python, and C++, currently achieved to 80%.</td> </tr> <tr> <td>Reusability</td> <td>Fully decoupled, minimized side effects, and adheres to OOP.</td> </tr> <tr> <td>Security</td> <td>Carefully designed security for member variables and methods. Read-write separation. Data structure software does not need to consider other security aspects.</td> </tr> <tr> <td>Scalability</td> <td>Data structure software does not involve load issues.</td> </tr> </table>