Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pre/Post Condition Discussion 03/13/2013. Quicksort int main(int argc, char* argv[]) { int numbers[SIZE] = {2,5,3…} struct Sorter s; // initialize generic.

Similar presentations


Presentation on theme: "Pre/Post Condition Discussion 03/13/2013. Quicksort int main(int argc, char* argv[]) { int numbers[SIZE] = {2,5,3…} struct Sorter s; // initialize generic."— Presentation transcript:

1 Pre/Post Condition Discussion 03/13/2013

2 Quicksort int main(int argc, char* argv[]) { int numbers[SIZE] = {2,5,3…} struct Sorter s; // initialize generic data producer setup_data(&s, (int*)numbers); int* (*qs)(int*, int, int) = &quicksort; s.sort_fn = qs; call_sort(&s); }

3 Quicksort int main(int argc, char* argv[]) { int numbers[SIZE] = {2,5,3…} struct Sorter s; // initialize generic data producer setup_data(&s, (int*)numbers); int* (*qs)(int*, int, int) = &quicksort; s.sort_fn = qs; call_sort(&s); } Functions we care about providing pre/postconditions for as they are a part of the composition strategy

4 Quicksort numbers[SIZE] = {2,3,5…} // Generic sorting class void setup_data(struct Sorter* sorter_, int* input) { int i; // store initial dataset internally for (i = 0; i < SIZE; ++i) sorter_->numbers_[i] = input[i]; }

5 Quicksort numbers[SIZE] = {2,3,5…} // Generic sorting class void setup_data(struct Sorter* sorter_, int* input) { int i; // store initial dataset internally for (i = 0; i < SIZE; ++i) sorter_->numbers_[i] = input[i]; } // precondition: sorter_ != NULL // precondition: input != NULL // postcondition: for all i, sorter_->numbers[i] == numbers[i]

6 Quicksort // quicksort recursive function int* quicksort(int* input, int p, int r) { int *ptr; ptr = input; if (p < r) { int j = partition(input, p, r); quicksort(input, p, j-1); quicksort(input, j+1, r); } return ptr; }

7 Quicksort // quicksort recursive function int* quicksort(int* input, int p, int r) { int *ptr; ptr = input; if (p < r) { int j = partition(input, p, r); quicksort(input, p, j-1); quicksort(input, j+1, r); } return ptr; } // precondition: input != NULL // precondition: p != r // postcondition: input[0] <=... <= input[SIZE-1]

8 Quicksort void call_sort(struct Sorter* sorter_) { if (sorter_->sort_fn != NULL) { sorter_->sort_fn(sorter_->numbers_, 0, SIZE-1); }

9 Quicksort void call_sort(struct Sorter* sorter_) { if (sorter_->sort_fn != NULL) { sorter_->sort_fn(sorter_->numbers_, 0, SIZE-1); } // precondition: sorter_ != NULL // precondition: sorter_->sort_fn != NULL // postcondition: sorter_->numbers_ numbers_[SIZE-1]

10 Mergesort (with strings) void mergesort_wrapper(char* input[]); int main(int argc, char* argv[]) { … // BEGIN COMPOSITION char* letters[SIZE] = {"d", "c", "a", "f", "b", "e", "g", "i", "k", "o"}; // call the wrapper function mergesort_wrapper(letters); // END COMPOSITION … } void mergesort_wrapper(char* input[]) { // convert cstring array into int array // call merge sort with bounds // return int array }

11 Mergesort (with strings) void mergesort_wrapper(char* input[]); int main(int argc, char* argv[]) { … // BEGIN COMPOSITION char* letters[SIZE] = {"d", "c", "a", "f", "b", "e", "g", "i", "k", "o"}; // call the wrapper function mergesort_wrapper(letters); // END COMPOSITION … } void mergesort_wrapper(char* input[]) { // convert cstring array into int array // call merge sort with bounds // return int array } // precondition-desc input must be converted to/from integer ??? // precondition-desc: input = array of strings // precondition: input != NULL // postcondition-desc: input = sorted array of strings // postcondition: input[0] <=... <= input[SIZE-1]

12 Mergesort (with strings) void mergesort_wrapper(char* input[]); int main(int argc, char* argv[]) { … // BEGIN COMPOSITION char* letters[SIZE] = {"d", "c", "a", "f", "b", "e", "g", "i", "k", "o"}; // call the wrapper function mergesort_wrapper(letters); // END COMPOSITION … } void mergesort_wrapper(char* input[]) { // convert cstring array into int array // call merge sort with bounds // return int array } // precondition-desc input must be converted to/from integer ??? // precondition-desc: input = array of strings // precondition: input != NULL // postcondition-desc: input = sorted array of strings // postcondition: input[0] <=... <= input[SIZE-1] Necessary information for CFG?

13 Mergesort (with strings) void mergesort_wrapper(char* input[]); int main(int argc, char* argv[]) { … // BEGIN COMPOSITION char* letters[SIZE] = {"d", "c", "a", "f", "b", "e", "g", "i", "k", "o"}; // call the wrapper function mergesort_wrapper(letters); // END COMPOSITION … } void mergesort_wrapper(char* input[]) { // convert cstring array into int array // call merge sort with bounds // return int array } How do we formally specify where to insert composed code? How do we formally specify necessary behavioral information? e.g. input must be converted to int array before being passed to merge sort Precondition? Method signature?


Download ppt "Pre/Post Condition Discussion 03/13/2013. Quicksort int main(int argc, char* argv[]) { int numbers[SIZE] = {2,5,3…} struct Sorter s; // initialize generic."

Similar presentations


Ads by Google