Skip to content

#2139 avoid lookupLinearSearch malloc slice #2140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 70 additions & 14 deletions calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14996,6 +14996,56 @@
return newStringFormulaArg(argsList.Back().Value.(formulaArg).Value())
}

// calcMatch returns the position of the value by given match type, criteria
// and lookup array for the formula function MATCH.
// matchType only contains -1, and 1.
func calcMatchMatrix(vertical bool, matchType int, criteria *formulaCriteria, lookupArray [][]formulaArg) formulaArg {
idx := -1
var calc = func(i int, arg formulaArg) bool {
switch matchType {
case -1:
if ok, _ := formulaCriteriaEval(arg, &formulaCriteria{
Type: criteriaGe, Condition: criteria.Condition,
}); ok {
idx = i
return false
}
if criteria.Condition.Type == ArgNumber {
return true
}
case 1:
if ok, _ := formulaCriteriaEval(arg, &formulaCriteria{
Type: criteriaLe, Condition: criteria.Condition,
}); ok {
idx = i
return false
}

Check warning on line 15022 in calc.go

View check run for this annotation

Codecov / codecov/patch

calc.go#L15020-L15022

Added lines #L15020 - L15022 were not covered by tests
if criteria.Condition.Type == ArgNumber {
return true
}
}
return false

Check warning on line 15027 in calc.go

View check run for this annotation

Codecov / codecov/patch

calc.go#L15027

Added line #L15027 was not covered by tests
}

if vertical {
for i, row := range lookupArray {
if ok := calc(i, row[0]); ok {
break
}
}
} else {
for i, cell := range lookupArray[0] {
if ok := calc(i, cell); ok {
break
}
}
}
if idx == -1 {
return newErrorFormulaArg(formulaErrorNA, formulaErrorNA)
}
return newNumberFormulaArg(float64(idx + 1))
}

// calcMatch returns the position of the value by given match type, criteria
// and lookup array for the formula function MATCH.
func calcMatch(matchType int, criteria *formulaCriteria, lookupArray []formulaArg) formulaArg {
Expand Down Expand Up @@ -15110,18 +15160,8 @@
// lookupLinearSearch sequentially checks each look value of the lookup array until
// a match is found or the whole list has been searched.
func lookupLinearSearch(vertical bool, lookupValue, lookupArray, matchMode, searchMode formulaArg) (int, bool) {
var tableArray []formulaArg
if vertical {
for _, row := range lookupArray.Matrix {
tableArray = append(tableArray, row[0])
}
} else {
tableArray = lookupArray.Matrix[0]
}
matchIdx, wasExact := -1, false
start:
for i, cell := range tableArray {
lhs := cell
var linearSearch = func(i int, cell, lhs formulaArg) bool {
if lookupValue.Type == ArgNumber {
if lhs = cell.ToNumber(); lhs.Type == ArgError {
lhs = cell
Expand All @@ -15135,12 +15175,28 @@
matchIdx = i
wasExact = true
if searchMode.Number == searchModeLinear {
break start
return true
}
}
if matchMode.Number == matchModeMinGreater || matchMode.Number == matchModeMaxLess {
matchIdx = int(calcMatch(int(matchMode.Number), formulaCriteriaParser(lookupValue), tableArray).Number)
continue
matchIdx = int(calcMatchMatrix(vertical, int(matchMode.Number), formulaCriteriaParser(lookupValue), lookupArray.Matrix).Number)
return false
}
return false
}

if vertical {
for i, row := range lookupArray.Matrix {
lhs := row[0]
if linearSearch(i, lhs, lhs) {
break
}
}
} else {
for i, lhs := range lookupArray.Matrix[0] {
if linearSearch(i, lhs, lhs) {
break
}
}
}
return matchIdx, wasExact
Expand Down