Skip to content

Commit

Permalink
Merge pull request #13 from gochore/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfogre authored Sep 24, 2020
2 parents f1fea1f + 692e189 commit 1e8179a
Show file tree
Hide file tree
Showing 53 changed files with 7,453 additions and 11,434 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
generage:
rm -f *_sequence.go
rm -f gen_*.go
go run cmd/generate/main.go

test:
go test -v
52 changes: 6 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,12 @@

Time sequence.

## Example
This is v2, work in process.

```go
package main
Switch to [v1](https://github.com/gochore/timeseq/tree/v1).

import (
"fmt"
"time"
## Install

"github.com/gochore/timeseq"
)

func main() {
now := time.Now()
seq := timeseq.Int64Sequence{
{
Time: now,
Value: 0,
},
{
Time: now.Add(time.Second),
Value: 1,
},
{
Time: now.Add(-time.Second),
Value: 2,
},
}
seq = append(seq, timeseq.Int64Item{
Time: now.Add(-2 * time.Second),
Value: -1,
})

// sort by time
seq.Sort()

// get the first one
fmt.Println(seq.First(nil))
// get the last one before now
fmt.Println(seq.Last(&now))

// get the sub sequence after now
subSeq := seq.Range(&now, nil)
// get the sub sequence's length
fmt.Println(subSeq.Len())
// get the first one of the sub sequence
fmt.Println(subSeq.First(nil))
}
```
```bash
go get github.com/gochore/timeseq/v2
```
5 changes: 5 additions & 0 deletions _draft/v1/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/gochore/timeseq/_draft/timeseq

go 1.15

require github.com/gochore/pt v1.1.2
2 changes: 2 additions & 0 deletions _draft/v1/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/gochore/pt v1.1.2 h1:3qS64f1ZrFgp28dperUHfo7U22gL/QeGWsaxQ0veIBE=
github.com/gochore/pt v1.1.2/go.mod h1:8fd2g+qoAO1QIorsgi9vLJCMudJ5m+nDhY8r1nPP6dg=
2 changes: 0 additions & 2 deletions int_sequence.go → _draft/v1/int_sequence.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Code generated by cmd/generate. DO NOT EDIT.

package timeseq

import (
Expand Down
6 changes: 2 additions & 4 deletions int_sequence_test.go → _draft/v1/int_sequence_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Code generated by cmd/generate. DO NOT EDIT.

package timeseq

import (
Expand Down Expand Up @@ -126,7 +124,7 @@ func TestIntSequence_Slice(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.s.Slice(tt.args.i, tt.args.j); !reflect.DeepEqual(got, tt.want) {
t.Errorf("IntSequence.Slice() = %v, want %v", got, tt.want)
t.Errorf("IntSequence.Interface() = %v, want %v", got, tt.want)
}
})
}
Expand All @@ -148,7 +146,7 @@ func TestIntSequence_Sort(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tt.s.Sort()
if !sort.IsSorted(sortableSequence{tt.s}) {
t.Error("IntSequence.Slice() failed")
t.Error("IntSequence.Interface() failed")
}
})
}
Expand Down
File renamed without changes.
62 changes: 62 additions & 0 deletions _draft/v1/timeseq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package timeseq

import (
"sort"
"time"
)

// Sort will sort sequence by time
func Sort(seq Sequence) {
if seq == nil {
return
}
sort.Sort(sortableSequence{seq})
}

// Range return sub sequence
func Range(seq Sequence, afterOrEqual, beforeOrEqual *time.Time) Sequence {
i := 0
if afterOrEqual != nil {
i = sort.Search(seq.Len(), func(i int) bool {
return !seq.Time(i).Before(*afterOrEqual)
})
}
j := seq.Len()
if beforeOrEqual != nil {
j = sort.Search(seq.Len(), func(j int) bool {
return !seq.Time(j).Before(*beforeOrEqual)
})
if j < seq.Len() && seq.Time(j).Equal(*beforeOrEqual) {
j++
}
}
return seq.Slice(i, j)
}

// First return the index of the first item
func First(seq Sequence, afterOrEqual *time.Time) int {
i := 0
if afterOrEqual != nil {
i = sort.Search(seq.Len(), func(i int) bool {
return !seq.Time(i).Before(*afterOrEqual)
})
if i >= seq.Len() {
i = -1
}
}
return i
}

// Last return the index of the last item
func Last(seq Sequence, beforeOrEqual *time.Time) int {
j := seq.Len() - 1
if beforeOrEqual != nil {
j = sort.Search(seq.Len(), func(i int) bool {
return !seq.Time(i).Before(*beforeOrEqual)
})
if j == seq.Len() || j < seq.Len() && !seq.Time(j).Equal(*beforeOrEqual) {
j--
}
}
return j
}
Loading

0 comments on commit 1e8179a

Please sign in to comment.