Simple2012

Untitled

Oct 18th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. int *int_sorted::insert(int value){
  2.     // int_sorted tmp(&value, 1);
  3.     // construct a int_sorted with requested value, size 1 then merge these two
  4.     *this = merge(int_sorted(&value, 1));
  5.     for (int *i = sint_buffer.begin(); i != sint_buffer.end(); i++)
  6.     {
  7.         if (*i == value)
  8.             return i; // Returns a const int pointer.. function wants a normal int pointer
  9.     }
  10.     return 0;
  11. }
  12.  
  13.  
  14. bool int_sorted::check_sorted(int_buffer sint_buff){
  15.     const int *i = sint_buff.begin();
  16.     const int *j = sint_buff.begin();
  17.     while (j != sint_buff.end()){  
  18.         j++;
  19.         if(*i > *j)
  20.             return false;
  21.         i++;
  22.     }
  23.     return true;
  24. }
  25.  
  26.  
  27. int_sorted int_sorted::merge (const int_sorted &merge_with) const{
  28.         // vi behöver inte ändra på värdena för this och merge_with så de kan bli const int *
  29.         const int *i = begin();         // bra, deklarera i som const int *
  30.         const int *j = merge_with.begin();      // bra, deklarera j som const int *
  31.  
  32.         int_buffer res(merge_with.size() + size()); // int_buffer to store result in
  33.         int *C = res.begin(); // pointer to loop through buffer
  34.  
  35.         // Loop through all values in both buffers
  36.         while(i != sint_buffer.end() && j != merge_with.end()){
  37.  
  38.                 // If i < j, add i to C otherwise add J to C
  39.                 if (*i < *j){ // Compare values
  40.                         *C = *i; // Give the value of *i to *C
  41.                         i++; // increment i to next position in the buffer
  42.                 }else{
  43.                         *C = *j; // Give the value of *j to *C
  44.                         j++; // increment j to next position in the buffer
  45.                 }
  46.                 C++; // increment C to next position in the buffer
  47.         }
  48.         // if one is empty, add rest to C
  49.         if (i != sint_buffer.end() && !(j != merge_with.end())){
  50.                 while(j != merge_with.end()){   // borde vara "i < end()" bara
  51.                         *C = *j; // Give value of *i to *C
  52.                         j++; // increment j to next position in the buffer
  53.                         C++; // increment C to next position in the buffer
  54.                 }
  55.                 return int_sorted(res.begin(), res.size());
  56.         }
  57.         // if one is empty, add rest to C
  58.         else if(!(i != sint_buffer.end()) && j != merge_with.end()){
  59.                 while(i != sint_buffer.end()){ // borde vara " j < merge_with.end()"
  60.                         *C = *i; // Give value of *i to *C
  61.                         i++; // increment i to next position in the buffer
  62.                         C++; // increment C to next position in the buffer
  63.                 }
  64.                 return int_sorted(res.begin(), res.size());
  65.         }
  66.         return int_sorted(res.begin(), res.size());
  67. }
Advertisement
Add Comment
Please, Sign In to add comment