Skip to content

Commit

Permalink
Merge pull request #67 from corywalker/fmt
Browse files Browse the repository at this point in the history
Initial format.
  • Loading branch information
corywalker committed Jul 23, 2017
2 parents 1514a5e + 4ffa485 commit c55ff0b
Show file tree
Hide file tree
Showing 35 changed files with 273 additions and 269 deletions.
40 changes: 20 additions & 20 deletions expreduce/allocations.go
@@ -1,7 +1,7 @@
package expreduce

type allocIterState struct {
currForm int
currForm int
remaining int
currFormI int
}
Expand Down Expand Up @@ -29,7 +29,7 @@ func (ai *allocIter) next() bool {
if p.currForm+1 >= len(ai.forms) {
if (ai.forms[p.currForm].startI <= p.remaining) && (p.remaining <= ai.forms[p.currForm].endI) {
ai.stack = append(ai.stack, allocIterState{
p.currForm+1, 0, p.remaining})
p.currForm + 1, 0, p.remaining})
}
} else {
// Optional forms fill eagerly instead of lazily.
Expand All @@ -39,14 +39,14 @@ func (ai *allocIter) next() bool {
for i := ai.forms[p.currForm].startI; i <= Min(ai.forms[p.currForm].endI, p.remaining); i++ {
if p.remaining-i >= 0 {
ai.stack = append(ai.stack, allocIterState{
p.currForm+1, p.remaining-i, i})
p.currForm + 1, p.remaining - i, i})
}
}
} else {
for i := Min(ai.forms[p.currForm].endI, p.remaining); i >= ai.forms[p.currForm].startI; i-- {
if p.remaining-i >= 0 {
ai.stack = append(ai.stack, allocIterState{
p.currForm+1, p.remaining-i, i})
p.currForm + 1, p.remaining - i, i})
}
}
}
Expand All @@ -60,7 +60,7 @@ func NewAllocIter(l int, forms []parsedForm) allocIter {
ai.forms = forms
ai.alloc = make([]int, len(forms))
ai.stack = []allocIterState{
allocIterState{0, l, 0},
{0, l, 0},
}
return ai
}
Expand All @@ -71,22 +71,22 @@ type assnIterState struct {
// to do all asignments of it to two BlankNullSequences, we have an
// underlying data structure called assnData which could contain {0, 1} or
// {1, 0} in the case where the assignment was {{1}, {0}}.
assnDataI int
assnDataI int
crossedBoundary bool
toFree int
toFree int
}

type assnIter struct {
forms []parsedForm
assnData []int
assnIndices []int
assns [][]int
formMatches [][]bool
orderless bool
taken []bool
stack []assnIterState
forms []parsedForm
assnData []int
assnIndices []int
assns [][]int
formMatches [][]bool
orderless bool
taken []bool
stack []assnIterState
iteratingOrderless bool
ai allocIter
ai allocIter
}

func (asi *assnIter) nextOrderless() bool {
Expand Down Expand Up @@ -118,7 +118,7 @@ func (asi *assnIter) nextOrderless() bool {
willCrossBoundary = formI != asi.assnIndices[p.assnDataI+1]
}

startI := p.lastTaken+1
startI := p.lastTaken + 1
if p.crossedBoundary {
startI = 0
}
Expand All @@ -127,10 +127,10 @@ func (asi *assnIter) nextOrderless() bool {
-1, 0, true, p.lastTaken,
})
}
for i := len(asi.taken)-1; i >= startI; i-- {
for i := len(asi.taken) - 1; i >= startI; i-- {
if !asi.taken[i] && asi.formMatches[formI][i] {
asi.stack = append(asi.stack, assnIterState{
i, p.assnDataI+1, willCrossBoundary, -1,
i, p.assnDataI + 1, willCrossBoundary, -1,
})
}
}
Expand All @@ -149,7 +149,7 @@ func (asi *assnIter) next() bool {
// ReplaceList[ExpreduceFlatFn[a,b,c],ExpreduceFlatFn[x___//pm,b//pm,y___//pm]->{{x},{y}}]
lasti := 0
for i := range asi.assns {
asi.assns[i] = asi.assnData[lasti:lasti+asi.ai.alloc[i]]
asi.assns[i] = asi.assnData[lasti : lasti+asi.ai.alloc[i]]
for j := lasti; j < lasti+asi.ai.alloc[i]; j++ {
asi.assnIndices[j] = i
}
Expand Down
62 changes: 33 additions & 29 deletions expreduce/allocations_test.go
Expand Up @@ -14,14 +14,14 @@ func newPf(startI int, endI int) parsedForm {
}
}

// sequenceAssignments[len_Integer, forms_List, orderless_?BooleanQ] :=
// sequenceAssignments[len_Integer, forms_List, orderless_?BooleanQ] :=
// ReplaceList[
// fn @@ Range[0, len - 1] /.
// fn -> If[orderless, ExpreduceOrderlessFn, foo],
// fn @@ Range[0, len - 1] /.
// fn -> If[orderless, ExpreduceOrderlessFn, foo],
// fn @@ Table[
// Pattern[Alphabet[][[i]] // ToExpression // Evaluate,
// Repeated[_, forms[[i]]]], {i, Length[forms]}] ->
// Table[{Alphabet[][[i]] // ToExpression}, {i, Length[forms]}] /.
// Pattern[Alphabet[][[i]] // ToExpression // Evaluate,
// Repeated[_, forms[[i]]]], {i, Length[forms]}] ->
// Table[{Alphabet[][[i]] // ToExpression}, {i, Length[forms]}] /.
// fn -> If[orderless, ExpreduceOrderlessFn, foo]]

func TestAllocations(t *testing.T) {
Expand Down Expand Up @@ -138,7 +138,8 @@ func TestAllocations(t *testing.T) {
}
ai = NewAllocIter(800000, forms)
num := 0
for num = 0; ai.next(); num++ {}
for num = 0; ai.next(); num++ {
}
assert.Equal(t, 800000, num)

// should be 1/2 n (1+n)/.n->(ncomps-1)
Expand All @@ -150,7 +151,8 @@ func TestAllocations(t *testing.T) {
newPf(0, 999999),
}
ai = NewAllocIter(1400, forms)
for num = 0; ai.next(); num++ {}
for num = 0; ai.next(); num++ {
}
assert.Equal(t, 979300, num)
}

Expand All @@ -176,30 +178,30 @@ func TestAssignments(t *testing.T) {
nComps := 3
ai := NewAssnIter(nComps, forms, allMatch(nComps, len(forms)), true)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{}, []int{0}, []int{1, 2}}, ai.assns)
assert.Equal(t, [][]int{{}, {0}, {1, 2}}, ai.assns)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{}, []int{1}, []int{0, 2}}, ai.assns)
assert.Equal(t, [][]int{{}, {1}, {0, 2}}, ai.assns)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{}, []int{2}, []int{0, 1}}, ai.assns)
assert.Equal(t, [][]int{{}, {2}, {0, 1}}, ai.assns)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{0}, []int{1}, []int{2}}, ai.assns)
assert.Equal(t, [][]int{{0}, {1}, {2}}, ai.assns)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{0}, []int{2}, []int{1}}, ai.assns)
assert.Equal(t, [][]int{{0}, {2}, {1}}, ai.assns)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{1}, []int{0}, []int{2}}, ai.assns)
assert.Equal(t, [][]int{{1}, {0}, {2}}, ai.assns)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{1}, []int{2}, []int{0}}, ai.assns)
assert.Equal(t, [][]int{{1}, {2}, {0}}, ai.assns)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{2}, []int{0}, []int{1}}, ai.assns)
assert.Equal(t, [][]int{{2}, {0}, {1}}, ai.assns)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{2}, []int{1}, []int{0}}, ai.assns)
assert.Equal(t, [][]int{{2}, {1}, {0}}, ai.assns)
assert.Equal(t, false, ai.next())

ai = NewAssnIter(nComps, forms, allMatch(nComps, len(forms)), false)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{}, []int{0}, []int{1, 2}}, ai.assns)
assert.Equal(t, [][]int{{}, {0}, {1, 2}}, ai.assns)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{0}, []int{1}, []int{2}}, ai.assns)
assert.Equal(t, [][]int{{0}, {1}, {2}}, ai.assns)
assert.Equal(t, false, ai.next())

// Test pinning a form to particular components.
Expand All @@ -210,15 +212,15 @@ func TestAssignments(t *testing.T) {
}
nComps = 3
formMatches := [][]bool{
[]bool{true, true, true},
[]bool{false, true, false},
[]bool{true, true, true},
{true, true, true},
{false, true, false},
{true, true, true},
}
ai = NewAssnIter(nComps, forms, formMatches, true)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{0}, []int{1}, []int{2}}, ai.assns)
assert.Equal(t, [][]int{{0}, {1}, {2}}, ai.assns)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{2}, []int{1}, []int{0}}, ai.assns)
assert.Equal(t, [][]int{{2}, {1}, {0}}, ai.assns)
assert.Equal(t, false, ai.next())

forms = []parsedForm{
Expand All @@ -227,12 +229,12 @@ func TestAssignments(t *testing.T) {
}
nComps = 3
formMatches = [][]bool{
[]bool{true, true, true},
[]bool{true, false, false},
{true, true, true},
{true, false, false},
}
ai = NewAssnIter(nComps, forms, formMatches, true)
assert.Equal(t, true, ai.next())
assert.Equal(t, [][]int{[]int{1, 2}, []int{0}}, ai.assns)
assert.Equal(t, [][]int{{1, 2}, {0}}, ai.assns)
assert.Equal(t, false, ai.next())

// should be 1/2 n (1+n)/.n->(ncomps-1)
Expand All @@ -246,11 +248,13 @@ func TestAssignments(t *testing.T) {
nComps = 1400
ai = NewAssnIter(nComps, forms, allMatch(nComps, len(forms)), false)
num := 0
for num = 0; ai.next(); num++ {}
for num = 0; ai.next(); num++ {
}
assert.Equal(t, 979300, num)

nComps = 8
ai = NewAssnIter(nComps, forms, allMatch(nComps, len(forms)), true)
for num = 0; ai.next(); num++ {}
for num = 0; ai.next(); num++ {
}
assert.Equal(t, 40824, num)
}
12 changes: 6 additions & 6 deletions expreduce/builtin.go
Expand Up @@ -32,7 +32,7 @@ type Definition struct {
toString ToStringFnType

Attributes []string
Default string
Default string
}

func ToTestInstructions(tc *Expression) []TestInstruction {
Expand Down Expand Up @@ -110,23 +110,23 @@ func (def *Definition) AnnotateWithDynamicTests(es *EvalState) {
if !headIsSym {
continue
}
if (headSym.Name == "System`ESimpleExamples") {
if headSym.Name == "System`ESimpleExamples" {
def.SimpleExamples = append(
def.SimpleExamples,
ToTestInstructions(testColExpr)...)
} else if (headSym.Name == "System`EFurtherExamples") {
} else if headSym.Name == "System`EFurtherExamples" {
def.FurtherExamples = append(
def.FurtherExamples,
ToTestInstructions(testColExpr)...)
} else if (headSym.Name == "System`ETests") {
} else if headSym.Name == "System`ETests" {
def.Tests = append(
def.Tests,
ToTestInstructions(testColExpr)...)
} else if (headSym.Name == "System`EKnownFailures") {
} else if headSym.Name == "System`EKnownFailures" {
def.KnownFailures = append(
def.KnownFailures,
ToTestInstructions(testColExpr)...)
} else if (headSym.Name == "System`EKnownDangerous") {
} else if headSym.Name == "System`EKnownDangerous" {
def.KnownDangerous = append(
def.KnownDangerous,
ToTestInstructions(testColExpr)...)
Expand Down
8 changes: 4 additions & 4 deletions expreduce/builtin_arithmetic.go
Expand Up @@ -220,8 +220,8 @@ func collectTerms(e *Expression) *Expression {

func getArithmeticDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "Plus",
Default: "0",
Name: "Plus",
Default: "0",
toString: func(this *Expression, form string, context *String, contextPath *Expression) (bool, string) {
return ToStringInfix(this.Parts[1:], " + ", form, context, contextPath)
},
Expand Down Expand Up @@ -265,8 +265,8 @@ func getArithmeticDefinitions() (defs []Definition) {
},
})
defs = append(defs, Definition{
Name: "Times",
Default: "1",
Name: "Times",
Default: "1",
toString: func(this *Expression, form string, context *String, contextPath *Expression) (bool, string) {
return ToStringInfix(this.Parts[1:], " * ", form, context, contextPath)
},
Expand Down
8 changes: 4 additions & 4 deletions expreduce/builtin_atoms.go
Expand Up @@ -2,7 +2,7 @@ package expreduce

func getAtomsDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "Rational",
Name: "Rational",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 3 {
return this
Expand All @@ -15,8 +15,8 @@ func getAtomsDefinitions() (defs []Definition) {
return this
},
})
defs = append(defs, Definition{Name: "String"})
defs = append(defs, Definition{Name: "Real"})
defs = append(defs, Definition{Name: "Integer"})
defs = append(defs, Definition{Name: "String"})
defs = append(defs, Definition{Name: "Real"})
defs = append(defs, Definition{Name: "Integer"})
return
}
14 changes: 7 additions & 7 deletions expreduce/builtin_boolean.go
Expand Up @@ -2,7 +2,7 @@ package expreduce

func GetBooleanDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "And",
Name: "And",
toString: func(this *Expression, form string, context *String, contextPath *Expression) (bool, string) {
return ToStringInfix(this.Parts[1:], " && ", form, context, contextPath)
},
Expand All @@ -11,7 +11,7 @@ func GetBooleanDefinitions() (defs []Definition) {
for i := 1; i < len(this.Parts); i++ {
this.Parts[i] = this.Parts[i].Eval(es)
if booleanQ(this.Parts[i], &es.CASLogger) {
if falseQ(this.Parts[i], &es.CASLogger) {
if falseQ(this.Parts[i], &es.CASLogger) {
return &Symbol{"System`False"}
}
} else {
Expand All @@ -28,7 +28,7 @@ func GetBooleanDefinitions() (defs []Definition) {
},
})
defs = append(defs, Definition{
Name: "Or",
Name: "Or",
toString: func(this *Expression, form string, context *String, contextPath *Expression) (bool, string) {
return ToStringInfix(this.Parts[1:], " || ", form, context, contextPath)
},
Expand All @@ -37,7 +37,7 @@ func GetBooleanDefinitions() (defs []Definition) {
for i := 1; i < len(this.Parts); i++ {
this.Parts[i] = this.Parts[i].Eval(es)
if booleanQ(this.Parts[i], &es.CASLogger) {
if trueQ(this.Parts[i], &es.CASLogger) {
if trueQ(this.Parts[i], &es.CASLogger) {
return &Symbol{"System`True"}
}
} else {
Expand All @@ -54,7 +54,7 @@ func GetBooleanDefinitions() (defs []Definition) {
},
})
defs = append(defs, Definition{
Name: "Not",
Name: "Not",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
Expand All @@ -69,11 +69,11 @@ func GetBooleanDefinitions() (defs []Definition) {
},
})
defs = append(defs, Definition{
Name: "TrueQ",
Name: "TrueQ",
legacyEvalFn: singleParamQLogEval(trueQ),
})
defs = append(defs, Definition{
Name: "BooleanQ",
Name: "BooleanQ",
legacyEvalFn: singleParamQLogEval(booleanQ),
})
defs = append(defs, Definition{Name: "AllTrue"})
Expand Down

0 comments on commit c55ff0b

Please sign in to comment.