Attachment "movefix.txt" to
ticket [578460ffff]
added by
btheado
2002-07-08 04:18:32.
? movefix.txt
Index: tree.tcl
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/struct/tree.tcl,v
retrieving revision 1.16
diff -u -r1.16 tree.tcl
--- tree.tcl 9 May 2002 05:46:04 -0000 1.16
+++ tree.tcl 7 Jul 2002 21:15:55 -0000
@@ -620,6 +620,7 @@
set index [expr {[lsearch -exact $children($parentNode) $val] + 1}]
}
+ # Validate each node to move
foreach node $args {
if { [string equal $node "root"] } {
error "cannot move root node"
@@ -638,27 +639,20 @@
}
set ancestor $parent($ancestor)
}
-
- # Remove this node from its parent's children list
+ }
+
+ # Remove each node from its current parent
+ foreach node $args {
set oldParent $parent($node)
set ind [lsearch -exact $children($oldParent) $node]
set children($oldParent) [lreplace $children($oldParent) $ind $ind]
# Update the nodes parent value
set parent($node) $parentNode
-
- # If the node is moving within its parent, and its old location
- # was before the new location, decrement the new location, so that
- # it gets put in the right spot
- if { [string equal $oldParent $parentNode] && $ind < $index } {
- incr index -1
- }
-
- # Add this node to its parent's children list
- set children($parentNode) [linsert $children($parentNode) $index $node]
-
- incr index
}
+
+ # Add all the nodes to the new parent's children list
+ set children($parentNode) [eval linsert [list $children($parentNode)] $index $args]
return
}
Index: tree.test
===================================================================
RCS file: /cvsroot/tcllib/tcllib/modules/struct/tree.test,v
retrieving revision 1.15
diff -u -r1.15 tree.test
--- tree.test 9 May 2002 05:46:04 -0000 1.15
+++ tree.test 7 Jul 2002 21:15:57 -0000
@@ -328,7 +328,7 @@
set result [mytree children root]
mytree destroy
set result
-} [list node0 node1 node2]
+} [list node2 node0 node1]
test tree-7.9 {move moves node within parent properly} {
tree mytree
mytree insert root end node0 node1 node2 node3
@@ -336,7 +336,7 @@
set result [mytree children root]
mytree destroy
set result
-} [list node0 node1 node2 node3]
+} [list node0 node2 node1 node3]
test tree-7.10 {move moves node within parent properly} {
tree mytree
mytree insert root end node0 node1 node2 node3
@@ -345,7 +345,8 @@
set result [mytree children root]
mytree destroy
set result
-} [list node1 node0 node4 node5 node6 node2 node3]
+} [list node1 node2 node0 node4 node5 node6 node3]
+
test tree-7.11 {move moves node in parent properly when oldInd < newInd} {
tree mytree
mytree insert root end node0 node1 node2 node3
@@ -353,7 +354,25 @@
set result [mytree children root]
mytree destroy
set result
+} [list node1 node2 node0 node3]
+
+test tree-7.12 {move node up one} {
+ tree mytree
+ mytree insert root end node0 node1 node2 node3
+ mytree move root [mytree index [mytree next node0]] node0
+ set result [mytree children root]
+ mytree destroy
+ set result
} [list node1 node0 node2 node3]
+
+test tree-7.13 {move node down one} {
+ tree mytree
+ mytree insert root end node0 node1 node2 node3
+ mytree move root [mytree index [mytree previous node2]] node2
+ set result [mytree children root]
+ mytree destroy
+ set result
+} [list node0 node2 node1 node3]
test tree-8.1 {parent gives error on fake node} {
tree mytree