diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2017-08-15 01:10:41 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2017-08-15 01:10:41 +0900 |
commit | 0d171ba1d81886c6f9caf61867129e6daa268cd6 (patch) | |
tree | e82b6b40439ff4f6fc936f31e65790d976ccddb6 /src/chunklist.go | |
parent | 2069bbc8b54fa77384e42274ee15af7b397af884 (diff) |
Remove special nilItem
Diffstat (limited to 'src/chunklist.go')
-rw-r--r-- | src/chunklist.go | 23 |
1 files changed, 12 insertions, 11 deletions
diff --git a/src/chunklist.go b/src/chunklist.go index 4a4458a6..599a1ad1 100644 --- a/src/chunklist.go +++ b/src/chunklist.go @@ -3,11 +3,14 @@ package fzf import "sync" // Chunk is a list of Items whose size has the upper limit of chunkSize -type Chunk []Item +type Chunk struct { + items [chunkSize]Item + count int +} // ItemBuilder is a closure type that builds Item object from a pointer to a // string and an integer -type ItemBuilder func([]byte, int) Item +type ItemBuilder func(*Item, []byte, int) bool // ChunkList is a list of Chunks type ChunkList struct { @@ -27,17 +30,16 @@ func NewChunkList(trans ItemBuilder) *ChunkList { } func (c *Chunk) push(trans ItemBuilder, data []byte, index int) bool { - item := trans(data, index) - if item.Nil() { - return false + if trans(&c.items[c.count], data, index) { + c.count++ + return true } - *c = append(*c, item) - return true + return false } // IsFull returns true if the Chunk is full func (c *Chunk) IsFull() bool { - return len(*c) == chunkSize + return c.count == chunkSize } func (cl *ChunkList) lastChunk() *Chunk { @@ -49,7 +51,7 @@ func CountItems(cs []*Chunk) int { if len(cs) == 0 { return 0 } - return chunkSize*(len(cs)-1) + len(*(cs[len(cs)-1])) + return chunkSize*(len(cs)-1) + cs[len(cs)-1].count } // Push adds the item to the list @@ -57,8 +59,7 @@ func (cl *ChunkList) Push(data []byte) bool { cl.mutex.Lock() if len(cl.chunks) == 0 || cl.lastChunk().IsFull() { - newChunk := Chunk(make([]Item, 0, chunkSize)) - cl.chunks = append(cl.chunks, &newChunk) + cl.chunks = append(cl.chunks, &Chunk{}) } if cl.lastChunk().push(cl.trans, data, cl.count) { |