trxcllnt commented on a change in pull request #9962: URL: https://github.com/apache/arrow/pull/9962#discussion_r611226795
########## File path: js/src/util/bit.ts ########## @@ -64,16 +64,51 @@ export function packBools(values: Iterable<any>) { } /** @ignore */ -export function* iterateBits<T>(bytes: Uint8Array, begin: number, length: number, context: any, - get: (context: any, index: number, byte: number, bit: number) => T) { - let bit = begin % 8; - let byteIndex = begin >> 3; - let index = 0, remaining = length; - for (; remaining > 0; bit = 0) { - let byte = bytes[byteIndex++]; - do { - yield get(context, index++, byte, bit); - } while (--remaining > 0 && ++bit < 8); +export class BitIterator<T> implements IterableIterator<T> { + bit: number; + byte: number; + byteIndex: number; + index: number; + + constructor( + private bytes: Uint8Array, + begin: number, + private length: number, + private context: any, + private get: (context: any, index: number, byte: number, bit: number) => T + ) { + this.bit = begin % 8; + this.byteIndex = begin >> 3; + this.byte = bytes[this.byteIndex]; + this.index = 0; + } + + next(): IteratorResult<T> { + if (this.index >= this.length) { + return { + done: true, + value: null + }; + } + + if (this.bit < 8) { + return { + value: this.get(this.context, this.index++, this.byte, this.bit++) + }; + } + + this.byteIndex++; + this.bit = 0; + + if (this.byteIndex < this.bytes.length) { + this.byte = this.bytes[this.byteIndex]; + } + + return this.next(); + } Review comment: Can we do this without recursion? ```suggestion next(): IteratorResult<T> { if (this.index < this.length) { if (this.bit === 8) { this.bit = 0; this.byte = this.bytes[this.byteIndex++]; } return { done: false, value: this.get(this.context, this.index++, this.byte, this.bit++) }; } return { done: true, value: null }; } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org