optimize algorithm to get list from model and to
authorca
Sat, 04 Apr 1998 10:45:57 +0200
changeset 838 d15daece0c54
parent 837 5d437be03bcf
child 839 c77a86a62ac2
optimize algorithm to get list from model and to recompute width of longest line
SelTreeV.st
SelectionInTreeView.st
TreeItem.st
--- a/SelTreeV.st	Sat Apr 04 08:48:46 1998 +0200
+++ b/SelTreeV.st	Sat Apr 04 10:45:57 1998 +0200
@@ -814,7 +814,7 @@
 buttonPress:button x:x y:y
     "check for indicator
     "
-    |expand node lineNr oldSize isExpandable|
+    |expand node lineNr isExpandable|
 
     lineNr := self indicatiorLineForButton:button atX:x y:y.
 
@@ -868,16 +868,7 @@
         self redrawLine:lineNr.
     ] ifFalse:[
      "/ with children; update list and redraw to end.
-        oldSize := list size.
-        model removeDependent:self.
-        model recomputeList.
-        model addDependent:self.
-        list := self listFromModel.
-
-        oldSize ~~ list size ifTrue:[
-            self redrawFromLine:lineNr.
-            self contentsChanged.
-        ]
+        self recomputeListFromNodeAt:lineNr
     ]
 
 
@@ -1252,19 +1243,30 @@
     "get list from model and return the new list.
      If listMessage is nil, try aspectMessage for backward compatibilty.
     "
-    |msg|
-
+    |msg list
+     runs "{ Class: SmallInteger }"
+     idx  "{ Class: SmallInteger }"
+    |
     widthOfWidestLine := nil.
 
     (msg := listMsg ? aspectMsg) notNil ifTrue:[
         listOfNodes := model perform:msg.
-        listOfNodes size ~~ 0 ifTrue:[
+
+        (runs := listOfNodes size) ~~ 0 ifTrue:[
             self refetchDeviceResources.
-            ^ listOfNodes collect:[:n|n name]
+            idx  := 1.
+            list := OrderedCollection new:runs.
+
+            runs timesRepeat:[
+                list add:(listOfNodes at:idx) name.
+                idx := idx + 1.
+            ].
+            ^ list
         ]
     ].
     listOfNodes := #().
   ^ #()
+
 !
 
 model:aModel
@@ -1359,7 +1361,7 @@
     "setup new list; keep selection dependent on the boolean state
      keepSelection
     "
-    |list|
+    |list time|
 
     list := aList.
 
@@ -1399,15 +1401,21 @@
         self redrawLine:anIndex.
     ] ifFalse:[
      "/ with children; update list and redraw to end.
-        model removeDependent:self.
-        model recomputeList.
-        model addDependent:self.
-        list := self listFromModel.
-        self redrawFromLine:anIndex.
-        self contentsChanged.
+        self recomputeListFromNodeAt:anIndex
     ]
 
 
+!
+
+recomputeListFromNodeAt:anIndex
+
+    model removeDependent:self.
+    model recomputeList.
+    model addDependent:self.
+
+    list := self listFromModel.
+    self redrawFromLine:anIndex.
+    self contentsChanged.
 ! !
 
 !SelectionInTreeView methodsFor:'private - drag and drop'!
@@ -1495,34 +1503,54 @@
 widthOfContents:firstLine and:lastLine
     "return the length (in pixels) of the longest line in a line-range
     "
-    |max      "{ Class: SmallInteger }"
-     thisLen  "{ Class: SmallInteger }"
-     first    "{ Class: SmallInteger }"
-     last     "{ Class: SmallInteger }" |
+    |parent name item tmpValue
+     max      "{ Class: SmallInteger }"
+     index    "{ Class: SmallInteger }"
+     runs     "{ Class: SmallInteger }"
+     level    "{ Class: SmallInteger }"
+     xOfStr   "{ Class: SmallInteger }"
+     dltX     "{ Class: SmallInteger }"
+     width    "{ Class: SmallInteger }"
+     newSz    "{ Class: SmallInteger }"
+     maxSz    "{ Class: SmallInteger }"|
 
-    listOfNodes size == 0 ifTrue:[^ 0].
-
-    last  := listOfNodes size.
-    first := firstLine.
-
-    (first > last) ifTrue:[^ 0].
+    (    (listOfNodes size == 0)
+     or:[(runs := lastLine min:(listOfNodes size)) < firstLine]
+    ) ifTrue:[
+        ^ 0
+    ].
 
-    (lastLine < last) ifTrue:[
-        last := lastLine
-    ].
-    max  := 0.
+    level  := 1.
+    xOfStr := self xOfStringLevel:level.
+    max    := xOfStr.
+    dltX   := imageInset + imageWidth.
+    width  := '1' widthOn:self.
+    parent := 4711.    "/ to force a computation
+    index  := firstLine.
+    runs   := runs - index + 1.
 
-    listOfNodes from:first to:last do:[:anItem||name|
-        thisLen := self xOfStringNode:anItem.
+    runs timesRepeat:[
+        item  := listOfNodes at:index.
+        name  := item name.
+        index := index + 1.
 
-        (name := anItem name) notNil ifTrue:[
-            thisLen := thisLen + (name widthOn:self).
-        ].
-        (thisLen > max) ifTrue:[
-            max := thisLen.
+        (newSz := name size) ~~ 0 ifTrue:[
+            item parent ~~ parent ifTrue:[
+                parent   := item parent.
+                tmpValue := item level.
+                xOfStr   := xOfStr + ((tmpValue - level) * dltX).
+                level    := tmpValue.
+                maxSz    := 0.
+            ].
+            maxSz < newSz ifTrue:[
+                maxSz := newSz.
+                max   := max max:(xOfStr + (maxSz * width)).
+            ]
         ]
     ].
-  ^ max + leftOffset.
+    ^ max + leftOffset.
+
+
 !
 
 withoutRedrawAt:anIndex put:aString
@@ -1537,12 +1565,7 @@
 
     (widthOfWidestLine := width) notNil ifTrue:[
         width := self widthOfContents:anIndex and:anIndex.
-
-        width > widthOfWidestLine ifTrue:[
-            widthOfWidestLine := width
-        ] ifFalse:[
-            widthOfWidestLine := nil "/ means: unknown
-        ]
+        widthOfWidestLine := widthOfWidestLine max:width.
     ].
 
 
@@ -1811,5 +1834,5 @@
 !SelectionInTreeView class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/Attic/SelTreeV.st,v 1.52 1998-04-03 08:41:09 ca Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/Attic/SelTreeV.st,v 1.53 1998-04-04 08:45:17 ca Exp $'
 ! !
--- a/SelectionInTreeView.st	Sat Apr 04 08:48:46 1998 +0200
+++ b/SelectionInTreeView.st	Sat Apr 04 10:45:57 1998 +0200
@@ -814,7 +814,7 @@
 buttonPress:button x:x y:y
     "check for indicator
     "
-    |expand node lineNr oldSize isExpandable|
+    |expand node lineNr isExpandable|
 
     lineNr := self indicatiorLineForButton:button atX:x y:y.
 
@@ -868,16 +868,7 @@
         self redrawLine:lineNr.
     ] ifFalse:[
      "/ with children; update list and redraw to end.
-        oldSize := list size.
-        model removeDependent:self.
-        model recomputeList.
-        model addDependent:self.
-        list := self listFromModel.
-
-        oldSize ~~ list size ifTrue:[
-            self redrawFromLine:lineNr.
-            self contentsChanged.
-        ]
+        self recomputeListFromNodeAt:lineNr
     ]
 
 
@@ -1252,19 +1243,30 @@
     "get list from model and return the new list.
      If listMessage is nil, try aspectMessage for backward compatibilty.
     "
-    |msg|
-
+    |msg list
+     runs "{ Class: SmallInteger }"
+     idx  "{ Class: SmallInteger }"
+    |
     widthOfWidestLine := nil.
 
     (msg := listMsg ? aspectMsg) notNil ifTrue:[
         listOfNodes := model perform:msg.
-        listOfNodes size ~~ 0 ifTrue:[
+
+        (runs := listOfNodes size) ~~ 0 ifTrue:[
             self refetchDeviceResources.
-            ^ listOfNodes collect:[:n|n name]
+            idx  := 1.
+            list := OrderedCollection new:runs.
+
+            runs timesRepeat:[
+                list add:(listOfNodes at:idx) name.
+                idx := idx + 1.
+            ].
+            ^ list
         ]
     ].
     listOfNodes := #().
   ^ #()
+
 !
 
 model:aModel
@@ -1359,7 +1361,7 @@
     "setup new list; keep selection dependent on the boolean state
      keepSelection
     "
-    |list|
+    |list time|
 
     list := aList.
 
@@ -1399,15 +1401,21 @@
         self redrawLine:anIndex.
     ] ifFalse:[
      "/ with children; update list and redraw to end.
-        model removeDependent:self.
-        model recomputeList.
-        model addDependent:self.
-        list := self listFromModel.
-        self redrawFromLine:anIndex.
-        self contentsChanged.
+        self recomputeListFromNodeAt:anIndex
     ]
 
 
+!
+
+recomputeListFromNodeAt:anIndex
+
+    model removeDependent:self.
+    model recomputeList.
+    model addDependent:self.
+
+    list := self listFromModel.
+    self redrawFromLine:anIndex.
+    self contentsChanged.
 ! !
 
 !SelectionInTreeView methodsFor:'private - drag and drop'!
@@ -1495,34 +1503,54 @@
 widthOfContents:firstLine and:lastLine
     "return the length (in pixels) of the longest line in a line-range
     "
-    |max      "{ Class: SmallInteger }"
-     thisLen  "{ Class: SmallInteger }"
-     first    "{ Class: SmallInteger }"
-     last     "{ Class: SmallInteger }" |
+    |parent name item tmpValue
+     max      "{ Class: SmallInteger }"
+     index    "{ Class: SmallInteger }"
+     runs     "{ Class: SmallInteger }"
+     level    "{ Class: SmallInteger }"
+     xOfStr   "{ Class: SmallInteger }"
+     dltX     "{ Class: SmallInteger }"
+     width    "{ Class: SmallInteger }"
+     newSz    "{ Class: SmallInteger }"
+     maxSz    "{ Class: SmallInteger }"|
 
-    listOfNodes size == 0 ifTrue:[^ 0].
-
-    last  := listOfNodes size.
-    first := firstLine.
-
-    (first > last) ifTrue:[^ 0].
+    (    (listOfNodes size == 0)
+     or:[(runs := lastLine min:(listOfNodes size)) < firstLine]
+    ) ifTrue:[
+        ^ 0
+    ].
 
-    (lastLine < last) ifTrue:[
-        last := lastLine
-    ].
-    max  := 0.
+    level  := 1.
+    xOfStr := self xOfStringLevel:level.
+    max    := xOfStr.
+    dltX   := imageInset + imageWidth.
+    width  := '1' widthOn:self.
+    parent := 4711.    "/ to force a computation
+    index  := firstLine.
+    runs   := runs - index + 1.
 
-    listOfNodes from:first to:last do:[:anItem||name|
-        thisLen := self xOfStringNode:anItem.
+    runs timesRepeat:[
+        item  := listOfNodes at:index.
+        name  := item name.
+        index := index + 1.
 
-        (name := anItem name) notNil ifTrue:[
-            thisLen := thisLen + (name widthOn:self).
-        ].
-        (thisLen > max) ifTrue:[
-            max := thisLen.
+        (newSz := name size) ~~ 0 ifTrue:[
+            item parent ~~ parent ifTrue:[
+                parent   := item parent.
+                tmpValue := item level.
+                xOfStr   := xOfStr + ((tmpValue - level) * dltX).
+                level    := tmpValue.
+                maxSz    := 0.
+            ].
+            maxSz < newSz ifTrue:[
+                maxSz := newSz.
+                max   := max max:(xOfStr + (maxSz * width)).
+            ]
         ]
     ].
-  ^ max + leftOffset.
+    ^ max + leftOffset.
+
+
 !
 
 withoutRedrawAt:anIndex put:aString
@@ -1537,12 +1565,7 @@
 
     (widthOfWidestLine := width) notNil ifTrue:[
         width := self widthOfContents:anIndex and:anIndex.
-
-        width > widthOfWidestLine ifTrue:[
-            widthOfWidestLine := width
-        ] ifFalse:[
-            widthOfWidestLine := nil "/ means: unknown
-        ]
+        widthOfWidestLine := widthOfWidestLine max:width.
     ].
 
 
@@ -1811,5 +1834,5 @@
 !SelectionInTreeView class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/SelectionInTreeView.st,v 1.52 1998-04-03 08:41:09 ca Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/SelectionInTreeView.st,v 1.53 1998-04-04 08:45:17 ca Exp $'
 ! !
--- a/TreeItem.st	Sat Apr 04 08:48:46 1998 +0200
+++ b/TreeItem.st	Sat Apr 04 10:45:57 1998 +0200
@@ -672,10 +672,20 @@
     "add all shown children and its shown sub children into
      a list
     "  
+    |item
+     size "{ Class: SmallInteger }"
+     idx  "{ Class: SmallInteger }"
+    |
+
     hide ifFalse:[
-        self childrenDo:[:aChild|  
-            aList add:aChild.
-            aChild recomputeList:aList
+        (size := self children size) ~~ 0 ifTrue:[
+            idx := 1.
+
+            size timesRepeat:[
+                aList add:(item := children at:idx).
+                item recomputeList:aList.
+                idx := idx + 1.
+            ]
         ]
     ]
 !
@@ -817,5 +827,5 @@
 !TreeItem class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libwidg2/TreeItem.st,v 1.18 1998-04-04 06:48:46 ca Exp $'
+    ^ '$Header: /cvs/stx/stx/libwidg2/TreeItem.st,v 1.19 1998-04-04 08:45:57 ca Exp $'
 ! !