custom-permutation 1.0.6 → 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,60 +1,108 @@
1
+ # Version
2
+ 1.1.0
3
+
4
+ # Fixed bugs
5
+ 23.09.2024 - Edge cases are handled and the codespace is simplified.
6
+ 25.09.2023 - unChoices did not reflect always, fixed now.
7
+
1
8
  # Custom Permutation Generator
2
9
 
3
- ```typescript
10
+ ## 1.1. `require`
11
+ ```ts
12
+ const CustomPermutation = require('custom-permutation')
13
+ ```
14
+ ## 1.2. ,`import`
15
+
16
+ ```ts
17
+ import CustomPermutation from "custom-permutation";
18
+ ```
19
+
20
+ ## 2. Constructor
21
+ ```ts
4
22
  CustomPermutation(elList:[els...], choices:{ index: [els...]] }, nonChoices:{ index: [els...]] })
5
23
  ```
6
24
 
7
- _example:_\
8
- `CustomPermutation(['a', 'b', 'c'], {1: ['a', 'b']}, {0: ['a']})`
25
+ ## 3. Usage explanation
26
+
27
+ _example:_
9
28
 
10
- Permutate 3 elements which are 'a', 'b' and 'c' with below rules
29
+ ```ts
30
+ CustomPermutation(["a", "b", "c"], {1: ["a", "b"]}, {0: ["a"]})
31
+ ```
32
+
33
+ Permutate 3 elements which are "a", "b" and "c" with below rules
11
34
 
12
- __1. choices rule:__ `{ 1: ['a', 'b'] }`
35
+ __- choices rule:__
36
+ ```json
37
+ { "1": ["a", "b"] }
38
+ ```
13
39
 
14
- At index=1 there can be just 'a' or 'b'
40
+ At index=1 there can only be the element "a" or "b"
15
41
 
16
- __2. nonChoices rule:__ `{ 0: ['a'] }`
42
+ __- nonChoices rule:__
43
+ ```json
44
+ { "0": ["a"] }
45
+ ```
17
46
 
18
- At index=0 there can NOT be element 'a'
47
+ At index=0 there can NOT be the element "a"
19
48
 
20
- _Note: given index are considered as 0 based: [index=0, index=1, etc]_
49
+ _Note: given index are considered as 0 based: [index=0, index=1, etc.]_
21
50
 
22
- __Result set:__
51
+ ## 4. Result set explanation:
23
52
 
24
53
  Let's see all permutations, and which ones are valid or not.
25
- - `['a', 'b', 'c']` : violates nonChoices rule: _first element is `'a'`, but shouldn't be `'a'`
26
- - `['a', 'c', 'b']` : violates nonChoices rule: _first element is `'a'`, but shouldn't be `'a'`_
27
- - `['b', 'a', 'c']` : ok
28
- - `['b', 'c', 'a']` : violates choices rule: _second element is `'c'` but `'a'` or `'b'` is desired_
29
- - `['c', 'a', 'b']` : ok
30
- - `['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"]`
31
60
 
32
61
  So there are just 3 results that should be generated with this parameters.
33
- ```javascript
34
- let customPerm = new CustomPermutation(['a', 'b', 'c'], { 1: ['a', 'b'] }, { 0: ['a'] });
35
- let gen = customPerm.generator();
36
- while (true) {
37
- let next = gen.next();
38
- if (next.done) {
39
- break;
40
- }
41
- console.log(next.value)
42
- }
43
-
44
- ```
45
- or
46
- ```javascript
47
- let customPerm = new CustomPermutation(['a', 'b', 'c'], { 1: ['a', 'b'] }, { 0: ['a'] });
48
- while (true) {
49
- let next = customPerm.next();
50
- if (!next) {
51
- break;
52
- }
53
- console.log(next)
54
- }
55
- ```
56
-
57
- __Console__\
58
- ['b', 'a', 'c']\
59
- ['c', 'a', 'b']\
60
- ['c', 'b', 'a']
62
+
63
+ ## 5. Complete example
64
+
65
+ ### 1. Create permutation
66
+
67
+ ```ts
68
+ let customPerm = new CustomPermutation(
69
+ ["a", "b", "c"],
70
+ { "1": ["a", "b"] },
71
+ { "0": ["a"] }
72
+ );
73
+ ```
74
+
75
+ ### 2. Get next value
76
+
77
+ #### 2.1. Use with next
78
+
79
+ ```ts
80
+ let next = customPerm.next();
81
+
82
+ while (next) {
83
+ console.log(next)
84
+ next = customPerm.next();
85
+ }
86
+ ```
87
+
88
+ #### 2.2. Or use with generator
89
+
90
+ ```ts
91
+ let generator = customPerm.generator();
92
+ let next = generator.next();
93
+
94
+ while (!next.done) {
95
+ console.log(next.value)
96
+ next = generator.next();
97
+ }
98
+ ```
99
+
100
+ ### 3. Output
101
+
102
+ __Console__
103
+
104
+ ```sh
105
+ ["b", "a", "c"]
106
+ ["c", "a", "b"]
107
+ ["c", "b", "a"]
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,E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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;