Remove count field from ChunkList
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent 487c8fe · Browse files · 3 files·+17 added−25 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+17 added−25 removed
- other:+17 added−25 removed
Provenance
- Commit
0558dfe- Tree
0ff7906- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
487c8fe
3 files changed+17 added−25 removed
modified:src/chunklist_test.go+2 added-5 removed
9 unmodified lines10 func TestChunkList(t *testing.T) {11 // FIXME global12 sortCriteria = []criterion{byScore, byLength}13 14- cl := NewChunkList(func(item *Item, s []byte, i int) bool {14+ cl := NewChunkList(func(item *Item, s []byte) bool {15 item.text = util.ToChars(s)16- item.text.Index = int32(i * 2)16 return true17 })18 19 // Snapshot22 unmodified lines42 if chunk1.count != 2 {43 t.Error("Snapshot should contain only two items")44 }45 if chunk1.items[0].text.ToString() != "hello" ||47- chunk1.items[0].Index() != 0 ||48- chunk1.items[1].text.ToString() != "world" ||46+ chunk1.items[1].text.ToString() != "world" {49- chunk1.items[1].Index() != 2 {47 t.Error("Invalid data")48 }49 if chunk1.IsFull() {50 t.Error("Chunk should not have been marked full yet")30 unmodified linesmodified:src/chunklist.go+7 added-15 removed
6 unmodified lines7 items [chunkSize]Item8 count int9 }10 11-// ItemBuilder is a closure type that builds Item object from a pointer to a12-// string and an integer13-type ItemBuilder func(*Item, []byte, int) bool11+// ItemBuilder is a closure type that builds Item object from byte array12+type ItemBuilder func(*Item, []byte) bool13 14 // ChunkList is a list of Chunks15 type ChunkList struct {16 chunks []*Chunk18- count int17 mutex sync.Mutex18 trans ItemBuilder19 }20 21 // NewChunkList returns a new ChunkList22 func NewChunkList(trans ItemBuilder) *ChunkList {23 return &ChunkList{24 chunks: []*Chunk{},27- count: 0,25 mutex: sync.Mutex{},26 trans: trans}27 }28 32-func (c *Chunk) push(trans ItemBuilder, data []byte, index int) bool {33- if trans(&c.items[c.count], data, index) {29+func (c *Chunk) push(trans ItemBuilder, data []byte) bool {30+ if trans(&c.items[c.count], data) {31 c.count++32 return true33 }34 return false23 unmodified lines58 if len(cl.chunks) == 0 || cl.lastChunk().IsFull() {59 cl.chunks = append(cl.chunks, &Chunk{})60 }61 65- if cl.lastChunk().push(cl.trans, data, cl.count) {66- cl.count++67- cl.mutex.Unlock()68- return true69- }62+ ret := cl.lastChunk().push(cl.trans, data)63 cl.mutex.Unlock()71- return false64+ return ret65 }66 67 // Snapshot returns immutable snapshot of the ChunkList68 func (cl *ChunkList) Snapshot() ([]*Chunk, int) {69 cl.mutex.Lock()70 71 ret := make([]*Chunk, len(cl.chunks))79- count := cl.count72 copy(ret, cl.chunks)73 74 // Duplicate the last chunk75 if cnt := len(ret); cnt > 0 {1 unmodified line77 ret[cnt-1] = &newChunk78 }79 80 cl.mutex.Unlock()89- return ret, count81+ return ret, CountItems(ret)82 }modified:src/core.go+8 added-5 removed
81 unmodified lines82 }83 84 // Chunk list85 var chunkList *ChunkList86+ var itemIndex int3287 header := make([]string, 0, opts.HeaderLines)88 if len(opts.WithNth) == 0 {88- chunkList = NewChunkList(func(item *Item, data []byte, index int) bool {89+ chunkList = NewChunkList(func(item *Item, data []byte) bool {90 if len(header) < opts.HeaderLines {91 header = append(header, string(data))92 eventBox.Set(EvtHeader, header)93 return false94 }95 item.text, item.colors = ansiProcessor(data)95- item.text.Index = int32(index)96+ item.text.Index = itemIndex97+ itemIndex++98 return true99 })100 } else {99- chunkList = NewChunkList(func(item *Item, data []byte, index int) bool {101+ chunkList = NewChunkList(func(item *Item, data []byte) bool {102 tokens := Tokenize(string(data), opts.Delimiter)103 trans := Transform(tokens, opts.WithNth)104 transformed := joinTokens(trans)105 if len(header) < opts.HeaderLines {1 unmodified line107 eventBox.Set(EvtHeader, header)108 return false109 }110 item.text, item.colors = ansiProcessor([]byte(transformed))109- item.text.Index = int32(index)111+ item.text.Index = itemIndex112 item.origText = &data113+ itemIndex++114 return true115 })116 }117 37 unmodified lines155 slab := util.MakeSlab(slab16Size, slab32Size)156 reader := NewReader(157 func(runes []byte) bool {158 item := Item{}156- if chunkList.trans(&item, runes, 0) {159+ if chunkList.trans(&item, runes) {160 if result, _, _ := pattern.MatchItem(&item, false, slab); result != nil {161 opts.Printer(item.text.ToString())162 found = true163 }117 unmodified lines