Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 6.10 KB | None | 0 0
  1. note
  2.     description: "Summary description for {DOMINANTS}."
  3.     author: "Christoph Schwizer"
  4.     date: "$Date$"
  5.     revision: "$Revision$"
  6.  
  7. class
  8.     DOMINANTS
  9.  
  10. create
  11.     make_with_values
  12.  
  13. feature {NONE} -- Initialization
  14.  
  15. cor: ARRAY[COORDINATE]
  16.  
  17.     make_with_values(n: INTEGER; a: ARRAY[INTEGER])
  18.             -- Initialization for `Current'.
  19.         require
  20.             coordinate_couples: n \\ 2 = 0
  21.         local
  22.             i: INTEGER
  23.             point: COORDINATE
  24.         do
  25.             create cor.make (0, (n / 2).rounded - 1)
  26.             -- write coordinates into array 'cor'
  27.             from i := 0 until i >= n loop
  28.                 create point.make_with_value (a[i], a[i + 1])
  29.                 cor[(i/2).rounded] := point
  30.                 i := i + 2
  31.             end
  32.             -- sort array according to their x-values
  33. io.put_string ("sorting...%N")
  34.             naturalmergesort (cor, 0, cor.count - 1, false)
  35. io.put_string ("sorting done!%N")
  36.         end
  37.  
  38. feature {APPLICATION}
  39.  
  40.     print_to_console
  41.         local
  42.             i: INTEGER
  43.         do
  44.             from i := 0 until i >= cor.count loop
  45.                 io.put_string ("(")
  46.                 io.put_integer (cor[i].return_x)
  47.                 io.put_character (',')
  48.                 io.put_integer (cor[i].return_y)
  49.                 io.put_string (")")
  50.                 io.put_character (' ')
  51.                 i := i + 1
  52.             end
  53.             io.new_line
  54.         end
  55.  
  56.     ans: INTEGER
  57.         local
  58.             count, i, j, cur_x, max_x, old_y, max_y: INTEGER
  59.         do
  60. --print_to_console
  61.             max_x := cor[cor.count - 1].return_x
  62.             from i := cor.count - 1 until i < 0 loop
  63.  
  64.                 -- if coordinate is all to the right, it sure is dominant
  65.                 if cor[i].return_x = max_x then
  66.                     count := count + 1
  67. --io.put_string ("taking (") io.put_integer (cor[i].return_x) io.put_string (",") io.put_integer (cor[i].return_y) io.put_string (")%N")
  68.                     if cor[i].return_y > max_y then
  69.                         max_y := cor[i].return_y
  70.                     end
  71.  
  72.                 -- elseif it has a bigger global y-maximum, it sure is dominant
  73.                 elseif cor[i].return_y >= max_y then
  74.                     count := count + 1
  75. --io.put_string ("taking (") io.put_integer (cor[i].return_x) io.put_string (",") io.put_integer (cor[i].return_y) io.put_string (")%N")
  76.                     if cor[i].return_x /= cur_x then
  77.                         old_y := max_y
  78.                     end
  79.                     max_y := cor[i].return_y
  80.  
  81.                 -- elseif it has the same x-coordinate as the global y-maximum and is bigger than the previous y-maximum, it is dominant too
  82.                 elseif cor[i].return_x = cur_x and then cor[i].return_y >= old_y then
  83.                     count := count + 1
  84. --io.put_string ("taking (") io.put_integer (cor[i].return_x) io.put_string (",") io.put_integer (cor[i].return_y) io.put_string (")%N")
  85.  
  86.                 elseif cor[i].return_x /= cur_x then
  87.                     old_y := max_y
  88.                 end
  89.                 cur_x := cor[i].return_x
  90.                 i := i - 1
  91.             end
  92.             result := count
  93.         end
  94.  
  95.  
  96. feature {NONE}
  97. sort_y: BOOLEAN
  98.  
  99.     naturalmergesort (a: ARRAY[COORDINATE]; l, r: INTEGER; sort_using_y: BOOLEAN)
  100.         local
  101.             ll, mm, rr, i, j: INTEGER
  102.         do
  103.             sort_y := sort_using_y
  104.     --------outer cycle-------------------------------------------------------------
  105.             from j := a.count until j <= 1
  106.             loop
  107.                 ll := l
  108.                 -- reset counter
  109.                 j := 0
  110.  
  111.     ------------inner cycle--------------------------------------------
  112.                 -- let 'll' run
  113.                 from ll := l    until ll > r
  114.                 loop
  115.                     -- let 'mm' run
  116.                     if sort_y then
  117.                         from mm := ll   until (mm >= r or a[mm].return_y > a[mm + 1].return_y)
  118.                         loop
  119.                             mm := mm + 1
  120.                         end
  121.                     else
  122.                         from mm := ll   until (mm >= r or a[mm].return_x > a[mm + 1].return_x)
  123.                         loop
  124.                             mm := mm + 1
  125.                         end
  126.                     end
  127.                     -- 'mm' now points to the last element of the first subsequence
  128.  
  129.                     -- after 'mm' is found let 'rr' run
  130.                     -- if 'mm' is not the very last element, set 'rr' to the last element of the second subsequence
  131.                     if mm < r then
  132.  
  133.                         if sort_y then
  134.                             from rr := mm + 1   until (rr >= r or a[rr].return_y > a[rr + 1].return_y)
  135.                             loop
  136.                                 rr := rr + 1
  137.                             end
  138.                         else
  139.                             from rr := mm + 1   until (rr >= r or a[rr].return_x > a[rr + 1].return_x)
  140.                             loop
  141.                                 rr := rr + 1
  142.                             end
  143.                         end
  144.  
  145.                         merge (a, ll, mm, rr)
  146.  
  147.                         -- test output
  148.                         --  io.put_string ("ll = ") io.put_integer (ll)
  149.                         --  io.put_string ("%Narray after merge: ")
  150.                         --  from i := 1
  151.                         --  until i >= a.count
  152.                         --  loop
  153.                         --      io.put_integer (a[i])
  154.                         --      io.put_character (' ')
  155.                         --      i := i + 1
  156.                         --  end
  157.                         --  io.new_line
  158.                         --  io.new_line
  159.  
  160.  
  161.                         -- if 'mm' is the very last element, so is 'rr'
  162.                         else rr := mm
  163.                     end
  164.  
  165.                     ll := rr + 1
  166.                     -- count inner cycle
  167.                     j := j + 1
  168.  
  169.                     -- test output 'j'
  170.                     -- io.put_string ("j = ")
  171.                     -- io.put_integer (j)
  172.                     -- io.new_line
  173.                 end
  174.     ------------inner cycle end-------------------------------
  175.  
  176.  
  177.                 ll := rr + 1
  178.             end
  179.     --------outer cycle end----------------------------------------------
  180.         end
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.     merge (arr: ARRAY[COORDINATE]; left, middle, right: INTEGER)
  188.     -- note: input arguments 'left', 'middle' and 'right' are the indices of the array
  189.     -- 'middle' is the last index of the first subsequence
  190.  
  191.     local
  192.         copy_arr: ARRAY[COORDINATE]
  193.         h, i, j, k: INTEGER
  194.     do
  195.         create copy_arr.make (0, arr.count - 1)
  196.         from i := left; j := middle + 1; k := left
  197.         until (i > middle or j > right)
  198.         loop
  199.             if sort_y then
  200.                 if
  201.                     arr[i].return_y <= arr[j].return_y
  202.                 then
  203.                     copy_arr[k] := arr[i]
  204.                     i := i + 1
  205.                 else
  206.                     copy_arr[k] := arr[j]
  207.                     j := j + 1
  208.                 end
  209.             else
  210.                 if
  211.                     arr[i].return_x <= arr[j].return_x
  212.                 then
  213.                     copy_arr[k] := arr[i]
  214.                     i := i + 1
  215.                 else
  216.                     copy_arr[k] := arr[j]
  217.                     j := j + 1
  218.                 end
  219.             end
  220.             k := k + 1
  221.  
  222.         end
  223.  
  224.         if i > middle
  225.         then -- first subsequence is done
  226.             from h := j until h > right
  227.             loop
  228.                 copy_arr[k + h - j] := arr[h]
  229.                 h := h + 1
  230.             end
  231.         else -- second subsequence is done
  232.             from h := i until h > middle
  233.             loop
  234.                 copy_arr[k + h - i] := arr[h]
  235.                 h := h + 1
  236.             end
  237.         end
  238.  
  239.  
  240.     -- test: check copy_arr for correctness
  241. --  io.put_string ("copy_arr: %N")
  242. --  from h := 0
  243. --  until h >= copy_arr.count
  244. --  loop io.put_integer (copy_arr[h])
  245. --  io.put_character (' ')
  246. --  h := h + 1
  247. --  end
  248.  
  249.         -- copy back from 'copy_arr'
  250.         from h := left  until h > right
  251.         loop
  252.             arr[h] := copy_arr[h]
  253.             h := h + 1
  254.         end
  255.     end
  256.  
  257.  
  258.  
  259. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement