home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / fed0217s.zip / source / sort.cpp < prev    next >
C/C++ Source or Header  |  2000-12-13  |  2KB  |  90 lines

  1. /*
  2. ** Module   :SORT.CPP
  3. ** Abstract :Block sort routine
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. **
  7. ** Log: Sun  29/03/1998     Created
  8. */
  9.  
  10. #include <string.h>
  11.  
  12. #include <buffer.h>
  13. #include <version.h>
  14.  
  15. #ifndef max
  16. #define max(a,b) (((a) > (b)) ? (a) : (b))
  17. #define min(a,b) (((a) < (b)) ? (a) : (b))
  18. #endif
  19.  
  20. void Buffer::sort()
  21. {
  22.     if(!mark_state)
  23.         return;
  24.  
  25.     if(!col_block)
  26.         return;
  27.  
  28.     int mark_beg_row   = min(old_abs_row, abs_row());
  29.     int mark_end_row   = max(old_abs_row, abs_row());
  30.     int mark_col_start = min(old_abs_col, abs_col());
  31.     int mark_col_end   = max(old_abs_col, abs_col());
  32.  
  33.     int len   = mark_end_row - mark_beg_row;
  34.     int width = mark_col_end - mark_col_start;
  35.  
  36.     if(!len)
  37.         return;
  38.  
  39.     len++;
  40.  
  41.     if(!width)
  42.         return;
  43.  
  44.     changed = 1;
  45.  
  46.     //Sort using shell sort
  47.  
  48.     unsigned long gap, i, j;
  49.     char *buf1;
  50.     char *buf2;
  51.  
  52.     for(i = mark_beg_row; i <= mark_end_row; i++)
  53.         track(opRestoreLine, (void *)line(i), (void *)i);
  54.  
  55.     buf1 = new char[width + 1];
  56.     buf2 = new char[width + 1];
  57.  
  58.     for (gap = 0; ++gap < len;)
  59.           gap *= 3;
  60.  
  61.     while ((gap /= 3) != 0)
  62.     {
  63.         for (i = gap; i < len; i += 1)
  64.         {
  65.             for (j = i - gap; ;j -= gap)
  66.             {
  67.                 //Compare
  68.                 line(j + mark_beg_row)->get_print(mark_col_start, buf1, width);
  69.                 line(j + gap + mark_beg_row)->get_print(mark_col_start, buf2, width);
  70.  
  71.                 if (__nstrcmp(buf1, buf2, cp_out) <= 0 )
  72.                     break;
  73.  
  74.                 //Swap
  75.                 Ptr tmp = ppData[j + mark_beg_row];
  76.                 ppData[j + mark_beg_row]     = ppData[j + gap + mark_beg_row];
  77.                 ppData[j + gap+mark_beg_row] = tmp;
  78.  
  79.                 if (j < gap)
  80.                       break;
  81.             }
  82.         }
  83.     }
  84.     delete buf1;
  85.     delete buf2;
  86.  
  87.     fill_hiliting(mark_beg_row, line(mark_beg_row)->state());
  88. }
  89.  
  90.