custom-permutation 1.0.7 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,12 +1,21 @@
1
+ # Version
2
+ 1.1.0
3
+
1
4
  # Fixed bugs
5
+ 23.09.2024 - Edge cases are handled and the codespace is simplified.
2
6
  25.09.2023 - unChoices did not reflect always, fixed now.
3
7
 
4
8
  # Custom Permutation Generator
5
9
 
6
- ## 1. require / import
10
+ ## 1.1. `require`
7
11
  ```ts
8
12
  const CustomPermutation = require('custom-permutation')
9
13
  ```
14
+ ## 1.2. ,`import`
15
+
16
+ ```ts
17
+ import CustomPermutation from "custom-permutation";
18
+ ```
10
19
 
11
20
  ## 2. Constructor
12
21
  ```ts
@@ -18,36 +27,36 @@ CustomPermutation(elList:[els...], choices:{ index: [els...]] }, nonChoices:{ in
18
27
  _example:_
19
28
 
20
29
  ```ts
21
- CustomPermutation(['a', 'b', 'c'], {1: ['a', 'b']}, {0: ['a']})
30
+ CustomPermutation(["a", "b", "c"], {1: ["a", "b"]}, {0: ["a"]})
22
31
  ```
23
32
 
24
- Permutate 3 elements which are 'a', 'b' and 'c' with below rules
33
+ Permutate 3 elements which are "a", "b" and "c" with below rules
25
34
 
26
35
  __- choices rule:__
27
36
  ```json
28
- { 1: ['a', 'b'] }
37
+ { "1": ["a", "b"] }
29
38
  ```
30
39
 
31
- At index=1 there can only be the element 'a' or 'b'
40
+ At index=1 there can only be the element "a" or "b"
32
41
 
33
42
  __- nonChoices rule:__
34
43
  ```json
35
- { 0: ['a'] }
44
+ { "0": ["a"] }
36
45
  ```
37
46
 
38
- At index=0 there can NOT be the element 'a'
47
+ At index=0 there can NOT be the element "a"
39
48
 
40
49
  _Note: given index are considered as 0 based: [index=0, index=1, etc.]_
41
50
 
42
51
  ## 4. Result set explanation:
43
52
 
44
53
  Let's see all permutations, and which ones are valid or not.
45
- - `['a', 'b', 'c']` : violates nonChoices rule: _first element is `'a'`, but shouldn't be `'a'`
46
- - `['a', 'c', 'b']` : violates nonChoices rule: _first element is `'a'`, but shouldn't be `'a'`_
47
- - `['b', 'a', 'c']` : ok
48
- - `['b', 'c', 'a']` : violates choices rule: _second element is `'c'` but `'a'` or `'b'` is desired_
49
- - `['c', 'a', 'b']` : ok
50
- - `['c', 'b', 'a']` : ok
54
+ - [ ] `["a", "b", "c"]` // violates nonChoices rule: _first element is `"a"`, but shouldn't be `"a"`
55
+ - [ ] `["a", "c", "b"]` // violates nonChoices rule: _first element is `"a"`, but shouldn't be `"a"`_
56
+ - [x] `["b", "a", "c"]`
57
+ - [ ] `["b", "c", "a"]` // violates choices rule: _second element is `"c"` but `"a"` or `"b"` is desired_
58
+ - [x] `["c", "a", "b"]`
59
+ - [x] `["c", "b", "a"]`
51
60
 
52
61
  So there are just 3 results that should be generated with this parameters.
53
62
 
@@ -57,9 +66,9 @@ So there are just 3 results that should be generated with this parameters.
57
66
 
58
67
  ```ts
59
68
  let customPerm = new CustomPermutation(
60
- ['a', 'b', 'c'],
61
- { 1: ['a', 'b'] },
62
- { 0: ['a'] }
69
+ ["a", "b", "c"],
70
+ { "1": ["a", "b"] },
71
+ { "0": ["a"] }
63
72
  );
64
73
  ```
65
74
 
@@ -71,10 +80,7 @@ let customPerm = new CustomPermutation(
71
80
  let next = customPerm.next();
72
81
 
73
82
  while (next) {
74
- // Use next
75
83
  console.log(next)
76
-
77
- // Get next
78
84
  next = customPerm.next();
79
85
  }
80
86
  ```
@@ -86,10 +92,7 @@ let generator = customPerm.generator();
86
92
  let next = generator.next();
87
93
 
88
94
  while (!next.done) {
89
- // Use next.value
90
95
  console.log(next.value)
91
-
92
- // Generate next one
93
96
  next = generator.next();
94
97
  }
95
98
  ```
@@ -99,7 +102,7 @@ while (!next.done) {
99
102
  __Console__
100
103
 
101
104
  ```sh
102
- ['b', 'a', 'c']\
103
- ['c', 'a', 'b']\
104
- ['c', 'b', 'a']
105
+ ["b", "a", "c"]
106
+ ["c", "a", "b"]
107
+ ["c", "b", "a"]
105
108
  ```
@@ -1,13 +1,12 @@
1
- import { CustomPermutationGenerator } from "./CustomPermutationGenerator";
1
+ import { CustomPermutationGenerator } from './CustomPermutationGenerator';
2
2
  export default class CustomPermutation {
3
3
  private listToPermutate;
4
4
  private choices;
5
5
  private nonChoices;
6
6
  private elementsOrderAbsolute?;
7
- private elementsOrderRelative?;
8
7
  private passFn?;
9
8
  customPermGen: CustomPermutationGenerator;
10
- constructor(listToPermutate: any[], choices: object, nonChoices: object, elementsOrderAbsolute?: any[], elementsOrderRelative?: any[], passFn?: Function);
9
+ constructor(listToPermutate: any[], choices: object, nonChoices: object, elementsOrderAbsolute?: any[], passFn?: (items: any[]) => boolean);
11
10
  next(): any;
12
11
  generator(): Generator<any, void, unknown>;
13
12
  }
@@ -1,74 +1,33 @@
1
1
  "use strict";
2
- var __generator = (this && this.__generator) || function (thisArg, body) {
3
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
4
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
5
- function verb(n) { return function (v) { return step([n, v]); }; }
6
- function step(op) {
7
- if (f) throw new TypeError("Generator is already executing.");
8
- while (_) try {
9
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
10
- if (y = 0, t) op = [op[0] & 2, t.value];
11
- switch (op[0]) {
12
- case 0: case 1: t = op; break;
13
- case 4: _.label++; return { value: op[1], done: false };
14
- case 5: _.label++; y = op[1]; op = [0]; continue;
15
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
16
- default:
17
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
18
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
19
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
20
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
21
- if (t[2]) _.ops.pop();
22
- _.trys.pop(); continue;
23
- }
24
- op = body.call(thisArg, _);
25
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
26
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
27
- }
28
- };
29
2
  Object.defineProperty(exports, "__esModule", { value: true });
30
- var CustomPermutationGenerator_1 = require("./CustomPermutationGenerator");
31
- var CustomPermutation = /** @class */ (function () {
32
- function CustomPermutation(listToPermutate, choices, nonChoices, elementsOrderAbsolute, elementsOrderRelative, passFn) {
3
+ const CustomPermutationGenerator_1 = require("./CustomPermutationGenerator");
4
+ class CustomPermutation {
5
+ constructor(listToPermutate, choices, nonChoices, elementsOrderAbsolute, passFn) {
33
6
  this.listToPermutate = listToPermutate;
34
7
  this.choices = choices;
35
8
  this.nonChoices = nonChoices;
36
9
  this.elementsOrderAbsolute = elementsOrderAbsolute;
37
- this.elementsOrderRelative = elementsOrderRelative;
38
10
  this.passFn = passFn;
39
11
  if (!(elementsOrderAbsolute === null || elementsOrderAbsolute === void 0 ? void 0 : elementsOrderAbsolute.length)) {
40
- elementsOrderAbsolute = new Array(listToPermutate.length, 0).map(function (x, i) { return i; });
12
+ elementsOrderAbsolute = Array.from(listToPermutate).map((x, i) => i);
41
13
  }
42
- if (!(elementsOrderRelative === null || elementsOrderRelative === void 0 ? void 0 : elementsOrderRelative.length)) {
43
- elementsOrderRelative = new Array(listToPermutate.length, 0).map(function (x, i) { return i; });
44
- }
45
- this.customPermGen = new CustomPermutationGenerator_1.CustomPermutationGenerator(listToPermutate, choices, nonChoices, elementsOrderAbsolute, elementsOrderRelative, passFn);
14
+ this.customPermGen = new CustomPermutationGenerator_1.CustomPermutationGenerator(this.listToPermutate, this.choices, this.nonChoices, this.elementsOrderAbsolute, this.passFn);
46
15
  }
47
- CustomPermutation.prototype.next = function () {
16
+ next() {
48
17
  return this.customPermGen.next();
49
- };
50
- CustomPermutation.prototype.generator = function () {
51
- var nextPerm;
52
- return __generator(this, function (_a) {
53
- switch (_a.label) {
54
- case 0:
55
- nextPerm = null;
56
- _a.label = 1;
57
- case 1:
58
- if (!true) return [3 /*break*/, 5];
59
- nextPerm = this.customPermGen.next();
60
- if (!nextPerm) return [3 /*break*/, 3];
61
- return [4 /*yield*/, nextPerm];
62
- case 2:
63
- _a.sent();
64
- return [3 /*break*/, 4];
65
- case 3: return [3 /*break*/, 5];
66
- case 4: return [3 /*break*/, 1];
67
- case 5: return [2 /*return*/];
18
+ }
19
+ *generator() {
20
+ let nextPerm = null;
21
+ while (true) {
22
+ nextPerm = this.customPermGen.next();
23
+ if (nextPerm) {
24
+ yield nextPerm;
68
25
  }
69
- });
70
- };
71
- return CustomPermutation;
72
- }());
26
+ else {
27
+ break;
28
+ }
29
+ }
30
+ }
31
+ }
73
32
  exports.default = CustomPermutation;
74
33
  //# sourceMappingURL=CustomPermutation.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"CustomPermutation.js","sourceRoot":"","sources":["../src/CustomPermutation.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2EAA0E;AAE1E;IAGE,2BACU,eAAsB,EACtB,OAAe,EACf,UAAkB,EAClB,qBAA6B,EAC7B,qBAA6B,EAC7B,MAAiB;QALjB,oBAAe,GAAf,eAAe,CAAO;QACtB,YAAO,GAAP,OAAO,CAAQ;QACf,eAAU,GAAV,UAAU,CAAQ;QAClB,0BAAqB,GAArB,qBAAqB,CAAQ;QAC7B,0BAAqB,GAArB,qBAAqB,CAAQ;QAC7B,WAAM,GAAN,MAAM,CAAW;QAGzB,IAAI,CAAC,CAAA,qBAAqB,aAArB,qBAAqB,uBAArB,qBAAqB,CAAE,MAAM,CAAA,EAAE;YAClC,qBAAqB,GAAG,IAAI,KAAK,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,UAAC,CAAC,EAAE,CAAC,IAAK,OAAA,CAAC,EAAD,CAAC,CAAC,CAAC;SAC/E;QAED,IAAI,CAAC,CAAA,qBAAqB,aAArB,qBAAqB,uBAArB,qBAAqB,CAAE,MAAM,CAAA,EAAE;YAClC,qBAAqB,GAAG,IAAI,KAAK,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,UAAC,CAAC,EAAE,CAAC,IAAK,OAAA,CAAC,EAAD,CAAC,CAAC,CAAC;SAC/E;QAED,IAAI,CAAC,aAAa,GAAG,IAAI,uDAA0B,CAAC,eAAe,EAAE,OAAO,EAAE,UAAU,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,CAAC,CAAC;IAClJ,CAAC;IAED,gCAAI,GAAJ;QACE,OAAO,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;IACnC,CAAC;IAEA,qCAAS,GAAV;;;;;oBACM,QAAQ,GAAG,IAAI,CAAC;;;yBAEb,IAAI;oBACT,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;yBACjC,QAAQ,EAAR,wBAAQ;oBACV,qBAAM,QAAQ,EAAA;;oBAAd,SAAc,CAAC;;wBAEf,wBAAM;;;;;KAGX;IAEH,wBAAC;AAAD,CAAC,AAxCD,IAwCC"}
1
+ {"version":3,"file":"CustomPermutation.js","sourceRoot":"","sources":["../src/CustomPermutation.ts"],"names":[],"mappings":";;AAAA,6EAA0E;AAE1E,MAAqB,iBAAiB;IAGpC,YACU,eAAsB,EACtB,OAAe,EACf,UAAkB,EAClB,qBAA6B,EAC7B,MAAkC;QAJlC,oBAAe,GAAf,eAAe,CAAO;QACtB,YAAO,GAAP,OAAO,CAAQ;QACf,eAAU,GAAV,UAAU,CAAQ;QAClB,0BAAqB,GAArB,qBAAqB,CAAQ;QAC7B,WAAM,GAAN,MAAM,CAA4B;QAE1C,IAAI,CAAC,CAAA,qBAAqB,aAArB,qBAAqB,uBAArB,qBAAqB,CAAE,MAAM,CAAA,EAAE;YAClC,qBAAqB,GAAG,KAAK,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC;SACtE;QAED,IAAI,CAAC,aAAa,GAAG,IAAI,uDAA0B,CACjD,IAAI,CAAC,eAAe,EACpB,IAAI,CAAC,OAAO,EACZ,IAAI,CAAC,UAAU,EACf,IAAI,CAAC,qBAAqB,EAC1B,IAAI,CAAC,MAAM,CACZ,CAAC;IACJ,CAAC;IAED,IAAI;QACF,OAAO,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;IACnC,CAAC;IAED,CAAC,SAAS;QACR,IAAI,QAAQ,GAAG,IAAI,CAAC;QAEpB,OAAO,IAAI,EAAE;YACX,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAC,IAAI,EAAE,CAAC;YACrC,IAAI,QAAQ,EAAE;gBACZ,MAAM,QAAQ,CAAC;aAChB;iBAAM;gBACL,MAAM;aACP;SACF;IACH,CAAC;CACF;AAvCD,oCAuCC"}
@@ -2,40 +2,28 @@ export declare class CustomPermutationGenerator {
2
2
  private elementList;
3
3
  private choicesByIndex;
4
4
  private nonChoicesByIndex;
5
- private elementsOrderAbs?;
6
- private elementsIndexRel;
5
+ private elementsOrderAbsolute?;
7
6
  private passFunction?;
8
- private randomizechoices?;
9
7
  set: any[];
10
- setAsStr: string[];
11
8
  permutationGenOfSet: any;
12
9
  nextIndexList: number[];
13
- choicesByIndexInSet: {};
10
+ finalChoicesByIndexInSet: {};
14
11
  history: any[];
15
12
  historyHashes: any[];
16
13
  current: any[];
17
14
  cursor: number;
18
- constructor(elementList: any[], choicesByIndex?: object, // ChoicesByIndex is relative to sub-sch so we need elementsIndexRel, a index list of sub-sch
19
- nonChoicesByIndex?: object, // nonChoices is also relative
20
- elementsOrderAbs?: any[], // Actual orders in final schdule
21
- elementsIndexRel?: any[], // Actual relative orders in sub-schedule
22
- passFunction?: Function, randomizechoices?: boolean);
23
- extendIndexesOfSameElements(choicesByIndex: any, indexesOfSameElements: any): void;
24
- init(): void;
25
- completeMissingChoicesIndexes(): void;
26
- setChoicesIndexesInSet(): void;
15
+ constructor(elementList: any[], choicesByIndex?: object, nonChoicesByIndex?: object, elementsOrderAbsolute?: any[], passFunction?: (items: any[]) => boolean);
27
16
  removeNonChoicesIndexes(): void;
17
+ setChoicesIndexesInSet(): void;
28
18
  getAllIndexesOfElementInList(el: any, list: any): any[];
19
+ completeRestOfIndexes(): void;
20
+ extendIndexesOfSameElements(choicesByIndex: any, indexesOfSameElements: any): void;
29
21
  prev(): any;
30
22
  next(): any;
31
- nextDistinct(): false | {
32
- done: any;
33
- value: any;
34
- };
23
+ nextDistinct(): any;
35
24
  saveCurrentToHistory(): void;
36
25
  getHash(elList: any[]): string;
37
26
  getElementListByInitialListIndexes(indexes: any): any[];
38
- consecutiveDifferent(list: any[]): boolean;
39
27
  reset(): void;
40
28
  getSet(): any[];
41
29
  isEmpty(): boolean;
@@ -1,166 +1,125 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CustomPermutationGenerator = void 0;
4
- var PermutationGeneratorForSet_1 = require("./PermutationGeneratorForSet");
5
- var CustomPermutationGenerator = /** @class */ (function () {
6
- // choicesByIndex: wisheds, unChoicesByIndex: unwisheds type is not but like SlotForPerson[], since it is a object instead of array
7
- function CustomPermutationGenerator(elementList, choicesByIndex, // ChoicesByIndex is relative to sub-sch so we need elementsIndexRel, a index list of sub-sch
8
- nonChoicesByIndex, // nonChoices is also relative
9
- elementsOrderAbs, // Actual orders in final schdule
10
- elementsIndexRel, // Actual relative orders in sub-schedule
11
- passFunction, randomizechoices) {
12
- if (choicesByIndex === void 0) { choicesByIndex = {}; }
13
- if (nonChoicesByIndex === void 0) { nonChoicesByIndex = {}; }
14
- if (elementsIndexRel === void 0) { elementsIndexRel = []; }
4
+ const PermutationGeneratorForSet_1 = require("./PermutationGeneratorForSet");
5
+ class CustomPermutationGenerator {
6
+ constructor(elementList, choicesByIndex = {}, nonChoicesByIndex = {}, elementsOrderAbsolute, passFunction) {
15
7
  this.elementList = elementList;
16
8
  this.choicesByIndex = choicesByIndex;
17
9
  this.nonChoicesByIndex = nonChoicesByIndex;
18
- this.elementsOrderAbs = elementsOrderAbs;
19
- this.elementsIndexRel = elementsIndexRel;
10
+ this.elementsOrderAbsolute = elementsOrderAbsolute;
20
11
  this.passFunction = passFunction;
21
- this.randomizechoices = randomizechoices;
22
12
  this.nextIndexList = [];
23
- this.choicesByIndexInSet = {}; // index of each choices in set
24
- this.history = []; // Permutation history
25
- this.historyHashes = []; // Permutation hashes, being used to distinguish new perm from olds
26
- this.current = []; // keeps the current perm of set
13
+ this.finalChoicesByIndexInSet = {};
14
+ this.history = [];
15
+ this.historyHashes = [];
16
+ this.current = [];
27
17
  this.cursor = 0;
28
- var indexList = Array(elementList.length).fill(0).map(function (el, ind) { return ind; });
29
- this.set = elementList.reduce(function (init, el, i) { return elementList.indexOf(el) == i ? init.concat(el) : init; }, []);
30
- this.setAsStr = this.set.map(function (x) { return String(x); });
31
- this.init();
32
- // debugger
33
- // Optimization: Take same element indexes and when removeing one remove all
34
- var indexesOfSameElements = {};
35
- elementList.forEach(function (element, i) {
18
+ const indexList = Array(elementList.length)
19
+ .fill(0)
20
+ .map((_, ind) => ind);
21
+ this.set = Array.from(new Set(elementList));
22
+ const indexesOfSameElements = {};
23
+ elementList.forEach((element, i) => {
36
24
  indexesOfSameElements[i] = [];
37
- // Zero means noone
38
- // if (element == 0) {
39
- // // indexesOfSameElements[i] = [i]; // To make an illusion such that zero is different than zero, to make it possible to be consecutive
40
- // return;
41
- // }
42
- elementList.forEach(function (innerElement, j) {
43
- if (element == innerElement) {
25
+ elementList.forEach((el, j) => {
26
+ if (element === el) {
44
27
  indexesOfSameElements[i].push(j);
45
28
  }
46
29
  });
47
30
  });
48
- // Can be uncomment, since it will increase the permutation number but it should be remain for exact results
49
31
  this.extendIndexesOfSameElements(this.choicesByIndex, indexesOfSameElements);
50
- // not make for choicesByIndex list so that options be little
51
32
  this.extendIndexesOfSameElements(this.nonChoicesByIndex, indexesOfSameElements);
52
- // let choicesByIndexInSetFiltered = Object.keys(this.choicesByIndexInSet).filter(x => elementsIndexRel.indexOf(Number(x)) >= 0)
53
- // .map(x => this.choicesByIndexInSet[x]);
54
- // Same like above
55
- // for(let i = 0; i < this.elementsIndexRel.length; i++) {
56
- // choicesByIndexInSetFiltered[i] = this.choicesByIndexInSet[elementsIndexRel[i]]
57
- // }
58
- this.permutationGenOfSet = new PermutationGeneratorForSet_1.PermutationGeneratorForSet(elementList, indexList, this.choicesByIndexInSet, indexesOfSameElements, elementsOrderAbs, passFunction, randomizechoices);
59
- }
60
- // collect indexes of same elements into nonChoicesByIndex array, eg, slot1 should be placed by person 1 whose indexes are 3,4,5
61
- CustomPermutationGenerator.prototype.extendIndexesOfSameElements = function (choicesByIndex, indexesOfSameElements) {
62
- if (!choicesByIndex) {
63
- return;
64
- }
65
- var choicesIndexKeys = Object.keys(choicesByIndex);
66
- for (var i = 0; i < choicesIndexKeys.length; i++) {
67
- var key = choicesIndexKeys[i];
68
- if (choicesByIndex[key]) {
69
- for (var j = 0; j < choicesByIndex[key].length; j++) {
70
- var ar = choicesByIndex[key].slice();
71
- // debugger
72
- if ((indexesOfSameElements[choicesByIndex[key][j]] || []).indexOf(choicesByIndex[key][j]) >= 0) {
73
- ar = ar.concat(indexesOfSameElements[choicesByIndex[key][j]]);
74
- }
75
- }
76
- choicesByIndex[key] = ar ? ar.filter(function (el, i) { return ar.indexOf(el) == i; }) : [];
77
- }
78
- }
79
- };
80
- // ? Need indexes to make a set, since element list may not be distinct
81
- CustomPermutationGenerator.prototype.init = function () {
82
33
  if (this.nonChoicesByIndex) {
83
- this.removeNonChoicesIndexes(); // Since set is changed to indexes, choices should be converted also to indexes
34
+ this.removeNonChoicesIndexes();
84
35
  }
85
36
  if (this.choicesByIndex) {
86
- this.setChoicesIndexesInSet(); // Since set is changed to indexes, choices should be converted also to indexes
37
+ this.setChoicesIndexesInSet();
87
38
  }
88
- this.completeMissingChoicesIndexes();
89
- };
90
- CustomPermutationGenerator.prototype.completeMissingChoicesIndexes = function () {
91
- var allIndexes = Array(this.elementList.length).fill(0).map(function (x, i) { return i; });
92
- for (var i = 0; i < this.elementList.length; i++) {
93
- if (!this.choicesByIndexInSet[i]) {
94
- this.choicesByIndexInSet[i] = allIndexes.slice();
95
- }
96
- }
97
- };
98
- // Index of choices in set
99
- CustomPermutationGenerator.prototype.setChoicesIndexesInSet = function () {
100
- var _this = this;
101
- Object.keys(this.choicesByIndex).forEach(function (key) {
102
- if (!_this.choicesByIndex[key])
103
- return; //?? bu gives error
104
- var indexesInList = [];
105
- for (var j = 0; j < _this.choicesByIndex[key].length; j++) {
106
- indexesInList = indexesInList.concat(_this.getAllIndexesOfElementInList(String(_this.choicesByIndex[key][j]), _this.elementList));
39
+ this.completeRestOfIndexes();
40
+ this.permutationGenOfSet = new PermutationGeneratorForSet_1.PermutationGeneratorForSet(elementList, indexList, this.finalChoicesByIndexInSet, indexesOfSameElements, this.elementsOrderAbsolute, passFunction);
41
+ }
42
+ removeNonChoicesIndexes() {
43
+ const allIndexes = Array(this.elementList.length)
44
+ .fill(0)
45
+ .map((x, i) => i);
46
+ Object.keys(this.nonChoicesByIndex).forEach((key) => {
47
+ let indexes = allIndexes.slice();
48
+ for (const el of this.nonChoicesByIndex[key]) {
49
+ const indexesToRemove = this.getAllIndexesOfElementInList(el, this.elementList);
50
+ indexes = indexes.filter((x) => indexesToRemove.indexOf(x) < 0);
107
51
  }
108
- _this.choicesByIndexInSet[key] = indexesInList;
52
+ this.finalChoicesByIndexInSet[key] = indexes;
109
53
  });
110
- };
111
- CustomPermutationGenerator.prototype.removeNonChoicesIndexes = function () {
112
- var _this = this;
113
- var allIndexes = Array(this.elementList.length).fill(0).map(function (x, i) { return i; });
114
- Object.keys(this.nonChoicesByIndex).forEach(function (key) {
115
- var indexes = allIndexes.slice();
116
- for (var j = 0; j < _this.nonChoicesByIndex[key].length; j++) {
117
- var indexesToRemove = _this.getAllIndexesOfElementInList(_this.nonChoicesByIndex[key][j], _this.elementList);
118
- indexes = indexes.filter(function (x) { return indexesToRemove.indexOf(x) < 0; });
54
+ }
55
+ setChoicesIndexesInSet() {
56
+ for (const key in this.choicesByIndex) {
57
+ if (!this.choicesByIndex[key]) {
58
+ continue;
119
59
  }
120
- _this.choicesByIndexInSet[key] = indexes;
121
- });
122
- };
123
- CustomPermutationGenerator.prototype.getAllIndexesOfElementInList = function (el, list) {
124
- var indexes = [];
125
- for (var i = 0; i < list.length; i++) {
126
- if (String(el) == String(list[i])) {
60
+ let indexesInList = [];
61
+ for (const el of this.choicesByIndex[key]) {
62
+ indexesInList = indexesInList.concat(this.getAllIndexesOfElementInList(el, this.elementList));
63
+ }
64
+ this.finalChoicesByIndexInSet[key] = indexesInList;
65
+ }
66
+ }
67
+ getAllIndexesOfElementInList(el, list) {
68
+ const indexes = [];
69
+ for (let i = 0; i < list.length; i++) {
70
+ if (String(el) === String(list[i])) {
127
71
  indexes.push(i);
128
72
  }
129
73
  }
130
74
  return indexes;
131
- };
132
- CustomPermutationGenerator.prototype.prev = function () {
133
- if (this.cursor > 1) { // cursor-1 is current, cursor-2 is prev
134
- return this.history[--this.cursor - 1];
75
+ }
76
+ completeRestOfIndexes() {
77
+ const allIndexes = Array(this.elementList.length)
78
+ .fill(0)
79
+ .map((x, i) => i);
80
+ for (let i = 0; i < this.elementList.length; i++) {
81
+ if (!this.finalChoicesByIndexInSet[i]) {
82
+ this.finalChoicesByIndexInSet[i] = allIndexes.slice();
83
+ }
135
84
  }
136
- };
137
- CustomPermutationGenerator.prototype.next = function () {
138
- var nextDistinct;
139
- while (true) {
140
- nextDistinct = this.nextDistinct();
141
- if (nextDistinct && !nextDistinct.done) {
142
- if (nextDistinct.value) {
143
- return nextDistinct.value;
144
- }
145
- else {
146
- return [];
85
+ }
86
+ extendIndexesOfSameElements(choicesByIndex, indexesOfSameElements) {
87
+ if (!choicesByIndex) {
88
+ return;
89
+ }
90
+ for (const key in choicesByIndex) {
91
+ if (choicesByIndex[key]) {
92
+ let clone;
93
+ for (const anotherKey of choicesByIndex[key]) {
94
+ clone = choicesByIndex[key].slice();
95
+ if ((indexesOfSameElements[anotherKey] || []).indexOf(anotherKey) >= 0) {
96
+ clone = clone.concat(indexesOfSameElements[anotherKey]);
97
+ }
147
98
  }
148
- }
149
- else if (nextDistinct && nextDistinct.done) {
150
- return null; // PermResultTypeEnum.ENDOFPERMUTATION;
99
+ choicesByIndex[key] = clone ? clone.filter((el, idx) => clone.indexOf(el) === idx) : [];
151
100
  }
152
101
  }
153
- };
154
- CustomPermutationGenerator.prototype.nextDistinct = function () {
102
+ }
103
+ prev() {
104
+ if (this.cursor > 1) {
105
+ // cursor-1 is current, cursor-2 is prev
106
+ return this.history[--this.cursor - 1];
107
+ }
108
+ }
109
+ next() {
110
+ let nextDistinctPerm = this.nextDistinct();
111
+ return !nextDistinctPerm.done ? nextDistinctPerm.value : null;
112
+ }
113
+ nextDistinct() {
155
114
  if (this.cursor < this.history.length) {
156
115
  return { value: this.history[this.cursor++], done: false };
157
116
  }
158
- var nextPerm = this.permutationGenOfSet.next();
117
+ const nextPerm = this.permutationGenOfSet.next();
159
118
  this.nextIndexList = nextPerm.value;
160
- var elList;
119
+ let elList;
161
120
  if (this.nextIndexList && this.nextIndexList.length > 0) {
162
121
  elList = this.getElementListByInitialListIndexes(nextPerm.value);
163
- var hash = this.getHash(elList);
122
+ const hash = this.getHash(elList);
164
123
  if (this.historyHashes.indexOf(hash) < 0 && (!this.passFunction || this.passFunction(elList))) {
165
124
  this.current = elList;
166
125
  this.history.push(elList);
@@ -168,70 +127,60 @@ var CustomPermutationGenerator = /** @class */ (function () {
168
127
  this.cursor++;
169
128
  }
170
129
  else {
171
- return false;
130
+ return this.nextDistinct();
172
131
  }
173
132
  }
174
133
  return { done: nextPerm.done, value: elList };
175
- };
176
- CustomPermutationGenerator.prototype.saveCurrentToHistory = function () {
134
+ }
135
+ saveCurrentToHistory() {
177
136
  this.history.push(this.current);
178
- var hash = this.getHash(this.current);
137
+ const hash = this.getHash(this.current);
179
138
  this.historyHashes.push(hash);
180
139
  this.cursor++;
181
- };
182
- CustomPermutationGenerator.prototype.getHash = function (elList) {
183
- var hash = '';
184
- for (var i = 0; i < elList.length; i++) {
185
- for (var j = 0; j < i + 1; j++) {
140
+ }
141
+ getHash(elList) {
142
+ let hash = '';
143
+ for (let i = 0; i < elList.length; i++) {
144
+ for (let j = 0; j < i + 1; j++) {
186
145
  hash += String(elList[i]);
187
146
  }
188
147
  }
189
148
  return hash;
190
- };
191
- CustomPermutationGenerator.prototype.getElementListByInitialListIndexes = function (indexes) {
192
- var _this = this;
149
+ }
150
+ getElementListByInitialListIndexes(indexes) {
193
151
  if (!indexes || !indexes.length) {
194
152
  return [];
195
153
  }
196
- var elList = [];
197
- indexes.forEach(function (index) {
154
+ const elList = [];
155
+ indexes.forEach((index) => {
198
156
  // if(this.elementList[index]) // Can be added, but costly
199
157
  {
200
- elList.push(_this.elementList[index]);
158
+ elList.push(this.elementList[index]);
201
159
  }
202
160
  });
203
161
  return elList;
204
- };
205
- CustomPermutationGenerator.prototype.consecutiveDifferent = function (list) {
206
- for (var i = 1; i < list.length; i++) {
207
- if (list[i] == list[i - 1]) {
208
- return false;
209
- }
210
- }
211
- return true;
212
- };
213
- CustomPermutationGenerator.prototype.reset = function () {
162
+ }
163
+ reset() {
214
164
  this.cursor = 0;
215
- };
216
- CustomPermutationGenerator.prototype.getSet = function () {
165
+ }
166
+ getSet() {
217
167
  return this.set || [];
218
- };
219
- CustomPermutationGenerator.prototype.isEmpty = function () {
168
+ }
169
+ isEmpty() {
220
170
  return !this.set || this.set.length === 0;
221
- };
222
- CustomPermutationGenerator.prototype.getCurrent = function () {
171
+ }
172
+ getCurrent() {
223
173
  if (this.cursor < this.history.length && this.cursor >= 0) {
224
174
  return this.history[this.cursor];
225
175
  }
226
176
  else {
227
177
  return null;
228
178
  }
229
- };
230
- CustomPermutationGenerator.prototype.last = function () {
179
+ }
180
+ last() {
231
181
  this.cursor = this.history.length - 1;
232
182
  return this.history[this.cursor];
233
- };
234
- return CustomPermutationGenerator;
235
- }());
183
+ }
184
+ }
236
185
  exports.CustomPermutationGenerator = CustomPermutationGenerator;
237
186
  //# sourceMappingURL=CustomPermutationGenerator.js.map