diff options
Diffstat (limited to 'documentation')
-rw-r--r-- | documentation/bubblesort.html | 86 |
1 files changed, 79 insertions, 7 deletions
diff --git a/documentation/bubblesort.html b/documentation/bubblesort.html index 8d66a84..126ab21 100644 --- a/documentation/bubblesort.html +++ b/documentation/bubblesort.html @@ -10,11 +10,11 @@ and in C++ - and see how much faster the C++ code is. </p> <p> -<pre class="language-php"> +<pre class="language-php"><code> <?php /** - * Bubblesort function in parameter + * Bubblesort function in PHP * * This function takes an unsorted array as input, sorts it, and returns * the output. It only uses normal PHP operation, and does not rely on @@ -23,7 +23,7 @@ * @param array An unsorted array of integers * @return array A sorted array */ -function bubblesort(array $input) +function scripted_bubblesort(array $input) { // number of elements in the array $count = count($input); @@ -32,16 +32,88 @@ function bubblesort(array $input) for ($i = 0; $i < $count; $i++) { // loop through the elements that were already processed - for ($j = 0; $j < $count - $i; $j++) + for ($j = 1; $j < $count - $i; $j++) { // move on if smaller - if ($input[$j] <= $input[$j+1]) continue; + if ($input[$j-1] <= $input[$j]) continue; // swap elements $temp = $input[$j]; - $input[$j] = $input[$j+1]; - $input[$j+1] = $temp; + $input[$j] = $input[$j-1]; + $input[$j-1] = $temp; } } } ?> +</code></pre> +</p> +<p> + And now exactly the same algorithm, but not as a PHP script, but a native + C++ extension. +</p> +<p> +<pre class="language-c++"><code> +#include <phpcpp.h> + +/** + * Bubblesort function in C++ + * + * This function takes an unsorted array as input, sorts it, and returns + * the output. Notice that we have not really done our best to make the + * implementation of this function as efficient as possible - we use stl + * containers for example - it is simple looking plain C++ function with + * a lot of room for improvements. + * + * @param array An unsorted array of integers + * @return array A sorted array + */ +Php::Value native_bubblesort(Php::Parameters ¶ms) +{ + // there is one input array, cast the PHP variable to a vector of ints + std::vector<int> input = params[0]; + + // loop through the array + for (size_t i = 0; i < input.size(); i++) + { + // loop through the elements that were already processed + for (size_t j = 1; j < input.size() - i; j++) + { + // move on if smaller + if (input[j-1] <= input[j]) continue; + + // swap elements + temp = input[j]; + input[j] = input[j-1]; + input[j-1] = temp; + } + } +} + +/** + * Switch to C context, because the Zend-engine calls the get_module() method + * in C context, and not in C++ context + */ +extern "C" { + + /** + * When a PHP extension starts up, the Zend engine calls the get_module() + * function to find out which functions and classes are offered by the + * extension + * + * @return void* pointer to memory address holding the extension information + */ + PHPCPP_EXPORT void *get_module() + { + // create an instance of the Php::Extension class + static Php::Extension extension("bubblesort", "1.0"); + + // add the bubblesort function to the extension + extension.add("native_bubblesort", native_bubblesort); + + // return the extension + return extension; + } +} + +</code></pre> +</p> |