/*
 * =====================================================================================
 *
 *       Filename:  sort.cpp
 *
 *    Description:  Template excersie of a sort alorithm
 *
 *        Version:  1.0
 *        Created:  03/11/2011 06:43:26 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Ruben Safir (),
 *        Company:  
 *
 * =====================================================================================
 */
#include <iostream>
#include <vector>
#include <string>
#include <fstream>

using namespace std;

template <class T>
void sort ( vector<T>& v )
{
        const size_t n = v.size();

        for (int gap = n/2; 0<gap; gap/=2){
//cout << " gap==> " << gap << " n ==> " << n << endl;  
                for (int i = gap;i < int(n);i++){
//cout << "i ==> " << i << " gap==> " << gap << " n ==> " << n << endl;
                 for (int j=i-gap;0<=j ;j-=gap ){
//cout << "COMPARE::i ==> " << i << " j==> " << j << " gap==> " << gap << " n ==> " << n << endl;      
//cout << "***Comparing " << j+gap << " and " << j << endl;
                if(v[j+gap]<v[j]){
                                T temp = v[j];
                                v[j] = v[j+gap];
                                v[j+gap] = temp;
                        }
                 }
                }
        }
}

template<class T>
void display(const vector<T>& v, const unsigned int wrapsize=79){
        const size_t vector_size = v.size();
        unsigned int line_size = 0;
        cout << "Displaying Vector of " << vector_size << " size" << endl;
        for(unsigned int i = 0; i < vector_size; ++i){
                line_size += (v[i]).size() + 1; //account for space
                //cout << endl << "Line Length ==> " << line_size << endl;
                if(line_size < wrapsize){
                        if((v[i]).size() == 0){
                                cout << endl << endl; //block paragraphing
                                line_size = 0;
                        }else
                                cout << v[i] << " ";
                }else{
                        cout << endl << v[i] << " ";
                        line_size = (v[i]).size();
                }
        }
        cout << endl;
}




/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  
 * =====================================================================================
 */
int main ( int argc, char *argv[] )
{
        const int size = 10;
        
        char mix[size] = {'g','d','f','a','e','b','h','c','i','j'};
        const char * end = mix + size;
        for(char * ptr = mix; ptr < end; ++ptr)
                cout << *ptr << endl;

        vector <char> list(mix, mix + sizeof(mix)/sizeof(char) );
        vector<char>::iterator spot = list.begin();
        for(;spot < list.end(); spot++){
                cout << *spot << " ";
        }
        cout << endl;
        sort<char>(list);
        spot = list.begin();
        for(;spot < list.end(); spot++){
                cout << *spot << " ";
        }

        filebuf fb;
        if(fb.open( "/home/ruben/bin/tom_walker.txt",ios::in)){
                istream story_stream(&fb);
                string lines;
                vector<string> eachline;
                while(getline( story_stream, lines)){
//                      cout << "Line Aquired\n";
//                      cout << lines << endl;
                                eachline.insert(eachline.end(),lines);
                                }
                display<string>(eachline,79);
//now for kickers let's sort it

                sort<string>(eachline);
                cout << "Sorted Display\n";
                display<string>(eachline, 79);
        }else{
                cout << "\nCan Not Open Text" << endl;
        }



        return 0;
}                               /* ----------  end of function main  ---------- */