summaryrefslogtreecommitdiffstats
path: root/src/chunklist.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-01-12 03:01:24 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-01-12 03:18:40 +0900
commit7a2bc2cada971c7a390d09b0afda34780ff56fb6 (patch)
treecaa19fa8bf404a854c9e2cdf6eb08194e1733a6f /src/chunklist.go
parent9dbf6b02d24b52ae43e36905bbb1e83087e1dfe9 (diff)
Lint
Diffstat (limited to 'src/chunklist.go')
-rw-r--r--src/chunklist.go20
1 files changed, 15 insertions, 5 deletions
diff --git a/src/chunklist.go b/src/chunklist.go
index 5bca6da8..73983b1d 100644
--- a/src/chunklist.go
+++ b/src/chunklist.go
@@ -2,12 +2,17 @@ package fzf
import "sync"
-const CHUNK_SIZE int = 100
+// Capacity of each chunk
+const ChunkSize int = 100
+// Chunk is a list of Item pointers whose size has the upper limit of ChunkSize
type Chunk []*Item // >>> []Item
+// Transformer is a closure type that builds Item object from a pointer to a
+// string and an integer
type Transformer func(*string, int) *Item
+// ChunkList is a list of Chunks
type ChunkList struct {
chunks []*Chunk
count int
@@ -15,6 +20,7 @@ type ChunkList struct {
trans Transformer
}
+// NewChunkList returns a new ChunkList
func NewChunkList(trans Transformer) *ChunkList {
return &ChunkList{
chunks: []*Chunk{},
@@ -27,34 +33,38 @@ func (c *Chunk) push(trans Transformer, data *string, index int) {
*c = append(*c, trans(data, index))
}
+// IsFull returns true if the Chunk is full
func (c *Chunk) IsFull() bool {
- return len(*c) == CHUNK_SIZE
+ return len(*c) == ChunkSize
}
func (cl *ChunkList) lastChunk() *Chunk {
return cl.chunks[len(cl.chunks)-1]
}
+// CountItems returns the total number of Items
func CountItems(cs []*Chunk) int {
if len(cs) == 0 {
return 0
}
- return CHUNK_SIZE*(len(cs)-1) + len(*(cs[len(cs)-1]))
+ return ChunkSize*(len(cs)-1) + len(*(cs[len(cs)-1]))
}
+// Push adds the item to the list
func (cl *ChunkList) Push(data string) {
cl.mutex.Lock()
defer cl.mutex.Unlock()
if len(cl.chunks) == 0 || cl.lastChunk().IsFull() {
- newChunk := Chunk(make([]*Item, 0, CHUNK_SIZE))
+ newChunk := Chunk(make([]*Item, 0, ChunkSize))
cl.chunks = append(cl.chunks, &newChunk)
}
cl.lastChunk().push(cl.trans, &data, cl.count)
- cl.count += 1
+ cl.count++
}
+// Snapshot returns immutable snapshot of the ChunkList
func (cl *ChunkList) Snapshot() ([]*Chunk, int) {
cl.mutex.Lock()
defer cl.mutex.Unlock()