[dev] New folder sort algorithm

Jan Schneider jan@horde.org
Fri, 21 Dec 2001 01:19:32 +0100


Zitat von Jan Schneider <jan@horde.org>:

> hand. Please try the patch with other servers before I commit it.

I missed some $offset references that aren't used anymore but produced 
warnings. Here a cleaned patch:

Index: Tree.php
===================================================================
RCS file: /repository/imp/lib/Tree.php,v
retrieving revision 1.46
diff -u -r1.46 Tree.php
--- Tree.php	24 Oct 2001 19:17:03 -0000	1.46
+++ Tree.php	20 Dec 2001 23:05:31 -0000
@@ -26,7 +26,6 @@
     var $delimiter = '/';               // hierarchy delimiter
     var $listcmd = 'imap_getmailboxes'; // default mailbox listing command
     var $prefix = '';                   // where we start listing folders
-    var $offset;
     
     function IMP_Tree()
     {
@@ -295,10 +294,8 @@
     
     /* Hierarchy sorting functions */
     
-    function hsort($list, $off = 1)
+    function hsort($list)
     {
-        $this->offset = $off;
-        reset($list);
         usort($list, array($this, 'tree_cmp'));
         return $list;
     }
@@ -315,21 +312,16 @@
             return 1;
         }
         
-        if (strstr($a->name, $this->delimiter)) {
-            $tmp = explode($this->delimiter, $aname);
-            $aa = $tmp[count($tmp) - $this->offset];
-        } else {
-            $aa = $aname;
-        }
+        $aa = explode($this->delimiter, $aname);
+        $bb = explode($this->delimiter, $bname);
         
-        if (strstr($b->name, $this->delimiter)) {
-            $tmp = explode($this->delimiter, $bname);
-            $bb = $tmp[count($tmp) - $this->offset];
-        } else {
-            $bb = $bname;
+        for ($i = 0; $i < min(count($aa), count($bb)); $i++) {
+            if ($aa[$i] != $bb[$i]) {
+                return strcmp(strtolower($aa[$i]), strtolower($bb[$i]));
+            }
         }
         
-        return strcmp(strToLower($aa), strToLower($bb));
+        return count($aa) - count($bb);
     }
     
     
@@ -344,7 +336,6 @@
         $pickle['delimiter'] = $this->delimiter;
         $pickle['listcmd'] = $this->listcmd;
         $pickle['prefix'] = $this->prefix;
-        $pickle['offset'] = $this->offset;
         $pickle['server'] = $this->server;
         
         return $pickle;
@@ -359,7 +350,6 @@
         $this->delimiter = $pickle['delimiter'];
         $this->listcmd = $pickle['listcmd'];
         $this->prefix = $pickle['prefix'];
-        $this->offset = $pickle['offset'];
         $this->server = $pickle['server'];
     }
     
@@ -541,9 +531,6 @@
     
     function insertInto($elt, $id)
     {
-        // find the element that will be before $elt
-        $this->offset = 1;
-        
         $tmp = explode($this->delimiter, $elt['value']);
         if (count($tmp) == 1) {
             $pkey = $this->first;
@@ -636,8 +623,7 @@
         }
         
         // sort the list
-        $list = $this->hsort($list, 1);
-        $list = $this->hsort($list, 2);
+        $list = $this->hsort($list);
         
         // toss them all in. stir well.
         $lastpid = '';


Jan.

::::::::::::::::::::::::::::::::::::::::
AMMMa AG - discover your knowledge
:::::::::::::::::::::::::::
Detmolder Str. 25-33 :: D-33604 Bielefeld
fon +49.521.96878-0 :: fax  +49.521.96878-20
http://www.ammma.de
::::::::::::::::::::::::::::::::::::::::::::::