<commit_before>var cadence = require('cadence')
var riffle = require('riffle')

function Forward (comparator, versions, iterator, next) {
    this._iterator = iterator
    this._comparator = comparator
    this._versions = versions
    this._next = next
}

function valid (versions) {
    return function (key) { return versions[key.version] }
}

Forward.prototype.next = cadence(function (step) {
    if (!this._next) return this._next
    step(function () {
        var next = this._next
        step(function () {
            this._iterator.next(valid(this._versions), step())
        }, function (candidate) {
            if (candidate && this._comparator(candidate.value, next.value) == 0) {
                next = candidate
            } else {
                this._next = candidate
                step(null, next)
            }
        })()
    })
})

Forward.prototype.unlock = function () {
    this._iterator.unlock()
}

exports.forward = cadence(function (step, strata, comparator, versions, key) {
    var composite = { value: key, version: 0 }
    step(function () {
        riffle.forward(strata, composite, step())
    }, function (iterator) {
        step (function () {
            iterator.next(valid(versions), step())
        }, function (next) {
            return new Forward(comparator, versions, iterator, next)
        })
    })
})

function Reverse (comparator, versions, iterator, next) {
    this._iterator = iterator
    this._comparator = comparator
    this._versions = versions
    this._next = next
}

Reverse.prototype.next = cadence(function (step) {
    if (!this._next) return this._next
    step(function () {
        var next = this._next
        step(function () {
            this._iterator.next(valid(this._versions), step())
        }, function (candidate) {
            if (!candidate || this._comparator(candidate.value, next.value) != 0) {
                this._next = candidate
                step(null, next)
            }
        })()
    })
})

Reverse.prototype.unlock = function () {
    this._iterator.unlock()
}

exports.reverse = cadence(function (step, strata, comparator, versions, key) {
    var composite = { value: key, version: Number.MAX_VALUE }
    step(function () {
        riffle.reverse(strata, composite, step())
    }, function (iterator) {
        step (function () {
            iterator.next(valid(versions), step())
        }, function (next) {
            return new Reverse(comparator, versions, iterator, next)
        })
    })
})<commit_msg>Return record and key from `next`.<commit_after>var cadence = require('cadence')
var riffle = require('riffle')

function Forward (comparator, versions, iterator, record, key) {
    this._iterator = iterator
    this._comparator = comparator
    this._versions = versions
    this._next = { record: record, key: key }
}

function valid (versions) {
    return function (key) { return versions[key.version] }
}

Forward.prototype.next = cadence(function (step) {
    if (!this._next) return step(null)
    step(function () {
        var next = this._next
        step(function () {
            this._iterator.next(valid(this._versions), step())
        }, function (record, key) {
            if (key && this._comparator(key.value, next.key.value) == 0) {
                next = { record: record, key: key }
            } else {
                this._next = record && { record: record, key: key }
                step(null, next.record, next.key)
            }
        })()
    })
})

Forward.prototype.unlock = function () {
    this._iterator.unlock()
}

exports.forward = cadence(function (step, strata, comparator, versions, key) {
    var composite = { value: key, version: 0 }
    step(function () {
        riffle.forward(strata, composite, step())
    }, function (iterator) {
        step (function () {
            iterator.next(valid(versions), step())
        }, function (record, key) {
            return new Forward(comparator, versions, iterator, record, key)
        })
    })
})

function Reverse (comparator, versions, iterator, record, key) {
    this._iterator = iterator
    this._comparator = comparator
    this._versions = versions
    this._next = { record: record, key: key }
}

Reverse.prototype.next = cadence(function (step) {
    if (!this._next) return step(null)
    step(function () {
        var next = this._next
        step(function () {
            this._iterator.next(valid(this._versions), step())
        }, function (record, key) {
            if (!key || this._comparator(key.value, next.key.value) != 0) {
                this._next = record && { record: record, key: key }
                step(null, next.record, next.key)
            }
        })()
    })
})

Reverse.prototype.unlock = function () {
    this._iterator.unlock()
}

exports.reverse = cadence(function (step, strata, comparator, versions, key) {
    var composite = { value: key, version: Number.MAX_VALUE }
    step(function () {
        riffle.reverse(strata, composite, step())
    }, function (iterator) {
        step (function () {
            iterator.next(valid(versions), step())
        }, function (record, key) {
            return new Reverse(comparator, versions, iterator, record, key)
        })
    })
})
{'fraction_non_alphanumeric': 0.1347544727826418, 'fraction_numerical': 0.0011419870574800152, 'mean_word_length': 2.3729139922978177, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7257779', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1477, 'n_words': 358}