התעוררתי ראיתי את האשכול והיה לי משעמם.שיפורים אפשריים
1.מיון המערכים של Folders/Files עם sort יעלה לך O(n log n).
במקרה הגרוע אם המערך ממויין כבר יעלה לך n^2.
2.בדיקה כמה עולה לך array_key_exists אני חושב שזה O(1) כי מקבלים אינדקס שהוא מצביע לKey של המערך.
במקסימום יעלה לך O של n.
זהו אין לי עוד רעיונות לשיפור, זה הקוד.
<?php function TreeList($root = null,$useThisFileRoot = false) { if ($useThisFileRoot) $root = dirname(__FILE__); $handle = opendir($root); if (!$handle) return false; while ($f = readdir($handle)) { if ($f != '.' && $f != '..'){ if (is_dir($f)) $array['folders'][] = $f; else $array['files'][] = $f; } } return $array; } function PrintTree(array $array) { if (array_key_exists("folders",$array)){ $folder_size = sizeof($array["folders"]); for ($i = 0; $i < $folder_size; ++$i) echo "<a href=\"index.php?f=".base64_encode($array["folders"][$i])."\">".$array["folders"][$i]."</a>"; } if (array_key_exists("files",$array)){ $files_size = sizeof($array["files"]); for ($i = 0; $i < $files_size; ++$i) echo "<div class=\"file\">".$array["files"][$i]."</div>"; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Tree Folders</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <style type="text/css"> a { color: black; font: normal bold 12px Arial} div.file{ font: normal normal 13px Arial} </style> </head> <body> <?php $getFolder = (isset($_GET['f']))?base64_decode($_GET['f']):null; $treeList = (isset($getFolder))?TreeList($getFolder,false):TreeList(null,true); PrintTree($treeList); ?> </body> </html>
|
עוד משהו קטן, קידדתי את שם התיקיות בקישור בbase64_encode זה הרבה יותר בטוח לדעתי מאשר להעביר את שם התיקיה עצמה בתור קישור.