Skip to content

Commit

Permalink
use Topolist compare method wherever we compare nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
chm-diederichs committed Feb 21, 2024
1 parent 0938a5f commit e7667a1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 40 deletions.
41 changes: 1 addition & 40 deletions lib/linearizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,39 +161,6 @@ module.exports = class Linearizer {
}
}

/* Tip ordering methods */

_orderTip () {
const tip = []
const stack = [...this.tails]

while (stack.length) {
const node = stack.pop()
if (node.ordering) continue

node.ordering = node.dependencies.size
stack.push(...node.dependents)
}

stack.push(...this.tails)
stack.sort(keySort)

while (stack.length) {
const node = stack.pop()
tip.push(node)

const batch = []

for (const dep of node.dependents) {
if (--dep.ordering === 0) batch.push(dep)
}

if (batch.length > 0) stack.push(...batch.sort(keySort))
}

return tip
}

/* Ack methods */

shouldAck (writer, pending = false) {
Expand Down Expand Up @@ -390,20 +357,14 @@ module.exports = class Linearizer {
}
}

// if same key, earlier node is first
function tieBreak (a, b) {
return keySort(a, b) > 0 // keySort sorts high to low
return Topolist.compare(a, b) < 0 // lowest key wis
}

function getFirst (set) {
return set[Symbol.iterator]().next().value
}

function keySort (a, b) {
const cmp = b4a.compare(a.writer.core.key, b.writer.core.key)
return cmp === 0 ? b.length - a.length : -cmp
}

function sameNode (a, b) {
return b4a.equals(a.key, b.writer.core.key) && a.length === b.length
}
4 changes: 4 additions & 0 deletions lib/topolist.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ module.exports = class TopoList {
this.shared = 0
}

static compare (a, b) {
return cmp(a, b)
}

mark () {
this.shared = this.tip.length
this.undo = 0
Expand Down

0 comments on commit e7667a1

Please sign in to comment.