ultra-mega-enumerator
Version:
Ultra Mega Enumerator is a lightweight library designed to enumerate various combinatorial objects.
157 lines • 4.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SupersetOf = exports.SubsetOf = exports.Ordering = exports.CustomComparisonChain = exports.Utils = void 0;
class Utils {
static calcIntervalVector(input) {
const n = input.length;
const m = Math.floor(n / 2);
const result = [];
for (let i = 1; i <= m; i++) {
let k = 0;
for (let j = 0; j < n; j++) {
if (input[j] && input[(i + j) % n]) {
k++;
}
}
// Adjust the count for even-sized arrays
if (i === m && n % 2 === 0) {
k = Math.floor(k / 2);
}
result.push(k);
}
return result;
}
static calcIntervalVectorFromBitSet(input) {
const n = input.size();
const m = Math.floor(n / 2);
const result = [];
for (let i = 1; i <= m; i++) {
let k = 0;
for (let j = 0; j < n; j++) {
if (input.get(j) && input.get((i + j) % n)) {
k++;
}
}
// Adjust the count for even-sized arrays
if (i === m && n % 2 === 0) {
k = Math.floor(k / 2);
}
result.push(k);
}
return result;
}
/**
* Rotates a sequence n positions to the right. If n is negative, rotates -n positions to the left.
*
* @param arr The array to be rotated.
* @param n The number of positions to rotate. Positive for right, negative for left.
* @return A new rotated array.
*/
static rotate(arr, n) {
const len = arr.length;
if (len === 0)
return []; // Return an empty array if input is empty
n = ((n % len) + len) % len; // Normalize n to be within the array length
return [...arr.slice(len - n), ...arr.slice(0, len - n)]; // Rotate the array
}
/**
* Rotates sequence 1 position to the right.
*
* @param arr The array to be rotated.
* @return A new array rotated to the right.
*/
static rotateRight(arr) {
return this.rotate(arr, 1);
}
/**
* Rotates sequence 1 position to the left.
*
* @param arr The array to be rotated.
* @return A new array rotated to the left.
*/
static rotateLeft(arr) {
return this.rotate(arr, -1);
}
}
exports.Utils = Utils;
class Ordering {
constructor(comparator) {
this.comparator = comparator;
}
static natural() {
return new Ordering((a, b) => {
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
});
}
nullsFirst() {
const originalComparator = this.comparator;
return new Ordering((a, b) => {
if (a === null && b === null)
return 0;
if (a === null)
return -1; // nulls come first
if (b === null)
return 1;
return originalComparator(a, b);
});
}
// This method returns the comparison function
getComparator() {
return this.comparator;
}
}
exports.Ordering = Ordering;
class CustomComparisonChain {
constructor() {
this.comparisons = [];
}
static start() {
return new CustomComparisonChain();
}
// Setting the values for comparison
setValues(a, b) {
this.a = a;
this.b = b;
return this; // for method chaining
}
compare(comparator) {
// Store a comparison function that takes the stored a and b
this.comparisons.push(() => comparator(this.a, this.b));
return this; // for method chaining
}
result() {
for (const compareFn of this.comparisons) {
const comparisonResult = compareFn();
if (comparisonResult !== 0) {
return comparisonResult;
}
}
return 0;
}
}
exports.CustomComparisonChain = CustomComparisonChain;
class SubsetOf {
constructor(pCS12) {
this.pCS12 = pCS12;
}
apply(input) {
// Check if pCS12 is a subset of input by comparing the size after intersection
return input.intersect(this.pCS12).getK() === input.getK();
}
}
exports.SubsetOf = SubsetOf;
class SupersetOf {
constructor(pCS12) {
this.pCS12 = pCS12;
}
apply(input) {
// Check if pCS12 is a subset of input by comparing the size after intersection
return this.pCS12.intersect(input).getK() === this.pCS12.getK();
}
}
exports.SupersetOf = SupersetOf;
//# sourceMappingURL=Utils.js.map