You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.9 KiB
64 lines
1.9 KiB
package sig |
|
|
|
import ( |
|
"sig-pub/pkg/indicator" |
|
) |
|
|
|
type IndicatorSummary struct { |
|
indicator.IIndicatorSummary |
|
indicatorContext IOffsetIndicatorContext |
|
summaryIndicatorNewer func() indicator.ISummaryIndicator |
|
cachedSummaryIndicators map[string]indicator.ISummaryIndicator |
|
} |
|
|
|
func NewIndicatorSummary(summaryIndicatorNewer func() indicator.ISummaryIndicator, indicatorContext IOffsetIndicatorContext) *IndicatorSummary { |
|
return &IndicatorSummary{ |
|
summaryIndicatorNewer: summaryIndicatorNewer, |
|
indicatorContext: indicatorContext, |
|
cachedSummaryIndicators: make(map[string]indicator.ISummaryIndicator), |
|
} |
|
} |
|
|
|
// Summary 0 10, 0 20, 1 30 |
|
func (s *IndicatorSummary) Summary(offset, count int16) (summary any, rok bool) { |
|
// key := fmt.Sprintf("%d,%d", offset, count) |
|
// summaryIndicator, ok := s.cachedSummaryIndicators[key] |
|
|
|
// if ok { |
|
// s.indicatorContext.AddOffset(offset + count + 1) |
|
// eliminater, ok := summaryIndicator.(indicator.ISummaryIndicatorEliminater) |
|
// if ok { |
|
// eliminater.Eliminate(s.indicatorContext) |
|
// } |
|
// s.indicatorContext.AddOffset(-(offset + count + 1)) |
|
|
|
// s.indicatorContext.AddOffset(offset) |
|
// summaryIndicator.Accumulate(s.indicatorContext) |
|
// summary, ok = summaryIndicator.Summary(s.indicatorContext) |
|
// s.indicatorContext.AddOffset(-offset) |
|
// return |
|
// } |
|
|
|
// 初始化, 计算全量数据 |
|
summaryIndicator := s.summaryIndicatorNewer() |
|
if err := summaryIndicator.Init(s.indicatorContext.Input()); err != nil { |
|
panic(err) |
|
} |
|
// s.cachedSummaryIndicators[key] = summaryIndicator |
|
|
|
// 设置当前相对offset |
|
offset = offset + count |
|
s.indicatorContext.AddOffset(offset) |
|
for range count { |
|
summaryIndicator.Accumulate(s.indicatorContext) |
|
|
|
offset-- |
|
s.indicatorContext.AddOffset(-1) |
|
} |
|
|
|
// 获取结果 |
|
summary, rok = summaryIndicator.Summary(s.indicatorContext) |
|
// 计算结束后还原offset |
|
s.indicatorContext.AddOffset(-offset) |
|
return |
|
}
|
|
|