Simple2012

Untitled

Oct 18th, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. #include <iostream>
  2. #include "int_sorted.h"
  3.  
  4.  
  5. int_sorted::int_sorted(const int *source, size_t size):sint_buffer(0){
  6.     if (!check_sorted(sint_buffer)){ // return if already sorted
  7.         int_buffer tmp(source, size);
  8.         for (int *i = tmp.begin(); i != tmp.end(); i++) // loop through int_buffer
  9.             insert(*i); // insert into to-be sorted buffer
  10.     }
  11. }
  12.  
  13. int_sorted int_sorted::sort(int *begin, int *end){
  14.     if (*begin == *end) return *this;
  15.     if (*begin == *end-1) return int_sorted(begin, 1);
  16.  
  17.     ptrdiff_t half = (end - begin)/2;
  18.     int *mid = begin+half;
  19.     return sort(begin, mid).merge(sort(mid,end) );
  20. }
  21.  
  22. size_t int_sorted::size() const{
  23.     return sint_buffer.size();
  24. }
  25.  
  26. /* insert (int value): Inserts a copy of value at the
  27.  * correct position in the sorted container.
  28.  * returns the insertion poin.
  29.  */
  30. int *int_sorted::insert(int value){
  31.     // int_sorted tmp(&value, 1);
  32.     // construct a int_sorted with requested value, size 1 then merge these two
  33.     *this = merge(int_sorted(&value, 1));
  34.     std::cout << "Doing something else" << std::endl;
  35.     for (int *i = sint_buffer.begin(); i != sint_buffer.end(); i++)
  36.     {
  37.         std::cout << "Trying to find position" << std::endl;
  38.         if (*i == value)
  39.             return i; // Returns a const int pointer.. function wants a normal int pointer
  40.     }
  41.     return 0;
  42. }
  43. bool int_sorted::check_sorted(int_buffer sint_buff){
  44.     if (sint_buff.begin() == sint_buff.end())
  45.         return true;
  46.     const int *i = sint_buff.begin();
  47.     const int *j = sint_buff.begin();
  48.     while (j != sint_buff.end()){  
  49.         j++;
  50.         if(*i > *j)
  51.             return false;
  52.         i++;
  53.     }
  54.     return true;
  55. }
  56.  
  57. int_sorted int_sorted::merge (const int_sorted &merge_with) const{
  58.     std::cout << "Entering merge" << std::endl;
  59.         // vi behöver inte ändra på värdena för this och merge_with så de kan bli const int *
  60.         const int *i = this->begin();         // bra, deklarera i som const int *
  61.         const int *j = merge_with.begin();      // bra, deklarera j som const int *
  62.  
  63.         int_buffer res(merge_with.size() + size()); // int_buffer to store result in
  64.         int *C = res.begin(); // pointer to loop through buffer
  65.  
  66.         // Loop through all values in both buffers
  67.         while(i != sint_buffer.end() && j != merge_with.end()){
  68.                 std::cout << "Inside merge while loop" << std::endl;
  69.                 // If i < j, add i to C otherwise add J to C
  70.                 if (*i < *j){ // Compare values
  71.                         *C = *i; // Give the value of *i to *C
  72.                         i++; // increment i to next position in the buffer
  73.                 }else{
  74.                         *C = *j; // Give the value of *j to *C
  75.                         j++; // increment j to next position in the buffer
  76.                 }
  77.                 C++; // increment C to next position in the buffer
  78.         }
  79.         // if one is empty, add rest to C
  80.         if (i != sint_buffer.end() && !(j != merge_with.end())){
  81.             std::cout << "Inside merge if1" << std::endl;
  82.                 while(i != merge_with.end()){   // borde vara "i < end()" bara
  83.                         std::cout << "Inside merge if1->merge loop" << std::endl;
  84.                         *C = *i; // Give value of *i to *C
  85.                         i++; // increment j to next position in the buffer
  86.                         C++; // increment C to next position in the buffer
  87.                 }
  88.                 return int_sorted(res.begin(), res.size());
  89.         }
  90.         // if one is empty, add rest to C
  91.         else if(!(i != sint_buffer.end()) && j != merge_with.end()){
  92.             std::cout << "Inside merge if2" << std::endl;
  93.                 while(j != sint_buffer.end()){ // borde vara " j < merge_with.end()"
  94.                     std::cout << "Inside merge if2->merge loop" << std::endl;
  95.                         *C = *j; // Give value of *i to *C
  96.                         j++; // increment i to next position in the buffer
  97.                         C++; // increment C to next position in the buffer
  98.                 }
  99.                 return int_sorted(res.begin(), res.size());
  100.         }
  101.         return int_sorted(res.begin(), res.size());
  102. }
  103.  
  104. int const *int_sorted::begin() const{
  105.     return sint_buffer.begin();
  106. }
  107. int const *int_sorted::end() const{
  108.     return sint_buffer.end();
  109.     // Alternativt: return &sint_buffer.begin()[size()-1];
  110. }
Advertisement
Add Comment
Please, Sign In to add comment