Skip to content
Snippets Groups Projects
Commit 1ff5d508 authored by Alexander Rose's avatar Alexander Rose
Browse files

added lociAreEqual helper

parent 1190256e
No related branches found
No related tags found
No related merge requests found
...@@ -22,4 +22,19 @@ export function isEmptyLoci(x: any): x is EmptyLoci { ...@@ -22,4 +22,19 @@ export function isEmptyLoci(x: any): x is EmptyLoci {
return !!x && x.kind === 'empty-loci'; return !!x && x.kind === 'empty-loci';
} }
export function areLociEqual(lociA: Loci, lociB: Loci) {
if (isEveryLoci(lociA) && isEveryLoci(lociB)) return true
if (isEmptyLoci(lociA) && isEmptyLoci(lociB)) return true
if (StructureElement.isLoci(lociA) && StructureElement.isLoci(lociB)) {
return StructureElement.areLociEqual(lociA, lociB)
}
if (Link.isLoci(lociA) && Link.isLoci(lociB)) {
return Link.areLociEqual(lociA, lociB)
}
if (Shape.isLoci(lociA) && Shape.isLoci(lociB)) {
return Shape.areLociEqual(lociA, lociB)
}
return false
}
export type Loci = StructureElement.Loci | Link.Loci | EveryLoci | EmptyLoci | Shape.Loci export type Loci = StructureElement.Loci | Link.Loci | EveryLoci | EmptyLoci | Shape.Loci
\ No newline at end of file
...@@ -68,4 +68,15 @@ export namespace Shape { ...@@ -68,4 +68,15 @@ export namespace Shape {
export function isLoci(x: any): x is Loci { export function isLoci(x: any): x is Loci {
return !!x && x.kind === 'group-loci'; return !!x && x.kind === 'group-loci';
} }
export function areLociEqual(a: Loci, b: Loci) {
if (a.groups.length !== b.groups.length) return false
for (let i = 0, il = a.groups.length; i < il; ++i) {
const groupA = a.groups[i]
const groupB = b.groups[i]
if (groupA.shape.id !== groupB.shape.id) return false
if (!OrderedSet.areEqual(groupA.ids, groupB.ids)) return false
}
return true
}
} }
\ No newline at end of file
...@@ -63,6 +63,17 @@ namespace StructureElement { ...@@ -63,6 +63,17 @@ namespace StructureElement {
return !!x && x.kind === 'element-loci'; return !!x && x.kind === 'element-loci';
} }
export function areLociEqual(a: Loci, b: Loci) {
if (a.elements.length !== b.elements.length) return false
for (let i = 0, il = a.elements.length; i < il; ++i) {
const elementA = a.elements[i]
const elementB = b.elements[i]
if (elementA.unit.id !== elementB.unit.id) return false
if (!OrderedSet.areEqual(elementA.indices, elementB.indices)) return false
}
return true
}
export function isLocation(x: any): x is StructureElement { export function isLocation(x: any): x is StructureElement {
return !!x && x.kind === 'element-location'; return !!x && x.kind === 'element-location';
} }
......
...@@ -32,6 +32,13 @@ namespace Link { ...@@ -32,6 +32,13 @@ namespace Link {
return !!x && x.kind === 'link-location'; return !!x && x.kind === 'link-location';
} }
export function areLocationsEqual(locA: Location, locB: Location) {
return (
locA.aIndex === locB.aIndex && locA.bIndex === locB.bIndex &&
locA.aUnit.id === locB.aUnit.id && locA.bUnit.id === locB.bUnit.id
)
}
export interface Loci { export interface Loci {
readonly kind: 'link-loci', readonly kind: 'link-loci',
readonly links: ReadonlyArray<Location> readonly links: ReadonlyArray<Location>
...@@ -45,6 +52,14 @@ namespace Link { ...@@ -45,6 +52,14 @@ namespace Link {
return !!x && x.kind === 'link-loci'; return !!x && x.kind === 'link-loci';
} }
export function areLociEqual(a: Loci, b: Loci) {
if (a.links.length !== b.links.length) return false
for (let i = 0, il = a.links.length; i < il; ++i) {
if (!areLocationsEqual(a.links[i], b.links[i])) return false
}
return true
}
export function getType(structure: Structure, link: Location<Unit.Atomic>): LinkType { export function getType(structure: Structure, link: Location<Unit.Atomic>): LinkType {
if (link.aUnit === link.bUnit) { if (link.aUnit === link.bUnit) {
const links = link.aUnit.links; const links = link.aUnit.links;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment