Skip to content
Myr

Remove pointer indirection by changing Chunk definition

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 7b5ccc4 · Browse files · 7 files+29 added−23 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+29 added−23 removed

  • other:+29 added−23 removed

7 files+29 added−23 removed

modified:src/chunklist_test.go+2 added-2 removed
9 unmodified lines
10func TestChunkList(t *testing.T) {11 // FIXME global12 sortCriteria = []criterion{byScore, byLength}1314 cl := NewChunkList(func(s []byte, i int) *Item {15 return &Item{text: util.ToChars(s), index: int32(i * 2)}14 cl := NewChunkList(func(s []byte, i int) Item {15 return Item{text: util.ToChars(s), index: int32(i * 2)}16 })1718 // Snapshot19 snapshot, count := cl.Snapshot()
60 unmodified lines
modified:src/chunklist.go+8 added-8 removed
1package fzf23import "sync"45// Chunk is a list of Item pointers whose size has the upper limit of chunkSize6type Chunk []*Item // >>> []Item5// Chunk is a list of Items whose size has the upper limit of chunkSize6type Chunk []Item78// ItemBuilder is a closure type that builds Item object from a pointer to a9// string and an integer10type ItemBuilder func([]byte, int) *Item10type ItemBuilder func([]byte, int) Item1112// ChunkList is a list of Chunks13type ChunkList struct {14 chunks []*Chunk
12 unmodified lines
27}2829func (c *Chunk) push(trans ItemBuilder, data []byte, index int) bool {30 item := trans(data, index)31 if item != nil {32 *c = append(*c, item)33 return true31 if item.Nil() {32 return false33 }34 *c = append(*c, item)35 return false35 return true36}3738// IsFull returns true if the Chunk is full39func (c *Chunk) IsFull() bool {
17 unmodified lines
57 cl.mutex.Lock()58 defer cl.mutex.Unlock()5960 if len(cl.chunks) == 0 || cl.lastChunk().IsFull() {61 newChunk := Chunk(make([]*Item, 0, chunkSize))61 newChunk := Chunk(make([]Item, 0, chunkSize))62 cl.chunks = append(cl.chunks, &newChunk)63 }6465 if cl.lastChunk().push(cl.trans, data, cl.count) {
28 unmodified lines
modified:src/core.go+8 added-8 removed
89 unmodified lines
90 // Chunk list91 var chunkList *ChunkList92 header := make([]string, 0, opts.HeaderLines)93 if len(opts.WithNth) == 0 {94 chunkList = NewChunkList(func(data []byte, index int) *Item {94 chunkList = NewChunkList(func(data []byte, index int) Item {95 if len(header) < opts.HeaderLines {96 header = append(header, string(data))97 eventBox.Set(EvtHeader, header)98 return nil98 return nilItem99 }100 chars, colors := ansiProcessor(data)101 return &Item{101 return Item{102 index: int32(index),103 trimLength: -1,104 text: chars,105 colors: colors}106 })107 } else {108 chunkList = NewChunkList(func(data []byte, index int) *Item {108 chunkList = NewChunkList(func(data []byte, index int) Item {109 tokens := Tokenize(util.ToChars(data), opts.Delimiter)110 trans := Transform(tokens, opts.WithNth)111 if len(header) < opts.HeaderLines {112 header = append(header, string(joinTokens(trans)))113 eventBox.Set(EvtHeader, header)114 return nil114 return nilItem115 }116 textRunes := joinTokens(trans)117 item := Item{118 index: int32(index),
3 unmodified lines
122123 trimmed, colors := ansiProcessorRunes(textRunes)124 item.text = trimmed125 item.colors = colors126 return &item126 return item127 })128 }129130 // Reader
36 unmodified lines
167 slab := util.MakeSlab(slab16Size, slab32Size)168 reader := Reader{169 func(runes []byte) bool {170 item := chunkList.trans(runes, 0)171 if item != nil {172 if result, _, _ := pattern.MatchItem(item, false, slab); result != nil {171 if !item.Nil() {172 if result, _, _ := pattern.MatchItem(&item, false, slab); result != nil {173 opts.Printer(item.text.ToString())174 found = true175 }176 }
116 unmodified lines
modified:src/item.go+6 added
17 unmodified lines
18func (item *Item) Index() int32 {19 return item.index20}2122var nilItem = Item{index: -1}2324func (item *Item) Nil() bool {25 return item.index < 026}2728func (item *Item) TrimLength() int32 {29 if item.trimLength >= 0 {30 return item.trimLength31 }
23 unmodified lines
modified:src/merger.go+1 added-1 removed
63 unmodified lines
64 if mg.tac {65 idx = mg.count - idx - 166 }67 chunk := (*mg.chunks)[idx/chunkSize]68 return &Result{item: (*chunk)[idx%chunkSize]}68 return &Result{item: &(*chunk)[idx%chunkSize]}69 }7071 if mg.sorted {72 return mg.mergedGet(idx)
49 unmodified lines
modified:src/pattern_test.go+2 added-2 removed
137 unmodified lines
138139 origBytes := []byte("junegunn.choi")140 for _, extended := range []bool{false, true} {141 chunk := Chunk{142 &Item{142 Item{143 text: util.RunesToChars([]rune("junegunn")),144 origText: &origBytes,145 transformed: trans},146 }
4 unmodified lines
151 reflect.DeepEqual(matches[0].item.transformed, trans)) {152 t.Error("Invalid match result", matches)153 }154155 match, offsets, pos := pattern.MatchItem(chunk[0], true, slab)155 match, offsets, pos := pattern.MatchItem(&chunk[0], true, slab)156 if !(match.item.text.ToString() == "junegunn" &&157 string(*match.item.origText) == "junegunn.choi" &&158 offsets[0][0] == 0 && offsets[0][1] == 5 &&159 reflect.DeepEqual(match.item.transformed, trans)) {
47 unmodified lines
modified:src/pattern.go+2 added-2 removed
279 unmodified lines
280func (p *Pattern) matchChunk(chunk *Chunk, space []*Result, slab *util.Slab) []*Result {281 matches := []*Result{}282283 if space == nil {284 for _, item := range *chunk {285 if match, _, _ := p.MatchItem(item, false, slab); match != nil {284 for idx := range *chunk {285 if match, _, _ := p.MatchItem(&(*chunk)[idx], false, slab); match != nil {286 matches = append(matches, match)287 }288 }289 } else {
109 unmodified lines