UNPKG

ultra-mega-enumerator

Version:

Ultra Mega Enumerator is a lightweight library designed to enumerate various combinatorial objects.

81 lines 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Necklace = void 0; const Utils_1 = require("../Utils"); class Necklace extends Array { constructor(k, p_Order, array) { super(...array); let ex = false; for (let i = 0; i < array.length; i++) { if (array[i] >= k || array[i] < 0) { ex = true; break; } } if (ex) { throw new Error("Necklace constructor received incoherent arguments."); } this.m_Order = p_Order; } getOrder() { return this.m_Order; } // Comparison method between two necklaces compareTo(other) { const minLength = Math.min(this.length, other.length); for (let i = 0; i < minLength; i++) { if (this[i] < other[i]) return -1; if (this[i] > other[i]) return 1; } // If they are the same for the length of the shorter one, check lengths if (this.length < other.length) return -1; if (this.length > other.length) return 1; return 0; // They are equal } // Method to calculate the period of the necklace getPeriod() { let p = this.length; // Initialize 'p' to the size of the necklace for (let i = 1; i < this.length; i++) { if (this.compareTo(Utils_1.Utils.rotate(Array.from(this), i)) === 0) { p = i; // Found a rotation that matches break; // Exit the loop } } return p; // Return the period } static generate(n, k) { Necklace.cnt = 0; const output = new Set(); const a = Array(n + 1).fill(0); Necklace.subGen(1, 1, n, k, a, output); Necklace.cnt = 0; return output; } static subGen(t, p, n, k, a, output) { if (t > n) { if (n % p === 0) { const tmp = Array(n); for (let i = 0; i < n; i++) { tmp[i] = a[i + 1]; } output.add(new Necklace(k, Necklace.cnt, tmp)); Necklace.cnt++; } } else { a[t] = a[t - p]; Necklace.subGen(t + 1, p, n, k, a, output); for (let j = a[t - p] + 1; j < k; j++) { a[t] = j; Necklace.subGen(t + 1, t, n, k, a, output); } } } } exports.Necklace = Necklace; Necklace.cnt = 0; //# sourceMappingURL=Necklace.js.map