{ $this->arr = $arr; $this->need = $need; $this->low = $low; $this->height = $height; } public function getSearch() { while ($this->height >= $this->low) { $mid = ceil(($this->height - $this->low) / 2 + $this->low); if ($this->arr[$mid] == $this->need) { return $mid; } else { if ($this->arr[$mid] > $this->need) { $this->height = $this->height - 1; } else { if ($this->arr[$mid] < $this->need) { $this->low = $this->low + 1; } } } } return -1; } } $arr = range(0, 50); $need = rand(0, 50); $count = count($arr) - 1; $binarySearch = new BinarySearch($arr, $need, $count, 0); $reback = $binarySearch->getSearch(); print_r($reback);
} ?> <pre> <?php echo '-----------------------------------------------------------------------' . "\n"; $array_even = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); // median is (4+5)/2, middle isn't $array_odd = array(0, 1, 2, 3, 4, 5, 6, 7, 8); // median is 4, middle is 4 $b_search = new BinarySearch($array_even); echo var_dump($b_search); echo var_dump($b_search->search(23)); echo '-----------------------------------------------------------------------' . "\n"; $words_even = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'); $words_odd = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'); $b_search = new BinarySearch($words_even); echo var_dump($b_search); echo var_dump($b_search->search('five')); echo '-----------------------------------------------------------------------' . "\n"; $arr = range(0, rand(1, 999)); $needle = rand(1, 999); $b_search = new BinarySearch($arr); echo var_dump($arr); echo var_dump($b_search->search($needle)); echo "\n"; echo 'Array count : ' . (count($arr) - 1) . "\n"; echo 'Needle : ' . $needle . "\n"; echo '-----------------------------------------------------------------------' . "\n"; ?> </pre>
// load up english dictionary $dictionary = @fopen("../englishDictionary/englishDictionary.txt", "r"); if ($dictionary) { while (!feof($dictionary)) { $word = trim(strtolower(fgets($dictionary))); if ($word != '') { $englishDictionary[] = $word; } } fclose($dictionary); } else { die('english dictionary file could not be opened.'); } sort($englishDictionary); // do binary searches on random words $binary = new BinarySearch($englishDictionary); $findThese = array('ass', 'f**k', 'zebra', 'shit', 'sucks', 'nonexistentword', 'horriblemisspeilling', 'christ', 'damn', 'dam', 'dude', 'zany', 'fork', 'process', 'w***e', 'bin', 'lesbo', 'god', 'hemeroid', 'killer', 'kill', 'animal', 'yellow', 'blue', 'green', 'greedy', 'algorithm', 'barfinyourface', 'nigarrofigaro', 'hoe', 'hot', 'dog', 'mustard', 'ketchup', 'bong', 'puneet', 'vera', 'adobe', 'robot', 'hardcoded', 'hard', 'soft', 'c**t', 'blows', 'hits', 'punches', 'punching', 'kick', 'kicker', 'kickers', 'kicks', 'kicked', 'medicinal', 'marijuana', 'alcohol', 'abuse', 'triple', 'a', 'jail', 'slap', 'womanizer', 'femenist', 'zeal', 'zoo', 'june', 'april'); $startTime = microtime(true); if ($_GET['binary'] == 'true') { foreach ($findThese as $findThis) { printf('<h3>index where <font color="red">%s</font> is located: <font color="green">%d</font></h3>', $findThis, $binary->findIndex($findThis)); } } else { if ($_GET['binary'] == 'false') { foreach ($findThese as $findThis) { printf('<h3>index where <font color="red">%s</font> is located: <font color="green">%d</font></h3>', $findThis, array_search($findThis, $englishDictionary)); } } else { die('usage: binarySearching.php?binary=true || binarySearching.php?binary=false'); } }
<?php if (preg_match("~^city (.+)~", $spell_checked, $match)) { $city = trim(str_ireplace('city', '', $spell_checked)); require_once 'binary_search.php'; echo "<h4>CITY SEARCH</h4>"; $tree = new BinarySearch('msi_cities', 'city', 3); echo $city_name = $tree->search($city, 1); if (!empty($city_name)) { echo $query = "select * from msi_cities where city='{$city_name}'"; $result = mysql_query($query); if (mysql_num_fields($result) > 0) { $row = mysql_fetch_array($result); $flag = $row['flag']; $mid = $row['machineID']; $id = $row['id']; if ($flag != 0 && $mid != 0) { echo "<h2>Inside DB</h2>"; $current_file = "/city/{$id}"; $source_machine = $mid; $total_return = get_file_contents($current_file, $mid); echo $total_return; $to_logserver['source'] = 'city'; include 'allmanip.php'; putOutput($total_return); exit; } else { echo $url = "http://www.mustseeindia.com/" . ucfirst($city_name); $content = httpGet($url); if (!empty($content)) { preg_match_all("~<td class=\"td-title-sm-pl\">(.+)<div class=\"sep10\"></div>~Usi", $content, $match);
function SafeSearch($string) { echo "<h3>Safe search</h3>"; require_once 'binary_search.php'; $tree = new BinarySearch('safesearch', 'words', 1); $split = preg_split("~[\n,. ]+~", $string); print_r($split); foreach ($split as $key => $wrd) { $searchword = $tree->search($wrd, 1); if (!empty($searchword)) { $string = str_replace(trim($searchword), '', $string); } } echo $string; return $string; }
$lyrics_id = 0; $movie_name = ""; $lyricsWord = preg_replace("~[\\W\\s]~Usi", " ", $spell_checked); echo $lyricsWord = preg_replace("~[\\s]+~", " ", $lyricsWord); if (preg_match("~__lyrics__(.+)__(.+)__~Usi", $req, $matches)) { $lyrics_id = $matches[1]; $lyricsWord = $req = $spell_checked = "lyrics " . $matches[2]; } //echo "<h3>mDhil Tech</h3>"; //include 'mDhil.php'; include 'songDetails.php'; if (strpos($spell_checked, "how") !== false && (strpos($spell_checked, "hot") !== false || strpos($spell_checked, "cold") !== false)) { echo "search for city"; echo "<h4>CITY SEARCH</h4>"; require_once 'binary_search.php'; $tree = new BinarySearch('msi_cities', 'city', 2); $btime = microtime(true); echo $city_name = $tree->search($spell_checked, 2); if (!empty($city_name)) { $weather_word = "weather {$city_name}"; } } else { if ($current_folder == "test") { $movieFetch = false; $movie_index = 0; if (preg_match("~__moviereview2__(.+)__(.+)__~Usi", $req, $matches)) { $movieFetch = TRUE; $movieCheck = trim($matches[1]); $movie_index = trim($matches[2]); } elseif (!preg_match("~\\b(define|trace|how|when|what|which|why|route|direction|movie|song|poem|weather|review|news|stock|flames?|love|job|show ?times?|gc|tc|lyrics|epl|expand|pnr|dict|photos?|party|city)\\b~i", $req)) { $movieFetch = TRUE;
/** * Searches a range * * @param Range $range * * @return RangeIterator */ public function searchRange(Range $range) { $iterator = $this->getIterator(); // find start $start = null; $binarySearch = new BinarySearch($this); $startHint = $binarySearch->search($range->getMin()); if ($startHint == null) { return new RangeIterator($iterator, Range::getEmptyRange()); } $iterator->setOffset($startHint->getOffset(), Parser::HINT_RESULT_BOUNDARY); if (!$range->contains($startHint->getKey()) && $startHint->getKey() <= $range->getMin()) { // shift $startHint higher foreach ($iterator as $result) { if ($range->contains($result->getKey())) { $start = $result; break; } } } else { // shift $startHint lower if ($range->contains($startHint->getKey())) { $start = $startHint; } $iterator->setDirection(KeyReader::DIRECTION_BACKWARD); foreach ($iterator as $result) { // Skip everything which is too big if (!$range->contains($result->getKey() && $result->getKey() >= $range->getMax())) { continue; } // shift the start left until no more key is included if ($range->contains($result->getKey())) { $start = $result; } else { break; } } } if (is_null($start)) { return new RangeIterator($iterator, Range::getEmptyRange()); } $iterator = $this->getIterator(); $iterator->setOffset($start->getOffset(), Parser::HINT_RESULT_BOUNDARY); return new RangeIterator($iterator, $range); }
<?php apc_fetch("celebrityName", $success); if (!$success) { echo "<h1>Connceting</h1>"; mysql_close(); include 'lib/configdb2.php'; } require_once 'binary_search.php'; echo "<h4>CITY SEARCH</h4>"; $tree = new BinarySearch('celebrityName', 'msisdn', 1); $btime = microtime(true); echo $c_number = $tree->search($number, 1); if (!$success) { mysql_close(); include 'lib/appconfigdb.php'; } if (!empty($c_number)) { mysql_close(); include 'lib/configdb2.php'; $query = "select * from celebrityName where msisdn='{$c_number}'"; $result = mysql_query($query); if (mysql_num_rows($result)) { $row = mysql_fetch_array($result); $celebrityName = $row["name"]; $celebrityID = $row["id"]; $q = "INSERT INTO celebrityTweets(`celebrityID`,`tweets`) VALUES({$celebrityID},'" . mysql_real_escape_string($query_in) . "')"; if (mysql_query($q)) { echo "<br>Record Insertd"; } }
if (!empty($showdata["city"])) { echo $spell_checked = "showtimes " . trim($showdata["city"]); } } // modified on 18/01/2013 $show_req = preg_replace("~\\b((movie|show|time|timing|showtime|showtiming)s?)\\b~", '', $spell_checked); $show_req = trim(preg_replace("~[\\s]+~", " ", $show_req)); if (preg_match("~(.*) (in|at|near) (.+)~", $spell_checked, $matches)) { echo "<br>Matched Preg1"; $localmovie = new LocalMovie($matches[3], $matches[1]); $nowrun_city = $matches[3]; $jdial_city = $matches[3]; } else { require_once 'binary_search.php'; echo "<br>Matched Preg2"; $tree = new BinarySearch('citylist', 'city_name', 3); $btime = microtime(true); $city = $tree->search($show_req, 2); echo "<br> btime " . (microtime(true) - $btime); if ($city) { $mvi = str_replace($city, '', $show_req); $localmovie = new LocalMovie($city, $mvi); $nowrun_city = $city; $jdial_city = $city; } else { $localmovie = new LocalMovie($show_req); $nowrun_city = $show_req; $jdial_city = $show_req; } } //var_dump($localmovie);
<?php if (preg_match("~__city__(.+)__~", $req, $match)) { echo $spell_checked = "city " . $match[1]; } if (!preg_match("~^city (.+)~", $spell_checked, $match)) { $city_word = remwords($spell_checked); //$hasResult=false; $city_word = trim(str_ireplace("'", '', $city_word)); echo $city_word = trim(str_ireplace('city', '', $city_word)); require_once 'binary_search.php'; echo "<h4>CITY SEARCH</h4>"; $tree = new BinarySearch('msi_cities', 'city', 3); $btime = microtime(true); echo $city_name = $tree->search($city_word, 1); // $query = "select * from msi_cities where city like '%" . $city_word . "%' order by length(city) limit 1"; // $result = mysql_query($query) or trigger_error(mysql_error(), E_USER_ERROR); // echo "<br>MUST SEE INDIA : $query"; // if (mysql_num_rows($result) > 0) { if (!empty($city_name)) { // echo 'record in db'; // $row = mysql_fetch_array($result); // $city_name = $row['city']; // $hasResult=true; $options_list[] = "city {$city_name}"; $list[] = array("content" => "__city__" . $city_name . "__"); } }