Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "int_sorted.h"
- int_sorted::int_sorted(const int *source, size_t size):sint_buffer(0){
- if (!check_sorted(sint_buffer)){ // return if already sorted
- int_buffer tmp(source, size);
- for (int *i = tmp.begin(); i != tmp.end(); i++) // loop through int_buffer
- insert(*i); // insert into to-be sorted buffer
- }
- }
- int_sorted int_sorted::sort(int *begin, int *end){
- if (*begin == *end) return *this;
- if (*begin == *end-1) return int_sorted(begin, 1);
- ptrdiff_t half = (end - begin)/2;
- int *mid = begin+half;
- return sort(begin, mid).merge(sort(mid,end) );
- }
- size_t int_sorted::size() const{
- return sint_buffer.size();
- }
- /* insert (int value): Inserts a copy of value at the
- * correct position in the sorted container.
- * returns the insertion poin.
- */
- 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));
- std::cout << "Doing something else" << std::endl;
- for (int *i = sint_buffer.begin(); i != sint_buffer.end(); i++)
- {
- std::cout << "Trying to find position" << std::endl;
- 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){
- if (sint_buff.begin() == sint_buff.end())
- return true;
- 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{
- std::cout << "Entering merge" << std::endl;
- // vi behöver inte ändra på värdena för this och merge_with så de kan bli const int *
- const int *i = this->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()){
- std::cout << "Inside merge while loop" << std::endl;
- // 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())){
- std::cout << "Inside merge if1" << std::endl;
- while(i != merge_with.end()){ // borde vara "i < end()" bara
- std::cout << "Inside merge if1->merge loop" << std::endl;
- *C = *i; // Give value of *i to *C
- i++; // 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()){
- std::cout << "Inside merge if2" << std::endl;
- while(j != sint_buffer.end()){ // borde vara " j < merge_with.end()"
- std::cout << "Inside merge if2->merge loop" << std::endl;
- *C = *j; // Give value of *i to *C
- j++; // 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());
- }
- int const *int_sorted::begin() const{
- return sint_buffer.begin();
- }
- int const *int_sorted::end() const{
- return sint_buffer.end();
- // Alternativt: return &sint_buffer.begin()[size()-1];
- }
Advertisement
Add Comment
Please, Sign In to add comment