Fix hashing function

This commit is contained in:
Cyn 2021-12-25 17:15:57 -05:00
parent dbc49d213b
commit 9be1236a2f
1 changed files with 21 additions and 14 deletions

View File

@ -1,4 +1,4 @@
import { NS, ProcessInfo } from "../NetscriptDefinitions"
import { NS, ProcessInfo } from '../NetscriptDefinitions'
export async function main(ns: NS): Promise<void> {
const hashes: any = {}
@ -24,8 +24,8 @@ export async function main(ns : NS) : Promise<void> {
})
for (const process of processes) {
if(process.filename != ns.getScriptName()) {
ns.tprint(`INFO: Restarting ${process.filename} ${process.args} -t ${process.threads}`)
if (process.filename != ns.getScriptName()) {
ns.kill(process.pid, ns.getHostname())
ns.run(process.filename, process.threads, ...process.args)
} else {
@ -41,6 +41,13 @@ export async function main(ns : NS) : Promise<void> {
}
}
function getHash(input : string) : number {
return input.split("").reduce( (a, b) => ((a << 5) - 1) + b.charCodeAt(0)|0, 0)
const getHash = (input: string): number => {
let hash = 0, i, chr
if (input.length === 0) return hash
for (i = 0; i < input.length; i++) {
chr = input.charCodeAt(i)
hash = ((hash << 5) - hash) + chr
hash |= 0 // Convert to 32bit integer
}
return hash
}