ptachoire/file-binary-search

This package is abandoned and no longer maintained. No replacement package was suggested.

Binary Search in a file.

dev-master 2012-10-01 13:18 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:21:34 UTC


README

Binary search into sorted formatted file.

Build Status

Usage

Usage sample :

require_once( 'src/autoload.php');
$file = tempnam("/tmp", "sample_file_binary_search_");
file_put_contents($file, implode("\n", range(10,20,2)));

$fbs = new \FileBinarySearch\FileBinarySearch($file);

var_dump( $fbs->search(10));
/*
string(2) "10"
*/

var_dump( $fbs->search(14));
/*
string(2) "14"
*/

var_dump( $fbs->search(15));
/*
bool(false)
 */

var_dump( $fbs->search(25));
/*
bool(false)
*/

If you have formated lines, you can use your own comparaison method

function mycompare( $tested_line, $searched_value ) {
    //returns < 0 if $tested_line < $searched_value
    //returns > 0 if $tested_line > $searched_value
    //returns 0 if $tested_line == $searched_value
}

$fbs = new \FileBinarySearch\FileBinarySearch($file, 'mycompare');

Unit Tests

phpunit

Thanks

Files structure inspired by Geocoder from William Durand