make compare rely less on fragment index

also indenting
This commit is contained in:
Bruno Windels 2019-05-11 09:51:27 +02:00
parent 99c8816bf9
commit d6ae313bbd

View file

@ -19,7 +19,7 @@ export default class SortKey {
// start default key right at the middle fragment key, min event key
// so we have the same amount of key address space either way
this.fragmentId = MID;
this.eventIndex = MIN;
this.eventIndex = MID;
}
this._fragmentIdComparer = fragmentIdComparer;
}
@ -92,10 +92,10 @@ export default class SortKey {
return this.eventIndex - otherKey.eventIndex;
} else {
// minKey and maxKey might not have fragmentIdComparer, so short-circuit this first ...
if (this.fragmentId === MIN || otherKey.fragmentId === MAX) {
if ((this.fragmentId === MIN && otherKey.fragmentId !== MIN) || (this.fragmentId !== MAX && otherKey.fragmentId === MAX)) {
return -1;
}
if (this.fragmentId === MAX || otherKey.fragmentId === MIN) {
if ((this.fragmentId === MAX && otherKey.fragmentId !== MAX) || (this.fragmentId !== MIN && otherKey.fragmentId === MIN)) {
return 1;
}
// ... then delegate to fragmentIdComparer.
@ -114,10 +114,31 @@ export function tests() {
const fragmentIdComparer = {compare: (a, b) => a - b};
return {
test_no_fragment_index(assert) {
const min = SortKey.minKey;
const max = SortKey.maxKey;
const a = new SortKey();
a.eventIndex = 1;
a.fragmentId = 1;
assert(min.compare(min) === 0);
assert(max.compare(max) === 0);
assert(a.compare(a) === 0);
assert(min.compare(max) < 0);
assert(max.compare(min) > 0);
assert(min.compare(a) < 0);
assert(a.compare(min) > 0);
assert(max.compare(a) > 0);
assert(a.compare(max) < 0);
},
test_default_key(assert) {
const k = new SortKey(fragmentIdComparer);
assert.equal(k.fragmentId, MID);
assert.equal(k.eventIndex, MIN);
assert.equal(k.eventIndex, MID);
},
test_inc(assert) {
@ -176,6 +197,7 @@ export function tests() {
b.fragmentId = 1;
b.eventIndex = 2;
assert(a.compare(b) > 0);
assert(b.compare(a) < 0);
},
test_cmp_max_larger_than_min(assert) {