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 09:28:05 +00:00
def return_stat sorting
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-14 10:54:53 +00:00
end
end
2019-10-17 09:28:05 +00:00
def comp sorting
if sorting.type == "String"
{:if => "!this.#{sorting.variable}.equals(other.#{sorting.variable})",
:ret => (return_stat 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}",
:ret => (return_stat sorting)[:ret]}
2019-10-14 10:54:53 +00:00
end
end
2019-10-17 09:28:05 +00:00
if defined? third
"if (#{(comp first)[:if]}) {
return #{(comp first)[:ret]};
} else if (#{(comp second)[:if]}) {
return #{(comp second)[:ret]};
}
return #{(comp third)[:ret]};"
2019-10-14 10:54:53 +00:00
else
2019-10-17 09:28:05 +00:00
"if (#{(comp first)[:if]}) {
return #{(comp first)[:ret]};
}
return #{(comp second)[:ret]};
"
2019-10-14 10:54:53 +00:00
end
2019-10-13 12:08:50 +00:00
end