daily pastebin goal
25%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3.  * Name: Friend Analyzer Class
  4.  * Author: Caleb Mingle
  5.  * Date: 4/14/2011 @ 10:31 PM.
  6.  */
  7. class Analyzer {
  8.     private $_data;
  9.     private $_steps;
  10.     private $_friends;
  11.     private $_path;
  12.     private $_startFriend;
  13.     private $_endFriend;
  14.     private $_delimeter;
  15.     private $_parents;
  16.     private $_checked;
  17.     private $_tmpPath;
  18.     private $_verbose;
  19.  
  20.     function __construct($file, $delimeter = '    ', $verbose = false) {
  21.         $this->_friends   = array();
  22.         $this->_data      = @file_get_contents($file);
  23.         $this->_delimeter = $delimeter;
  24.         $this->_verbose   = $verbose;
  25.         $this->__processFriends();
  26.         $this->_steps     = 2;
  27.         $this->_parents   = array();
  28.     }
  29.  
  30.     /*
  31.      * This will find the path.  Will check to see if startFriend is a direct friend to endFriend,
  32.      * then process from there.
  33.      */
  34.     public function findPath($startFriend, $endFriend) {
  35.         $this->_startFriend = $startFriend;
  36.         $this->_endFriend   = $endFriend;
  37.  
  38.         if(in_array($endFriend, $this->_friends[$startFriend])) {
  39.             $this->_path[] = $startFriend;
  40.             $this->_path[] = $endFriend;
  41.             return;
  42.         }
  43.  
  44.         $check = $this->__match($startFriend);
  45.  
  46.         if(!$check === false) {
  47.             $key = array_keys($check);
  48.             $key = $this->_friends[$this->_startFriend][$key[0]];
  49.             $this->_steps++;
  50.  
  51.             $this->_path[] = $this->_startFriend;
  52.             $this->_path[] = $key;
  53.             $this->_path[] = $this->_endFriend;
  54.             return;
  55.         }
  56.         $this->_path[] = $this->_startFriend;
  57.  
  58.         $this->__processInitialFriends();
  59.  
  60.         $this->_path[] = $this->_endFriend;
  61.     }
  62.  
  63.     /*
  64.      * Since we know that someone HAS to be friends with our startFriend, we need to start here.
  65.      * We start by procesing all of the friends of startFriend, and their friends.
  66.      */
  67.     private function __processInitialFriends() {
  68.         $start_friends = $this->_friends[$this->_startFriend];
  69.  
  70.         if($this->_verbose) {
  71.             echo "<pre>";
  72.             print_r($this->_friends);
  73.         }
  74.  
  75.         $this->log("__processInitialFriends()", "Processing friends for {$this->_startFriend}.");
  76.  
  77.         $this->_checked[] = $this->_startFriend;
  78.  
  79.         foreach($start_friends as $subfriend) {
  80.  
  81.             if(!$this->__processFriend($subfriend)) {
  82.                 $this->log("__processInitialFriends()", "Failed for {$subfriend}.");
  83.                 $this->_path  = array($this->_startFriend);
  84.                 $this->_steps = 0;
  85.             } else {
  86.                 break;
  87.             }
  88.         }
  89.  
  90.         $this->__optimizePath();
  91.     }
  92.  
  93.     /*
  94.      * Makes sure there are NO shorter paths to the target before reporting back.
  95.      */
  96.     private function __optimizePath() {
  97.         $temp_path = array();
  98.  
  99.         $index      = 1;
  100.         $stop       = count($this->_path) - 1;
  101.         $is_counter = 0;
  102.         $last_added = 0;
  103.  
  104.         foreach($this->_path as $friend) {
  105.            
  106.             for($i = $index; $i <= $stop; $i++) {
  107.                 $is_counter++;
  108.                 if(in_array($friend, $this->_friends[$this->_path[$i]])) {
  109.                     if(!in_array($this->_path[$i], $temp_path)) {
  110.                         $temp_path[$friend] = $this->_path[$i];
  111.                     }
  112.                 }
  113.             }
  114.  
  115.             $is_counter = 0;
  116.             $index++;
  117.         }
  118.  
  119.         $this->_path   = array();
  120.         $this->_path[] = $this->_startFriend;
  121.         $this->_path   = array_merge($this->_path, $temp_path);
  122.        
  123.     }
  124.  
  125.     /*
  126.      * Process individual friend, report back.
  127.      */
  128.     private function __processFriend($friend) {
  129.         $match = $this->__match($friend);
  130.  
  131.         $this->_checked[] = $friend;
  132.  
  133.         if(!($match === false)) {
  134.             $this->log("__processFriend({$friend})", "Match found on {$friend}.");
  135.  
  136.  
  137.  
  138.             $key = array_keys($match);
  139.             $key = $key[0];
  140.             $key = $this->_friends[$friend][$key];
  141.             $this->_parents[$key] = $friend;
  142.  
  143.             $this->__processParents($key);
  144.             $this->_tmpPath = array_reverse($this->_tmpPath);
  145.             $this->_path    = array_merge($this->_path, $this->_tmpPath);
  146.  
  147.             $this->_steps++;
  148.             $this->_path[] = $key;
  149.  
  150.             return true;
  151.         }
  152.  
  153.         $this->log("__processFriend($friend)", "No match found for {$friend}.");
  154.  
  155.         $friends = $this->_friends[$friend]; // friends of Susie.
  156.  
  157.         foreach($friends as $subfriend) {
  158.             $this->log("__processFriend($friend)", "Processing subfriend: {$subfriend}.");
  159.  
  160.             if(in_array($subfriend, $this->_checked)) {
  161.                 $this->log("__processFriend($friend)", "Already checked: {$subfriend}");
  162.                 continue;
  163.             }
  164.  
  165.             $this->_checked[] = $subfriend;
  166.             $this->_parents[$subfriend] = $friend;
  167.  
  168.             if($this->__processFriend($subfriend)) {
  169.                 return true;
  170.             } else {
  171.                 $this->log("__processFriend({$subfriend})", "Failed for {$subfriend}.");
  172.             }
  173.         }
  174.  
  175.     }
  176.  
  177.     /*
  178.      * Will process the hierarchy of people above $friend.  Recursive.
  179.      */
  180.     private function __processParents($friend) {
  181.         if(!key_exists($friend, $this->_parents)) {
  182.             return;
  183.         } else {
  184.             $this->_steps++;
  185.             $this->_tmpPath[] = $this->_parents[$friend];
  186.             $this->__processParents($this->_parents[$friend]);
  187.         }
  188.     }
  189.  
  190.     /*
  191.      * This will determine if there are matches (intersection) in the current friends AND the target friends.
  192.      * Will return either false OR the compare array.
  193.      */
  194.     private function __match($current_index) {
  195.         $compare = array_intersect($this->_friends[$current_index], $this->_friends[$this->_endFriend]);
  196.         return (!count($compare)) ? false : $compare;
  197.     }
  198.  
  199.     /*
  200.      * Process friends function.
  201.      * This will split the file into an array based on new lines, then create our relationships.
  202.      */
  203.     private function __processFriends() {
  204.         $lines = explode("\n", $this->_data);
  205.  
  206.         foreach($lines as $line) {
  207.             if(!empty($line)) {
  208.                 $friend_data = explode($this->_delimeter, $line);
  209.                 $left  = trim($friend_data[0]);
  210.                 $right = trim($friend_data[1]);
  211.  
  212.                 $this->_friends[$left][] = $right; // adds the right side as a new friend of left.
  213.                 $this->_friends[$right][] = $left; // adds the left side as a new friend of right.
  214.             }
  215.         }
  216.     }
  217.  
  218.     /*
  219.      * Will return the path array for processing.
  220.      */
  221.     public function getPath() {
  222.         return $this->_path;
  223.     }
  224.  
  225.     /*
  226.      * Will return the steps taken.
  227.      */
  228.     public function getSteps() {
  229.         return $this->_steps;
  230.     }
  231.  
  232.     /*
  233.      * Used in verbose mode to output information.
  234.      */
  235.     private function log($function, $message) {
  236.         if($this->_verbose) {
  237.             echo ">>> {$function}: {$message}<br />";
  238.         }
  239.     }
  240. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top