Skip to content

Commit

Permalink
Counter (#24)
Browse files Browse the repository at this point in the history
* add counter for node.index

* rm extra comment
  • Loading branch information
ddddddO authored Dec 30, 2021
1 parent d3221eb commit 54c5422
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 38 deletions.
30 changes: 30 additions & 0 deletions counter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package gtree

import (
"sync"
)

// for Node.index
type counter struct {
n uint
mu sync.Mutex
}

func newCounter() *counter {
return &counter{}
}

func (c *counter) next() uint {
c.mu.Lock()
defer c.mu.Unlock()

c.n += 1
return c.n
}

func (c *counter) reset() {
c.mu.Lock()
defer c.mu.Unlock()

c.n = 0
}
29 changes: 7 additions & 22 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (n *Node) validate() error {
return nil
}

type generateFunc func(row string) *Node
type generateFunc func(row string, idx uint) *Node

type spaceType string

Expand Down Expand Up @@ -105,7 +105,7 @@ var (
nodeIdxMu sync.Mutex
)

func generateFuncTab(row string) *Node {
func generateFuncTab(row string, idx uint) *Node {
var (
text = ""
hierarchy = rootHierarchyNum
Expand Down Expand Up @@ -142,15 +142,10 @@ func generateFuncTab(row string) *Node {
hierarchy = 0
}

nodeIdxMu.Lock()
defer nodeIdxMu.Unlock()

nodeIdx++

return newNode(text, hierarchy, nodeIdx)
return newNode(text, hierarchy, idx)
}

func generateFuncTwoSpaces(row string) *Node {
func generateFuncTwoSpaces(row string, idx uint) *Node {
var (
text = ""
hierarchy = rootHierarchyNum
Expand Down Expand Up @@ -183,15 +178,10 @@ func generateFuncTwoSpaces(row string) *Node {
}
}

nodeIdxMu.Lock()
defer nodeIdxMu.Unlock()

nodeIdx++

return newNode(text, hierarchy, nodeIdx)
return newNode(text, hierarchy, idx)
}

func generateFuncFourSpaces(row string) *Node {
func generateFuncFourSpaces(row string, idx uint) *Node {
var (
text = ""
hierarchy = rootHierarchyNum
Expand Down Expand Up @@ -224,10 +214,5 @@ func generateFuncFourSpaces(row string) *Node {
}
}

nodeIdxMu.Lock()
defer nodeIdxMu.Unlock()

nodeIdx++

return newNode(text, hierarchy, nodeIdx)
return newNode(text, hierarchy, idx)
}
20 changes: 5 additions & 15 deletions programmable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gtree

import (
"io"
"sync"

"github.com/pkg/errors"
)
Expand All @@ -30,24 +29,20 @@ func ExecuteProgrammably(w io.Writer, root *Node, optFns ...OptFn) error {
return err
}

idxCounter.reset()

tree := newTree(conf.encode, conf.formatLastNode, conf.formatIntermedialNode)
tree.addRoot(root)
return tree.grow().expand(w)
}

var (
programableNodeIdx uint
programableNodeIdxMu sync.Mutex
idxCounter = newCounter()
)

// NewRoot creates a starting node for building tree.
func NewRoot(text string) *Node {
programableNodeIdxMu.Lock()
defer programableNodeIdxMu.Unlock()

programableNodeIdx++

return newNode(text, rootHierarchyNum, programableNodeIdx)
return newNode(text, rootHierarchyNum, idxCounter.next())
}

// Add adds a node and returns an instance of it.
Expand All @@ -59,12 +54,7 @@ func (parent *Node) Add(text string) *Node {
}
}

programableNodeIdxMu.Lock()
defer programableNodeIdxMu.Unlock()

programableNodeIdx++

current := newNode(text, parent.hierarchy+1, programableNodeIdx)
current := newNode(text, parent.hierarchy+1, idxCounter.next())
current.setParent(parent)
parent.addChild(current)
return current
Expand Down
4 changes: 3 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,21 @@ func Execute(w io.Writer, r io.Reader, optFns ...OptFn) error {
func sprout(scanner *bufio.Scanner, conf *config) (treeer, error) {
var (
stack *stack
counter = newCounter()
generateNodeFunc = decideGenerateFunc(conf.space)
tree = newTree(conf.encode, conf.formatLastNode, conf.formatIntermedialNode)
)

for scanner.Scan() {
row := scanner.Text()
currentNode := generateNodeFunc(row)
currentNode := generateNodeFunc(row, counter.next())

if err := currentNode.validate(); err != nil {
return nil, err
}

if currentNode.isRoot() {
counter.reset()
tree.addRoot(currentNode)
stack = newStack()
stack.push(currentNode)
Expand Down

0 comments on commit 54c5422

Please sign in to comment.