BiMap.sys.mjs (3149B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const lazy = {}; 6 7 ChromeUtils.defineESModuleGetters(lazy, { 8 generateUUID: "chrome://remote/content/shared/UUID.sys.mjs", 9 }); 10 11 /** 12 * A bidirectional map that maintains two-way mappings between UUIDs and objects. 13 * 14 * This class ensures that each object maps to exactly one UUID 15 * and vice versa. Also it allows efficient lookup in both directions: 16 * 17 * - from a UUID to an object 18 * - from an object to a UUID 19 */ 20 export class BiMap { 21 #idToObject; 22 #objectToId; 23 24 constructor() { 25 this.#idToObject = new Map(); 26 this.#objectToId = new Map(); 27 } 28 29 /** 30 * Clears all the mappings. 31 */ 32 clear() { 33 this.#idToObject = new Map(); 34 this.#objectToId = new Map(); 35 } 36 37 /** 38 * Deletes a mapping by the given object. 39 * 40 * @param {object} object 41 * The object to remove from the BiMap. 42 */ 43 deleteByObject(object) { 44 const id = this.#objectToId.get(object); 45 46 if (id !== undefined) { 47 this.#objectToId.delete(object); 48 this.#idToObject.delete(id); 49 } 50 } 51 52 /** 53 * Deletes a mapping by the given id. 54 * 55 * @param {string} id 56 * The id to remove from the BiMap. 57 */ 58 deleteById(id) { 59 const object = this.#idToObject.get(id); 60 61 if (object !== undefined) { 62 this.#idToObject.delete(id); 63 this.#objectToId.delete(object); 64 } 65 } 66 67 /** 68 * Retrieves the id for the given object, or inserts a new mapping if not found. 69 * 70 * @param {object} object 71 * The object to look up or insert. 72 * 73 * @returns {string} 74 * The id associated with the object. 75 */ 76 getOrInsert(object) { 77 if (this.hasObject(object)) { 78 return this.getId(object); 79 } 80 81 const id = lazy.generateUUID(); 82 this.#objectToId.set(object, id); 83 this.#idToObject.set(id, object); 84 85 return id; 86 } 87 88 /** 89 * Retrieves the id associated with the given object. 90 * 91 * @param {object} object 92 * The object to look up. 93 * 94 * @returns {string} 95 * The id associated with the object, or undefined if not found. 96 */ 97 getId(object) { 98 return this.#objectToId.get(object); 99 } 100 101 /** 102 * Retrieves the object associated with the given id. 103 * 104 * @param {string} id 105 * The id to look up. 106 * 107 * @returns {object} 108 * The object associated with the id, or undefined if not found. 109 */ 110 getObject(id) { 111 return this.#idToObject.get(id); 112 } 113 114 /** 115 * Checks whether the BiMap contains the given id. 116 * 117 * @param {string} id 118 * The id to check for. 119 * 120 * @returns {boolean} 121 * True if the id exists in the BiMap, false otherwise. 122 */ 123 hasId(id) { 124 return this.#idToObject.has(id); 125 } 126 127 /** 128 * Checks whether the BiMap contains the given object. 129 * 130 * @param {object} object 131 * The object to check for. 132 * 133 * @returns {boolean} 134 * True if the object exists in the BiMap, false otherwise. 135 */ 136 hasObject(object) { 137 return this.#objectToId.has(object); 138 } 139 }