Remove count field from ChunkList

This commit is contained in:
Junegunn Choi 2017-08-16 12:26:06 +09:00
parent 487c8fe88f
commit 0558dfee79
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
3 changed files with 17 additions and 25 deletions

View File

@ -8,14 +8,12 @@ type Chunk struct {
count int count int
} }
// ItemBuilder is a closure type that builds Item object from a pointer to a // ItemBuilder is a closure type that builds Item object from byte array
// string and an integer type ItemBuilder func(*Item, []byte) bool
type ItemBuilder func(*Item, []byte, int) bool
// ChunkList is a list of Chunks // ChunkList is a list of Chunks
type ChunkList struct { type ChunkList struct {
chunks []*Chunk chunks []*Chunk
count int
mutex sync.Mutex mutex sync.Mutex
trans ItemBuilder trans ItemBuilder
} }
@ -24,13 +22,12 @@ type ChunkList struct {
func NewChunkList(trans ItemBuilder) *ChunkList { func NewChunkList(trans ItemBuilder) *ChunkList {
return &ChunkList{ return &ChunkList{
chunks: []*Chunk{}, chunks: []*Chunk{},
count: 0,
mutex: sync.Mutex{}, mutex: sync.Mutex{},
trans: trans} trans: trans}
} }
func (c *Chunk) push(trans ItemBuilder, data []byte, index int) bool { func (c *Chunk) push(trans ItemBuilder, data []byte) bool {
if trans(&c.items[c.count], data, index) { if trans(&c.items[c.count], data) {
c.count++ c.count++
return true return true
} }
@ -62,13 +59,9 @@ func (cl *ChunkList) Push(data []byte) bool {
cl.chunks = append(cl.chunks, &Chunk{}) cl.chunks = append(cl.chunks, &Chunk{})
} }
if cl.lastChunk().push(cl.trans, data, cl.count) { ret := cl.lastChunk().push(cl.trans, data)
cl.count++
cl.mutex.Unlock()
return true
}
cl.mutex.Unlock() cl.mutex.Unlock()
return false return ret
} }
// Snapshot returns immutable snapshot of the ChunkList // Snapshot returns immutable snapshot of the ChunkList
@ -76,7 +69,6 @@ func (cl *ChunkList) Snapshot() ([]*Chunk, int) {
cl.mutex.Lock() cl.mutex.Lock()
ret := make([]*Chunk, len(cl.chunks)) ret := make([]*Chunk, len(cl.chunks))
count := cl.count
copy(ret, cl.chunks) copy(ret, cl.chunks)
// Duplicate the last chunk // Duplicate the last chunk
@ -86,5 +78,5 @@ func (cl *ChunkList) Snapshot() ([]*Chunk, int) {
} }
cl.mutex.Unlock() cl.mutex.Unlock()
return ret, count return ret, CountItems(ret)
} }

View File

@ -11,9 +11,8 @@ func TestChunkList(t *testing.T) {
// FIXME global // FIXME global
sortCriteria = []criterion{byScore, byLength} sortCriteria = []criterion{byScore, byLength}
cl := NewChunkList(func(item *Item, s []byte, i int) bool { cl := NewChunkList(func(item *Item, s []byte) bool {
item.text = util.ToChars(s) item.text = util.ToChars(s)
item.text.Index = int32(i * 2)
return true return true
}) })
@ -44,9 +43,7 @@ func TestChunkList(t *testing.T) {
t.Error("Snapshot should contain only two items") t.Error("Snapshot should contain only two items")
} }
if chunk1.items[0].text.ToString() != "hello" || if chunk1.items[0].text.ToString() != "hello" ||
chunk1.items[0].Index() != 0 || chunk1.items[1].text.ToString() != "world" {
chunk1.items[1].text.ToString() != "world" ||
chunk1.items[1].Index() != 2 {
t.Error("Invalid data") t.Error("Invalid data")
} }
if chunk1.IsFull() { if chunk1.IsFull() {

View File

@ -83,20 +83,22 @@ func Run(opts *Options, revision string) {
// Chunk list // Chunk list
var chunkList *ChunkList var chunkList *ChunkList
var itemIndex int32
header := make([]string, 0, opts.HeaderLines) header := make([]string, 0, opts.HeaderLines)
if len(opts.WithNth) == 0 { if len(opts.WithNth) == 0 {
chunkList = NewChunkList(func(item *Item, data []byte, index int) bool { chunkList = NewChunkList(func(item *Item, data []byte) bool {
if len(header) < opts.HeaderLines { if len(header) < opts.HeaderLines {
header = append(header, string(data)) header = append(header, string(data))
eventBox.Set(EvtHeader, header) eventBox.Set(EvtHeader, header)
return false return false
} }
item.text, item.colors = ansiProcessor(data) item.text, item.colors = ansiProcessor(data)
item.text.Index = int32(index) item.text.Index = itemIndex
itemIndex++
return true return true
}) })
} else { } else {
chunkList = NewChunkList(func(item *Item, data []byte, index int) bool { chunkList = NewChunkList(func(item *Item, data []byte) bool {
tokens := Tokenize(string(data), opts.Delimiter) tokens := Tokenize(string(data), opts.Delimiter)
trans := Transform(tokens, opts.WithNth) trans := Transform(tokens, opts.WithNth)
transformed := joinTokens(trans) transformed := joinTokens(trans)
@ -106,8 +108,9 @@ func Run(opts *Options, revision string) {
return false return false
} }
item.text, item.colors = ansiProcessor([]byte(transformed)) item.text, item.colors = ansiProcessor([]byte(transformed))
item.text.Index = int32(index) item.text.Index = itemIndex
item.origText = &data item.origText = &data
itemIndex++
return true return true
}) })
} }
@ -153,7 +156,7 @@ func Run(opts *Options, revision string) {
reader := NewReader( reader := NewReader(
func(runes []byte) bool { func(runes []byte) bool {
item := Item{} item := Item{}
if chunkList.trans(&item, runes, 0) { if chunkList.trans(&item, runes) {
if result, _, _ := pattern.MatchItem(&item, false, slab); result != nil { if result, _, _ := pattern.MatchItem(&item, false, slab); result != nil {
opts.Printer(item.text.ToString()) opts.Printer(item.text.ToString())
found = true found = true