Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int *int_sorted::insert(int value){
- // int_sorted tmp(&value, 1);
- // construct a int_sorted with requested value, size 1 then merge these two
- *this = merge(int_sorted(&value, 1));
- for (int *i = sint_buffer.begin(); i != sint_buffer.end(); i++)
- {
- if (*i == value)
- return i; // Returns a const int pointer.. function wants a normal int pointer
- }
- return 0;
- }
- bool int_sorted::check_sorted(int_buffer sint_buff){
- const int *i = sint_buff.begin();
- const int *j = sint_buff.begin();
- while (j != sint_buff.end()){
- j++;
- if(*i > *j)
- return false;
- i++;
- }
- return true;
- }
- int_sorted int_sorted::merge (const int_sorted &merge_with) const{
- // vi behöver inte ändra på värdena för this och merge_with så de kan bli const int *
- const int *i = begin(); // bra, deklarera i som const int *
- const int *j = merge_with.begin(); // bra, deklarera j som const int *
- int_buffer res(merge_with.size() + size()); // int_buffer to store result in
- int *C = res.begin(); // pointer to loop through buffer
- // Loop through all values in both buffers
- while(i != sint_buffer.end() && j != merge_with.end()){
- // If i < j, add i to C otherwise add J to C
- if (*i < *j){ // Compare values
- *C = *i; // Give the value of *i to *C
- i++; // increment i to next position in the buffer
- }else{
- *C = *j; // Give the value of *j to *C
- j++; // increment j to next position in the buffer
- }
- C++; // increment C to next position in the buffer
- }
- // if one is empty, add rest to C
- if (i != sint_buffer.end() && !(j != merge_with.end())){
- while(j != merge_with.end()){ // borde vara "i < end()" bara
- *C = *j; // Give value of *i to *C
- j++; // increment j to next position in the buffer
- C++; // increment C to next position in the buffer
- }
- return int_sorted(res.begin(), res.size());
- }
- // if one is empty, add rest to C
- else if(!(i != sint_buffer.end()) && j != merge_with.end()){
- while(i != sint_buffer.end()){ // borde vara " j < merge_with.end()"
- *C = *i; // Give value of *i to *C
- i++; // increment i to next position in the buffer
- C++; // increment C to next position in the buffer
- }
- return int_sorted(res.begin(), res.size());
- }
- return int_sorted(res.begin(), res.size());
- }
Advertisement
Add Comment
Please, Sign In to add comment