solarite 0.2.2 → 0.2.4

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.
@@ -1,92 +0,0 @@
1
- import {InUseArray, InUseMap} from "./InUseMap.js";
2
- import Testimony, {assert} from "../../tests/Testimony.js";
3
-
4
- Testimony.test('InUseArray.constructor', () => {
5
- const arr = new InUseArray();
6
- assert.eq(arr.count, 0, 'Initial count should be 0');
7
- assert.eq(arr.length, 0, 'Initial length should be 0');
8
- });
9
-
10
- Testimony.test('InUseArray.use - with no value', () => {
11
- const arr = new InUseArray(1, 2, 3);
12
- const val = arr.use();
13
- assert.eq(val, 1, 'Should return the first value');
14
- assert.eq(arr.count, 1, 'Count should be incremented');
15
- });
16
-
17
- Testimony.test('InUseArray.use - with specific value', () => {
18
- const arr = new InUseArray(1, 2, 3);
19
- arr.count = 1; // Mark the first element as in-use
20
- const val = arr.use(3);
21
- assert.eq(val, 3, 'Should return the specified value');
22
- assert.eq(arr.count, 2, 'Count should be incremented');
23
- assert.eq(arr[1], 3, 'Value should be moved to the in-use section');
24
- assert.eq(arr[2], 2, 'Remaining value should be in the available section');
25
- });
26
-
27
- Testimony.test('InUseArray.use', () => {
28
- const arr = new InUseArray(1, 2, 3);
29
- let val = arr.use(4);
30
- assert.eq(val, undefined,'Should return undefined');
31
- assert.eq(arr.use(), 1);
32
- assert.eq(arr.use(3), 3);
33
- assert.eq(arr.use(), 2);
34
- assert.eq(arr.use(), undefined);
35
- });
36
-
37
- Testimony.test('InUseArray.use - all in use', () => {
38
- const arr = new InUseArray(1, 2, 3);
39
- arr.count = 3;
40
- const val = arr.use();
41
- assert.eq(val, undefined, 'Should return undefined');
42
- });
43
-
44
- Testimony.test('InUseArray.free', () => {
45
- const arr = new InUseArray(1, 2, 3);
46
- arr.count = 3;
47
- arr.free();
48
- assert.eq(arr.count, 0, 'Count should be reset to 0');
49
- });
50
-
51
- Testimony.test('InUseArray.reset', () => {
52
- const arr = new InUseArray(1, 2, 3);
53
- arr.reset();
54
- assert.eq(arr.length, 0, 'Array should be empty');
55
- });
56
-
57
-
58
-
59
-
60
- Testimony.test('InUseMap.add', () => {
61
- const map = new InUseMap();
62
- map.add('key', 'value');
63
- assert.eq(map.data['key'][0], 'value', 'Value should be added to the map');
64
- });
65
-
66
- Testimony.test('InUseMap.getAll', () => {
67
- const map = new InUseMap();
68
- map.add('key', 'value');
69
- const values = map.getAll('key');
70
- assert.eq(values.length, 1, 'Should return an array with one value');
71
- assert.eq(values[0], 'value', 'Should return the correct value');
72
- });
73
-
74
- Testimony.test('InUseMap.use', () => {
75
- const map = new InUseMap();
76
- map.add('key', 'value');
77
- let value = map.use('key');
78
- assert.eq(value, 'value', 'Should return and mark the value as in-use');
79
- value = map.use('key');
80
- assert.eq(value, undefined, 'Should return and mark the value as in-use');
81
- });
82
-
83
- Testimony.test('InUseMap.hasValue', () => {
84
- const map = new InUseMap();
85
- map.add('key1', 'value1');
86
- map.add('key2', 'value2');
87
- const keys = map.hasValue('value1');
88
- assert.eq(keys.length, 1, 'Should return an array with one key');
89
- assert.eq(keys[0], 'key1', 'Should return the correct key');
90
- });
91
-
92
-
@@ -1,98 +0,0 @@
1
-
2
-
3
-
4
- /**
5
- * An array that keeps track of how many elements are in-use.
6
- * In-use elements are always at the beginning of the array. */
7
- export class InUseArray extends Array {
8
-
9
- /**
10
- * @type {int} Everything before this number is in-use. */
11
- count = 0;
12
-
13
-
14
- /**
15
- * Get a value and mark it as in-use.
16
- * @param val {undefined|*} Optional. If set, find this specific value. Otherwise find any value.
17
- * @return {undefined|*} The value that is now marked as in-use. */
18
- use(val=undefined) {
19
- if (val) {
20
- // If val doesn't exist or is already in-use, return undefined.
21
- let index = this.indexOf(val);
22
- if (index === -1 || index < this.count)
23
- return undefined;
24
-
25
- // Swap the first available with val.
26
- this[index] = this[this.count];
27
- this[this.count] = val;
28
- this.count++;
29
- return val;
30
- }
31
-
32
- // Try to get last available.
33
- else {
34
- if (this.count >= this.length)
35
- return undefined;
36
- let result = this[this.count];
37
- this.count++;
38
- return result;
39
- }
40
- }
41
-
42
- free() {
43
- this.count = 0;
44
- }
45
-
46
- reset() {
47
- this.length = 0;
48
- }
49
- }
50
-
51
- /**
52
- *
53
- */
54
- export class InUseMap {
55
-
56
- /** @type {Object<string, InUseArray>} */
57
- data = {};
58
-
59
- // Set a new value for a key
60
- add(key, value) {
61
- let data = this.data;
62
- let array = data[key]
63
- if (!array) {
64
- array = new InUseArray();
65
- data[key] = array;
66
- }
67
- array.push(value);
68
- }
69
-
70
- // Get all values for a key
71
- getAll(key) {
72
- return this.data[key] || [];
73
- }
74
-
75
- // Find a value matching the given key, mark it as inUse, and return it.
76
- use(key, val=undefined) {
77
- return this.data[key]?.use(val) || undefined
78
- }
79
-
80
- // temporary
81
- delete(key, val=undefined) {
82
- return this.data[key]?.use(val) || undefined
83
- }
84
-
85
- freeAll() {
86
- for (let key in this.data)
87
- this.data[key].free();
88
- }
89
-
90
- hasValue(val) {
91
- let data = this.data;
92
- let names = [];
93
- for (let name in data)
94
- if (data[name].includes(val))
95
- names.push(name)
96
- return names;
97
- }
98
- }
@@ -1,117 +0,0 @@
1
- export default class LinkedList {
2
-
3
- constructor(input) {
4
- this.head = null;
5
- this.tail = null;
6
- this.map = new Map();
7
-
8
- if (Array.isArray(input)) {
9
- input.forEach(val => this.push(val));
10
- } else if (input instanceof LinkedList) {
11
- for (let val of input) {
12
- this.push(val);
13
- }
14
- }
15
- }
16
-
17
- push(value) {
18
- if (!this.head) {
19
- this.head = value;
20
- this.tail = value;
21
- return;
22
- }
23
-
24
- this.map.set(value, { prev: this.tail, next: null });
25
- this.map.set(this.tail, { ...this.map.get(this.tail), next: value });
26
- this.tail = value;
27
- }
28
-
29
- unshift(value) {
30
- if (!this.head) {
31
- this.push(value);
32
- return;
33
- }
34
-
35
- this.map.set(value, { prev: null, next: this.head });
36
- this.map.set(this.head, { ...this.map.get(this.head), prev: value });
37
- this.head = value;
38
- }
39
-
40
- shift() {
41
- if (!this.head) return;
42
-
43
- const secondNode = this.map.get(this.head).next;
44
-
45
- if (secondNode) {
46
- this.map.set(secondNode, { ...this.map.get(secondNode), prev: null });
47
- } else {
48
- this.tail = null;
49
- }
50
-
51
- this.head = secondNode;
52
- }
53
-
54
- pop() {
55
- if (!this.tail) return;
56
-
57
- const penultimate = this.map.get(this.tail).prev;
58
-
59
- if (penultimate) {
60
- this.map.set(penultimate, { ...this.map.get(penultimate), next: null });
61
- } else {
62
- this.head = null;
63
- }
64
-
65
- this.tail = penultimate;
66
- }
67
-
68
- *[Symbol.iterator]() {
69
- let currentNode = this.head;
70
-
71
- while (currentNode) {
72
- yield currentNode;
73
- const mapData = this.map.get(currentNode);
74
- currentNode = mapData ? mapData.next : null;
75
- }
76
- }
77
-
78
- slice(start, end) {
79
- const newList = new LinkedList();
80
- let currentNode = start;
81
-
82
- while (currentNode && currentNode !== end) {
83
- newList.push(currentNode);
84
- currentNode = this.map.get(currentNode).next;
85
- }
86
- if (end) {
87
- newList.push(end);
88
- }
89
-
90
- return newList;
91
- }
92
-
93
-
94
- splice(start, end, newList) {
95
- // TODO: assert items in newList aren't already in our list.
96
-
97
- const firstNewNode = newList instanceof LinkedList ? newList.head : newList[0];
98
- const lastNewNode = newList instanceof LinkedList ? newList.tail : newList[newList.length - 1];
99
-
100
- if (firstNewNode) {
101
- this.map.set(start, { ...this.map.get(start), next: firstNewNode });
102
- this.map.set(firstNewNode, { ...this.map.get(firstNewNode), prev: start });
103
- }
104
-
105
- const afterEnd = end ? this.map.get(end).next : null;
106
- if (lastNewNode) {
107
- this.map.set(lastNewNode, { ...this.map.get(lastNewNode), next: afterEnd });
108
- if (afterEnd) {
109
- this.map.set(afterEnd, { ...this.map.get(afterEnd), prev: lastNewNode });
110
- } else {
111
- this.tail = lastNewNode;
112
- }
113
- } else if (afterEnd) {
114
- this.map.set(afterEnd, { ...this.map.get(afterEnd), prev: start });
115
- }
116
- }
117
- }
@@ -1,115 +0,0 @@
1
- import Testimony, {assert} from "../../tests/Testimony.js";
2
- import LinkedList from "./LinkedList.js";
3
-
4
- Testimony.test('LinkedList.constructor with Array input', () => {
5
- const linkedList = new LinkedList(['A', 'B', 'C']);
6
- assert.eq([...linkedList], ['A', 'B', 'C']);
7
- });
8
-
9
- Testimony.test('LinkedList.constructor with LinkedList input', () => {
10
- const originalList = new LinkedList(['X', 'Y', 'Z']);
11
- const copiedList = new LinkedList(originalList);
12
- assert.eq([...copiedList], ['X', 'Y', 'Z']);
13
- });
14
-
15
- Testimony.test('LinkedList.constructor with mixed input', () => {
16
- const originalList = new LinkedList(['D', 'E']);
17
- const extendedList = new LinkedList([...originalList, 'F', 'G']);
18
- assert.eq([...extendedList], ['D', 'E', 'F', 'G']);
19
- });
20
-
21
- Testimony.test('LinkedList.constructor with no input', () => {
22
- const emptyList = new LinkedList();
23
- assert.eq([...emptyList], []);
24
- });
25
-
26
-
27
- Testimony.test('LinkedList.push', () => {
28
- const linkedList = new LinkedList();
29
- linkedList.push("A");
30
- linkedList.push("B");
31
- assert.eq([...linkedList], ['A', 'B']);
32
- });
33
-
34
- Testimony.test('LinkedList.unshift', () => {
35
- const linkedList = new LinkedList();
36
- linkedList.unshift("B");
37
- linkedList.unshift("A");
38
- assert.eq([...linkedList], ['A', 'B']);
39
- });
40
-
41
- Testimony.test('LinkedList.shift', () => {
42
- const linkedList = new LinkedList();
43
- linkedList.push("A");
44
- linkedList.push("B");
45
- linkedList.shift();
46
- assert.eq([...linkedList], ['B']);
47
- linkedList.shift();
48
- assert.eq([...linkedList], []);
49
- });
50
-
51
- Testimony.test('LinkedList.pop', () => {
52
- const linkedList = new LinkedList();
53
- linkedList.push("A");
54
- linkedList.push("B");
55
- linkedList.pop();
56
- assert.eq([...linkedList], ['A']);
57
- linkedList.pop();
58
- assert.eq([...linkedList], []);
59
- });
60
-
61
- Testimony.test('LinkedList.slice', () => {
62
- const linkedList = new LinkedList();
63
- linkedList.push("A");
64
- linkedList.push("B");
65
- linkedList.push("C");
66
- const subList = linkedList.slice("A", "B");
67
- assert.eq([...subList], ['A', 'B']);
68
- });
69
-
70
- Testimony.test('LinkedList._splice', () => {
71
- const linkedList = new LinkedList();
72
- linkedList.push("A");
73
- linkedList.push("B");
74
- linkedList.splice("A", null, ["X", "Y"]);
75
- assert.eq([...linkedList], ['A', 'X', 'Y', 'B']);
76
- linkedList.splice("X", "Y");
77
- assert.eq([...linkedList], ['B']);
78
- });
79
-
80
-
81
-
82
-
83
- function buildData(count = rowCount) {
84
- function _random(max) {
85
- return Math.round(Math.random()*1000)%max;
86
- }
87
-
88
- var adjectives = ["pretty", "large", "big", "small", "tall", "short", "long", "handsome", "plain", "quaint", "clean", "elegant", "easy", "angry", "crazy", "helpful", "mushy", "odd", "unsightly", "adorable", "important", "inexpensive", "cheap", "expensive", "fancy"];
89
- var colours = ["red", "yellow", "blue", "green", "pink", "brown", "purple", "brown", "white", "black", "orange"];
90
- var nouns = ["table", "chair", "house", "bbq", "desk", "car", "pony", "cookie", "sandwich", "burger", "pizza", "mouse", "keyboard"];
91
- var data = [];
92
- for (let i=0; i<count; i++)
93
- data.push({id: i, label: adjectives[_random(adjectives.length)] + " " + colours[_random(colours.length)] + " " + nouns[_random(nouns.length)] });
94
- return data;
95
- }
96
-
97
-
98
- Testimony.test('LinkedList.benchmark', () => {
99
-
100
- let data = buildData(10_000)
101
- let start = performance.now()
102
- let list = new LinkedList(data);
103
- console.log(performance.now() - start)
104
-
105
- start = performance.now()
106
- let i = 0;
107
- for (let item in list) {
108
- i++;
109
- }
110
- console.log(i)
111
- console.log(performance.now() - start)
112
-
113
-
114
-
115
- });