-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:AbmSourav/dataStructure into release
- Loading branch information
Showing
9 changed files
with
727 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { BlockChainApi, BlockType, DataType } from "./helper.d.ts"; | ||
import { searchGenerator, iteratorGenerator } from "./generators.ts" | ||
import { Block } from "./blockNode.ts"; | ||
|
||
export class BlockChain implements BlockChainApi { | ||
#chain: Array<any> | ||
length: number | ||
|
||
constructor() { | ||
this.#chain = new Array() | ||
this.length = this.#chain.length | ||
} | ||
|
||
createBlock(data: DataType<any>) { | ||
const newBlock = new Block(data, this.#chain.length + 1) | ||
|
||
if (this.#chain.length === 0) { | ||
this.#chain.push(newBlock) | ||
this.length++ | ||
|
||
return true | ||
} | ||
|
||
if (this.#chain.length > 0) { | ||
const prevBlock = this.latestBlock() | ||
newBlock.prevHash = prevBlock!.hash | ||
this.#chain.push(newBlock) | ||
this.length++ | ||
|
||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
search(key: string) { | ||
if (this.#chain.length === 0) { | ||
return false | ||
} | ||
|
||
const iterator = searchGenerator(key, this.#chain) | ||
let iteratorNext = iterator.next() | ||
while (iteratorNext.value) { | ||
return iteratorNext.value | ||
} | ||
|
||
return false; | ||
} | ||
|
||
latestBlock() { | ||
if (this.#chain.length <= 0) return false | ||
return this.#chain[this.#chain.length - 1] | ||
} | ||
|
||
iterator() { | ||
return iteratorGenerator(this.#chain) | ||
} | ||
|
||
checkValidation() { | ||
const iterator = this.iterator() | ||
let iteratNext = iterator.next() | ||
let prevHash: null|string = null | ||
let prevBlock: null|BlockType = null | ||
|
||
while (iteratNext.value) { | ||
const block = iteratNext.value | ||
const regeneratedHash = block.regenerateHash() | ||
if (block.hash !== regeneratedHash) { | ||
console.error(`index: ${block.index}, block is corrupted`) | ||
return false | ||
} | ||
if (prevBlock != null && block.prevHash !== prevHash) { | ||
console.error(`index: ${prevBlock!.index}, block is corrupted`) | ||
return false | ||
} | ||
|
||
prevHash = block.regenerateHash() | ||
prevBlock = block | ||
iteratNext = iterator.next() | ||
} | ||
|
||
console.log('The Chain is valid.') | ||
return true | ||
} | ||
|
||
log() { | ||
console.log(this.#chain) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { DataType } from "./helper.d.ts"; | ||
import { createHash } from "https://deno.land/std/hash/mod.ts"; | ||
|
||
function blockHash(index: number, data: any, time: Date) { | ||
let hash = createHash("sha256") | ||
hash = hash.update(JSON.stringify(data) + index + time) | ||
|
||
return hash.toString(); | ||
} | ||
|
||
export class Block { | ||
index: number | ||
data: DataType<any> | ||
hash: string | ||
prevHash: string|null | ||
time: Date | ||
|
||
constructor(data: DataType<any>, index: number) { | ||
this.index = index | ||
this.data = data | ||
this.time = new Date() | ||
this.hash = blockHash(index, data, this.time) | ||
this.prevHash = null; | ||
} | ||
|
||
regenerateHash() { | ||
const data: DataType<any>|any = this.data | ||
const hash = blockHash(this.index, data, this.time) | ||
|
||
return hash.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export function *searchGenerator(key: string|number, chain: Array<any>) { | ||
for (let block in chain) { | ||
if (chain[block].data.key === key) { | ||
yield chain[block] | ||
} | ||
} | ||
return false | ||
} | ||
|
||
export function *iteratorGenerator(chain: Array<any>) { | ||
for (let block in chain) { | ||
yield chain[block] | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
export type DataType<T> = { | ||
key: string | ||
value: T | ||
} | ||
|
||
// singly linked list node class | ||
export type BlockType = { | ||
index: number | ||
data: DataType<any> | ||
time: Date | ||
hash: string | ||
prevHash: null|string | ||
}|null | ||
|
||
// singly linked list interface | ||
export interface BlockChainApi { | ||
length: number | ||
createBlock(data: any): boolean | ||
search(key: string): boolean|BlockType | ||
latestBlock(): boolean|BlockType | ||
log(): void; | ||
iterator(): Generator | ||
checkValidation(): boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
## BlockChain Api | ||
<p><code>sha256</code> has been used for creating Hashes.</p> | ||
|
||
```ts | ||
DataType<T> = { | ||
key: string|number | ||
value: T | ||
} | ||
|
||
// Time Complexity: O(1) | ||
length: number; | ||
|
||
// *generator function, it returens an iterator. | ||
// loop through all blocks in the chain | ||
iterator(): Generator | ||
|
||
// Time Complexity: O(n) | ||
// check for 'the blockChain is corrupted/manipulated or valid'. | ||
checkValidation(): boolean | ||
|
||
// Time Complexity: O(1) | ||
createBlock(data: any): boolean | ||
|
||
// Time Complexity: O(1) | ||
latestBlock(): boolean|BlockType | ||
|
||
// Time Complexity: O(n) | ||
log(): void; | ||
|
||
// Time Complexity: O(n) | ||
search(key: string): boolean|BlockType | ||
|
||
``` | ||
|
||
<br> | ||
<br> | ||
|
||
## Examples | ||
```ts | ||
import { BlockChain } from "https://deno.land/x/datastructure/mod.ts"; | ||
|
||
const blockChain = new BlockChain() | ||
|
||
// iterator method returns a *generator function | ||
const iterator = blockChain.iterator() | ||
let iteratorNext = iterator.next() | ||
// console.log(iteratorNext.next(), iteratorNext.next()); | ||
while (iteratorNext.done === false) { | ||
console.log(iteratorNext.value); | ||
iteratorNext = iterator.next() | ||
} | ||
|
||
// length or count of total blocks. | ||
blockChain.length | ||
|
||
// create a block in the blockChain. | ||
blockChain.createBlock({key: 'sourav', value: {name: "Sourav"}}) | ||
blockChain.createBlock({key: 'abm', value: "AbmSourav"}) | ||
blockChain.createBlock({key: 'JS', value: ['JS', 'TS']}) | ||
|
||
// get latest created block | ||
blockChain.latestBlock() | ||
|
||
// console all values | ||
blockChain.log() | ||
|
||
// searching data in in the blocks | ||
blockChain.search('JS') | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { BlockChain } from "./blockChain.ts"; | ||
|
||
const blockChain = new BlockChain() | ||
|
||
blockChain.createBlock({key: 'sourav', value: "Sourav"}) | ||
blockChain.createBlock({key: 'abm', value: "AbmSourav"}) | ||
blockChain.createBlock({key: 'apple', value: "Apple Inc."}) | ||
|
||
// const it = blockChain.iterator() | ||
// console.log(it.next(), it.next(), it.next(), it.next()); | ||
|
||
// const latestBlock = blockChain.latestBlock() | ||
// console.log(latestBlock); | ||
|
||
// latestBlock.data.key = '1' | ||
// blockChain.test() | ||
// console.log(blockChain.checkValidation()); | ||
|
||
// blockChain.log() | ||
// console.log(blockChain.search('apple')) | ||
console.log(blockChain.length) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,13 +9,14 @@ | |
* @email <[email protected]> | ||
* @authorUrl https://abmsourav.com | ||
* @sourceCode https://github.com/AbmSourav/dataStructure | ||
* @version 1.0.4 | ||
* @version 1.0.5 | ||
* | ||
* Copyright (c) 2021 Keramot UL Islam | ||
*/ | ||
|
||
export { HashTable } from "./hashTable/hashTable.bundle.js"; | ||
export { SinglyLinkedList } from "./linkedList/singly/singlyLinkedList.bundle.js"; | ||
export { DoublyLinkedList } from "./linkedList/doubly/doublyLinkedList.bundle.js"; | ||
export { Stack } from "./stack/stack.bundle.js" | ||
export { Queue } from "./queue/queue.bundle.js" | ||
export { BlockChain } from "./blockChain/blockChain.bundle.js"; | ||
export { HashTable } from "./hashTable/hashTable.bundle.js"; | ||
export { SinglyLinkedList } from "./linkedList/singly/singlyLinkedList.bundle.js"; | ||
export { DoublyLinkedList } from "./linkedList/doubly/doublyLinkedList.bundle.js"; | ||
export { Stack } from "./stack/stack.bundle.js" | ||
export { Queue } from "./queue/queue.bundle.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,12 @@ | |
* @email <[email protected]> | ||
* @authorUrl https://abmsourav.com | ||
* @sourceCode https://github.com/AbmSourav/dataStructure | ||
* @version 1.0.4 | ||
* @version 1.0.5 | ||
* | ||
* Copyright (c) 2021 Keramot UL Islam | ||
*/ | ||
|
||
export { BlockChain } from "./blockChain/blockChain.ts"; | ||
export { HashTable } from "./hashTable/hashTable.ts"; | ||
export { SinglyLinkedList } from "./linkedList/singly/singlyLinkedList.ts"; | ||
export { DoublyLinkedList } from "./linkedList/doubly/doublyLinkedList.ts"; | ||
|