intprog_hackery/sorting_algos.rb

34 lines
1.1 KiB
Ruby
Raw Normal View History

2019-10-14 10:54:53 +00:00
# TODO: Make variadic, such that we only need one method
2019-10-17 09:28:05 +00:00
def compare_to(sortings)
first, second, third = *sortings
2019-10-14 10:54:53 +00:00
2019-10-17 11:44:22 +00:00
return_stat = lambda{ |sorting|
2019-10-17 09:28:05 +00:00
if sorting.type == "String"
{:ret => "this.#{sorting.variable}.compareTo(other.#{sorting.variable})"}
2019-10-14 10:54:53 +00:00
else
2019-10-17 09:28:05 +00:00
{:ret => sorting.comparator == ">" ? "this.#{sorting.variable} - other.#{sorting.variable}" : "other.#{sorting.variable} - this.#{sorting.variable}"}
2019-10-17 11:44:22 +00:00
end}
2019-10-14 10:54:53 +00:00
2019-10-17 11:44:22 +00:00
comp = lambda {|sorting|
2019-10-17 09:28:05 +00:00
if sorting.type == "String"
{:if => "!this.#{sorting.variable}.equals(other.#{sorting.variable})",
2019-10-17 11:44:22 +00:00
:ret => (return_stat.call sorting)[:ret]}
2019-10-14 10:54:53 +00:00
else
2019-10-17 09:28:05 +00:00
{:if => "this.#{sorting.variable} != other.#{sorting.variable}",
2019-10-17 11:44:22 +00:00
:ret => (return_stat.call sorting)[:ret]}
end}
2019-10-14 10:54:53 +00:00
2019-10-21 14:21:00 +00:00
pls = sortings.slice(1,sortings.length - 2).map do |ele|
2019-10-21 14:25:55 +00:00
"else if (#{(comp.call ele)[:if]}) {
\t\t\treturn #{(comp.call ele)[:ret]};
\t\t}"
2019-10-21 14:21:00 +00:00
end
2019-10-21 14:25:55 +00:00
"\t\tif (#{(comp.call first)[:if]}) {
\t\t\treturn #{(comp.call first)[:ret]};
\t\t}
\t\t#{pls.join "\n"}
\t\treturn #{(comp.call sortings[sortings.length-1])[:ret]};"
2019-10-21 14:21:00 +00:00
2019-10-13 12:08:50 +00:00
end