Skip to content

Commit

Permalink
use explicit field name in _Value and reducerInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
reusee committed Nov 1, 2022
1 parent 0775ea4 commit 8508ab7
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 65 deletions.
4 changes: 2 additions & 2 deletions debug_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ func DebugDefs(

var valueInfo ValueDebugInfo
for _, value := range values {
valueInfo.DefTypes = append(valueInfo.DefTypes, value.DefType)
valueInfo.DefTypes = append(valueInfo.DefTypes, value.typeInfo.DefType)
}

t := typeIDToType(values[0].TypeID)
t := typeIDToType(values[0].typeInfo.TypeID)
info.Values[t] = valueInfo

return nil
Expand Down
8 changes: 4 additions & 4 deletions dscope.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

type _Value struct {
*_TypeInfo
*_Initializer
typeInfo *_TypeInfo
initializer *_Initializer
}

type _TypeInfo struct {
Expand Down Expand Up @@ -149,11 +149,11 @@ func (scope Scope) get(id _TypeID, t reflect.Type) (
)
}
var values []reflect.Value
values, err = value.get(scope.appendPath(id))
values, err = value.initializer.get(scope.appendPath(id))
if err != nil { // NOCOVER
return ret, err
}
return values[value.Position], nil
return values[value.typeInfo.Position], nil

} else {
// reducer
Expand Down
80 changes: 41 additions & 39 deletions fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ type _Forker struct {
type posAtSorted int

type reducerInfo struct {
*_TypeInfo
OriginType reflect.Type
ReducerType *reflect.Type
typeInfo *_TypeInfo
originType reflect.Type
reducerType *reflect.Type
}

func newForker(
Expand Down Expand Up @@ -63,7 +63,7 @@ func newForker(
id := getTypeID(t)

newValuesTemplate = append(newValuesTemplate, _Value{
_TypeInfo: &_TypeInfo{
typeInfo: &_TypeInfo{
TypeID: id,
DefType: defType,
Position: i,
Expand All @@ -84,7 +84,7 @@ func newForker(
t := defType.Elem()
id := getTypeID(t)
newValuesTemplate = append(newValuesTemplate, _Value{
_TypeInfo: &_TypeInfo{
typeInfo: &_TypeInfo{
TypeID: id,
DefType: defType,
DefIsMulti: false,
Expand Down Expand Up @@ -113,7 +113,8 @@ func newForker(
posesAtTemplate = append(posesAtTemplate, posAtTemplate(i))
}
sort.Slice(posesAtTemplate, func(i, j int) bool {
return newValuesTemplate[posesAtTemplate[i]].TypeID < newValuesTemplate[posesAtTemplate[j]].TypeID
return newValuesTemplate[posesAtTemplate[i]].typeInfo.TypeID <
newValuesTemplate[posesAtTemplate[j]].typeInfo.TypeID
})
posesAtSorted := make([]posAtSorted, len(posesAtTemplate))
for i, j := range posesAtTemplate {
Expand All @@ -122,20 +123,21 @@ func newForker(

sortedNewValuesTemplate := append(newValuesTemplate[:0:0], newValuesTemplate...)
sort.Slice(sortedNewValuesTemplate, func(i, j int) bool {
return sortedNewValuesTemplate[i].TypeID < sortedNewValuesTemplate[j].TypeID
return sortedNewValuesTemplate[i].typeInfo.TypeID <
sortedNewValuesTemplate[j].typeInfo.TypeID
})
valuesTemplate := scope.values.Append(sortedNewValuesTemplate)

colors := make(map[_TypeID]int)
downstreams := make(map[_TypeID][]_Value)
var traverse func(values []_Value, path []_TypeID) error
traverse = func(values []_Value, path []_TypeID) error {
id := values[0].TypeID
id := values[0].typeInfo.TypeID
color := colors[id]
switch color {
case 1:
return we.With(
e5.Info("found dependency loop in definition %v", values[0].DefType),
e5.Info("found dependency loop in definition %v", values[0].typeInfo.DefType),
func() e5.WrapFunc {
buf := new(strings.Builder)
for i, id := range path {
Expand All @@ -154,12 +156,12 @@ func newForker(
}
colors[id] = 1
for _, value := range values {
if value.DefType.Kind() != reflect.Func {
if value.typeInfo.DefType.Kind() != reflect.Func {
continue
}
numIn := value.DefType.NumIn()
numIn := value.typeInfo.DefType.NumIn()
for i := 0; i < numIn; i++ {
requiredType := value.DefType.In(i)
requiredType := value.typeInfo.DefType.In(i)
id2 := getTypeID(requiredType)
downstreams[id2] = append(
downstreams[id2],
Expand All @@ -169,13 +171,13 @@ func newForker(
value2, ok := valuesTemplate.Load(id2)
if !ok {
return we.With(
e5.Info("dependency not found in definition %v", value.DefType),
e5.Info("dependency not found in definition %v", value.typeInfo.DefType),
e5.Info("no definition for %v", requiredType),
)(
ErrDependencyNotFound,
)
}
if err := traverse(value2, append(path, value.TypeID)); err != nil {
if err := traverse(value2, append(path, value.typeInfo.TypeID)); err != nil {
return err
}
}
Expand All @@ -193,7 +195,7 @@ func newForker(
}

for _, value := range values {
id := getTypeID(value.DefType)
id := getTypeID(value.typeInfo.DefType)
i := sort.Search(len(defTypeIDs), func(i int) bool {
return id >= defTypeIDs[i]
})
Expand All @@ -212,15 +214,15 @@ func newForker(
}

if len(values) > 1 {
t := typeIDToType(values[0].TypeID)
t := typeIDToType(values[0].typeInfo.TypeID)
if getReducerType(t) == nil {
return we.With(
e5.Info("%v has multiple definitions", t),
)(
ErrBadDefinition,
)
}
reducers[values[0].TypeID] = t
reducers[values[0].typeInfo.TypeID] = t
}

return nil
Expand Down Expand Up @@ -255,14 +257,14 @@ func newForker(
return
}
for _, downstream := range downstreams[id] {
if _, ok := scope.values.LoadOne(downstream.TypeID); !ok {
if _, ok := scope.values.LoadOne(downstream.typeInfo.TypeID); !ok {
continue
}
if _, ok := set[downstream.TypeID]; !ok {
resetIDs = append(resetIDs, downstream.TypeID)
set[downstream.TypeID] = struct{}{}
if _, ok := set[downstream.typeInfo.TypeID]; !ok {
resetIDs = append(resetIDs, downstream.typeInfo.TypeID)
set[downstream.typeInfo.TypeID] = struct{}{}
}
resetDownstream(downstream.TypeID)
resetDownstream(downstream.typeInfo.TypeID)
}
colors[id] = 1
}
Expand All @@ -289,25 +291,25 @@ func newForker(
}
markType := getReducerMarkType(t, id)
resetReducers = append(resetReducers, reducerInfo{
_TypeInfo: &_TypeInfo{
typeInfo: &_TypeInfo{
TypeID: getTypeID(markType),
DefType: reflect.TypeOf(func() {}),
},
OriginType: t,
ReducerType: getReducerType(t),
originType: t,
reducerType: getReducerType(t),
})
resetReducerSet[id] = true
}
// new reducers
for _, value := range newValuesTemplate {
resetReducer(value.TypeID)
resetReducer(value.typeInfo.TypeID)
}
// reset reducers
for _, id := range resetIDs {
resetReducer(id)
}
sort.Slice(resetReducers, func(i, j int) bool {
return resetReducers[i].TypeID < resetReducers[j].TypeID
return resetReducers[i].typeInfo.TypeID < resetReducers[j].typeInfo.TypeID
})

return &_Forker{
Expand Down Expand Up @@ -343,7 +345,7 @@ func (f *_Forker) Fork(s Scope, defs []any) Scope {
throw(err)
}
sort.Slice(values, func(i, j int) bool {
return values[i].TypeID < values[j].TypeID
return values[i].typeInfo.TypeID < values[j].typeInfo.TypeID
})
scope.values = &_StackedMap{
Values: values,
Expand All @@ -363,17 +365,17 @@ func (f *_Forker) Fork(s Scope, defs []any) Scope {
for i := 0; i < numValues; i++ {
info := f.NewValuesTemplate[n]
newValues[f.PosesAtSorted[n]] = _Value{
_TypeInfo: info._TypeInfo,
_Initializer: initializer,
typeInfo: info.typeInfo,
initializer: initializer,
}
n++
}
case reflect.Ptr:
info := f.NewValuesTemplate[n]
initializer := newInitializer(def, nil)
newValues[f.PosesAtSorted[n]] = _Value{
_TypeInfo: info._TypeInfo,
_Initializer: initializer,
typeInfo: info.typeInfo,
initializer: initializer,
}
n++
}
Expand All @@ -389,20 +391,20 @@ func (f *_Forker) Fork(s Scope, defs []any) Scope {
panic("impossible")
}
for _, value := range vs {
if value.DefIsMulti {
if value.typeInfo.DefIsMulti {
// multiple types using the same definiton
found := false
for _, d := range resetValues {
if d.ID == value.ID {
if d.initializer.ID == value.initializer.ID {
found = true
value._Initializer = d._Initializer
value.initializer = d.initializer
}
}
if !found {
value._Initializer = value.reset()
value.initializer = value.initializer.reset()
}
} else {
value._Initializer = value.reset()
value.initializer = value.initializer.reset()
}
resetValues = append(resetValues, value)
}
Expand All @@ -415,8 +417,8 @@ func (f *_Forker) Fork(s Scope, defs []any) Scope {
reducerValues := make([]_Value, 0, len(f.ResetReducers))
for _, info := range f.ResetReducers {
reducerValues = append(reducerValues, _Value{
_TypeInfo: info._TypeInfo,
_Initializer: newInitializer(info.OriginType, info.ReducerType),
typeInfo: info.typeInfo,
initializer: newInitializer(info.originType, info.reducerType),
})
}
scope.values = scope.values.Append(reducerValues)
Expand Down
4 changes: 2 additions & 2 deletions initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func (i *_Initializer) getSlow(scope Scope) (ret []reflect.Value, err error) {
vs := make([]reflect.Value, len(values))
for i, value := range values {
var values []reflect.Value
values, err = value.get(pathScope)
values, err = value.initializer.get(pathScope)
if err != nil { // NOCOVER
return
}
vs[i] = values[value.Position]
vs[i] = values[value.typeInfo.Position]
}
switch *i.ReducerType {
case reducerType:
Expand Down
12 changes: 6 additions & 6 deletions stacked_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s *_StackedMap) Load(id _TypeID) ([]_Value, bool) {
// find left bound
for left < right {
idx = (left + right) >> 1
id2 = s.Values[idx].TypeID
id2 = s.Values[idx].typeInfo.TypeID
// need to find the first value
if id2 >= id {
right = idx
Expand All @@ -30,7 +30,7 @@ func (s *_StackedMap) Load(id _TypeID) ([]_Value, bool) {
}
start = -1
for ; idx < l; idx++ {
id2 = s.Values[idx].TypeID
id2 = s.Values[idx].typeInfo.TypeID
if id2 == id {
if start < 0 {
// found start
Expand Down Expand Up @@ -63,7 +63,7 @@ func (s *_StackedMap) LoadOne(id _TypeID) (ret _Value, ok bool) {
right = uint(len(s.Values))
for left < right {
idx = (left + right) >> 1
id2 = s.Values[idx].TypeID
id2 = s.Values[idx].typeInfo.TypeID
if id2 > id {
right = idx
} else if id2 < id {
Expand All @@ -84,14 +84,14 @@ func (s *_StackedMap) Range(fn func([]_Value) error) error {
var start, end int
for s != nil {
for j, d := range s.Values {
if _, ok := keys[d.TypeID]; ok {
if _, ok := keys[d.typeInfo.TypeID]; ok {
continue
}
keys[d.TypeID] = struct{}{}
keys[d.typeInfo.TypeID] = struct{}{}
start = j
end = start + 1
for _, follow := range s.Values[j+1:] {
if follow.TypeID == d.TypeID {
if follow.typeInfo.TypeID == d.typeInfo.TypeID {
end++
} else {
break
Expand Down
24 changes: 12 additions & 12 deletions stacked_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import "testing"
func TestStackedMap(t *testing.T) {
var m *_StackedMap
m = m.Append([]_Value{
{_TypeInfo: &_TypeInfo{TypeID: 1, Position: 1}},
{_TypeInfo: &_TypeInfo{TypeID: 2, Position: 2}},
{_TypeInfo: &_TypeInfo{TypeID: 2, Position: 3}},
{_TypeInfo: &_TypeInfo{TypeID: 2, Position: 4}},
{_TypeInfo: &_TypeInfo{TypeID: 3, Position: 5}},
{typeInfo: &_TypeInfo{TypeID: 1, Position: 1}},
{typeInfo: &_TypeInfo{TypeID: 2, Position: 2}},
{typeInfo: &_TypeInfo{TypeID: 2, Position: 3}},
{typeInfo: &_TypeInfo{TypeID: 2, Position: 4}},
{typeInfo: &_TypeInfo{TypeID: 3, Position: 5}},
})
m = m.Append([]_Value{
{_TypeInfo: &_TypeInfo{TypeID: 3, Position: 6}},
{typeInfo: &_TypeInfo{TypeID: 3, Position: 6}},
})

vs, ok := m.Load(1)
Expand All @@ -22,7 +22,7 @@ func TestStackedMap(t *testing.T) {
if len(vs) != 1 {
t.Fatal()
}
if vs[0].Position != 1 {
if vs[0].typeInfo.Position != 1 {
t.Fatal()
}

Expand All @@ -33,13 +33,13 @@ func TestStackedMap(t *testing.T) {
if len(vs) != 3 {
t.Fatalf("got %d", len(vs))
}
if vs[0].Position != 2 {
if vs[0].typeInfo.Position != 2 {
t.Fatal()
}
if vs[1].Position != 3 {
if vs[1].typeInfo.Position != 3 {
t.Fatal()
}
if vs[2].Position != 4 {
if vs[2].typeInfo.Position != 4 {
t.Fatal()
}

Expand All @@ -52,8 +52,8 @@ func TestStackedMap(t *testing.T) {
if err := m.Range(func(ds []_Value) error {
for _, d := range ds {
n++
if d.TypeID == 3 {
if d.Position != 6 {
if d.typeInfo.TypeID == 3 {
if d.typeInfo.Position != 6 {
t.Fatal()
}
}
Expand Down

0 comments on commit 8508ab7

Please sign in to comment.