node-red-contrib-genetic-charging-strategy
Version:
A module for Node-RED that adds a battery charging strategy to node-red-contrib-power-saver. It uses genetic algorithms to find the best schedule
99 lines (98 loc) • 2.66 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DoublyLinkedList = exports.Node = void 0;
// Define the Node class for each node in the doubly linked list
class Node {
constructor(data) {
this.data = data;
this.previous = null;
this.next = null;
}
}
exports.Node = Node;
// Define the DoublyLinkedList class
class DoublyLinkedList {
constructor() {
this._head = null;
this._tail = null;
this.length = 0;
}
isEmpty() {
return this.length === 0;
}
get head() {
if (this.isEmpty())
throw new Error('List is empty');
return this._head;
}
get tail() {
if (this.isEmpty())
throw new Error('List is empty');
return this._tail;
}
// Function to insert a node at the front of the list
insertFront(data) {
const newNode = new Node(data);
if (!this._head) {
this._head = newNode;
this._tail = newNode;
}
else {
newNode.next = this.head;
this.head.previous = newNode;
this._head = newNode;
}
this.length++;
return this;
}
// Function to insert a node at the end of the list
insertBack(data) {
const newNode = new Node(data);
if (!this._tail) {
this._head = newNode;
this._tail = newNode;
}
else {
newNode.previous = this.tail;
this.tail.next = newNode;
this._tail = newNode;
}
this.length++;
return this;
}
toArray() {
return this.reduce((acc, data) => {
acc.push(data);
return acc;
}, []);
}
toString() {
return JSON.stringify(this.toArray());
}
reduce(callback, initialValue) {
let accumulator = initialValue;
let node = this.head;
while (node) {
accumulator = callback(accumulator, node.data, node);
node = node.next;
}
return accumulator;
}
forEach(callbackFn) {
this.reduce((acc, data) => {
callbackFn(data);
return acc;
}, 0);
}
map(callbackFn) {
return this.reduce((acc, data, node) => acc.insertBack(callbackFn(data, node)), new DoublyLinkedList());
}
filter(predicate) {
return this.reduce((acc, data, node) => {
if (predicate(data, node))
acc.insertBack(data);
return acc;
}, new DoublyLinkedList());
}
}
exports.DoublyLinkedList = DoublyLinkedList;