Skip to content

Commit

Permalink
Get rid of recursion N+1 while setting node children
Browse files Browse the repository at this point in the history
  • Loading branch information
s0ber committed Nov 19, 2014
1 parent d131301 commit 7b5135a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
26 changes: 18 additions & 8 deletions spec/vtree_src/node_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,38 @@ describe 'Node', ->
@node.setParent(@secondNode)
expect(@node.parent).to.be.equal @secondNode

describe '.setChildren', ->
it 'creates list of provided nodes array, which parent is current node and saves them as @children', ->
@secondNode.setParent(@node)
@fourthNode.setParent(@node)

@node.setChildren([@secondNode, @thirdNode, @fourthNode])
describe '.prependChild', ->
it 'prepends provided Node instance to @children array', ->
@node.prependChild(@secondNode)
expect(@node.children).to.be.eql [@secondNode]
@node.prependChild(@fourthNode)
expect(@node.children).to.be.eql [@fourthNode, @secondNode]

describe '.appendChild', ->
it 'appends provided Node instance to @children array', ->
@node.appendChild(@secondNode)
expect(@node.children).to.be.eql [@secondNode]
@node.appendChild(@fourthNode)
expect(@node.children).to.be.eql [@secondNode, @fourthNode]

describe '.removeChild', ->
it 'removes provided child from @children', ->
@secondNode.setParent(@node)
@fourthNode.setParent(@node)

@node.setChildren([@secondNode, @fourthNode])
@node.appendChild(@secondNode)
@node.appendChild(@fourthNode)

@node.removeChild(@secondNode)
expect(@node.children).to.be.eql [@fourthNode]

it 'does nothing if provided child is not in @children', ->
@secondNode.setParent(@node)
@fourthNode.setParent(@node)

@node.setChildren([@secondNode, @fourthNode])
@node.appendChild(@secondNode)
@node.appendChild(@fourthNode)

@node.removeChild(@thirdNode)
expect(@node.children).to.be.eql [@secondNode, @fourthNode]

Expand Down
10 changes: 5 additions & 5 deletions src/vtree_src/node.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class Node
setParent: (node) ->
@parent = node

setChildren: (nodes) ->
@children = []
for node in nodes
@children.push(node) if node.parent is @
@children
prependChild: (node) ->
@children.unshift(node)

appendChild: (node) ->
@children.push(node)

removeChild: (node) ->
return if (nodeIndex = _.indexOf(@children, node)) is -1
Expand Down
14 changes: 7 additions & 7 deletions src/vtree_src/tree_manager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,9 @@ class TreeManager
setChildrenForNodes: (nodes) ->
return unless nodes.length

node = nodes.shift()

# setting child nodes for first node in list
node.setChildren(nodes)

# setting child nodes for remaining nodes
@setChildrenForNodes(nodes)
for i in [(nodes.length - 1)..0]
node = nodes[i]
node.parent?.prependChild(node)

activateInitialNodes: ->
@activateRootNodes(@initialNodes)
Expand Down Expand Up @@ -114,6 +110,10 @@ class TreeManager

newNodes.push(node)

# destroy tree structure
for node in newNodes
node.children.length = 0

@setParentsForNodes(newNodes)
@setChildrenForNodes(newNodes)
@activateNode(refreshedNode)
Expand Down

0 comments on commit 7b5135a

Please sign in to comment.