Project sequence evidence through item mappings
This commit is contained in:
50
internal/engine/sequence_projection.go
Normal file
50
internal/engine/sequence_projection.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package engine
|
||||
|
||||
type sequenceItemProjectionIndex struct {
|
||||
outputByInput []int
|
||||
inputByOutput []int
|
||||
}
|
||||
|
||||
func newSequenceItemProjectionIndex(projections []sequenceItemProjection) sequenceItemProjectionIndex {
|
||||
maxInput := -1
|
||||
maxOutput := -1
|
||||
for _, projection := range projections {
|
||||
if projection.inputIndex > maxInput {
|
||||
maxInput = projection.inputIndex
|
||||
}
|
||||
if projection.outputIndex > maxOutput {
|
||||
maxOutput = projection.outputIndex
|
||||
}
|
||||
}
|
||||
index := sequenceItemProjectionIndex{
|
||||
outputByInput: make([]int, maxInput+1),
|
||||
inputByOutput: make([]int, maxOutput+1),
|
||||
}
|
||||
for input := range index.outputByInput {
|
||||
index.outputByInput[input] = -1
|
||||
}
|
||||
for output := range index.inputByOutput {
|
||||
index.inputByOutput[output] = -1
|
||||
}
|
||||
for _, projection := range projections {
|
||||
if projection.inputIndex >= 0 && projection.inputIndex < len(index.outputByInput) && projection.outputIndex >= 0 && projection.outputIndex < len(index.inputByOutput) {
|
||||
index.outputByInput[projection.inputIndex] = projection.outputIndex
|
||||
index.inputByOutput[projection.outputIndex] = projection.inputIndex
|
||||
}
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
func (index sequenceItemProjectionIndex) outputForInput(inputIndex int) (int, bool) {
|
||||
if inputIndex < 0 || inputIndex >= len(index.outputByInput) || index.outputByInput[inputIndex] < 0 {
|
||||
return 0, false
|
||||
}
|
||||
return index.outputByInput[inputIndex], true
|
||||
}
|
||||
|
||||
func (index sequenceItemProjectionIndex) inputForOutput(outputIndex int) (int, bool) {
|
||||
if outputIndex < 0 || outputIndex >= len(index.inputByOutput) || index.inputByOutput[outputIndex] < 0 {
|
||||
return 0, false
|
||||
}
|
||||
return index.inputByOutput[outputIndex], true
|
||||
}
|
||||
Reference in New Issue
Block a user