cgen: fix rune array sort (#9561)
parent
9ba8d02a5a
commit
3637bac716
|
@ -807,6 +807,21 @@ fn test_sort() {
|
||||||
// assert users.map(it.name).join(' ') == 'Alice Bob Peter'
|
// assert users.map(it.name).join(' ') == 'Alice Bob Peter'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_rune_sort() {
|
||||||
|
mut bs := [`f`, `e`, `d`, `b`, `c`, `a`]
|
||||||
|
bs.sort()
|
||||||
|
println(bs)
|
||||||
|
assert '$bs' == '[`a`, `b`, `c`, `d`, `e`, `f`]'
|
||||||
|
|
||||||
|
bs.sort(a > b)
|
||||||
|
println(bs)
|
||||||
|
assert '$bs' == '[`f`, `e`, `d`, `c`, `b`, `a`]'
|
||||||
|
|
||||||
|
bs.sort(a < b)
|
||||||
|
println(bs)
|
||||||
|
assert '$bs' == '[`a`, `b`, `c`, `d`, `e`, `f`]'
|
||||||
|
}
|
||||||
|
|
||||||
fn test_sort_by_different_order_of_a_b() {
|
fn test_sort_by_different_order_of_a_b() {
|
||||||
mut x := [1, 2, 3]
|
mut x := [1, 2, 3]
|
||||||
x.sort(a < b)
|
x.sort(a < b)
|
||||||
|
|
|
@ -248,6 +248,20 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
|
||||||
}
|
}
|
||||||
// Register a new custom `compare_xxx` function for qsort()
|
// Register a new custom `compare_xxx` function for qsort()
|
||||||
g.table.register_fn(name: compare_fn, return_type: ast.int_type)
|
g.table.register_fn(name: compare_fn, return_type: ast.int_type)
|
||||||
|
|
||||||
|
if node.args.len == 0 {
|
||||||
|
styp_arg := g.typ(typ)
|
||||||
|
g.definitions.writeln('int $compare_fn ($styp_arg* a, $styp_arg* b) {')
|
||||||
|
sym := g.table.get_type_symbol(typ)
|
||||||
|
if !is_reverse && sym.has_method('<') {
|
||||||
|
g.definitions.writeln('\tif (${styp}__lt(*a, *b)) { return -1; } else { return 1; }}')
|
||||||
|
} else if is_reverse && sym.has_method('<') {
|
||||||
|
g.definitions.writeln('\tif (${styp}__lt(*b, *a)) { return -1; } else { return 1; }}')
|
||||||
|
} else {
|
||||||
|
g.definitions.writeln('if (*a < *b) return -1;')
|
||||||
|
g.definitions.writeln('if (*a > *b) return 1; return 0; }\n')
|
||||||
|
}
|
||||||
|
} else {
|
||||||
infix_expr := node.args[0].expr as ast.InfixExpr
|
infix_expr := node.args[0].expr as ast.InfixExpr
|
||||||
// Variables `a` and `b` are used in the `.sort(a < b)` syntax, so we can reuse them
|
// Variables `a` and `b` are used in the `.sort(a < b)` syntax, so we can reuse them
|
||||||
// when generating the function as long as the args are named the same.
|
// when generating the function as long as the args are named the same.
|
||||||
|
@ -278,18 +292,20 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
|
||||||
op2 = 'string_gt(a_, b_)'
|
op2 = 'string_gt(a_, b_)'
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
deref_str := if infix_expr.left_type.is_ptr() { '*' } else { '' }
|
||||||
if is_reverse {
|
if is_reverse {
|
||||||
op1 = 'a_ > b_'
|
op1 = '${deref_str}a_ > ${deref_str}b_'
|
||||||
op2 = 'a_ < b_'
|
op2 = '${deref_str}a_ < ${deref_str}b_'
|
||||||
} else {
|
} else {
|
||||||
op1 = 'a_ < b_'
|
op1 = '${deref_str}a_ < ${deref_str}b_'
|
||||||
op2 = 'a_ > b_'
|
op2 = '${deref_str}a_ > ${deref_str}b_'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
g.definitions.writeln('if ($op1) return -1;')
|
g.definitions.writeln('if ($op1) return -1;')
|
||||||
g.definitions.writeln('if ($op2) return 1; return 0; }\n')
|
g.definitions.writeln('if ($op2) return 1; return 0; }\n')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if is_reverse && !compare_fn.ends_with('_reverse') {
|
if is_reverse && !compare_fn.ends_with('_reverse') {
|
||||||
compare_fn += '_reverse'
|
compare_fn += '_reverse'
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue