Skip to content

Commit

Permalink
Merge pull request #27 from gochore/dev
Browse files Browse the repository at this point in the history
feat: use shorter time key
  • Loading branch information
wolfogre authored Jan 13, 2021
2 parents 7cee8a4 + f8715bc commit 61d08b2
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 21 deletions.
34 changes: 33 additions & 1 deletion cmd/generate/gen_x_seq.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,46 @@ import (
"time"
)

// {{.Name}} is a time point with {{.Type}} value inside
type {{.Name}} struct {
Time time.Time
Value {{.Type}}
}

// IsZero return if time and value are both zero
func (v {{.Name}}) IsZero() bool {
return v.Value == 0 && v.Time.IsZero()
}

// IsZero return if time and value are both equal
func (v {{.Name}}) Equal(n {{.Name}}) bool {
return v.Value == n.Value && v.Time.Equal(n.Time)
}

// {{.Name}}s is a alias of {{.Name}} slice
type {{.Name}}s []{{.Name}}

// Len implements Interface.Len()
func (s {{.Name}}s) Len() int {
return len(s)
}

// Swap implements Interface.Swap()
func (s {{.Name}}s) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

// Time implements Interface.Time()
func (s {{.Name}}s) Time(i int) time.Time {
return s[i].Time
}

// Slice implements Interface.Slice()
func (s {{.Name}}s) Slice(i, j int) Interface {
return s[i:j]
}

// {{.Name}}Seq is a wrapper with useful methods of {{.Name}} slice
type {{.Name}}Seq struct {
slice {{.Name}}s

Expand All @@ -47,12 +56,14 @@ type {{.Name}}Seq struct {
valueSlice []int
}

// New{{.Name}}Seq return *{{.Name}}Seq with copied slice inside
func New{{.Name}}Seq(slice {{.Name}}s) *{{.Name}}Seq {
temp := make({{.Name}}s, len(slice))
copy(temp, slice)
return Wrap{{.Name}}Seq(temp)
}

// Wrap{{.Name}}Seq return *{{.Name}}Seq with origin slice inside
func Wrap{{.Name}}Seq(slice {{.Name}}s) *{{.Name}}Seq {
if !IsSorted(slice) {
Sort(slice)
Expand Down Expand Up @@ -91,23 +102,27 @@ func (s *{{.Name}}Seq) resetIndex() {
s.indexOnce = sync.Once{}
}

// {{.Name}}s return a replica of inside slice
func (s *{{.Name}}Seq) {{.Name}}s() {{.Name}}s {
slice := make({{.Name}}s, len(s.slice))
copy(slice, s.slice)
return slice
}

// Len return length of inside slice
func (s *{{.Name}}Seq) Len() int {
return len(s.slice)
}

// Index return element of inside slice, return zero if index is out of range
func (s *{{.Name}}Seq) Index(i int) {{.Name}} {
if i < 0 || i >= len(s.slice) {
return {{.Name}}{}
}
return s.slice[i]
}

// Time return the first element with time t, return zero if not found
func (s *{{.Name}}Seq) Time(t time.Time) {{.Name}} {
got := s.MTime(t)
if len(got) == 0 {
Expand All @@ -116,6 +131,7 @@ func (s *{{.Name}}Seq) Time(t time.Time) {{.Name}} {
return got[0]
}

// MTime return all elements with time t, return nil if not found
func (s *{{.Name}}Seq) MTime(t time.Time) {{.Name}}s {
s.buildIndex()
index := s.timeIndex[newTimeKey(t)]
Expand All @@ -129,6 +145,7 @@ func (s *{{.Name}}Seq) MTime(t time.Time) {{.Name}}s {
return ret
}

// Value return the first element with value v, return zero if not found
func (s *{{.Name}}Seq) Value(v {{.Type}}) {{.Name}} {
got := s.MValue(v)
if len(got) == 0 {
Expand All @@ -137,6 +154,7 @@ func (s *{{.Name}}Seq) Value(v {{.Type}}) {{.Name}} {
return got[0]
}

// MValue return all elements with value v, return nil if not found
func (s *{{.Name}}Seq) MValue(v {{.Type}}) {{.Name}}s {
s.buildIndex()
index := s.valueIndex[v]
Expand All @@ -150,14 +168,16 @@ func (s *{{.Name}}Seq) MValue(v {{.Type}}) {{.Name}}s {
return ret
}

func (s *{{.Name}}Seq) Visit(fn func(i int, v {{.Name}}) (stop bool)) {
// Traverse call fn for every element one by one, break if fn return true
func (s *{{.Name}}Seq) Traverse(fn func(i int, v {{.Name}}) (stop bool)) {
for i, v := range s.slice {
if fn != nil && fn(i, v) {
break
}
}
}

// Sum return sum of all values
func (s *{{.Name}}Seq) Sum() {{.Type}} {
var ret {{.Type}}
for _, v := range s.slice {
Expand All @@ -166,10 +186,12 @@ func (s *{{.Name}}Seq) Sum() {{.Type}} {
return ret
}

// Count return count of elements, same as Len
func (s *{{.Name}}Seq) Count() int {
return s.Len()
}

// Max return the element with max value, return zero if empty
func (s *{{.Name}}Seq) Max() {{.Name}} {
var max {{.Name}}
found := false
Expand All @@ -184,6 +206,7 @@ func (s *{{.Name}}Seq) Max() {{.Name}} {
return max
}

// Max return the element with min value, return zero if empty
func (s *{{.Name}}Seq) Min() {{.Name}} {
var min {{.Name}}
found := false
Expand All @@ -198,20 +221,24 @@ func (s *{{.Name}}Seq) Min() {{.Name}} {
return min
}

// First return the first element, return zero if empty
func (s *{{.Name}}Seq) First() {{.Name}} {
if len(s.slice) == 0 {
return {{.Name}}{}
}
return s.slice[0]
}

// Last return the last element, return zero if empty
func (s *{{.Name}}Seq) Last() {{.Name}} {
if len(s.slice) == 0 {
return {{.Name}}{}
}
return s.slice[len(s.slice)-1]
}

// Percentile return the element matched with percentile pct, return zero if empty,
// the pct's valid range is be [0, 1], it will be treated as 1 if greater than 1, as 0 if smaller than 0
func (s *{{.Name}}Seq) Percentile(pct float64) {{.Name}} {
s.buildIndex()
if len(s.slice) == 0 {
Expand All @@ -230,11 +257,13 @@ func (s *{{.Name}}Seq) Percentile(pct float64) {{.Name}} {
return s.slice[s.valueSlice[i]]
}

// Range return a sub *{{.Name}}Seq with specified interval
func (s *{{.Name}}Seq) Range(interval Interval) *{{.Name}}Seq {
slice := Range(s.slice, interval).({{.Name}}s)
return new{{.Name}}Seq(slice)
}

// Merge merge slices to inside slice according to the specified rule
func (s *{{.Name}}Seq) Merge(fn func(t time.Time, v1, v2 *{{.Type}}) *{{.Type}}, slices ...{{.Name}}s) error {
if fn == nil {
return errors.New("nil fn")
Expand Down Expand Up @@ -302,6 +331,7 @@ func (s *{{.Name}}Seq) Merge(fn func(t time.Time, v1, v2 *{{.Type}}) *{{.Type}},
return nil
}

// Aggregate aggregate inside slice according to the specified rule
func (s *{{.Name}}Seq) Aggregate(fn func(t time.Time, slice {{.Name}}s) *{{.Type}}, duration time.Duration, interval Interval) error {
if fn == nil {
return errors.New("nil fn")
Expand Down Expand Up @@ -363,6 +393,7 @@ func (s *{{.Name}}Seq) Aggregate(fn func(t time.Time, slice {{.Name}}s) *{{.Type
return nil
}

// Trim remove the elements which make fn return true
func (s *{{.Name}}Seq) Trim(fn func(i int, v {{.Name}}) bool) error {
if fn == nil {
return errors.New("nil fn")
Expand All @@ -385,6 +416,7 @@ func (s *{{.Name}}Seq) Trim(fn func(i int, v {{.Name}}) bool) error {
return nil
}

// Clone return a new *{{.Name}}Seq with copied slice inside
func (s *{{.Name}}Seq) Clone() *{{.Name}}Seq {
if s == nil {
return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/generate/gen_x_seq_test.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func Test{{.Name}}Seq_MValue(t *testing.T) {
}
}

func Test{{.Name}}Seq_Visit(t *testing.T) {
func Test{{.Name}}Seq_Traverse(t *testing.T) {
data := Random{{.Name}}s(100)

type args struct {
Expand Down Expand Up @@ -363,7 +363,7 @@ func Test{{.Name}}Seq_Visit(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := New{{.Name}}Seq(data)
s.Visit(tt.args.fn)
s.Traverse(tt.args.fn)
})
}
}
Expand Down
Loading

0 comments on commit 61d08b2

Please sign in to comment.