Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | 5x 1008x 1005x 292x 292x 5x 5000x 3994x 1006x 1006x 5x 5x 195x 195x 24612x 14013x 14013x 50993x 5x 59x 34788x 33651x 1137x 59x 535x 114216x 22596x 22596x 91620x 114216x 535x 535x 10159x 29x | import { Transaction } from './transaction_pool'; interface QueueIndex { [index: string]: Transaction | undefined; } interface RemoveForReduceObject { readonly affected: Transaction[]; readonly unaffected: Transaction[]; } interface ReduceUntilObject { readonly affected: Transaction[]; readonly conditionFailedOnce: boolean; readonly unaffected: Transaction[]; } export class Queue { private readonly _index: QueueIndex; private _transactions: ReadonlyArray<Transaction>; public get transactions(): ReadonlyArray<Transaction> { return this._transactions; } public get index(): QueueIndex { return this._index; } public constructor() { this._transactions = []; this._index = {}; } public dequeueUntil( condition: (transaction: Transaction) => boolean, ): ReadonlyArray<Transaction> { const reduceResult: ReduceUntilObject = this._transactions.reduceRight( ( { affected, unaffected, conditionFailedOnce }: ReduceUntilObject, transaction: Transaction, ) => { // Add transaction to the unaffected list if the condition failed for this transaction or any previous transaction if (conditionFailedOnce || !condition(transaction)) { return { affected, unaffected: [transaction, ...unaffected], conditionFailedOnce: true, }; } // Delete the index of the transaction which passed the condition // tslint:disable-next-line:no-dynamic-delete delete this._index[transaction.id]; return { affected: [...affected, transaction], unaffected, conditionFailedOnce: false, }; }, { affected: [], unaffected: [], conditionFailedOnce: false, }, ); this._transactions = reduceResult.unaffected; return reduceResult.affected; } public enqueueMany(transactions: ReadonlyArray<Transaction>): void { this._transactions = [...transactions, ...this._transactions]; transactions.forEach((transaction: Transaction) => { this._index[transaction.id] = transaction; }); } public enqueueOne(transaction: Transaction): void { this._transactions = [transaction, ...this._transactions]; this._index[transaction.id] = transaction; } public exists(id: string): boolean { return !!this._index[id]; } public filter( condition: (transaction: Transaction) => boolean, ): ReadonlyArray<Transaction> { return this._transactions.filter(condition); } public peekUntil( condition: (transaction: Transaction) => boolean, ): ReadonlyArray<Transaction> { const reduceResult: ReduceUntilObject = this._transactions.reduceRight( ( { affected, unaffected, conditionFailedOnce }: ReduceUntilObject, transaction: Transaction, ) => { // Add transaction to the unaffected list if the condition failed for this transaction or any previous transaction if (conditionFailedOnce || !condition(transaction)) { return { affected, unaffected, conditionFailedOnce: true, }; } return { affected: [...affected, transaction], unaffected, conditionFailedOnce: false, }; }, { affected: [], unaffected: [], conditionFailedOnce: false, }, ); return reduceResult.affected; } public removeFor( condition: (transaction: Transaction) => boolean, ): ReadonlyArray<Transaction> { const { unaffected, affected } = this._transactions.reduce( (reduceObject: RemoveForReduceObject, transaction: Transaction) => { if (condition(transaction)) { reduceObject.affected.push(transaction); // tslint:disable-next-line no-dynamic-delete delete this._index[transaction.id]; } else { reduceObject.unaffected.push(transaction); } return reduceObject; }, { unaffected: [], affected: [] }, ); this._transactions = unaffected; return affected; } public size(): number { return this._transactions.length; } public sizeBy(condition: (transaction: Transaction) => boolean): number { return this._transactions.filter(condition).length; } } |