Skip to content

Commit

Permalink
Queue implementation with test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmak committed Dec 28, 2020
1 parent eac655b commit 4061984
Show file tree
Hide file tree
Showing 10 changed files with 1,483 additions and 1 deletion.
116 changes: 116 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env
.env.test

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
235 changes: 234 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,235 @@
# sumo-queue
Queue Implementation

<p align="center">
<a href="https://nodei.co/npm/sumo-queue/" target="_blank"><img src="https://nodei.co/npm/sumo-queue.png"></a>
</p>

<br />
<p align="center">

<h3 align="center">SUMO QUEUE</h3>

<p align="center">
Queue Data Structure Implementation. Use it for large arrays.
<br />
<a href="https://github.com/sanmak/sumo-queue"><strong>Explore the docs »</strong></a>
<br />
<br />
·
<a href="https://github.com/sanmak/sumo-queue/issues">Report Bug</a>
·
<a href="https://github.com/sanmak/sumo-queue/issues">Request Feature</a>
</p>
</p>

## Dead Simple to Use

```javascript
# Import Package
const Queue = require("sumo-queue");

const queue = new Queue(2);
// OUTPUT: 1609158613385wbQvkB5djUXB debug log: A new queue 1609158613385wbQvkB5djUXB is initialized with capacity 2

queue.enqueue(1);
// OUTPUT: 1609158613385wbQvkB5djUXB debug log: Node added {"currentPointerValue":1,"nextPointer":null}

console.log(queue.iterate());
// OUTPUT: [ 1 ]

console.log(queue.size);
// OUTPUT: 1

queue.bulkEnqueue([2, 3]);
/* OUTPUT:
1609158613385wbQvkB5djUXB debug log: Node added {"currentPointerValue":2,"nextPointer":null}
1609158613385wbQvkB5djUXB debug log: Queue is full
*/
console.log(queue.iterate());
// OUTPUT: [ 1, 2 ]

console.log(queue.size);
// OUTPUT: 2

console.log(queue.first());
/* OUTPUT:
SumoNode {
currentPointerValue: 1,
nextPointer: SumoNode { currentPointerValue: 2, nextPointer: null }
}
*/

console.log(queue.first().currentPointerValue);
// OUTPUT: 1

console.log(queue.last());
/* OUTPUT:
SumoNode { currentPointerValue: 2, nextPointer: null }
*/

console.log(queue.isEmpty());
// OUTPUT: false

console.log(queue.isFull());
// OUTPUT: true

```

<!-- TABLE OF CONTENTS -->
<details open="open">
<summary>Table of Contents</summary>
<ol>
<li>
<a href="#about-the-project">About The Project</a>
<ul>
<li><a href="#built-with">Built With</a></li>
</ul>
</li>
<li>
<a href="#getting-started">Getting Started</a>
<ul>
<li><a href="#installation">Installation</a></li>
</ul>
</li>
<li><a href="#usage">Usage</a></li>
<li><a href="#roadmap">Roadmap</a></li>
<li><a href="#contributing">Contributing</a></li>
<li><a href="#license">License</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#contact">Sponsor</a></li>
</ol>
</details>



<!-- ABOUT THE PROJECT -->
## About The Package

In computer science, a queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence. (Wikipedia).

When you are dealing with larger arrays, it's always suggested to use Queue, because of constant time complexity O(1).

Sumo Queue has all the queue operations implemented. Use it especially for the larger array to attain O(1) complexity.

### Built With

This package is built with raw javascript and Mocha / Chai is used for testing.

* [Javascript](https://www.javascript.com/)
* [Mocha](https://mochajs.org/)
* [Chai](https://www.chaijs.com/)



<!-- GETTING STARTED -->
## Getting Started

Install this package and follow examples given below. We have `examples` folder which has implementation code as well.
### Installation

1. Install NPM packages
```sh
npm i --save sumo-queue
```

<!-- USAGE EXAMPLES -->
## Usage

Once this package is added in your project as mentioned in the `installation`, you need to import this package and create a `QUEUE` class to start using it.

## Eg:

```javascript
# Import Package
const Queue = require("sumo-queue");
const queue = new Queue(2);
// OUTPUT: 1609158613385wbQvkB5djUXB debug log: A new queue 1609158613385wbQvkB5djUXB is initialized with capacity 2
queue.enqueue(1);
// OUTPUT: 1609158613385wbQvkB5djUXB debug log: Node added {"currentPointerValue":1,"nextPointer":null}
console.log(queue.iterate());
// OUTPUT: [ 1 ]
console.log(queue.size);
// OUTPUT: 1
queue.bulkEnqueue([2, 3]);
/* OUTPUT:
1609158613385wbQvkB5djUXB debug log: Node added {"currentPointerValue":2,"nextPointer":null}
1609158613385wbQvkB5djUXB debug log: Queue is full
*/
console.log(queue.iterate());
// OUTPUT: [ 1, 2 ]
console.log(queue.size);
// OUTPUT: 2
console.log(queue.first());
/* OUTPUT:
SumoNode {
currentPointerValue: 1,
nextPointer: SumoNode { currentPointerValue: 2, nextPointer: null }
}
*/
console.log(queue.first().currentPointerValue);
// OUTPUT: 1
console.log(queue.last());
/* OUTPUT:
SumoNode { currentPointerValue: 2, nextPointer: null }
*/
console.log(queue.isEmpty());
// OUTPUT: false
console.log(queue.isFull());
// OUTPUT: true
```
## Test
Test cases is written in `test/test.js`. To test this package, run `npm run test`.
<!-- ROADMAP -->
## Roadmap
See the [open issues](https://github.com/sanmak/sumo-queue/issues) for a list of proposed features (and known issues).
<!-- CONTRIBUTING -->
## Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are **greatly appreciated**.
1. Fork the Project
2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)
3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the Branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request
<!-- LICENSE -->
## License
Distributed under the MIT License. See `LICENSE` for more information.
<!-- CONTACT -->
## Contact
SANKET MAKHIJA - [@sanket_dude](https://twitter.com/sanket_dude) - sanket[dot]mahija[at]gmail[dot]com
## Sponsor ❤️
Consider sponsoring this package and help open source community and contributions.
<a href="https://ko-fi.com/E1E72C2MM" target="_blank"> <img style={kofiStyle} src="https://cdn.ko-fi.com/cdn/kofi1.png?v=2"></img> </a>
18 changes: 18 additions & 0 deletions examples/simple.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const Queue = require("../");

const queue = new Queue(2);
queue.enqueue(1);
console.log(queue.iterate());
console.log(queue.size);

queue.bulkEnqueue([2, 3]);
console.log(queue.iterate());
console.log(queue.size);

console.log(queue.first());
console.log(queue.first().currentPointerValue);
console.log(queue.last());

console.log(queue.isEmpty());

console.log(queue.isFull());
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require("./src/index");
Loading

0 comments on commit 4061984

Please sign in to comment.