intprog_hackery/sorting_algos.rb

39 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|
" else if (#{(comp ele)[:if]}) {
return #{(comp ele)[:ret]};
}"
end
2019-10-17 09:28:05 +00:00
"if (#{(comp first)[:if]}) {
return #{(comp first)[:ret]};
}
2019-10-21 14:21:00 +00:00
#{pls.join "\n"}
return #{(comp sortings[sortings.length-1])[:ret]};"
2019-10-21 14:22:00 +00:00
2019-10-13 12:08:50 +00:00
end