You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.3 KiB

3 years ago
/**
* Mnemonist Heap Comparators
* ===========================
*
* Default comparators & functions dealing with comparators reversing etc.
*/
var DEFAULT_COMPARATOR = function(a, b) {
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
};
var DEFAULT_REVERSE_COMPARATOR = function(a, b) {
if (a < b)
return 1;
if (a > b)
return -1;
return 0;
};
/**
* Function used to reverse a comparator.
*/
function reverseComparator(comparator) {
return function(a, b) {
return comparator(b, a);
};
}
/**
* Function returning a tuple comparator.
*/
function createTupleComparator(size) {
if (size === 2) {
return function(a, b) {
if (a[0] < b[0])
return -1;
if (a[0] > b[0])
return 1;
if (a[1] < b[1])
return -1;
if (a[1] > b[1])
return 1;
return 0;
};
}
return function(a, b) {
var i = 0;
while (i < size) {
if (a[i] < b[i])
return -1;
if (a[i] > b[i])
return 1;
i++;
}
return 0;
};
}
/**
* Exporting.
*/
exports.DEFAULT_COMPARATOR = DEFAULT_COMPARATOR;
exports.DEFAULT_REVERSE_COMPARATOR = DEFAULT_REVERSE_COMPARATOR;
exports.reverseComparator = reverseComparator;
exports.createTupleComparator = createTupleComparator;