|
| 1 | +# The Search Algorithms Implemantation in PHP 8 |
| 2 | + |
| 3 | +The search algorithm implementation in PHP 8 to find the value with the query as fast. |
| 4 | + |
| 5 | +# Usage |
| 6 | + |
| 7 | +## Binary Search |
| 8 | + |
| 9 | +### Array |
| 10 | + |
| 11 | +```php |
| 12 | +$array = []; |
| 13 | + |
| 14 | +for ($index = 0; $index < 100 * 10000; $index++) { |
| 15 | + $array[] = $index + 1; |
| 16 | +} |
| 17 | + |
| 18 | +$search = new BinarySearch(); |
| 19 | + |
| 20 | +$search ->setCompareCallback(fn ($current, $searchValue) => $current === $searchValue) |
| 21 | + ->setDirectionCallback(fn ($current, $searchValue) => $current < $searchValue) |
| 22 | + ->setArray($array) |
| 23 | + ->setSearchValue(98589) |
| 24 | + ->search(); |
| 25 | + |
| 26 | +$foundIndex = $search->getFoundIndex(); |
| 27 | +$foundValue = $search->getFoundValue(); |
| 28 | +``` |
| 29 | + |
| 30 | +### Arrays in array |
| 31 | + |
| 32 | +```php |
| 33 | +$array = []; |
| 34 | + |
| 35 | +for ($index = 0; $index < 100 * 10000; $index++) { |
| 36 | + $array[] = [ |
| 37 | + "id" => $index + 1, |
| 38 | + "first" => "Baris {$index}", |
| 39 | + "last" => "Manco {$index}", |
| 40 | + ]; |
| 41 | +} |
| 42 | + |
| 43 | +$search = new BinarySearch(); |
| 44 | + |
| 45 | +$search->setCompareCallback(fn ($current, $searchValue) => $current["id"] === $searchValue) |
| 46 | + ->setDirectionCallback(fn ($current, $searchValue) => $current["id"] < $searchValue) |
| 47 | + ->setType("arrays in array") |
| 48 | + ->setArray($array) |
| 49 | + ->setSearchValue(81300) |
| 50 | + ->search(); |
| 51 | + |
| 52 | +$foundIndex = $search->getFoundIndex(); |
| 53 | +$foundValue = $search->getFoundValue(); |
| 54 | +``` |
| 55 | + |
| 56 | +### Objects in array |
| 57 | + |
| 58 | +```php |
| 59 | + |
| 60 | +$array = []; |
| 61 | + |
| 62 | +for ($index = 0; $index < 100 * 10000; $index++) { |
| 63 | + $array[] = (object) [ |
| 64 | + "id" => $index + 1, |
| 65 | + "first" => "Baris {$index}", |
| 66 | + "last" => "Manco {$index}", |
| 67 | + ]; |
| 68 | +} |
| 69 | + |
| 70 | +$search = new BinarySearch(); |
| 71 | +$search |
| 72 | + ->setCompareCallback(fn ($current, $searchValue) => $current->id === $searchValue) |
| 73 | + ->setDirectionCallback(fn ($current, $searchValue) => $current->id < $searchValue) |
| 74 | + ->setType("objects in array") |
| 75 | + ->setArray($array) |
| 76 | + ->setSearchValue(81300) |
| 77 | + ->search(); |
| 78 | + |
| 79 | +$foundIndex = $search->getFoundIndex(); |
| 80 | +$foundValue = $search->getFoundValue(); |
| 81 | +``` |
| 82 | + |
| 83 | +## Tests |
| 84 | + |
| 85 | +```bash |
| 86 | +composer test |
| 87 | +``` |
| 88 | + |
| 89 | +## Contributing |
| 90 | + |
| 91 | +If you want to contribute to the development of this library, you can open an issue or submit a pull request. |
| 92 | + |
| 93 | +# License |
| 94 | + |
| 95 | +Licensed under the GPL3. See <a href="https://github.com/baygin/php-search-algorithms/blob/master/LICENSE"> LICENSE </a> for more information. |
0 commit comments