intprog_hackery/sorting_algos.rb

39 lines
1.1 KiB
Ruby

# TODO: Make variadic, such that we only need one method
def compare_to(sortings)
first, second, third = *sortings
def return_stat sorting
if sorting.type == "String"
{:ret => "this.#{sorting.variable}.compareTo(other.#{sorting.variable})"}
else
{:ret => sorting.comparator == ">" ? "this.#{sorting.variable} - other.#{sorting.variable}" : "other.#{sorting.variable} - this.#{sorting.variable}"}
end
end
def comp sorting
if sorting.type == "String"
{:if => "!this.#{sorting.variable}.equals(other.#{sorting.variable})",
:ret => (return_stat sorting)[:ret]}
else
{:if => "this.#{sorting.variable} != other.#{sorting.variable}",
:ret => (return_stat sorting)[:ret]}
end
end
if defined? third
"if (#{(comp first)[:if]}) {
return #{(comp first)[:ret]};
} else if (#{(comp second)[:if]}) {
return #{(comp second)[:ret]};
}
return #{(comp third)[:ret]};"
else
"if (#{(comp first)[:if]}) {
return #{(comp first)[:ret]};
}
return #{(comp second)[:ret]};
"
end
end