refactor: use actual maps for uuidmap

This commit is contained in:
Aman Harwara
2023-06-25 16:14:52 +05:30
parent 195420101a
commit 63b95391a6

View File

@@ -6,39 +6,39 @@ import { addIfUnique, removeFromArray } from '../Utils/Utils'
*/ */
export class UuidMap { export class UuidMap {
/** uuid to uuids that we have a relationship with */ /** uuid to uuids that we have a relationship with */
private directMap: Partial<Record<string, string[]>> = {} private directMap: Map<string, string[]> = new Map()
/** uuid to uuids that have a relationship with us */ /** uuid to uuids that have a relationship with us */
private inverseMap: Partial<Record<string, string[]>> = {} private inverseMap: Map<string, string[]> = new Map()
public get directMapSize(): number { public get directMapSize(): number {
return Object.keys(this.directMap).length return this.directMap.size
} }
public get inverseMapSize(): number { public get inverseMapSize(): number {
return Object.keys(this.inverseMap).length return this.inverseMap.size
} }
public makeCopy(): UuidMap { public makeCopy(): UuidMap {
const copy = new UuidMap() const copy = new UuidMap()
copy.directMap = Object.assign({}, this.directMap) copy.directMap = new Map(this.directMap)
copy.inverseMap = Object.assign({}, this.inverseMap) copy.inverseMap = new Map(this.inverseMap)
return copy return copy
} }
public existsInDirectMap(uuid: string): boolean { public existsInDirectMap(uuid: string): boolean {
return uuid in this.directMap return this.directMap.has(uuid)
} }
public existsInInverseMap(uuid: string): boolean { public existsInInverseMap(uuid: string): boolean {
return uuid in this.inverseMap return this.inverseMap.has(uuid)
} }
public getDirectRelationships(uuid: string): string[] { public getDirectRelationships(uuid: string): string[] {
return this.directMap[uuid] || [] return this.directMap.get(uuid) || []
} }
public getInverseRelationships(uuid: string): string[] { public getInverseRelationships(uuid: string): string[] {
return this.inverseMap[uuid] || [] return this.inverseMap.get(uuid) || []
} }
public establishRelationship(uuidA: string, uuidB: string): void { public establishRelationship(uuidA: string, uuidB: string): void {
@@ -52,8 +52,8 @@ export class UuidMap {
} }
public setAllRelationships(uuid: string, relationships: string[]): void { public setAllRelationships(uuid: string, relationships: string[]): void {
const previousDirect = this.directMap[uuid] || [] const previousDirect = this.directMap.get(uuid) || []
this.directMap[uuid] = relationships this.directMap.set(uuid, relationships)
/** Remove all previous values in case relationships have changed /** Remove all previous values in case relationships have changed
* The updated references will be added afterwards. * The updated references will be added afterwards.
@@ -70,41 +70,41 @@ export class UuidMap {
public removeFromMap(uuid: string): void { public removeFromMap(uuid: string): void {
/** Items that we reference */ /** Items that we reference */
const directReferences = this.directMap[uuid] || [] const directReferences = this.directMap.get(uuid) || []
for (const directReference of directReferences) { for (const directReference of directReferences) {
removeFromArray(this.inverseMap[directReference] || [], uuid) removeFromArray(this.inverseMap.get(directReference) || [], uuid)
} }
delete this.directMap[uuid] this.directMap.delete(uuid)
/** Items that are referencing us */ /** Items that are referencing us */
const inverseReferences = this.inverseMap[uuid] || [] const inverseReferences = this.inverseMap.get(uuid) || []
for (const inverseReference of inverseReferences) { for (const inverseReference of inverseReferences) {
removeFromArray(this.directMap[inverseReference] || [], uuid) removeFromArray(this.directMap.get(inverseReference) || [], uuid)
} }
delete this.inverseMap[uuid] this.inverseMap.delete(uuid)
} }
private establishDirectRelationship(uuidA: string, uuidB: string): void { private establishDirectRelationship(uuidA: string, uuidB: string): void {
const index = this.directMap[uuidA] || [] const index = this.directMap.get(uuidA) || []
addIfUnique(index, uuidB) addIfUnique(index, uuidB)
this.directMap[uuidA] = index this.directMap.set(uuidA, index)
} }
private establishInverseRelationship(uuidA: string, uuidB: string): void { private establishInverseRelationship(uuidA: string, uuidB: string): void {
const inverseIndex = this.inverseMap[uuidB] || [] const inverseIndex = this.inverseMap.get(uuidB) || []
addIfUnique(inverseIndex, uuidA) addIfUnique(inverseIndex, uuidA)
this.inverseMap[uuidB] = inverseIndex this.inverseMap.set(uuidB, inverseIndex)
} }
private deestablishDirectRelationship(uuidA: string, uuidB: string): void { private deestablishDirectRelationship(uuidA: string, uuidB: string): void {
const index = this.directMap[uuidA] || [] const index = this.directMap.get(uuidA) || []
removeFromArray(index, uuidB) removeFromArray(index, uuidB)
this.directMap[uuidA] = index this.directMap.set(uuidA, index)
} }
private deestablishInverseRelationship(uuidA: string, uuidB: string): void { private deestablishInverseRelationship(uuidA: string, uuidB: string): void {
const inverseIndex = this.inverseMap[uuidB] || [] const inverseIndex = this.inverseMap.get(uuidB) || []
removeFromArray(inverseIndex, uuidA) removeFromArray(inverseIndex, uuidA)
this.inverseMap[uuidB] = inverseIndex this.inverseMap.set(uuidB, inverseIndex)
} }
} }