/**
* Hashes task queue values for backward compatibility.
*
* @param {any} value The value to hash.
* @returns {string} The hashed value.
*/
function hashValue(value) {
// Use a simple hashing algorithm (can be replaced with a more robust one if needed).
let hash = 0;
const str = String(value); // Convert to string to handle various data types
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char; // Multiply by 31 and add the char code
hash = hash & hash; // Convert to 32bit integer
}
return hash.toString(); // Return as string
}
/**
* Creates a map of original values to their hashed representations.
*
* @param {Array<any>} originalValues An array of task queue values.
* @returns {Object<string, any>} A map where keys are hashed values and values are the original values.
*/
function createHashMap(originalValues) {
const hashMap = {};
for (const value of originalValues) {
const hash = hashValue(value);
hashMap[hash] = value;
}
return hashMap;
}
/**
* Replaces old values with their hashed equivalents in a task queue.
*
* @param {Array<any>} taskQueue The task queue array.
* @param {Object<string, any>} hashMap The hash map created using createHashMap.
* @returns {Array<any>} The modified task queue with hashed values.
*/
function hashTaskQueue(taskQueue, hashMap) {
const hashedQueue = [];
for (const value of taskQueue) {
const hash = hashValue(value);
hashedQueue.push(hashMap[hash]); // Use the hashed value from the map
}
return hashedQueue;
}
//Example Usage (can be removed for production)
// const originalTaskQueue = [1, "hello", {name: "John"}];
// const hashMap = createHashMap(originalTaskQueue);
// const hashedTaskQueue = hashTaskQueue(originalTaskQueue, hashMap);
// console.log("Original Task Queue:", originalTaskQueue);
// console.log("Hashed Task Queue:", hashedTaskQueue);
Add your comment