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

added InterUnitLinks.getBondIndices

parent 6d9c41b9
No related branches found
No related tags found
No related merge requests found
...@@ -23,6 +23,7 @@ class InterUnitBonds { ...@@ -23,6 +23,7 @@ class InterUnitBonds {
/** Array of inter-unit bonds */ /** Array of inter-unit bonds */
readonly bonds: ReadonlyArray<InterUnitBonds.Bond> readonly bonds: ReadonlyArray<InterUnitBonds.Bond>
private readonly bondKeyIndex: Map<string, number> private readonly bondKeyIndex: Map<string, number>
private readonly elementKeyIndex: Map<string, number[]>
/** Get an array of unit-pair-bonds that are linked to the given unit */ /** Get an array of unit-pair-bonds that are linked to the given unit */
getLinkedUnits(unit: Unit): ReadonlyArray<InterUnitBonds.UnitPairBonds> { getLinkedUnits(unit: Unit): ReadonlyArray<InterUnitBonds.UnitPairBonds> {
...@@ -32,8 +33,8 @@ class InterUnitBonds { ...@@ -32,8 +33,8 @@ class InterUnitBonds {
/** Index into this.bonds */ /** Index into this.bonds */
getBondIndex(indexA: StructureElement.UnitIndex, unitA: Unit, indexB: StructureElement.UnitIndex, unitB: Unit): number { getBondIndex(indexA: StructureElement.UnitIndex, unitA: Unit, indexB: StructureElement.UnitIndex, unitB: Unit): number {
const key = InterUnitBonds.getBondKey(indexA, unitA, indexB, unitB) const bondKey = InterUnitBonds.getBondKey(indexA, unitA, indexB, unitB)
const index = this.bondKeyIndex.get(key) const index = this.bondKeyIndex.get(bondKey)
return index !== undefined ? index : -1 return index !== undefined ? index : -1
} }
...@@ -48,26 +49,44 @@ class InterUnitBonds { ...@@ -48,26 +49,44 @@ class InterUnitBonds {
return this.getBond(l.aIndex, l.aUnit, l.bIndex, l.bUnit); return this.getBond(l.aIndex, l.aUnit, l.bIndex, l.bUnit);
} }
/** Indices into this.bonds */
getBondIndices(index: StructureElement.UnitIndex, unit: Unit): ReadonlyArray<number> {
const elementKey = InterUnitBonds.getElementKey(index, unit)
const indices = this.elementKeyIndex.get(elementKey)
return indices !== undefined ? indices : []
}
constructor(private map: Map<number, InterUnitBonds.UnitPairBonds[]>) { constructor(private map: Map<number, InterUnitBonds.UnitPairBonds[]>) {
let count = 0 let count = 0
const bonds: (InterUnitBonds.Bond)[] = [] const bonds: (InterUnitBonds.Bond)[] = []
const bondKeyIndex = new Map<string, number>() const bondKeyIndex = new Map<string, number>()
const elementKeyIndex = new Map<string, number[]>()
this.map.forEach(pairBondsArray => { this.map.forEach(pairBondsArray => {
pairBondsArray.forEach(pairBonds => { pairBondsArray.forEach(pairBonds => {
count += pairBonds.bondCount count += pairBonds.bondCount
pairBonds.linkedElementIndices.forEach(indexA => { pairBonds.linkedElementIndices.forEach(indexA => {
pairBonds.getBonds(indexA).forEach(bondInfo => { pairBonds.getBonds(indexA).forEach(bondInfo => {
const { unitA, unitB } = pairBonds const { unitA, unitB } = pairBonds
const key = InterUnitBonds.getBondKey(indexA, unitA, bondInfo.indexB, unitB)
bondKeyIndex.set(key, bonds.length) const bondKey = InterUnitBonds.getBondKey(indexA, unitA, bondInfo.indexB, unitB)
bondKeyIndex.set(bondKey, bonds.length)
const elementKey = InterUnitBonds.getElementKey(indexA, unitA)
const e = elementKeyIndex.get(elementKey)
if (e === undefined) elementKeyIndex.set(elementKey, [bonds.length])
else e.push(bonds.length)
bonds.push({ ...bondInfo, indexA, unitA, unitB }) bonds.push({ ...bondInfo, indexA, unitA, unitB })
}) })
}) })
}) })
}) })
this.bondCount = count this.bondCount = count
this.bonds = bonds this.bonds = bonds
this.bondKeyIndex = bondKeyIndex this.bondKeyIndex = bondKeyIndex
this.elementKeyIndex = elementKeyIndex
} }
} }
...@@ -102,15 +121,19 @@ namespace InterUnitBonds { ...@@ -102,15 +121,19 @@ namespace InterUnitBonds {
export interface Bond { export interface Bond {
readonly unitA: Unit.Atomic, readonly unitA: Unit.Atomic,
readonly unitB: Unit.Atomic, readonly unitB: Unit.Atomic,
readonly indexA: number, readonly indexA: StructureElement.UnitIndex,
readonly indexB: number, readonly indexB: StructureElement.UnitIndex,
readonly order: number, readonly order: number,
readonly flag: LinkType.Flag readonly flag: LinkType.Flag
} }
export function getBondKey(indexA: number, unitA: Unit, indexB: number, unitB: Unit) { export function getBondKey(indexA: StructureElement.UnitIndex, unitA: Unit, indexB: StructureElement.UnitIndex, unitB: Unit) {
return `${indexA}|${unitA.id}|${indexB}|${unitB.id}` return `${indexA}|${unitA.id}|${indexB}|${unitB.id}`
} }
export function getElementKey(index: StructureElement.UnitIndex, unit: Unit) {
return `${index}|${unit.id}`
}
} }
const emptyArray: any[] = []; const emptyArray: any[] = [];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment